/* Request.c - Support file of Intuition for pmod23 */

#include <stdio.h>
#include "pmod23.h"

USHORT __chip UP_ImageData[] = {
	0xFFFF,0xFFFF,0xFFFF,0x8000,0xC000,0x0000,0x0000,0x0000,
	0xC000,0x0000,0x0000,0x0000,0xC000,0x0000,0x0000,0x0000,
	0xC000,0x0000,0x0000,0x0000,0xC000,0x0000,0x0000,0x0000,
	0xC000,0x0000,0x0000,0x0000,0xC000,0x0000,0x0000,0x0000,
	0xC000,0x0000,0x0000,0x0000,0xC000,0x0000,0x0000,0x0000,
	0xC000,0x0000,0x0000,0x0000,0x8000,0x0000,0x0000,0x0000,
	0x0000,0x0000,0x0000,0x4000,0x0000,0x0000,0x0000,0xC000,
	0x0000,0x0000,0x0000,0xC000,0x0000,0x0000,0x0000,0xC000,
	0x0000,0x0000,0x0000,0xC000,0x0000,0x0000,0x0000,0xC000,
	0x0000,0x0000,0x0000,0xC000,0x0000,0x0000,0x0000,0xC000,
	0x0000,0x0000,0x0000,0xC000,0x0000,0x0000,0x0000,0xC000,
	0x0000,0x0000,0x0000,0xC000,0x7FFF,0xFFFF,0xFFFF,0xC000
};

USHORT __chip DOWN_ImageData[] = {
	0x0000,0x0000,0x0000,0x4000,0x0000,0x0000,0x0000,0xC000,
	0x0000,0x0000,0x0000,0xC000,0x0000,0x0000,0x0000,0xC000,
	0x0000,0x0000,0x0000,0xC000,0x0000,0x0000,0x0000,0xC000,
	0x0000,0x0000,0x0000,0xC000,0x0000,0x0000,0x0000,0xC000,
	0x0000,0x0000,0x0000,0xC000,0x0000,0x0000,0x0000,0xC000,
	0x0000,0x0000,0x0000,0xC000,0x7FFF,0xFFFF,0xFFFF,0xC000,
	0xFFFF,0xFFFF,0xFFFF,0x8000,0xC000,0x0000,0x0000,0x0000,
	0xC000,0x0000,0x0000,0x0000,0xC000,0x0000,0x0000,0x0000,
	0xC000,0x0000,0x0000,0x0000,0xC000,0x0000,0x0000,0x0000,
	0xC000,0x0000,0x0000,0x0000,0xC000,0x0000,0x0000,0x0000,
	0xC000,0x0000,0x0000,0x0000,0xC000,0x0000,0x0000,0x0000,
	0xC000,0x0000,0x0000,0x0000,0x8000,0x0000,0x0000,0x0000
};

struct Image Abort_UP_Image = {
	0,0,	/* XY origin relative to container TopLeft */
	50,12,	/* Image width and height in pixels */
	2,	/* number of bitplanes in Image */
	UP_ImageData,	/* pointer to ImageData */
	0x0003,0x0000,	/* PlanePick and PlaneOnOff */
	NULL	/* next Image structure */
};

struct Image Abort_DOWN_Image = {
	0,0,	/* XY origin relative to container TopLeft */
	50,12,	/* Image width and height in pixels */
	2,	/* number of bitplanes in Image */
	DOWN_ImageData,	/* pointer to ImageData */
	0x0003,0x0000,	/* PlanePick and PlaneOnOff */
	NULL	/* next Image structure */
};

struct IntuiText Abort_IText = {
	3,0,JAM2,	/* front and back text pens, drawmode and fill byte */
	6,2,	/* XY origin relative to container TopLeft */
	NULL,	/* font pointer or NULL for default */
	"Abort",	/* pointer to text */
	NULL	/* next IntuiText structure */
};

GAD Abort_Gad = {
	NULL,	/* next gadget */
	250,44,	/* origin XY of hit box relative to window TopLeft */
	50,12,	/* hit box width and height */
	GADGHIMAGE | GADGIMAGE,	/* gadget flags */
	RELVERIFY | GADGIMMEDIATE,	/* activation flags */
	BOOLGADGET,	/* gadget type flags */
	(APTR)&Abort_UP_Image,	/* gadget border or image to be rendered */
	(APTR)&Abort_DOWN_Image,	/* alternate imagery for selection */
	&Abort_IText,	/* first IntuiText structure */
	NULL,	/* gadget mutual-exclude long word */
	NULL,	/* SpecialInfo structure */
	3,	/* gadget ID */
	NULL	/* pointer to user-definable data */
};

struct Image Retry_UP_Image = {
	0,0,	/* XY origin relative to container TopLeft */
	50,12,	/* Image width and height in pixels */
	2,	/* number of bitplanes in Image */
	UP_ImageData,	/* pointer to ImageData */
	0x0003,0x0000,	/* PlanePick and PlaneOnOff */
	NULL	/* next Image structure */
};

struct Image Retry_DOWN_Image = {
	0,0,	/* XY origin relative to container TopLeft */
	50,12,	/* Image width and height in pixels */
	2,	/* number of bitplanes in Image */
	DOWN_ImageData,	/* pointer to ImageData */
	0x0003,0x0000,	/* PlanePick and PlaneOnOff */
	NULL	/* next Image structure */
};

struct IntuiText Retry_IText = {
	1,0,JAM2,	/* front and back text pens, drawmode and fill byte */
	5,2,	/* XY origin relative to container TopLeft */
	NULL,	/* font pointer or NULL for default */
	"Retry",	/* pointer to text */
	NULL	/* next IntuiText structure */
};

GAD Retry_Gad = {
	&Abort_Gad,	/* next gadget */
	133,44,	/* origin XY of hit box relative to window TopLeft */
	50,12,	/* hit box width and height */
	GADGHIMAGE | GADGIMAGE,	/* gadget flags */
	RELVERIFY | GADGIMMEDIATE,	/* activation flags */
	BOOLGADGET,	/* gadget type flags */
	(APTR)&Retry_UP_Image,	/* gadget border or image to be rendered */
	(APTR)&Retry_DOWN_Image,	/* alternate imagery for selection */
	&Retry_IText,	/* first IntuiText structure */
	NULL,	/* gadget mutual-exclude long word */
	NULL,	/* SpecialInfo structure */
	2,	/* gadget ID */
	NULL	/* pointer to user-definable data */
};

struct Image Skip_UP_Image = {
	0,0,	/* XY origin relative to container TopLeft */
	50,12,	/* Image width and height in pixels */
	2,	/* number of bitplanes in Image */
	UP_ImageData,	/* pointer to ImageData */
	0x0003,0x0000,	/* PlanePick and PlaneOnOff */
	NULL	/* next Image structure */
};

struct Image Skip_DOWN_Image = {
	0,0,	/* XY origin relative to container TopLeft */
	50,12,	/* Image width and height in pixels */
	2,	/* number of bitplanes in Image */
	DOWN_ImageData,	/* pointer to ImageData */
	0x0003,0x0000,	/* PlanePick and PlaneOnOff */
	NULL	/* next Image structure */
};

struct IntuiText Skip_IText = {
	1,0,JAM2,	/* front and back text pens, drawmode and fill byte */
	9,2,	/* XY origin relative to container TopLeft */
	NULL,	/* font pointer or NULL for default */
	"Skip",	/* pointer to text */
	NULL	/* next IntuiText structure */
};

GAD Skip_Gad = {
	&Retry_Gad,	/* next gadget */
	13,44,	/* origin XY of hit box relative to window TopLeft */
	50,12,	/* hit box width and height */
	GADGHIMAGE | GADGIMAGE,	/* gadget flags */
	RELVERIFY | GADGIMMEDIATE,	/* activation flags */
	BOOLGADGET,	/* gadget type flags */
	(APTR)&Skip_UP_Image,	/* gadget border or image to be rendered */
	(APTR)&Skip_DOWN_Image,	/* alternate imagery for selection */
	&Skip_IText,	/* first IntuiText structure */
	NULL,	/* gadget mutual-exclude long word */
	NULL,	/* SpecialInfo structure */
	1,	/* gadget ID */
	NULL	/* pointer to user-definable data */
};

UBYTE UNDOBUFFER[81], String_GadSIBuff[81];

struct StringInfo String_GadSInfo = {
	String_GadSIBuff,	/* buffer where text will be edited */
	UNDOBUFFER,	/* optional undo buffer */
	0,	/* character position in buffer */
	81,	/* maximum number of characters to allow */
	0,	/* first displayed character buffer position */
	0,0,0,0,0,	/* Intuition initialized and maintained variables */
	0,	/* Rastport of gadget */
	0,	/* initial value for integer gadgets */
	NULL	/* alternate keymap (fill in if you set the flag) */
};

SHORT File_BorderVectors[] = {
	0,0,
	286,0,
	286,13,
	0,13,
	0,1
};
struct Border File_Border = {
	-4,-3,	/* XY origin relative to container TopLeft */
	3,0,JAM2,	/* front pen, back pen and drawmode */
	5,	/* number of XY vectors */
	File_BorderVectors,	/* pointer to XY vectors */
	NULL	/* next border in list */
};

char String_Text[25];

struct IntuiText String_IText = {
	1,0,JAM2,	/* front and back text pens, drawmode and fill byte */
	-4,-13,	/* XY origin relative to container TopLeft */
	NULL,	/* font pointer or NULL for default */
	String_Text,	/* pointer to text */
	NULL	/* next IntuiText structure */
};

GAD String_Gad = {
	&Skip_Gad,	/* next gadget */
	17,28,	/* origin XY of hit box relative to window TopLeft */
	280,10,	/* hit box width and height */
	NULL,	/* gadget flags */
	RELVERIFY | GADGIMMEDIATE,	/* activation flags */
	STRGADGET,	/* gadget type flags */
	(APTR)&File_Border,	/* gadget border or image to be rendered */
	NULL,	/* alternate imagery for selection */
	&String_IText,	/* first IntuiText structure */
	NULL,	/* gadget mutual-exclude long word */
	(APTR)&String_GadSInfo,	/* SpecialInfo structure */
	0,	/* gadget ID */
	NULL	/* pointer to user-definable data */
};

struct NewWindow ReqWindow = {
	174,88,	/* window XY origin relative to TopLeft of screen */
	312,61,	/* window width and height */
	0,1,	/* detail and block pens */
	GADGETDOWN | GADGETUP,	/* IDCMP flags */
	SMART_REFRESH | WINDOWDRAG | WINDOWDEPTH | ACTIVATE,	/* other window flags */
	&String_Gad,	/* first gadget in gadget list */
	NULL,	/* custom CHECKMARK imagery */
	NAME,	/* window title */
	NULL,	/* custom screen pointer */
	NULL,	/* custom bitmap */
	5,5,	/* minimum width and height */
	-1,-1,	/* maximum width and height */
	WBENCHSCREEN	/* destination screen type */
};

struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;


/* Sets up the intuition environment for the user. */
char Open_All(void) {
	IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", INTUITION_REV);
	if(IntuitionBase == NULL) return(0);
	GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", GRAPHICS_REV);
	if(GfxBase == NULL) {
		CloseLibrary(IntuitionBase);
		return(0);
	} /* if */
	return(1);
} /* Open_All() */


/* Closes the intuition and graphics libs. */
void CloseLibs(void) {
	CloseLibrary(GfxBase);
	CloseLibrary(IntuitionBase);
} /* CloseLibs() */


/*
** Brings up the spiffy error requestor
*/
void Do_Request(char *string, GRESPTR result, char req_type) {
	struct Window *Wind;
	USHORT gadID;
	char option[81] = "-";

	/* check the argument type and place the appropriate text in the window */
	if(req_type == FILE_REQ)
		strcpy(String_Text, "Could not find file:");
	else if(req_type == OPTION_REQ)
		sprintf(String_Text, "Unknown option \"%c\":", *string);
	strcpy(String_GadSIBuff, string);
	strcpy(UNDOBUFFER, string);
	if(Open_All() == 0) {
		printf("%s: Couldn't open Intuition\n", NAME);
		result->ID = NOTHING;
		return;
	} /* if */
	if((Wind = (struct Window *)OpenWindow(&ReqWindow)) == NULL) {
		printf("%s: Could not open window\n", NAME);
		CloseLibs();
		result->ID = NOTHING;
		return;
	} /* if */
	/* Whee! Give a beep! */
	DisplayBeep(Wind->WScreen);
	/* poll the message port for one of the buttons */
	while((gadID = pop_gadget(Wind)) == NOTHING);
	result->ID = gadID;
	switch(gadID) {
		case RETRY_UP:
			if(req_type == OPTION_REQ) {
				/* ugh.  Make sure this arg has a dash in front of it. */
				strcat(option, String_GadSIBuff);
				strcpy(String_GadSIBuff, option);
			} /* if */
			result->string = String_GadSIBuff;
		case SKIP_UP:
		case ABORT_UP:
			break;
		default:
			printf("%s: Unknown gadget ID: %u\n", NAME, gadID);
			result->ID = NOTHING;
	} /* switch */
	CloseWindow(Wind);
	CloseLibs();
} /* Do_Request() */


/* checks the requester's message port for buttons and returns it's ID */
USHORT pop_gadget(struct Window *Wind) {
	ULONG class;
	USHORT gadget;
	APTR address;
	struct IntuiMessage *my_message;

	/* Check if we have recieved a message: */
	Wait(1 << Wind->UserPort->mp_SigBit);

	/* Collect the message: */
	my_message = (struct IntuiMessage *)GetMsg(Wind->UserPort);

	/* Have we collected the message sucessfully? */
	if(my_message) {
		/* After we have collected the message we can read it, and save any */
		/* important values which we maybe want to check later: */

		class = my_message->Class;      /* Save the IDCMP flag. */
		address = my_message->IAddress; /* Save the address. */

		/* After we have read it we reply as fast as possible: */
		/* REMEMBER! Do never try to read a message after you have replied! */
		/* Some other process has maybe changed it. */

		ReplyMsg(my_message);

		/* Check which IDCMP flag was sent: */
		if(class == GADGETUP) {
			/* check which gadget was struck */
			gadget = ((GADPTR)address)->GadgetID;
			if(gadget >= SKIP_UP && gadget <= ABORT_UP)
				return(gadget);
		} /* if */
	} /* if */
	return(NOTHING);
} /* pop_gadget() */
