/*
     options.c : Options input prossing and support subroutines
	             (Part of Autograf)

	 By Joel Swank September 3, 1988
	 Version 1.0

	 */

#include <intuition/intuition.h>
#include <intuition/intuitionbase.h> 
#include <graphics/gfxbase.h> 
#include <graphics/gfx.h> 
#include <graphics/gfxmacros.h> 
#include <graphics/display.h> 
#include <graphics/text.h> 
#include <ctype.h> 
#include "optwind.h"

#ifdef NULL
#undef NULL
#endif
#define NULL ((void *)0)

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

 *  Hooks to the rest of the program

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

#define MenuList1 Menu1

extern struct IntuiText OptText1;
extern struct Menu Menu1;
extern struct Window	*Wind;
extern struct RastPort *rp;

/* flags for which grafs to display */
extern int cpm;
extern int ppg;
extern int mpg;

extern int syr;		/* for requested start year */
extern int eyr;		/* for requested end year */

/* list of the gadgets on the options window */
struct Gadget *gadlist[10] = { &Gadget1, &Gadget2, &Gadget3, &Gadget4, 
					&Gadget5, &Gadget6, &Gadget7, NULL };

/*
 * get_opt : get options via option window
 *
 */

get_opt()
{
	int tyr, i;
	struct IntuiMessage	*msg;
	UWORD code;
	ULONG class;
	APTR object;

	ClearMenuStrip(Wind);
	SetWindowTitles(Wind,(UBYTE *) "AutoGraf Options Selection", -1L);
	clr_grf();

	PrintIText(rp,&IntuiTextList1,0L,0L);	/* Print the gadget text */
	PrintIText(rp,&OptText1,0L,0L);	/* Print the support text */

	i=0;
	while (gadlist[i] != NULL)
		{
		AddGadget(Wind,gadlist[i],(USHORT) ~0);
		i++;
		}
	RefreshGadgets(gadlist[0],Wind,0L);

	while (1) {
	Wait(1L<<Wind->UserPort->mp_SigBit );
	while(msg = (struct IntuiMessage *) GetMsg(Wind->UserPort)) {
		code = msg->Code;  /* MENUNUM */
		object = msg->IAddress;  /* Gadget */
		class = msg->Class;
		switch(class) {
			case CLOSEWINDOW:
				ReplyMsg(msg);
				done(0);
			case GADGETUP:
				clr_msg();
				if (Gadget3.Flags & SELECTED) cpm = TRUE;
					else cpm = FALSE;
				if (Gadget5.Flags & SELECTED) ppg = TRUE;
					else ppg = FALSE;
				if (Gadget4.Flags & SELECTED) mpg = TRUE;
					else mpg = FALSE;
				if (Gadget6.Flags & SELECTED) 
					{
					ppg = TRUE;
					cpm = TRUE;
					mpg = TRUE;
					}
				if (Gadget1SIBuff[0] == '\0') syr = -1;
					else
					if ((tyr = val_yr(Gadget1SIBuff)) == -1)
						{
						lite_msg();
						PrintIText(rp,&ErrText1,0L,0L);
						break;
					}else 
						{
						syr = tyr;
						}
				if (Gadget2SIBuff[0] == '\0') eyr = -1;
					else
					if ((tyr = val_yr(Gadget2SIBuff)) == -1)
						{
						lite_msg();
						PrintIText(rp,&ErrText2,0L,0L);
						break;
					}else  {
						eyr = tyr;
						if (syr != -1 && syr > eyr)
							{
							lite_msg();
							PrintIText(rp,&ErrText3,0L,0L);
							break;
							}
						}
				if (msg->IAddress == &Gadget7) goto Dun;  /* OK button */
			case REFRESHWINDOW:
				break;
		}
		ReplyMsg(msg);
	}
	}	/* end while(Wait())    */
  Dun:
	i=0;
	while (gadlist[i] != NULL)
		{
		RemoveGadget(Wind,gadlist[i]);
		i++;
		}

	if (mpg == FALSE && ppg == FALSE && cpm == FALSE) ppg = TRUE;
	SetMenuStrip(Wind,&MenuList1);
	clr_grf();
}

/*
 * lite_msg : Set the err-message area of the window to white
 *
 */
lite_msg()
{
	SetAPen(rp,1L);
	RectFill(rp, XOPT+4L, YOPT+3L,XOPT+155L,YOPT+13L);
	SetAPen(rp,3L);
}

/*
 * clr_msg : clear the err-message area of the screen
 *
 */
clr_msg()
{
	SetAPen(rp,0L);
	RectFill(rp, XOPT+4L, YOPT+3L,XOPT+155L,YOPT+13L);
	SetAPen(rp,3L);
}


/*
 * val_yr : validate a year string gadget contents and return
 *          -1 if invalid or year if valid.
 */

val_yr(buffer)
char *buffer;
{
	int yr;
	char *bufp;
	bufp = buffer;
	while (*bufp != '\0')
		{
		if (!isdigit(*bufp)) return -1;
		bufp++;
		}
	yr = atoi(buffer);
	if (yr <100 && yr >= 0) return yr+1900;
	if (yr <2200 && yr >= 1900) return yr;
	return -1;
}

