/********************************************************************
 *																	*
 *	WinLIB PRO 0.64A												*
 *	Test program													*
 *																	*
 *	Copyright (C) 1993 - 1994, Clever Bits and Bitgate Software.	*
 *	All rights reserved.											*
 *																	*
 *	The main demonstration program.  Although we try to demonstrate	*
 *	all of the useful functions of WinLIB PRO, we cannot assure		*
 *	that the entire functionality will be displayed.				*
 *																	*
 ********************************************************************
 *																	*
 *	Update log:														*
 *																	*
 *	[12.11.93 - 29.3.94] Ken Hollis									*
 *		TextDispatchers		- removed, added version info and fixed	*
 *							  some routines							*
 *		AboutDispatcher		- changed the about boxes to NAME|MOVER	*
 *							  since it doesn't make sense to have it*
 *							  closable or fullable since it IS MODAL*
 *		EtcDispatcher		- added resource stuff, sliders, and	*
 *							  popups; looks better now.				*
 *							- added MN_SELECTED (sideroot) items	*
 *							- added accessory calling routine		*
 *		DlgDispatchers		- fixed some incoherence				*
 *							- added slider routines					*
 *							- added slider counter stuff			*
 *							- added popup object display			*
 *							- fixed popup selection display stuff	*
 *		WndDispatcher		- added timer message (removed timer	*
 *								dispatchers; it was a waste)		*
 *							- added task switching management		*
 *		[resource]			- fixed resource file up a bit			*
 *							- added resource info section			*
 *							- added key equivalents for all dialogs *
 *							- added size check in Dialog 4			*
 *							- popups and sliders are now drawn in 3D*
 *		[global]			- added dominance settings to all wins	*
 *																	*
 ********************************************************************/

#include "winlib.h"
#include "nkcc.h"
#include <vdi.h>
#include <stddef.h>
#include <stdio.h>
#include <tos.h>
#include <ext.h>

#include "test.h"

#define RSCFILE "TEST.RSC"
#define	__DEMO__ "** RELEASE THIS AND DIE! **"

GLOBAL int Dlg6Dispatcher(WINDOW *win, int msg_buf[]);

int handle, ap_id, acc_id, AEShandle;
OBJECT *menubar;

char *ColIndex[] =
{ " ", " ", "White", "Black", "Red", "Green", "Blue", "Cyan", "Yellow", "Magenta" };

int wind1parms = 5, wind2parms = 1;
int timecount = 0;

int WindowDispatcher(WINDOW *win, int msg_buf[8])
{
	int pxyarray[4];
	GRECT own, temp;

	switch (*msg_buf) {
		case WM_CREATED:
			win->menubar[ENABONE].ob_state |= DISABLED;
			win->menubar[RESUONE].ob_state |= DISABLED;
			win->menubar[ENABALL].ob_state |= DISABLED;
			win->menubar[RESUALL].ob_state |= DISABLED;

			WCreateTimer(win, T_RUNNING, 500, &wind1parms);
			return TRUE;

		case WM_KILL:
			WStopTimer(win);
			break;

		case WM_TIMER:
			{
				int x, y, w, h;

				if (num_colors >= 256) {
					(*(int *) (win->user))++;
					if ((*(int *) (win->user) >= 255))
						(*(int *) (win->user)) = 0;
				} else if (num_colors > 4) {
					(*(int *) (win->user))++;
					if ((*(int *) (win->user) >= 15))
						(*(int *) (win->user)) = 0;
				} else if (num_colors > 2) {
					(*(int *) (win->user))++;
					(*(int *) (win->user)) &= 0x3;
				} else {
					(*(int *) (win->user))++;
					(*(int *) (win->user)) &= 0x1;
				}

				WWindGet(win, WF_WORKXYWH, &x, &y, &w, &h);
				WRedrawWindow(win, x, y, w, h);	
			}

			return TRUE;

		case WM_TIMECHG:
			switch(msg_buf[4]) {
				case T_RUNNING:
					win->menubar[ENABONE].ob_state |= DISABLED;
					win->menubar[RESUONE].ob_state |= DISABLED;
					win->menubar[STOPONE].ob_state &= ~DISABLED;
					win->menubar[PAUSONE].ob_state &= ~DISABLED;
					return TRUE;

				case T_STOPPED:
					win->menubar[ENABONE].ob_state &= ~DISABLED;
					win->menubar[RESUONE].ob_state &= ~DISABLED;
					win->menubar[STOPONE].ob_state |= DISABLED;
					win->menubar[PAUSONE].ob_state |= DISABLED;
					return TRUE;

				case T_ALLSTOP:
				case T_ALLRUN:
					return TRUE;
			}

			return TRUE;

		case MN_SELECTED:
			switch (msg_buf[4]) {
				case ENABONE:
				case RESUONE:
					WStartTimer(win);
					return FALSE;

				case STOPONE:
				case PAUSONE:
					WStopTimer(win);
					return FALSE;
			}

			return FALSE;

		case WM_CLOSED:
			if (msg_buf[4] == WC_OBJECTABLE) {
				int i;

				i = WFormCustAlert(1, " Multitasking Timer ",
					"Closing this window will stop",
					"the multitasking timer",
					"display.  Do you really wish",
					"to do this?",
					" ", "[Okay", " ", "C[ancel");

				if (i == 1)
					WCloseWindow(win, WC_NOTOBJECTABLE);
				else
					return TRUE;
			} else return FALSE;

		case WM_REDRAW:
			wind_update(BEG_UPDATE);
			graf_mouse(M_OFF, NULL);

			vswr_mode(handle, MD_REPLACE);
			vsf_color(handle, *(int *) win->user);

			own.g_x = msg_buf[4];
			own.g_y = msg_buf[5];
			own.g_w = msg_buf[6];
			own.g_h = msg_buf[7];

			WWindGet(win, WF_FIRSTXYWH, &temp.g_x, &temp.g_y, &temp.g_w, &temp.g_h);
			while (temp.g_w && temp.g_h)
			{
				if (rc_intersect(&temp, &own))
				{
					pxyarray[0] = temp.g_x;
					pxyarray[1] = temp.g_y;
					pxyarray[2] = temp.g_x + temp.g_w - 1;
					pxyarray[3] = temp.g_y + temp.g_h - 1;

					v_bar(handle, pxyarray);
				}
				WWindGet(win, WF_NEXTXYWH, &temp.g_x, &temp.g_y, &temp.g_w, &temp.g_h);
			}

			graf_mouse(M_ON, NULL);
			wind_update(END_UPDATE);

			return TRUE;
	}

	return FALSE;
}

int Window2Dispatcher(WINDOW *win, int msg_buf[8])
{
	int pxyarray[4];
	GRECT own, temp;

	switch (*msg_buf) {
		case WM_CREATED:
			win->menubar[ENABONE].ob_state |= DISABLED;
			win->menubar[RESUONE].ob_state |= DISABLED;
			win->menubar[ENABALL].ob_state |= DISABLED;
			win->menubar[RESUALL].ob_state |= DISABLED;

			ChangeObjectText(win->tree, TCCOUNT, "0", 3, TE_RIGHT);
			ChangeObjectText(win->tree, TDISP, "Timer version 0.01", 3, TE_CNTR);

			WCreateTimer(win, T_RUNNING, 1000, 0);
			return TRUE;

		case WM_KILL:
			WStopTimer(win);
			break;

		case WM_TIMER:
			{
				int x, y;
				char *cnt;

				timecount++;

				sprintf(cnt, "%d", timecount);
				ChangeObjectText(win->tree, TCCOUNT, cnt, 3, TE_RIGHT);

				objc_offset(win->tree, TCCOUNT, &x, &y);
				WRedrawWindow(win, x, y, win->tree[TCCOUNT].ob_width, win->tree[TCCOUNT].ob_height);
			}
			return TRUE;

		case WM_TIMECHG:
			switch(msg_buf[4]) {
				case T_RUNNING:
					win->menubar[ENABONE].ob_state |= DISABLED;
					win->menubar[RESUONE].ob_state |= DISABLED;
					win->menubar[STOPONE].ob_state &= ~DISABLED;
					win->menubar[PAUSONE].ob_state &= ~DISABLED;
					return TRUE;

				case T_STOPPED:
					win->menubar[ENABONE].ob_state &= ~DISABLED;
					win->menubar[RESUONE].ob_state &= ~DISABLED;
					win->menubar[STOPONE].ob_state |= DISABLED;
					win->menubar[PAUSONE].ob_state |= DISABLED;
					return TRUE;

				case T_ALLSTOP:
				case T_ALLRUN:
					return TRUE;
			}

			return TRUE;

		case MN_SELECTED:
			switch (msg_buf[4]) {
				case ENABONE:
				case RESUONE:
					WStartTimer(win);
					return FALSE;

				case STOPONE:
				case PAUSONE:
					WStopTimer(win);
					return FALSE;
			}

			return FALSE;

		case WM_CLOSED:
			if (msg_buf[4] == WC_OBJECTABLE) {
				int i;

				i = WFormCustAlert(1, " Multitasking Timer ",
					"Closing this window will stop",
					"the multitasking timer",
					"display.  Do you really wish",
					"to do this?",
					" ", "[Okay", " ", "C[ancel");

				if (i == 1)
					WCloseWindow(win, WC_NOTOBJECTABLE);
				else
					return TRUE;
			} else return FALSE;
	}

	return FALSE;
}

int TextDispatcher(WINDOW *win, int msg_buf[8])
{
	return FALSE;
}

int ToolDispatcher(WINDOW *win, int msg_buf[8])
{
	switch(*msg_buf) {
		case WM_CLOSED:
			if (msg_buf[4] == WC_OBJECTABLE)
				WFormCustAlert(1, " Toolbar ",
								"Sorry, the toolbar cannot be",
								"closed.",
								"", "", "", " ", "[Okay", " ");
			return TRUE;

		case WM_ICONIFIED:
			WFormCustAlert(1, " Toolbar ",
							"Sorry, the toolbar cannot be",
							"iconified yet.",
							"", "", "", " ", "[Okay", " ");
			return TRUE;

		case WM_DIALOG:
			switch(msg_buf[3]) {
				case SAVE1:
				case SAVE2:
				case SAVE3:
				case LOAD1:
				case LOAD2:
				case LOAD3:
					WFormCustAlert(1, " Toolbar ",
									"Sorry, this is only a demo",
									"therefore, the toolbar is",
									"not actually functional.",
									"It is just to show how",
									"WinLIB PRO can use one.", " ", "[Okay", " ");
					return FALSE;
			}
	}

	return FALSE;
}

int AboutDispatcher(WINDOW *win, int msg_buf[8])
{
	OBJECT *ptr;

	switch (*msg_buf) {
		case WM_CREATED:
			rsrc_gaddr(R_TREE, MENU2, &ptr);
			return FALSE;

		case WM_KILL:
			rsrc_gaddr(R_TREE, MENU2, &ptr);
			return FALSE;
		
		case WM_DIALOG:
			switch(msg_buf[3]) {
				case OKBOX:		WCloseWindow(win, WC_NOTOBJECTABLE);
								return TRUE;
				case INFOBOX:	WCloseWindow(win, WC_NOTOBJECTABLE);
								WCreateWindow(W_TEXTWIN, 0, W_TEXT_ATTRIBUTES, "WinLIB PRO Version Info", NULL,
												-1, 0, -1,
												TextDispatcher, 0,
												30, 50, 150, 100, NULL, D_SWITCHABLE);
								return TRUE;
			}

	}

	return FALSE;
}


int ResourceDispatcher(WINDOW *win, int msg_buf[8])
{
	switch (*msg_buf) {
		case WM_CREATED:
			return FALSE;

		case WM_KILL:
			return FALSE;
	}

	return FALSE;
}


int Dlg1Dispatcher(WINDOW *win, int msg_buf[8])
{
	switch (*msg_buf) {
		case WM_CREATED:
			break;

		case WM_DIALOG:
			switch (msg_buf[3]) {
				case OKBUTTON:
					objc_change(win->tree, OKBUTTON, 0, 0, 0, 0, 0, NORMAL, 0);
					WCloseWindow(win, WC_NOTOBJECTABLE);
					return TRUE;
			}
			break;

		case MN_SELECTED:
			switch (msg_buf[4]) {
				case QUIT_M2:
					WCloseWindow(win, WC_OBJECTABLE);
					return FALSE;
		
				case ABOUT_M2:
					WCreateWindow(W_DIALOGICON | W_UNUNTOPPABLE, 0, NAME|INFO|MOVER|FULLER, "About me...", NULL,
									ABOUTME, 0, -1,
									AboutDispatcher,
									0,
									-1, -1, -1, -1, NULL, D_SWITCHABLE);
					return FALSE;
			}
			break;
	}

	return FALSE;
}


int Dlg2Dispatcher(WINDOW *win, int msg_buf[8])
{
	switch (*msg_buf) {
		case WM_CREATED:
			return FALSE;

		case WM_KILL:
			return FALSE;

		case WM_DIALOG:
			switch (msg_buf[3])
			{
				case EXITER:
					objc_change(win->tree, EXITER, 0, 0, 0, 0, 0, NORMAL, 0);
					WCloseWindow(win, WC_NOTOBJECTABLE);
					return TRUE;
			}
			break;

		case MN_SELECTED:
			switch (msg_buf[4]) {
				case QUIT_M2:
					WCloseWindow(win, WC_OBJECTABLE);
					return FALSE;
		
				case ABOUT_M2:
					WCreateWindow(W_DIALOGICON | W_UNUNTOPPABLE, 0, NAME|MOVER|INFO|FULLER, "About me...", " Copyright \275 1992 - 1994",
									ABOUTME, 0, -1,
									AboutDispatcher,
									0,
									-1, -1, -1, -1, NULL, D_SWITCHABLE);
					return FALSE;
			}
			break;
	}

	return FALSE;
}


int Dlg3Dispatcher(WINDOW *win, int msg_buf[8])
{
	switch (*msg_buf) {
		case WM_CREATED:
			return FALSE;

		case WM_KILL:
			return FALSE;

		case WM_DIALOG:
			{
				OBJECT *p_up;
				int tm;

				rsrc_gaddr(R_TREE, POPUP, &p_up);

				switch (msg_buf[3]) {
					case PU1:
					case CYCLE1:
						PDropdown_WinXY(win, PU1, p_up);
						tm = PDoPopup(win, POPUP, menubar, FALSE, win->tree[PU1].ob_x, win->tree[PU1].ob_y, win->tree[PU1].ob_width, win->tree[PU1].ob_height);
						if (tm>0) {
							win->popups[1] = tm;
							ChangeObjectText(win->tree, PU1, ColIndex[win->popups[1]], 3, TE_CNTR);
							WObjc_Draw(win->tree, PU1, 1, desk.g_x, desk.g_y, desk.g_w, desk.g_h);
						}
						return FALSE;

					case PU2:
					case CYCLE2:
						PDropdown_WinXY(win, PU2, p_up);
						tm = PDoPopup(win, POPUP, menubar, FALSE, win->tree[PU2].ob_x, win->tree[PU2].ob_y, win->tree[PU2].ob_width, win->tree[PU2].ob_height);
						if (tm>0) {
							win->popups[2] = tm;
							ChangeObjectText(win->tree, PU2, ColIndex[win->popups[2]], 3, TE_CNTR);
							WObjc_Draw(win->tree, PU2, 1, desk.g_x, desk.g_y, desk.g_w, desk.g_h);
						}
						return FALSE;

					case PU3:
					case CYCLE3:
						PDropdown_WinXY(win, PU3, p_up);
						tm = PDoPopup(win, POPUP, menubar, FALSE, win->tree[PU3].ob_x, win->tree[PU3].ob_y, win->tree[PU3].ob_width, win->tree[PU3].ob_height);
						if (tm>0) {
							win->popups[3] = tm;
							ChangeObjectText(win->tree, PU3, ColIndex[win->popups[3]], 3, TE_CNTR);
							WObjc_Draw(win->tree, PU3, 1, desk.g_x, desk.g_y, desk.g_w, desk.g_h);
						}
						return FALSE;

					case PU4:
					case CYCLE4:
						PDropdown_WinXY(win, PU4, p_up);
						tm = PDoPopup(win, POPUP, menubar, FALSE, win->tree[PU4].ob_x, win->tree[PU4].ob_y, win->tree[PU4].ob_width, win->tree[PU4].ob_height);
						if (tm>0) {
							win->popups[4] = tm;
							ChangeObjectText(win->tree, PU4, ColIndex[win->popups[4]], 3, TE_CNTR);
							WObjc_Draw(win->tree, PU4, 1, desk.g_x, desk.g_y, desk.g_w, desk.g_h);
						}
						return FALSE;

					case PU5:
					case CYCLE5:
						PDropdown_WinXY(win, PU5, p_up);
						tm = PDoPopup(win, POPUP, menubar, FALSE, win->tree[PU5].ob_x, win->tree[PU5].ob_y, win->tree[PU5].ob_width, win->tree[PU5].ob_height);
						if (tm>0) {
							win->popups[5] = tm;
							ChangeObjectText(win->tree, PU5, ColIndex[win->popups[5]], 3, TE_CNTR);
							WObjc_Draw(win->tree, PU5, 1, desk.g_x, desk.g_y, desk.g_w, desk.g_h);
						}
						return FALSE;

					case PU6:
					case CYCLE6:
						PDropdown_WinXY(win, PU6, p_up);
						tm = PDoPopup(win, POPUP, menubar, FALSE, win->tree[PU6].ob_x, win->tree[PU6].ob_y, win->tree[PU6].ob_width, win->tree[PU6].ob_height);
						if (tm>0) {
							win->popups[6] = tm;
							ChangeObjectText(win->tree, PU6, ColIndex[win->popups[6]], 3, TE_CNTR);
							WObjc_Draw(win->tree, PU6, 1, desk.g_x, desk.g_y, desk.g_w, desk.g_h);
						}
						return FALSE;

					case PU7:
					case CYCLE7:
						PDropdown_WinXY(win, PU7, p_up);
						tm = PDoPopup(win, POPUP, menubar, FALSE, win->tree[PU7].ob_x, win->tree[PU7].ob_y, win->tree[PU7].ob_width, win->tree[PU7].ob_height);
						if (tm>0) {
							win->popups[7] = tm;
							ChangeObjectText(win->tree, PU7, ColIndex[win->popups[7]], 3, TE_CNTR);
							WObjc_Draw(win->tree, PU7, 1, desk.g_x, desk.g_y, desk.g_w, desk.g_h);
						}
						return FALSE;

					case PU8:
					case CYCLE8:
						PDropdown_WinXY(win, PU8, p_up);
						tm = PDoPopup(win, POPUP, menubar, FALSE, win->tree[PU8].ob_x, win->tree[PU8].ob_y, win->tree[PU8].ob_width, win->tree[PU8].ob_height);
						if (tm>0) {
							win->popups[8] = tm;
							ChangeObjectText(win->tree, PU8, ColIndex[win->popups[8]], 3, TE_CNTR);
							WObjc_Draw(win->tree, PU8, 1, desk.g_x, desk.g_y, desk.g_w, desk.g_h);
						}
						return FALSE;

					case PU9:
					case CYCLE9:
						PDropdown_WinXY(win, PU9, p_up);
						tm = PDoPopup(win, POPUP, menubar, FALSE, win->tree[PU9].ob_x, win->tree[PU9].ob_y, win->tree[PU9].ob_width, win->tree[PU9].ob_height);
						if (tm>0) {
							win->popups[9] = tm;
							ChangeObjectText(win->tree, PU9, ColIndex[win->popups[9]], 3, TE_CNTR);
							WObjc_Draw(win->tree, PU9, 1, desk.g_x, desk.g_y, desk.g_w, desk.g_h);
						}
						return FALSE;

					case PU10:
					case CYCLE10:
						PDropdown_WinXY(win, PU10, p_up);
						tm = PDoPopup(win, POPUP, menubar, FALSE, win->tree[PU10].ob_x, win->tree[PU10].ob_y, win->tree[PU10].ob_width, win->tree[PU10].ob_height);
						if (tm>0) {
							win->popups[10] = tm;
							ChangeObjectText(win->tree, PU10, ColIndex[win->popups[10]], 3, TE_CNTR);
							WObjc_Draw(win->tree, PU10, 1, desk.g_x, desk.g_y, desk.g_w, desk.g_h);
						}
						return FALSE;

					case PU11:
					case CYCLE11:
						PDropdown_WinXY(win, PU11, p_up);
						tm = PDoPopup(win, POPUP, menubar, FALSE, win->tree[PU11].ob_x, win->tree[PU11].ob_y, win->tree[PU11].ob_width, win->tree[PU11].ob_height);
						if (tm>0) {
							win->popups[11] = tm;
							ChangeObjectText(win->tree, PU11, ColIndex[win->popups[11]], 3, TE_CNTR);
							WObjc_Draw(win->tree, PU11, 1, desk.g_x, desk.g_y, desk.g_w, desk.g_h);
						}
						return FALSE;

					case PU12:
					case CYCLE12:
						PDropdown_WinXY(win, PU12, p_up);
						tm = PDoPopup(win, POPUP, menubar, FALSE, win->tree[PU12].ob_x, win->tree[PU12].ob_y, win->tree[PU12].ob_width, win->tree[PU12].ob_height);
						if (tm>0) {
							win->popups[12] = tm;
							ChangeObjectText(win->tree, PU12, ColIndex[win->popups[12]], 3, TE_CNTR);
							WObjc_Draw(win->tree, PU12, 1, desk.g_x, desk.g_y, desk.g_w, desk.g_h);
						}
						return FALSE;
				}
			}

		case MN_SELECTED:
			switch (msg_buf[4]) {
				case QUIT_M2:
					WCloseWindow(win, WC_OBJECTABLE);
					return FALSE;
		
				case ABOUT_M2:
					WCreateWindow(W_DIALOGICON | W_UNUNTOPPABLE, 0, NAME|MOVER|INFO|FULLER, "About me...", " Copyright \275 1992 - 1994",
									ABOUTME, 0, -1,
									AboutDispatcher,
									0,
									-1, -1, -1, -1, NULL, D_SWITCHABLE);
					return FALSE;
			}
			break;
	}

	return FALSE;
}


int Dlg4Dispatcher(WINDOW *win, int msg_buf[8])
{
	switch (*msg_buf) {
		case WM_CREATED:
			HorizontalSlideReset(win, 1, 100, 0, HORTRK1, HORSL1,
								HORSL1, 0);
			HorizontalSlideReset(win, 2, 100, 0, HORTRK2, HORSL2,
								HORSL2, 0);
			HorizontalSlideReset(win, 5, 100, 0, HORTRK3, HORSL3,
								HORSL3, 0);
			HorizontalSlideReset(win, 6, 100, 0, HORTRK4, HORSL4,
								HORSL4, 0);
			VerticalSlideReset(win, 3, 100, 0, VERTSL1, VERTTRK1,
								VERTSL1, 0);
			VerticalSlideReset(win, 4, 100, 0, VERTSL2, VERTTRK2,
								VERTSL2, 0);
			return FALSE;

		case WM_KILL:
			return FALSE;

		case WM_DIALOG:
			switch(msg_buf[3]) {
				case HORLT1:
				case HORRT1:
				case HORSL1:
				case HORTRK1:
					DoHorizontalSlide(win, 1, HORTRK1, HORSL1,
									HORLT1, HORRT1, msg_buf[3],
									5, HORSL1);
					return FALSE;

				case HORLT2:
				case HORRT2:
				case HORSL2:
				case HORTRK2:
					DoHorizontalSlide(win, 2, HORTRK2, HORSL2,
									HORLT2, HORRT2, msg_buf[3],
									5, HORSL2);
					return FALSE;

				case HORLT3:
				case HORRT3:
				case HORSL3:
				case HORTRK3:
					DoHorizontalSlide(win, 5, HORTRK3, HORSL3,
									HORLT3, HORRT3, msg_buf[3],
									5, HORSL3);
					return FALSE;

				case HORLT4:
				case HORRT4:
				case HORSL4:
				case HORTRK4:
					DoHorizontalSlide(win, 6, HORTRK4, HORSL4,
									HORLT4, HORRT4, msg_buf[3],
									5, HORSL4);
					return FALSE;

				case VERTTRK1:
				case VERTUP1:
				case VERTDN1:
				case VERTSL1:
					DoVerticalSlide(win, 3, VERTTRK1, VERTSL1,
									VERTUP1, VERTDN1, msg_buf[3],
									5, VERTSL1);
					return FALSE;

				case VERTTRK2:
				case VERTUP2:
				case VERTDN2:
				case VERTSL2:
					DoVerticalSlide(win, 4, VERTTRK2, VERTSL2,
									VERTUP2, VERTDN2, msg_buf[3],
									5, VERTSL2);
					return FALSE;
			}
			break;

		case MN_SELECTED:
			switch (msg_buf[4]) {
				case QUIT_M2:
					WCloseWindow(win, WC_OBJECTABLE);
					return FALSE;
		
				case ABOUT_M2:
					WCreateWindow(W_DIALOGICON | W_UNUNTOPPABLE, 0, NAME|MOVER|INFO|FULLER, "About me...", " Copyright \275 1992 - 1994",
									ABOUTME, 0, -1,
									AboutDispatcher,
									0,
									-1, -1, -1, -1, NULL, D_SWITCHABLE);
					return FALSE;
			}
			break;
	}

	return FALSE;
}

int Dlg5Dispatcher(WINDOW *win, int msg_buf[8])
{
	switch (*msg_buf) {
		case WM_KILL:
			return FALSE;
	}

	return FALSE;
}

int KeyDispatcher(int key)
{
	if ((key & NKF_CTRL) && (key & NKF_ALT)) {
		switch (key & 0xFF) {
			case NK_F1:	
				WCreateWindow(W_TEXTWIN, 0, W_TEXT_ATTRIBUTES, "WinLIB PRO Version Info", NULL,
								-1, 0, -1,
								TextDispatcher, 0,
								30, 50, 150, 100, NULL, D_SWITCHABLE);
				return TRUE;
		}
	} else {
		if (key & NKF_CTRL)
			switch (key & 0xff) {
				case 'u':
				case 'U':
					WCloseWindow(NULL, WC_OBJECTABLE);
					return TRUE;

				case 'w':
				case 'W':
					WTopWindow(NULL);
					return TRUE;

				case 'q':
				case 'Q':
					if (_app)
						WDie(0);	/* Close windows and die */
					return TRUE;

				case 'i':
				case 'I':
					if (!WFindWindow(ABOUTME))
						WCreateWindow( W_DIALOGICON | W_UNUNTOPPABLE, 0, NAME|MOVER|INFO|FULLER, "About me...", " Copyright \275 1992 - 1994",
										ABOUTME, 0, -1,
										AboutDispatcher,
										0,
										-1, -1, -1, -1, NULL, D_SWITCHABLE);
					return TRUE;

				case 'r':
				case 'R':
					if (!WFindWindow(RSCINFO))
						WCreateWindow(W_DIALOGICON | W_UNUNTOPPABLE, 0, NAME|MOVER|FULLER, "Resource info...", NULL,
										RSCINFO, 0, -1,
										ResourceDispatcher,
										0,
										-1, -1, -1, -1, NULL, D_SWITCHABLE);
					return TRUE;

				case NK_F1:
					WCreateWindow(W_TIMER | W_OPEN | W_FULLERICONIFIES, 0, NAME|CLOSER|SIZER|MOVER|FULLER, "Window 1", NULL,
									-1, 0, MENU3,
									WindowDispatcher,
									&wind1parms,
									10, 20, 200, 40, NULL, D_NONE);
					return TRUE;

				case NK_F2:
					WCreateWindow(W_DIALOGICON, S_MULTICOPYABLE, NAME|CLOSER|MOVER|FULLER, "Dialog 1", NULL,
									DIALOG1, EDITABLE1, MENU2,
									Dlg1Dispatcher,
									0,
									10, -1, -1, -1, NULL, D_NONE);
					return TRUE;

				case NK_F3:
					if (!WFindWindow(DIALOG2))
						WCreateWindow(W_DIALOGICON, 0, NAME|CLOSER|MOVER|FULLER, "Dialog 2", NULL,
										DIALOG2, EDITME, MENU2,
										Dlg2Dispatcher,
										0,
										-1, -1, -1, -1, NULL, D_NONE);
					return TRUE;

				case NK_F4:
					if (!WFindWindow(DIALOG3))
						WCreateWindow(W_DIALOGICON, 0, NAME|CLOSER|MOVER|FULLER, "Dialog 3", NULL,
										DIALOG3, 0, MENU2,
										Dlg3Dispatcher,
										0,
										-1, -1, -1, -1, NULL, D_NONE);
					return TRUE;

				case NK_F5:
					if (!WFindWindow(DIALOG4))
						WCreateWindow(W_DIALOGICON, 0, NAME|CLOSER|MOVER|FULLER, "Dialog 4", NULL,
										DIALOG4, 0, MENU2,
										Dlg4Dispatcher,
										0,
										-1, -1, -1, -1, NULL, D_NONE);
					return TRUE;

				case NK_F6:
					if (!WFindWindow(DIALOG5))
						WCreateWindow(W_DIALOGICON, 0, NAME|CLOSER|MOVER, "Help window", NULL,
										DIALOG5, 0, -1,
										Dlg6Dispatcher,
										0,
										-1, -1, -1, -1, NULL, D_ALWAYSTOP);
					return TRUE;

				case NK_F7:
					if (!WFindWindow(DIALOG6))
						WCreateWindow(W_TIMER | W_DIALOGMINI, 0, NAME|CLOSER|MOVER|FULLER, "Timer dialog", NULL,
										DIALOG6, 0, MENU3,
										Window2Dispatcher,
										&wind2parms,
										-1, -1, -1, -1, NULL, D_NONE);
					return TRUE;
			}
	}

	return FALSE;
}


int EtcDispatcher(int msg_buf[])
{
	switch (msg_buf[0]) {
		case MN_SELECTED:
			switch(msg_buf[4]) {
				case 6:
					WUseInternalAccs(FALSE);
					WMenuBar(menubar, 1);
					return FALSE;
					
				case ABOUT1:
					if (!WFindWindow(ABOUTME))
						WCreateWindow(W_DIALOGICON | W_UNUNTOPPABLE, 0, NAME|MOVER|INFO|FULLER, "About me...", " Copyright \275 1992 - 1994",
										ABOUTME, 0, -1,
										AboutDispatcher,
										0,
										-1, -1, -1, -1, NULL, D_SWITCHABLE);
					return FALSE;

				case QUIT:
					WDie(0);
					return FALSE;

				default:
					if (msg_buf[5] == 10)
						switch(msg_buf[6]) {
							case D1:
								WCreateWindow(W_DIALOGICON, S_MULTICOPYABLE, NAME|CLOSER|MOVER|FULLER, "Dialog 1", NULL,
											DIALOG1, EDITABLE1, MENU2,
											Dlg1Dispatcher,
											0,
											10, -1, -1, -1, NULL, D_NONE);
								return FALSE;

							case D2:
								if (!WFindWindow(DIALOG2))
									WCreateWindow(W_DIALOGICON, 0, NAME|CLOSER|MOVER|FULLER, "Dialog 2", NULL,
												DIALOG2, EDITME, MENU2,
												Dlg2Dispatcher,
												0,
												-1, -1, -1, -1, NULL, D_NONE);
								return FALSE;

							case D3:
								if (!WFindWindow(DIALOG3))
									WCreateWindow(W_DIALOGICON, 0, NAME|CLOSER|MOVER|FULLER, "Dialog 3", NULL,
												DIALOG3, 0, MENU2,
												Dlg3Dispatcher,
												0,
												-1, -1, -1, -1, NULL, D_NONE);
								return FALSE;

							case D4:
								if (!WFindWindow(DIALOG4))
									WCreateWindow(W_DIALOGICON, 0, NAME|CLOSER|MOVER|FULLER, "Dialog 4", NULL,
												DIALOG4, 0, MENU2,
												Dlg4Dispatcher,
												0,
												-1, -1, -1, -1, NULL, D_NONE);
								return FALSE;

							case D5:
								if (!WFindWindow(DIALOG5))
									WCreateWindow(W_ALERT, 0, NAME|CLOSER|MOVER, "Help window", NULL,
												DIALOG5, 0, -1,
												Dlg6Dispatcher,
												0,
												-1, -1, -1, -1, NULL, D_ALWAYSTOP);
								return FALSE;

							case W1:
								WCreateWindow(W_TIMER | W_OPEN | W_FULLERICONIFIES, 0, NAME|CLOSER|SIZER|MOVER|FULLER, "Window 1", NULL,
											-1, 0, MENU3,
											WindowDispatcher,
											&wind1parms,
											10, 20, 200, 40, NULL, D_NONE);
								return FALSE;

							case W2:
								if (!WFindWindow(DIALOG6))
									WCreateWindow(W_TIMER | W_DIALOGMINI, 0, NAME|CLOSER|MOVER|FULLER, "Timer dialog", NULL,
												DIALOG6, 0, MENU3,
												Window2Dispatcher,
												&wind2parms,
												-1, -1, -1, -1, NULL, D_NONE);
								return FALSE;

							case RSI1:
								if (!WFindWindow(RSCINFO))
									WCreateWindow(W_DIALOGICON | W_UNUNTOPPABLE, 0, NAME|MOVER|FULLER, "Resource info...", NULL,
												RSCINFO, 0, -1,
												ResourceDispatcher,
												0,
												-1, -1, -1, -1, NULL, D_SWITCHABLE);
								return FALSE;
						}

					if (msg_buf[5] == 11)
						switch(msg_buf[6]) {
							case HELP1:
								WFormCustAlert(1, " Hypertext ",
									"Sorry, the hypertext section",
									"of WinLIB PRO is yet to be",
									"re-designed, therefore it is",
									"currently disabled.  Wait",
									"for the next release.", " ", "[Okay", " ");
								return FALSE;

							case HELP2:
								WCreateWindow(W_TEXTWIN, 0, W_TEXT_ATTRIBUTES, "WinLIB PRO Version Info", NULL,
											-1, 0, -1,
											TextDispatcher, 0,
											30, 50, 150, 100, NULL, D_SWITCHABLE);
								return FALSE;
						}

					if (msg_buf[5] == 12) {
						switch(msg_buf[6]) {
							case HELPFILE:
							case TEXTFILE:
							case DESKFILE:
							case SETFILE:
								WFormCustAlert(1, " Load/Save ",
									"Sorry, loading and saving of",
									"any and all functions of",
									"WinLIB PRO are disabled at",
									"this time.  Wait for the",
									"next release.", " ", "[Okay", " ");
								break;
						}
						return FALSE;
					}

					if (msg_buf[5] == 13) {
						switch(msg_buf[6]) {
							case CLWIN:
								WCloseWindow(NULL, WC_OBJECTABLE);
								return FALSE;

							case NXWIN:
								WTopWindow(NULL);
								return FALSE;
						}
					}

					if (msg_buf[5] == 14) {
						switch(msg_buf[6]) {
							case DA1:
							case DA2:
							case DA3:
							case DA4:
							case DA5:
							case DA6:
								if (AES_VERSION<0x0400) {
									WMenuBar(menubar, FALSE);
									WUseInternalAccs(TRUE);
								} else
									WCallAccessory(msg_buf[6] - DA1);

								return FALSE;
						}
					}
					return FALSE;
			}
	}

	return FALSE;
}

int main()
{
	int gl_hchar, gl_wchar, gl_hbox, gl_wbox;
	int a, b, c, d, e, i;

	static int work_in[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 2};
	static int work_out[57];

	ap_id = appl_init();

	work_in[0] = handle = graf_handle(&gl_wchar, &gl_hchar, &gl_wbox, &gl_hbox);
	v_opnvwk(work_in, &handle, work_out);
	WInit(ap_id, work_out, 0, EtcDispatcher, KeyDispatcher, "  WinLIB Pro 0.58", "WLIBTEST", TRUE);

	AEShandle = handle;

	WHotkeyLevel(HOTKEY_UNDERLINE);

	if (xrsrc_load(RSCFILE)) {
		OBJECT *pup, *ps;
		LOCAL long *dummy;

		rsrc_gaddr(R_TREE, MENU1, &menubar);
		rsrc_gaddr(R_TREE, POPUP, &pup);
		rsrc_gaddr(R_TREE, PROCSEL, &ps);

		WMenuAttach(menubar, WINDS, DIALSEL, TRUE);

/*		get_cookie('WPRO', dummy);

		if (dummy == 0)
			WFormCustAlert(1, " WinLIB Cookie ",
						"Hmmm..  The WinLIB Pro cookie",
						"was not found in the cookie-",
						"jar.  Assuming all program",
						"defaults.  Please run COOKIE",
						"INSTALL at boot-time.", " ", "[Okay", " "); */

		if (desk_accessories[1])
			ChangeObjectText(ps, DA1, desk_accessories[1], 3, TE_LEFT);
		if (desk_accessories[2])
			ChangeObjectText(ps, DA2, desk_accessories[2], 3, TE_LEFT);
		if (desk_accessories[3])
			ChangeObjectText(ps, DA3, desk_accessories[3], 3, TE_LEFT);
		if (desk_accessories[4])
			ChangeObjectText(ps, DA4, desk_accessories[4], 3, TE_LEFT);
		if (desk_accessories[5])
			ChangeObjectText(ps, DA5, desk_accessories[5], 3, TE_LEFT);
		if (desk_accessories[6])
			ChangeObjectText(ps, DA6, desk_accessories[6], 3, TE_LEFT);

		WMenuBar(menubar, 1);
		if (num_colors<=4) {
			pup[PBLUE].ob_state = DISABLED;
			pup[PCYAN].ob_state = DISABLED;
			pup[PYELLOW].ob_state = DISABLED;
			pup[PMAGENTA].ob_state = DISABLED;

			pup[PUBLUE].ob_state = DISABLED;
			pup[PUCYAN].ob_state = DISABLED;
			pup[PUYELLOW].ob_state = DISABLED;
			pup[PUMAGENT].ob_state = DISABLED;
		}

		if (num_colors == 2) {
			pup[PRED].ob_state = DISABLED;
			pup[PGREEN].ob_state = DISABLED;
			pup[PBLUE].ob_state = DISABLED;
			pup[PCYAN].ob_state = DISABLED;
			pup[PYELLOW].ob_state = DISABLED;
			pup[PMAGENTA].ob_state = DISABLED;

			pup[PURED].ob_state = DISABLED;
			pup[PUGREEN].ob_state = DISABLED;
			pup[PUBLUE].ob_state = DISABLED;
			pup[PUCYAN].ob_state = DISABLED;
			pup[PUYELLOW].ob_state = DISABLED;
			pup[PUMAGENT].ob_state = DISABLED;
		}

		WGrafMouse(ARROW);

		if (!WCheckSpeedup())
			form_alert(1,"[2][Uh oh...  No screen speed-|"
							 "up utilities were detected.|"
							 "Graphics routines may be a|"
                             "bit on the slow side.][ Okay ]");

		WFormCustAlert(1, " WinLIB PRO ",
					"New version (0.64A)",
					"This version compiled on",
					__DATE__" at "__TIME__,
					__DEMO__,
					"Unregistered demo copy", " ", "[Okay", " ");

/*		WCreateWindow(W_DIALOGICON, 0, NAME|CLOSER|MOVER|FULLER, "Toolbar", NULL,
						TOOLBOX, 0, -1,
						ToolDispatcher,
						0,
						0, desk.g_y, -1, -1, NULL, D_NONE); */

		WDoDial(menubar);

		WMenuAttach(menubar, WINDS, DIALSEL, FALSE);

		WMenuBar(menubar, 0);
		Rsrc_Free();
	}
	else
		WFormCustAlert(1, " Resource file??? ",
						  "The resource file could not",
						  "be located.  Please move the",
						  "program's resource to the",
						  "root directory, and try",
						  "booting again.", " ", "[Okay", " ");

	WTerm();
	v_clsvwk(handle);
	appl_exit();

	return 0;
}