/* Date Requester Package
 * Author:		Mark R. Rinfret
 * Description:
 *		This source file contains a canned date requester routine which
 * supports time and date entry in MM/DD/YY HH:MM:SS notation (time is
 * optional) to a DateStamp structure.  This package is dependent upon
 * the package "dates.c" (same author) for DateStamp/string conversions.
 *
 * History:		(most recent change first)
 *
 * 07/20/87 -MRR- Created this file.
 */


#include <intuition/intuition.h> 
#include <intuition/intuitionbase.h> 
#include <libraries/dosextens.h> 
#include <graphics/gfxbase.h> 
#include <graphics/gfx.h> 
#include <graphics/display.h> 

#include <graphics/text.h> 
#include <functions.h>

#include <ctype.h> 

/**********************************************************************
 *                    Gadget Structure Definitions
 * 
 * The following structures were defined using EGAD! (by the Programmer's
 * Network) and further modified by the author.
 * 
 **********************************************************************/
/*  Definitions for Gadget ID numbers */ 
#define DATEGAD 0

/**********************************************************************
 *  Text attribute structures used in rendering IntuiTexts
 **********************************************************************/


static char def_font[] ="topaz.font";

static struct TextAttr TxtAt_Plain = { 
	(UBYTE *)def_font, 8, FS_NORMAL, FPF_ROMFONT
	};



/**********************************************************************
 *  Border Definitions for dategad gadget
 **********************************************************************/

static SHORT dategad_Pairs_1[] = {
  0,     0,   
  150,   0,   
  150,   9,   
  0,     9,   
  0,     0    
};

/**********************************************************************
 *  String information for the dategad string gadget.
 **********************************************************************/

static UBYTE dategad_sbuf_1[19] = "00/00/00 00:00:00";
static UBYTE dategad_ubuf_1[19];

static struct StringInfo dategad_txstr_1 = {
  dategad_sbuf_1, dategad_ubuf_1, 	/* Buffer, UndoBuffer  */
  0, 18, 0,  						/* BufferPos, MaxChars, DispPos   */
  0, 18,      						/* UndoPos, NumChars */
  0, 0, 0,  						/* DispCount, CLeft, CTop */
  0x0, 0,   						/* LayerPtr, LongInt */
  0x0        						/* AltKeyMap */
};


/**********************************************************************
 *  Gadget Structure definition for the dategad gadget.
 **********************************************************************/

static struct Gadget dategad = {
  NULL,     /* NextGadget pointer */
  64, 40,    /* LeftEdge, TopEdge  */
  150, 9,    /* Width, Height      */
  /* Gadget Flags */
   GADGHCOMP,
  /* Activation Flags */
	RELVERIFY /* | ENDGADGET */,
  /* GadgetType */
  REQGADGET | STRGADGET,		/* string gadget belongs to requester */
	NULL,						/* GadgetRender - no border */
  NULL,    						/* SelectRender */
   NULL,    					/* GadgetText */
  0x0,    						/* MutualExclude */
  (APTR)&dategad_txstr_1,   	/* SpecialInfo */
  DATEGAD,    					/* GadgetID */
  NULL							/* UserData Pointer */
};


static struct Requester daterequest;	/* requester for the date */

static SHORT datereq_pairs1[] = {
  30,  10,   
  230, 10,   
  250, 30,   
  250, 50,   
  230, 70,
  30,  70,
  10,  50,
  10,  30,
  30,  10
};

static struct Border datereq_bord1 = {
  -1,  -1,       /* LeftEdge, TopEdge */
  2,  1,  JAM2,  /* FrontPen, BackPen, DrawMode*/
  9,             /* Count of XY pairs */  
  (SHORT *)&datereq_pairs1, /* XY pairs */
  NULL           /* Next Border */
};

static struct IntuiText datereqtext = {
	2, 1,						/* FrontPen / BackPen */
	JAM2,						/* DrawMode */
	20,							/* LeftEdge */
	30,							/* TopEdge */
	&TxtAt_Plain,				/* ITextFont */
	(UBYTE *) "Enter date:",	/* IText */
	NULL						/* NextText */
	};

static initialized = 0;			/* set to 1 after initialization */

static InitDateRequest()
{
	InitRequester(&daterequest);
	daterequest.LeftEdge = 60;
	daterequest.TopEdge = 10;
	daterequest.Width = 260;
	daterequest.Height = 80;
	daterequest.ReqGadget = &dategad;
	daterequest.ReqText = &datereqtext;
	daterequest.ReqBorder = &datereq_bord1;
	daterequest.BackFill = 0;
	++initialized;
}

/* Request a date from the user.
 * Called with:
 *		window:			pointer to window structure
 *		prompt:			string to be used as the prompt text
 *		default_date:	pointer to default date or NULL
 *		date:			pointer to date result (returned)
 * Returns:
 *		status code:	0 => success, 1 => failure
 *		(actually, always returns 0 - can't leave without good date)
 */

DateRequest(window,prompt,default_date,date)
	struct Window *window; char *prompt; 
	struct DateStamp *default_date,*date;
{
	ULONG class;				/* message class */
	USHORT code;				/* message code */
	USHORT gadgid;				/* gadget ID */
	APTR Iadr;					/* address field from message */
	struct IntuiMessage *msg;	/* Intuition message pointer */
	SHORT x,y;					/* mouse x and y position */

	if (!initialized)
		InitDateRequest();
	datereqtext.IText = prompt;
	if (default_date)			/* write default date in requester? */
		DS2Str(dategad_sbuf_1,"%02m/%02d/%02y %02h:%02n:%02s",
			default_date);
	else
		*dategad_sbuf_1 = '\0';

	Request(&daterequest, window);

	for (;;) {
		Wait(1L << window->UserPort->mp_SigBit);
		while (msg = GetMsg(window->UserPort)) {
			class = msg->Class;
			code = msg->Code;
			Iadr = (APTR) msg->IAddress;
			x = msg->MouseX;
			y = msg->MouseY;
			ReplyMsg(msg);		/* acknowledge the message */

			switch (class) {

			case REQSET: 
				ActivateGadget(&dategad,window,&daterequest);
				break;
			
			case GADGETUP:
				if ( ! Str2DS(dategad_sbuf_1, date) ) {
					EndRequest(&daterequest, window);
					return 0;
				}
				else
					DisplayBeep(window->WScreen); /* bad conversion */
				break;

			default:
				break;			/* ignore the rest */
			}					/* end switch(class) */
		}
	}
}

/* #define DEBUG */

#ifdef DEBUG

/* --- Only compiled in the debug version --- */

#include <exec/memory.h>

/* New window structure */

struct NewWindow newwindow = {
	0,0,640,200,0,1,

/* IDCMP Flags */

	MOUSEMOVE | MENUPICK | MOUSEBUTTONS | 
	CLOSEWINDOW | GADGETDOWN | GADGETUP | REQSET, 

/* Flags */
	WINDOWCLOSE | WINDOWDEPTH | ACTIVATE | RMBTRAP | REPORTMOUSE,

	NULL,							/* First gadget */
	NULL,							/* Checkmark */
	(UBYTE *)"Date Requester Test Program",	/* Window title */
	NULL,							/* No custom streen */
	NULL,							/* Not a super bitmap window */
	0,0,640,200,					/* Not used, but set up anyway */
	WBENCHSCREEN
};
struct IntuitionBase *IntuitionBase;
struct Window *mywindow;
struct DateStamp *ds;

static struct IntuiText MoreText = {
	AUTOFRONTPEN,				/* FrontPen */
	AUTOBACKPEN,				/* BackPen */
	JAM2,						/* DrawMode */
	AUTOLEFTEDGE,				/* LeftEdge */
	AUTOTOPEDGE,				/* TopEdge */
	AUTOITEXTFONT,				/* ITextFont */
	(UBYTE *) "Want to play some more?", /* IText */
	NULL						/* NextText */
	};

static struct IntuiText YesText = {
	AUTOFRONTPEN,				/* FrontPen */
	AUTOBACKPEN,				/* BackPen */
	AUTODRAWMODE,				/* DrawMode */
	AUTOLEFTEDGE,				/* LeftEdge */
	AUTOTOPEDGE,				/* TopEdge */
	AUTOITEXTFONT,				/* ITextFont */
	(UBYTE *) "Sure!", 			/* IText */
	NULL						/* NextText */
	};

static struct IntuiText NoText = {
	AUTOFRONTPEN,				/* FrontPen */
	AUTOBACKPEN,				/* BackPen */
	JAM2,						/* DrawMode */
	AUTOLEFTEDGE,				/* LeftEdge */
	AUTOTOPEDGE,				/* TopEdge */
	AUTOITEXTFONT,				/* ITextFont */
	(UBYTE *) "Nope!", 			/* IText */
	NULL						/* NextText */
	};

main()
{
	short keep_going;

	if (!(IntuitionBase = (struct IntuitionBase *)
		OpenLibrary("intuition.library",33L))) {
		puts("Failed to open Intuition library!");
		exit(1);
	}

	ds = (struct DateStamp *)
		AllocMem((long) sizeof(struct DateStamp), MEMF_PUBLIC);
	DateStamp(ds);				/* get current date/time */

	mywindow = OpenWindow(&newwindow);

	do {
		DateRequest(mywindow, "Enter date and time", ds, ds);
		keep_going = AutoRequest(mywindow, &MoreText, &YesText, &NoText,
						NULL, NULL, 220L, 50L);
	} while (keep_going);

	if (mywindow)
		CloseWindow(mywindow);

	if (IntuitionBase)
		CloseLibrary(IntuitionBase);
}

#endif
