#ifndef MAKE_ID
#define MAKE_ID(a,b,c,d) ((ULONG) (a)<<24 | (ULONG) (b)<<16 | (ULONG) (c)<<8 | (ULONG) (d))
#endif

#ifdef _DCC
#define __inline
#endif

#include "Search_Window.h"
#include "Search_WindowExtern.h"


struct ObjSearch_Window * CreateSearch_Window(void)
{
	struct ObjSearch_Window * Object;

	APTR	Find_Group, REQ_BackGround, REQ_ForeGround, Text_Label, GR_grp_26;
	APTR	Case_Label, Words_Label, Wildcards_Label, Reverse_Label, Button_Group;
	APTR	Space_5;
	static const struct Hook Search_CloseWindowHook = { { NULL,NULL },(VOID *)Search_CloseWindow,NULL,NULL };
	static const struct Hook Search_StartSearchHook = { { NULL,NULL },(VOID *)Search_StartSearch,NULL,NULL };


	if (!(Object = AllocVec(sizeof(struct ObjSearch_Window), MEMF_PUBLIC|MEMF_CLEAR)))
		return(NULL);

	Object->STR_Text_Label = "Enter text to search for.";
	Object->STR_Case_Label = "Ignore Case";
	Object->STR_Words_Label = "Only Words";
	Object->STR_Wildcards_Label = "Wildcards";
	Object->STR_Reverse_Label = "Reverse Search";

	Text_Label = TextObject,
		MUIA_FramePhantomHoriz, TRUE,
		MUIA_Background, MUII_WindowBack,
		MUIA_Frame, MUIV_Frame_Text,
		MUIA_Text_Contents, Object->STR_Text_Label,
		MUIA_Text_PreParse, "\033c",
		MUIA_Text_SetMin, TRUE,
	End;

	Object->Search_String = StringObject,
		MUIA_Frame, MUIV_Frame_String,
	End;

	Object->Ignore_Case = CheckMark(TRUE);

	Case_Label = TextObject,
		MUIA_FramePhantomHoriz, TRUE,
		MUIA_Background, MUII_TextBack,
		MUIA_Frame, MUIV_Frame_Text,
		MUIA_Text_Contents, Object->STR_Case_Label,
		MUIA_Text_SetMin, TRUE,
	End;

	Object->Only_Words = CheckMark(FALSE);

	Words_Label = TextObject,
		MUIA_FramePhantomHoriz, TRUE,
		MUIA_Background, MUII_TextBack,
		MUIA_Frame, MUIV_Frame_Text,
		MUIA_Text_Contents, Object->STR_Words_Label,
		MUIA_Text_SetMin, TRUE,
	End;

	Object->WildCards = CheckMark(FALSE);

	Wildcards_Label = TextObject,
		MUIA_FramePhantomHoriz, TRUE,
		MUIA_Background, MUII_TextBack,
		MUIA_Frame, MUIV_Frame_Text,
		MUIA_Text_Contents, Object->STR_Wildcards_Label,
		MUIA_Text_SetMin, TRUE,
	End;

	Object->Reverse_Search = CheckMark(FALSE);

	Reverse_Label = TextObject,
		MUIA_FramePhantomHoriz, TRUE,
		MUIA_Background, MUII_TextBack,
		MUIA_Frame, MUIV_Frame_Text,
		MUIA_Text_Contents, Object->STR_Reverse_Label,
		MUIA_Text_SetMin, TRUE,
	End;

	GR_grp_26 = GroupObject,
		MUIA_Group_Columns, 4,
		Child, Object->Ignore_Case,
		Child, Case_Label,
		Child, Object->Only_Words,
		Child, Words_Label,
		Child, Object->WildCards,
		Child, Wildcards_Label,
		Child, Object->Reverse_Search,
		Child, Reverse_Label,
	End;

	REQ_ForeGround = GroupObject,
		MUIA_Background, MUII_TextBack,
		MUIA_Frame, MUIV_Frame_ReadList,
		Child, Text_Label,
		Child, Object->Search_String,
		Child, GR_grp_26,
	End;

	Object->OK_Buttons = TextObject,
		ButtonFrame,
		MUIA_Weight, 25,
		MUIA_Background, MUII_ButtonBack,
		MUIA_Text_Contents, "OK",
		MUIA_Text_PreParse, "\033c",
		MUIA_InputMode, MUIV_InputMode_RelVerify,
	End;

	Space_5 = HSpace(80);

	Object->CANCEL_Button = TextObject,
		ButtonFrame,
		MUIA_Weight, 25,
		MUIA_Background, MUII_ButtonBack,
		MUIA_Text_Contents, "CANCEL",
		MUIA_Text_PreParse, "\033c",
		MUIA_InputMode, MUIV_InputMode_RelVerify,
	End;

	Button_Group = GroupObject,
		MUIA_Group_Horiz, TRUE,
		Child, Object->OK_Buttons,
		Child, Space_5,
		Child, Object->CANCEL_Button,
	End;

	REQ_BackGround = GroupObject,
		Child, REQ_ForeGround,
		Child, Button_Group,
	End;

	Find_Group = GroupObject,
		MUIA_Background, MUII_RequesterBack,
		Child, REQ_BackGround,
	End;

	Object->Search_Window = WindowObject,
		MUIA_Window_Title, "Search...",
		MUIA_Window_ID, MAKE_ID('5', 'W', 'I', 'N'),
		MUIA_Window_SizeGadget, FALSE,
		MUIA_Window_NoMenus, TRUE,
		WindowContents, Find_Group,
	End;


	if (!Object->Search_Window)
	{
		FreeVec(Object);
		return(NULL);
	}

	DoMethod(Object->Search_Window,
		MUIM_Notify, MUIA_Window_CloseRequest, TRUE,
		Object->Search_Window,
		2,
		MUIM_CallHook, &Search_CloseWindowHook
		);

	DoMethod(Object->OK_Buttons,
		MUIM_Notify, MUIA_Pressed, FALSE,
		Object->Search_Window,
		2,
		MUIM_CallHook, &Search_StartSearchHook
		);

	DoMethod(Object->CANCEL_Button,
		MUIM_Notify, MUIA_Pressed, FALSE,
		Object->Search_Window,
		2,
		MUIM_CallHook, &Search_CloseWindowHook
		);

	DoMethod(Object->Search_Window,
		MUIM_Window_SetCycleChain, Object->Search_String,
		Object->Ignore_Case,
		Object->Only_Words,
		Object->WildCards,
		Object->Reverse_Search,
		Object->OK_Buttons,
		Object->CANCEL_Button,
		0
		);


	return(Object);
}

void DisposeSearch_Window(struct ObjSearch_Window * Object)
{
	MUI_DisposeObject(Object->Search_Window);
	FreeVec(Object);
}
