/*
 *
 *  AM --- AmigaMail
 *  (C) 1991, 1992 by Christian Riede
 *
 *  AM is distributed in the hope that it will be useful, but WITHOUT ANY
 *  WARRANTY.  No author or distributor accepts responsibility to anyone
 *  for the consequences of using it or for whether it serves any
 *  particular purpose or works at all, unless he says so in writing.
 *  Refer to the GNU General Public License, Version 1, for full details.
 *  
 *  Everyone is granted permission to copy, modify and redistribute AM,
 *  but only under the conditions described in the GNU General Public
 *  License, Version 1.  A copy of this license is supposed to have been 
 *  given to you along with AM so you can know your rights and responsi-
 *  bilities.  It should be in a file named COPYING.  Among other things,
 *  the copyright notice and this notice must be preserved on all copies.
 *
 *  
 *
 */

#include "am.h"

#define REQ_WIDTH 400
#define REQ_HEIGHT 100

#define OK 1
#define OKA 2
#define CANCEL 3
#define ANSWER 4

/* reentrant */
/* returns 0 for CANCEL, 1 for OK and 2 for OKA */
int RequestSearchString(char *SearchPattern)
{
	int ok,run;
	struct IntuiMessage *Msg;
	ULONG Class;
	ULONG Code;
	APTR IAddr;
	char *savepattern;
	struct Window *ReqWindow;
	struct Gadget *GList,*gad,*StringGadget;

	/* we need a copy of the global TextAttr for the gadets */
	static struct TextAttr RSTextAttr;

	struct NewGadget OKGadget = 
	{
		0,0,0,0,
		(UBYTE *)"OK (Header)",&RSTextAttr,
		OK,
		PLACETEXT_IN,
		0,	
		0
	};
	
	struct NewGadget CANCELGadget = 
	{
		0,0,0,0,
		(UBYTE *)"CANCEL",&RSTextAttr,
		CANCEL,
		PLACETEXT_IN,
		0,	
		0
	};
	
	struct NewGadget OKAGadget = 
	{
		0,0,0,0,
		(UBYTE *)"OK (All)",&RSTextAttr,
		OKA,
		PLACETEXT_IN,
		0,	
		0
	};
	
	struct NewGadget AnswerGadget = 
	{
		0,0,0,0,
		(UBYTE *)"Search Pattern",&RSTextAttr,
		ANSWER,
		PLACETEXT_ABOVE,
		0,	
		0
	};

	RSTextAttr = TextAttr;

	if (!(savepattern = AllocMem(strlen(SearchPattern)+1,MEMF_CLEAR)))
		return(FALSE);

	/* remember the original pattern for the case the user presses CANCEL */
	strcpy(savepattern,SearchPattern);

	if (Screen) /* custom screen requested */
	{
		if (!(ReqWindow = OpenWindowTags(0,
			WA_CustomScreen,Screen,
			WA_Left,Screen->Width/2-REQ_WIDTH/2,
			WA_Top,Screen->Height/2-REQ_HEIGHT/2,
			WA_Width,REQ_WIDTH,
			WA_Height,REQ_HEIGHT,
			WA_MinWidth,640L,
			WA_MinHeight,180L,
			WA_MaxWidth,1024L,
			WA_MaxHeight,1024L,
			WA_DragBar,TRUE,
			WA_DepthGadget,TRUE,
			WA_Activate,TRUE,
			WA_SmartRefresh,TRUE,
			WA_GimmeZeroZero,TRUE,
			TAG_END)))
		{
			SimpleRequest(Window,"Can't open Request-Window");
			return(FALSE);
		}
	}
	else if (PubScreen) /* public screen requested */
	{
		if (!(ReqWindow = OpenWindowTags(0,
			WA_PubScreen,(UBYTE *)PubScreen,
			WA_Left,PubScreen->Width/2-REQ_WIDTH/2,
			WA_Top,PubScreen->Height/2-REQ_HEIGHT/2,
			WA_Width,REQ_WIDTH,
			WA_Height,REQ_HEIGHT,
			WA_MinWidth,640L,
			WA_MinHeight,180L,
			WA_MaxWidth,1024L,
			WA_MaxHeight,1024L,
			WA_DragBar,TRUE,
			WA_DepthGadget,TRUE,
			WA_Activate,TRUE,
			WA_SmartRefresh,TRUE,
			WA_GimmeZeroZero,TRUE,
			TAG_END)))
		{
			SimpleRequest(Window,"Can't open Request-Window");
			return(FALSE);
		}
	}
	else
	{
		FreeMem(savepattern,strlen(savepattern)+1);
		return(FALSE);
	}
		
	/* adjust gadgets to window dimensions */
	OKGadget.ng_LeftEdge = 5;
	OKGadget.ng_TopEdge = ReqWindow->GZZHeight-RSTextAttr.ta_YSize-8;
	OKGadget.ng_Width = 100;                           /* !!!!!!!!!!!!!!! */
	OKGadget.ng_Height = RSTextAttr.ta_YSize+4;

	CANCELGadget.ng_LeftEdge = ReqWindow->GZZWidth-OKGadget.ng_Width-5;
	CANCELGadget.ng_TopEdge = OKGadget.ng_TopEdge;
	CANCELGadget.ng_Width = OKGadget.ng_Width;
	CANCELGadget.ng_Height = OKGadget.ng_Height;

	OKAGadget.ng_LeftEdge = ReqWindow->GZZWidth/2-50;
	OKAGadget.ng_TopEdge = OKGadget.ng_TopEdge;
	OKAGadget.ng_Width = 100;
	OKAGadget.ng_Height = RSTextAttr.ta_YSize+4;

	AnswerGadget.ng_LeftEdge = 5;
	AnswerGadget.ng_TopEdge = RSTextAttr.ta_YSize+15;
	AnswerGadget.ng_Width = ReqWindow->GZZWidth-10;
	AnswerGadget.ng_Height = RSTextAttr.ta_YSize+4;

	/* get visualinfo */
	if(!(OKGadget.ng_VisualInfo = GetVisualInfo(ReqWindow->WScreen,TAG_END)))
	{
		SimpleRequest(Window,"Can't get visual info");
		CloseWindow(ReqWindow);
		return(FALSE);;
	}

	CANCELGadget.ng_VisualInfo = OKGadget.ng_VisualInfo;
	OKAGadget.ng_VisualInfo = OKGadget.ng_VisualInfo;
	AnswerGadget.ng_VisualInfo = OKGadget.ng_VisualInfo;
	
	/* allocate context */
	if (!(gad = CreateContext(&GList)))
	{
			CloseWindow(ReqWindow);
			SimpleRequest(Window,"Can't create context");
			return(FALSE);
	}

	/* create OK gadget */
	if(!(gad = CreateGadget(BUTTON_KIND,gad,&OKGadget,
		GA_RELVERIFY,TRUE,
		TAG_END)))
	{
			FreeGadgets(GList);
			CloseWindow(ReqWindow);
			SimpleRequest(Window,"Can't create ok gadget");
			return(FALSE);
	}


	/* create CANCEL gadget */
	if(!(gad = CreateGadget(BUTTON_KIND,gad,&CANCELGadget,
		GA_RELVERIFY,TRUE,
		TAG_END)))
	{
			FreeGadgets(GList);
			CloseWindow(ReqWindow);
			SimpleRequest(Window,"Can't create cancel gadget");
			return(FALSE);
	}

	/* create OKA gadget */
	if(!(gad = CreateGadget(BUTTON_KIND,gad,&OKAGadget,
		GA_RELVERIFY,TRUE,
		TAG_END)))
	{
			FreeGadgets(GList);
			CloseWindow(ReqWindow);
			SimpleRequest(Window,"Can't create oka gadget");
			return(FALSE);
	}


	/* get StringGadget */
	if (!(StringGadget = gad = CreateGadget(STRING_KIND,gad,&AnswerGadget,
		GTST_MaxChars,MAIL_FIELDLENGTH,
		GTST_String,SearchPattern,
		TAG_END)))
	{
			FreeGadgets(GList);
			CloseWindow(ReqWindow);
			SimpleRequest(Window,"Can't create String gadget");
			return(FALSE);
	}

	/* show up gadgets */
	AddGList(ReqWindow,GList,-1,-1,NULL);
	RefreshGList(GList,ReqWindow,NULL,-1);
	GT_RefreshWindow(ReqWindow,0);

	ActivateGadget(StringGadget,ReqWindow,0);

	ModifyIDCMP(ReqWindow,GADGETUP|LISTVIEWIDCMP);

	run = TRUE;
	while (run)
	{
		Wait(1L<<ReqWindow->UserPort->mp_SigBit);
		while (Msg = GT_GetIMsg(ReqWindow->UserPort))
		{
			/* remember values */
			Class = Msg->Class;
			Code  = Msg->Code;	
			IAddr = Msg->IAddress;

			/* replay message */
			GT_ReplyIMsg(Msg);

			/* dispatch */
			switch(Class)
			{
				case REFRESHWINDOW:
					GT_BeginRefresh(ReqWindow);
					GT_EndRefresh(ReqWindow,TRUE);
					break;
				case GADGETUP:
					switch(((struct Gadget *)IAddr)->GadgetID)
					{
						case CANCEL:
							strcpy(SearchPattern,savepattern);
							run = FALSE;
							ok = 0;
							break;
						case OK:
							strcpy(SearchPattern,(char *)((struct StringInfo *)StringGadget->SpecialInfo)->Buffer);
							run = FALSE;
							ok = 1;
							break;
						case OKA:
							strcpy(SearchPattern,(char *)((struct StringInfo *)StringGadget->SpecialInfo)->Buffer);
							run = FALSE;
							ok = 2;
							break;
						case ANSWER:
							strcpy(SearchPattern,(char *)((struct StringInfo *)StringGadget->SpecialInfo)->Buffer);
							run = FALSE;
							ok = 1;
							break;
						default:
							puts("unknown gadget");
					}
				case MOUSEBUTTONS:
					break;
				case INTUITICKS:
					break;
				default:
					printf("Unknkown IntuiMessage->Class:%x\n",Class);
					break;
			}
		}
	}

	/* cleanup */
	RemoveGList(ReqWindow,GList,-1); 
	FreeGadgets(GList);
	FreeVisualInfo(OKGadget.ng_VisualInfo);
	FreeMem(savepattern,strlen(savepattern)+1);
	CloseWindow(ReqWindow);

	return(ok);
}
