#include <intuition/simplereq.h>

/****************************************************************/
/*								*/
/* SimpleRequest(Banner, Gadgets, Window)			*/
/*								*/
/*	Banner == string of any length, saying something.	*/
/*	Gadgets == a string like "Yes|No", using the vertical	*/
/*		   bar to separate gadgets (any number).	*/
/*	Window == the window (and, essentially, screen) to put	*/
/*		  the requester in (since it's just a window)   */
/*								*/
/****************************************************************/
/*            Created by Steve Tibbett, 10-March-1989		*/
/****************************************************************/
/* Note: It's a first draft.  It's a bit messy, it can be fixed */
/****************************************************************/

static struct NewWindow NW;
static struct TextFont *TF;
static struct Window *ReqWindow;		/* The requester's window */
static struct Remember *GadRemember = 0;	/* Intuition Remember struct */
static struct TextFont *RPFont;		/* It's font */
static struct RastPort *RP;			/* Our rastport */

SimpleRequest(Banner, GadgetString, Window, Flags)
char *Banner;
char *GadgetString;
struct Window *Window;
short Flags;
{
struct IntuiMessage *Message;		/* For talking to requester */
struct Gadget *IAddress;		/* To check which gadget was hit */
struct Screen *Screen;			/* Screen to appear on */
char buf[255], buf2[255];		/* Buffers... for banner and gads */
char *TextLines[10], *GadLines[10];	/* Pointers into the buffers */
short x;				/* Temporary */
short NumLines = 0, NumGads = 0;	/* Number of text lines & gads */
short LongestLine = 0;			/* Longest text line */
char LongestLineNum = 0;		/* It's number */
short LongestGad = 0;			/* Longest gadget text */
short WindowWidth;			/* Width of the requester window */
short WindowHeight;			/* Height of the requester window */
char YSize;			/* Y size of the source RP's font */
char TopTextY;			/* Where the top line of text will appear */
short temp;			/* Temporary */
short LongestGadLen = 0;	/* Longest gadget's IntuiTextLength() */
char GadgetMode;		/* 0 == horizontal, 1 == vertical */
short ActualGadWidth;		/* Width of ALL gadgets (they're all alike) */
short GadsYStart;		/* Where the gads start on Y */

GadgetMode = 0;		/* GadgetMode: 0=Horizontal, 1=Vertical */

/* Get the font of the Window that called us */
RPFont = Window->RPort->Font;

/* Get the Y Size of that font */
YSize = RPFont->tf_YSize;

/* Save a pointer to it's TextFont structure for later */
TF = RPFont;

/* First we'll figure how many text lines, and get pointers to each line */

strcpy(buf, Banner);
TextLines[NumLines++] = buf;
TextLines[NumLines] = 0;

temp = strlen(buf);
for (x=0; x<temp; x++)	
	{
	if (buf[x] == '|')	/* Parse out the '|' */
		{
		TextLines[NumLines++] = &buf[x+1];
		TextLines[NumLines] = 0;
		buf[x] = 0;
		};
	};


/* Next find the longest line */
for (x=0; x<NumLines; x++)
	{
	char l;
	l = strlen(TextLines[x]);
	if (l > LongestLine) 
		{
		LongestLineNum = x;
		LongestLine = l;	
		};
	};

/* Okay, now figure the minimum window width and height */
WindowWidth = 40 + TextLen(TextLines[LongestLineNum]);	/* 20 pixels border on each side */
WindowHeight = (NumLines * (YSize+2)) + 15 + YSize;	/* 3 for the title bar... YSize for the Title Bar. */
if (Flags & SR_NODRAG) WindowHeight -= (YSize+3);
if (Flags & SR_NODRAG)
	TopTextY = YSize+3;
	else TopTextY = YSize+3+YSize+1;


/** Do the same thing for the gadget line! **/

strcpy(buf2, GadgetString);
GadLines[NumGads++] = buf2;
GadLines[NumGads] = 0;
temp = strlen(buf2);
for (x=0; x<temp; x++)
	{
	if (buf2[x] == '|')
		{
		GadLines[NumGads++] = &buf2[x+1];
		GadLines[NumGads] = 0;
		buf2[x] = 0;
		};
	};

/* Next find the longest line */

for (x=0; x<NumGads; x++)
	{
	short l;
	l = strlen(GadLines[x]);
	if (l > LongestGad) 
		{
		LongestGad = l;	
		};
	
	l = TextLen(GadLines[x]);
	if (l > LongestGadLen) LongestGadLen = l;
	};


/**********************************************************************/

/* Figure the window width and height */
temp = LongestGadLen + TextLen("  ") + 13;	/* 10 offset per gadget */
temp = temp * NumGads;
temp += 48;
if (temp > WindowWidth) WindowWidth = temp;
WindowHeight = WindowHeight + 3 + (2*YSize);

/* Go about opening the window */
Screen = Window->WScreen;
GadsYStart = WindowHeight-(YSize+12);

if (WindowWidth > Screen->Width)
	{
	GadgetMode = 1;
	WindowHeight = WindowHeight + ((8+YSize)*(NumGads-1));
	WindowWidth = 40 + TextLen(TextLines[LongestLineNum]);	/* 20 pixels border on each side */
	temp = LongestGadLen + TextLen("  ") + 61;
	if (temp > WindowWidth) WindowWidth = temp;
	};

if (Flags & SR_NOCENTERWINDOW) {
	NW.LeftEdge = 0;
	NW.TopEdge = 0;
	} else {
	NW.LeftEdge = (Screen->Width/2) - (WindowWidth/2);
	NW.TopEdge = (Screen->Height/2) - (WindowHeight/2);
	};

NW.Width = WindowWidth;
NW.Height = WindowHeight;
NW.DetailPen = 0;
NW.BlockPen = 1;
NW.IDCMPFlags = GADGETUP|VANILLAKEY|DISKINSERTED;

if (Flags & SR_NODRAG) {
	NW.Flags = ACTIVATE|SMART_REFRESH|NOCAREREFRESH;
	} else NW.Flags = ACTIVATE|WINDOWDRAG|SMART_REFRESH|NOCAREREFRESH;

if (Flags & SR_NODRAG) 
	NW.Title = 0;
	else NW.Title = Window->Title;

if (Screen->Flags & CUSTOMSCREEN) {
	NW.Type = CUSTOMSCREEN;
	NW.Screen = Screen;
	} else NW.Type = WBENCHSCREEN;

/** Open the window **/
ReqWindow = OpenWindow(&NW);
if (ReqWindow == 0) return(FALSE);
RP = ReqWindow->RPort;

/** Do the background **/
SetDrMd(RP, JAM2);
SetAPen(RP, 1);
if (Flags & SR_NODRAG)
	RectFill(ReqWindow->RPort, 4, 2, WindowWidth-5, WindowHeight-3);
	else RectFill(ReqWindow->RPort, 4, YSize+3, WindowWidth-5, WindowHeight-3);

/** Put the text up **/
SetAPen(RP, 0);
SetBPen(RP, 1);

for (x=0; x<NumLines; x++)
	{
	if (Flags & SR_NOCENTERTEXT)
		Move(ReqWindow->RPort, 16, TopTextY + (x*(YSize+1)));
		else Move(ReqWindow->RPort, (WindowWidth/2) - (TextLen(TextLines[x])/2), TopTextY + (x*(YSize+1)));
	Text(ReqWindow->RPort, TextLines[x], strlen(TextLines[x]));
	};


/** The text is up, now add the gadgets **/
ActualGadWidth = LongestGadLen + TextLen("  ");

if (GadgetMode == 0)
	{
	for (x=0; x<NumGads; x++)
		{
		short GadCenter, GadWidth;
 		GadWidth = WindowWidth/NumGads;
		GadCenter = (GadWidth * x) + (GadWidth/2);
		CreateGadget(GadCenter-(ActualGadWidth/2)-2, GadsYStart, 
			ActualGadWidth, YSize+3, x, RELVERIFY, GadLines[x]);
		};
	} else
	{
	short GadX;

	GadX = WindowWidth/2 - (ActualGadWidth/2) - 2;

	for (x=0; x<NumGads; x++)
		{
		CreateGadget(GadX, GadsYStart + ((YSize+8)*x),
			ActualGadWidth, YSize+3, x, RELVERIFY, GadLines[x]);
		};
	};


/** Okay, it's up, let's wait for something to happen. **/

while (TRUE)
	{
	WaitPort(ReqWindow->UserPort);
	Message = (struct IntuiMessage *)GetMsg(ReqWindow->UserPort);
	
	if (Message->Class == GADGETUP)
		{
		IAddress = (struct Gadget *)Message->IAddress;
		
		ReplyMsg((struct Message *)Message);
		CloseWindow(ReqWindow);
		FreeRemember(&GadRemember, TRUE);

		return(IAddress->GadgetID);
		};
	ReplyMsg((struct Message *)Message);
	};
}


/*******************************************************************/
/*  CreateGadget.  Does just that, with your Remember struct.      */
/*******************************************************************/
CreateGadget(GadX, GadY, SizeX, SizeY, GadgetID, Flags, text)
long GadX, GadY, SizeX, SizeY, GadgetID, Flags;
char *text;
{
struct Gadget *Gadget;
short x,y,center;

Gadget = (struct Gadget *)AllocRemember((struct Remember **)&GadRemember, sizeof(struct Gadget), MEMF_PUBLIC|MEMF_CLEAR);
if (Gadget == 0) return(FALSE);

/* Gadget Imagery.  No text for now.  */
SetAPen(RP, 0);
SetDrMd(RP, JAM2);
RectFill(RP, GadX+5, GadY+3, GadX+4+SizeX, GadY+2+SizeY);
SetAPen(RP, 3);
RectFill(RP, GadX, GadY, GadX+SizeX, GadY+SizeY);
SetAPen(RP, 0);
RectFill(RP, GadX+2, GadY+1, GadX+SizeX-2, GadY+SizeY-1);

Gadget->LeftEdge   = GadX+2;
Gadget->TopEdge    = GadY+1;
Gadget->Width      = SizeX-3;
Gadget->Height     = SizeY-1;
Gadget->Flags      = GADGHCOMP;
Gadget->Activation = Flags;
Gadget->GadgetType = BOOLGADGET;
Gadget->GadgetID   = GadgetID;

AddGadget(ReqWindow, Gadget, -1);

y = GadY + 3 + RPFont->tf_Baseline;
x = TextLen(text);

center = GadX + (SizeX/2) - (x/2);
	
SetAPen(RP, 1);
SetBPen(RP, 0);

Move(RP, center, y-1);
Text(RP, text, strlen(text));
}


static struct IntuiText DummyIntuiText = { 0,0, JAM2, 0, 0, 0,0,0 };
static struct TextAttr DummyTA;

/**************************************************************/
/* TextLen(text) - Give it a string, it returns the pixel len */
/**************************************************************/
TextLen(text)
char *text;
{
/* set the dummy TextAttr to the TextFont's fields */
DummyTA.ta_Name = TF->tf_Message.mn_Node.ln_Name;
DummyTA.ta_YSize = TF->tf_YSize;

/* Set the dummy IntuiText to point to the dummy TextFont and String */
DummyIntuiText.IText = text;
DummyIntuiText.ITextFont = &DummyTA;

/* And get the pixel length of the string */
return(IntuiTextLength(&DummyIntuiText));
}
