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

Programma ..... CustReq.c - V2.0i

Autore ........ Jonathan Potter, 3 William Street, Clarence Park, Australia

Funzione ...... Comando ASK modificato ed ampliato per Intuition

Software ...... Aztec C V3.6a || Lattice C V5.04

Hardware ...... Amiga 500/2500, Kickstart & Workbench V1.2/1.3

Sintassi ...... CustReq "titolo" "testo" "pos" "neg" [default] [timeout]

Note .......... Traduzione ed adattamento ad EAD di Luigi R. Callegari

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

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

#ifdef LATTICE
     void main( int, char *argv[] );
#    include <proto/exec.h>
#    include <proto/intuition.h>
#    include <proto/graphics.h>
#    include <string.h>
#    include <stdlib.h>
#else
#    include <functions.h>
#endif

#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};

struct Window        *QWindow;
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 ( 1L << QWindow -> UserPort -> mp_SigBit );
	MyMsg = (struct IntuiMessage *)GetMsg( QWindow -> UserPort );

	Class = MyMsg -> Class; Code = MyMsg -> Code;
     key=toupper((char ) Code);

	ReplyMsg((struct Message *)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 );
}

void 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(0L);
	}
	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((struct Library *)IntuitionBase);
	CloseLibrary((struct Library *)GfxBase);
	if (ret) exit(5L);
	exit(0L);
}
