/*
 *  Skeleton Workbench application program
 *
 *    Version 1
 *
 *    by Joel Swank 1-9-89
 *
 */

/********* INCLUDES ************************ */


#include <exec/types.h>
#include <exec/io.h>
#include <exec/memory.h>
#include <libraries/dos.h>
#include <intuition/intuition.h>


/* Text in helpwin.h  */
extern struct IntuiText opfailtxt ;
extern struct IntuiText oktxt ;
extern struct IntuiText cantxt ;
extern struct IntuiText retrytxt ;

/* Data in doargs.c   */
extern char title[];
extern char filename[];

/* Defines for setting/clearing GADGDISABLED flag */
#define OffGad(gad) (gad).Flags = gad.Flags | GADGDISABLED;
#define OnGad(gad) (gad).Flags = gad.Flags & ~(GADGDISABLED);


struct IntuitionBase *IntuitionBase ;
struct GfxBase *GfxBase ;

struct Window *wG = NULL;
struct RastPort *rpG;
struct IntuiMessage *message;	/* the message from the IDCMP */

struct Window *OpenWindow();
void *OpenLibrary();
struct IntuiMessage *GetMsg();
struct MenuItem *ItemAddress();

/* get the PowerWindows 2.0 code */
#include "skwindow.h"

/* internal modes */
long delay = 10L;      /* draw speed control */
char deftitle[]  /* Default Window title */
      = "No Title Specified";



main(argc,argv)
int argc;
char *argv[];
{
	UWORD code;
	ULONG class;
	APTR object;
 

	strcpy(title,deftitle);    /* set default title */

	/* Open the libraries */

	IntuitionBase = (struct IntuitionBase *)
		OpenLibrary("intuition.library", 0L);
	if (IntuitionBase == NULL)
		{
		done(11);
		}
	GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 0L);
	if (GfxBase == NULL)
		{
		done(10);
		}

	/*   Get the arguments */
	if (argc == 0) getWBargs();
	else getCLIargs(argc,argv);

	/* Set up the Gadgets */
	OffGad(Stop_Gad)


	wG = OpenWindow(&NewWindowStructure1);	/* open the window */
	if ( wG == NULL )
		{
		done(12);
		}

	rpG = wG->RPort;	/* get a rastport pointer for the window */

	SetMenuStrip(wG,&MenuList1);	/* attach my Menu */

	do_title();      /* write the title */


	/* Wait for some User interaction */
	while(1)
	{
		WaitPort(wG->UserPort);
			while( (message = (struct IntuiMessage *)
				GetMsg(wG->UserPort) ) != NULL)
			{
				code = message->Code;  /* MENUNUM */
				object = message->IAddress;  /* Gadget */
				class = message->Class;  /* IDCMP Flags */
				ReplyMsg(message);  /* Free the sender */
				switch (class)
					{
					case CLOSEWINDOW :
						done(0);   /* close gadget clicked */
					case GADGETUP:
						if (object == (APTR) &Go_Gad)
							{
							do_it();
							break;
							}
						/* add more gadget checks here */
						break;
					case MENUPICK:    /* menu selection made */
						/* I get a NULL message whenever the user brings
						   up the menu but doesn't select anything    */
						if (code != MENUNULL) do_pick((USHORT)code);
						break;
					}
			}
	} 
}

/*
 * Cleanup and exit
 */

done(how)
int how;
{
	if (wG) ClearMenuStrip(wG);
	if (wG) CloseWindow(wG);
	if (GfxBase != NULL) CloseLibrary(GfxBase);
	if (IntuitionBase != NULL) CloseLibrary(IntuitionBase);
	exit(how);

 /* exit codes 
  * 0  User requested
  * 10 graphics lib open fail
  * 11 intuition lib open fail
  * 12 main window open fail
  * 13 _abort() called
  * 14 Bad CLI args
  */
}


/*
 * do_pick : handle chain of menu selections
 */

do_pick(menunum)
USHORT menunum;
{
struct MenuItem *item, *ItemAddress();
	while (menunum != MENUNULL)
		{
		switch(MENUNUM(menunum))
			{
			case 0:     /* Project Menu */
			switch(ITEMNUM(menunum))
				{
				case 0: /* Open */
					do_open();
					break;
				case 1: /* About */
					about();
					break;
				case 2: /* Help */
					help();
					break;
				case 3: /* Quit */
					done(0);
				}
			break;
			case 1:     /* Options Menu */
			switch(ITEMNUM(menunum))
				{

				/* Three mutually excluded items. Really nothing to do.
				   intuition keeps track, I just read the CHECKED flag
				   in the SubItem              */

				case 0:     /* WB Color Number */
				switch(SUBNUM(menunum))
					{
					case 0:   /* 1 */
						break;
					case 1:   /* 2 */
						break;
					case 2:   /* 3 */
						break;
					}
				break;
				}
			break;
			default:	/* What's this garbage ? */
				menunum = MENUNULL;
			}       /* end switch MENUNUM  */

		/* Get chain to next selection. NextSelect contains another item
		   when the user makes multiple menu selections             */
		item = ItemAddress(&MenuList1,(long) menunum);
		menunum = item->NextSelect;
		}
}


/*
 *  do something
 */

do_it()
{
	ULONG class;
	struct IntuiMessage *message;	/* the message from the IDCMP */
	long color;

	/* get color from menu structure */
	if (SubItem1.Flags &CHECKED) color = 1;
	if (SubItem2.Flags &CHECKED) color = 2;
	if (SubItem3.Flags &CHECKED) color = 3;

	off_gads(); /* disable gads */

	while (1)  /* do till stop  or close msg comes in */
		{

		if ( (message = (struct IntuiMessage *)
		  GetMsg(wG->UserPort) ) != NULL)
			{
			class = message->Class;
			ReplyMsg(message);
			if (class == CLOSEWINDOW)  done(0); /* exit */
			if (class == MENUPICK) continue; /* ignore menus */
			break;    /* else STOP */
			}

		/* Just something to do: Flash a couple of lines */

		SetAPen(rpG,color);
		Move(rpG,50L,100L);
		Draw(rpG,80L,100L);
		if (delay) Delay(delay);

		SetAPen(rpG,0L);
		Draw(rpG,50L,100L);
		if (delay) Delay(delay);

		SetAPen(rpG,color);
		Move(rpG,65L,115L);
		Draw(rpG,65L,85L);
		if (delay) Delay(delay);

		SetAPen(rpG,0L);
		Draw(rpG,65L,115L);
		SetAPen(rpG,color);
		if (delay) Delay(delay);

		}

	on_gads(); /* enable gads */
}

/*
 * Sample open routine that does nothing but test an AutoRequest
 */

do_open()
{

	/* Put in a call to your favorite file requester */

	while (1 /* would normally attempt open here */ )
		{
		if (AutoRequest(wG,&opfailtxt,&retrytxt,&cantxt,
			0L,0L,300L,75L)) continue;
		return;
		}

}



/*
 * turn back on gadgets after doing it
 */

on_gads()
{
	RemoveGList(wG,&GadgetList1,-1L); /* remove all gadgets */
	OffGad(Stop_Gad);   /* disable STOP */
	OnGad(Go_Gad);      /* Enable all the rest */
	redraw_scr();	     /* add back gadgets and refresh screen */
	SetMenuStrip(wG,&MenuList1);	/* attach my Menu */
}

/*
 * disable gadgets while doing it
 */

off_gads()
{
	ClearMenuStrip(wG);      /* disable menus */
	RemoveGList(wG,&GadgetList1,-1L); /* remove all gadgets */
	OnGad(Stop_Gad);     /* enable STOP */
	OffGad(Go_Gad);      /* Disable all the rest */
	redraw_scr();	     /* add back gadgets and refresh screen */
}

/*
 * Redraw Window
 *  Assumes Gadgets have been removed
 */

redraw_scr()
{
	SetAPen(rpG,0L);
	RectFill(rpG,2L,15L,(long)(wG->Width-3),(long)(wG->Height-2));
	AddGList(wG,&GadgetList1,0L,-1L,NULL);
	RefreshGList(&GadgetList1,wG,NULL,-1L);
	do_title();
}

/*
 * Put up title line
 */

do_title()
{
	long xpos, txtlen;
	if (title[0] == '\0') return;
	txtlen = (long) strlen(title);
	xpos = (wG->Width - 4 - TextLength(rpG,title,txtlen)) / 2 + 2;
	if (xpos < 3 ) xpos = 3;
	SetAPen(rpG,3L);
    Move(rpG,xpos,25L);
	Text(rpG,title,txtlen);
}

#ifdef AZTEC_C
_abort()
{
	done(13);
}
#endif
