#include <intuition/intuitionbase.h>
#include <graphics/gfxbase.h>

#define OKAY 1
#define CANCEL 0

static short SimpleRequestXYBorder[]={
	0,0,101,0,101,12,0,12,0,0};
static struct Border SimpleRequestBorder={
	-1,-1,6,0,JAM1,5,SimpleRequestXYBorder,NULL};
static struct IntuiText OkayText={
	0,1,JAM1,5,2,NULL,NULL,NULL};
static struct IntuiText CancelText={
	0,1,JAM1,5,2,NULL,NULL,NULL};
static struct Gadget OkayGadget={
	NULL,8,30,100,11,GADGHCOMP,RELVERIFY,BOOLGADGET,
	(APTR)&SimpleRequestBorder,NULL,&OkayText,NULL,NULL,OKAY,NULL};
static struct Gadget CancelGadget={
	&OkayGadget,120,30,100,11,GADGHCOMP,RELVERIFY,BOOLGADGET,
	(APTR)&SimpleRequestBorder,NULL,&CancelText,NULL,NULL,CANCEL,NULL};

static struct IntuiText SimpleRequestText={
	0,1,JAM1,8,17,NULL,NULL,NULL};

static struct NewWindow SimpleRequestWindow={
	50,30,230,46,0,3,GADGETUP|VANILLAKEY|INTUITICKS,
	ACTIVATE|RMBTRAP|WINDOWDRAG,NULL,
	NULL,"Request",NULL,NULL,0,0,0,0,WBENCHSCREEN};

static struct Window *QWindow;
static struct IntuiMessage *MyMsg;
struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;

request(hail,text,x,y,pos,neg,dfault,time)
char *hail, *text;
int x,y;
char *pos, *neg;
BOOL dfault;
int time;
{
	int Class,Code,ticks=0;
	char key;
	struct Screen *Screen;
	Screen=IntuitionBase->ActiveWindow->WScreen;
	SimpleRequestWindow.Width=strlen(text)*8+16;
	SimpleRequestText.LeftEdge=8;
	if (SimpleRequestWindow.Width<230) {
		SimpleRequestWindow.Width=230;
		SimpleRequestText.LeftEdge=(214-(strlen(text)*8))/2+8;
	}
	if (SimpleRequestWindow.Width>Screen->Width)
		SimpleRequestWindow.Width=Screen->Width;
	CancelGadget.LeftEdge=(SimpleRequestWindow.Width)-108;
	if (x==-1 || y==-1) {
		x=Screen->MouseX;
		y=Screen->MouseY;
	}
	if (x==-2 || y==-2) {
		x=Screen->MouseX-20;
		y=Screen->MouseY-35;
	}
	if (x==-3 || y==-3) {
		x=Screen->MouseX-SimpleRequestWindow.Width+20;
		y=Screen->MouseY-35;
	}		
	if (x<0) x=0; if (y<0) y=0;
	if (x>Screen->Width-SimpleRequestWindow.Width)
		x=Screen->Width-SimpleRequestWindow.Width;
	if (y>Screen->Height-46)
		y=Screen->Height-46;
	SimpleRequestWindow.TopEdge=y; SimpleRequestWindow.LeftEdge=x;
	CancelText.IText=neg; CancelText.LeftEdge=(100-(strlen(neg)*8))/2;
	OkayText.IText=pos; OkayText.LeftEdge=(100-(strlen(pos)*8))/2;
	if (hail) SimpleRequestWindow.Title=hail;
	QWindow=(struct Window *) OpenWindow(&SimpleRequestWindow);
	SetAPen(QWindow->RPort,1);
	RectFill(QWindow->RPort,4,11,(SimpleRequestWindow.Width-5),43);
	AddGList(QWindow,&CancelGadget,-1,2,NULL);
	RefreshGList(&CancelGadget,QWindow,NULL,2);
	SimpleRequestText.IText=text;
	PrintIText(QWindow->RPort,&SimpleRequestText,0,0);
	waitforinput:
	Wait (1<<QWindow->UserPort->mp_SigBit);
	MyMsg=GetMsg(QWindow->UserPort);
	Class=MyMsg->Class; Code=MyMsg->Code; key=toupper((char ) Code);
	ReplyMsg(MyMsg);
	if (Class==INTUITICKS && time>0) {
		++ticks;
		if ((ticks/10)<time) goto waitforinput;
		Class=VANILLAKEY;
		if (dfault) key='Y'; else key='N';
	}
	if (Class==VANILLAKEY && key!='Y' && key!='N') goto waitforinput;
	CloseWindow(QWindow);
	RemoveGList(QWindow,&CancelGadget,2);
	if (((struct Gadget *) MyMsg->IAddress)->GadgetID==OKAY || key=='Y') return(TRUE);
	return(FALSE);
}

main(argc,argv)
int argc;
char *argv[];
{
	int dfault,timeout,ret;
	printf("\x9b;33mCustReq v2\x9b;0;3m (c) 1989 Jonathan Potter\x9b;0m\n");
	if (argc<5) {
		printf("USAGE : CustReq \"hailstring\" \"string\" \"pos\" \"neg\" \
[dfault] [timeout]\n");
		exit(0);
	}
	IntuitionBase=(struct IntuitionBase *) OpenLibrary("intuition.library",0);
	GfxBase=(struct GfxBase *) OpenLibrary("graphics.library",0);
	if (argc>5) dfault=atoi(argv[5]);
	if (argc>6) timeout=atoi(argv[6]);
	if (dfault)
		ret=request(argv[1],argv[2],-2,-2,argv[3],argv[4],1,timeout);
	else
		ret=request(argv[1],argv[2],-3,-3,argv[3],argv[4],0,timeout);
	CloseLibrary(IntuitionBase);
	CloseLibrary(GfxBase);
	if (ret) exit(5);
	exit(0);
}
