#include <exec/types.h>
#include <exec/exec.h>
#include <intuition/intuition.h>
#include <graphics/gfx.h>
#include <functions.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include <simpmenu.h>
#include <simpreq.h>

#define MAXBAUDITEMS 7
static struct MenuItem *baudi[MAXBAUDITEMS];

#define MAXPARIITEMS 15
static struct MenuItem *parii[MAXPARIITEMS];

#define MAXPROTITEMS 4
static struct MenuItem *proti[MAXPROTITEMS];

#define MAXECHOITEMS 2
static struct MenuItem *echoi[MAXECHOITEMS];

#define MAXSTRIITEMS 2
static struct MenuItem *strii[MAXSTRIITEMS];

#define MAXXFERITEMS 3
static struct MenuItem *xferi[MAXXFERITEMS];

#define MAXKERMITEMS 3
static struct MenuItem *kermi[MAXKERMITEMS];

#define MAXXMODITEMS 4
static struct MenuItem *xmodi[MAXXMODITEMS];

#define MAXTRANITEMS 3
static struct MenuItem *trani[MAXTRANITEMS];

#define MAXPROGITEMS 2
static struct MenuItem *progi[MAXPROGITEMS];

#define MAXSCRNITEMS 4
static struct MenuItem *scrni[MAXSCRNITEMS];

#define MAXDPTHITEMS 3
static struct MenuItem *dpthi[MAXDPTHITEMS];

#define MAXRENDITEMS 4
static struct MenuItem *rendi[MAXRENDITEMS];

#define MAXOPERITEMS 13
static struct MenuItem *operi[MAXOPERITEMS];

#define MAXGRAPITEMS 1
static struct MenuItem *grapi[MAXGRAPITEMS];

void TestRoutine(struct Window *);
BOOL WaitForClose(struct Window *);
void main(int, char **);

void TestRoutine( struct Window *w )
{
	struct Menu *menu = NULL;
	
	printf("Start\n");
	
	menu = SMenu(NULL, "VLT  ");
	
	SItem(menu, " Capture Session",      'O', 0L);
	SItem(menu, " End Capture",          'E', 0L);
	SItem(menu, " Open Console",          0 , 0L);
	SItem(menu, " History Buffer",        0 , 0L);
	SSItem(menu, " View",                'V', 0L);
	SSItem(menu, " Edit / Send",         ')', 0L);
	SItem(menu, " Save Configuration",    0 , 0L);
	SItem(menu, " Version",               0 , 0L);
	SItem(menu, " Exit",                  0 , 0L);
	
	SMenu(menu, "Comm ");
	
	SItem(menu, " Select Device",   0 , 0L);
	SItem(menu, " Select Unit",     0 , 0L);
	SItem(menu, " Baud",            0 , 0L);
	baudi[0] = SSItem(menu, "     110",        0 , 0x7eL);
	baudi[1] = SSItem(menu, "     300",        0 , 0x7dL);
	baudi[2] = SSItem(menu, "    1200",        0 , 0x7bL);
	baudi[3] = SSItem(menu, "    2400",        0 , 0x77L);
	baudi[4] = SSItem(menu, "    4800",        0 , 0x6fL);
	baudi[5] = SSItem(menu, "    9600",        0 , 0x5fL);
	baudi[6] = SSItem(menu, "   19200",        0 , 0x3fL);
	SItem(menu, " Parity",          0 , 0L);
	/*
	 *   Notice the sequence of the array index! Needs to be this way for
	 *   proper checkmarking: use definition of p_parity, not "subnum".
	 */
	parii[0] = SSItem(menu, "   8N1",          0 , 0x7ffeL);
	parii[5] = SSItemB(menu, "   8N2",         0 , 0x7ffdL);
	parii[10]= SSItemB(menu, "   7N2",         0 , 0x7ffbL);
	parii[1] = SSItem(menu, "   8M1",          0 , 0x7ff7L);
	parii[6] = SSItemB(menu, "   7M1",         0 , 0x7fefL);
	parii[11]= SSItemB(menu, "   7M2",         0 , 0x7fdfL);
	parii[2] = SSItem(menu, "   8S1",          0 , 0x7fbfL);
	parii[7] = SSItemB(menu, "   7S1",         0 , 0x7f7fL);
	parii[12]= SSItemB(menu, "   7S2",         0 , 0x7effL);
	parii[3] = SSItem(menu, "   8E1",          0 , 0x7dffL);
	parii[8] = SSItemB(menu, "   7E1",         0 , 0x7bffL);
	parii[13]= SSItemB(menu, "   7E2",         0 , 0x77ffL);
	parii[4] = SSItem(menu, "   8O1",          0 , 0x6fffL);
	parii[9] = SSItemB(menu, "   7O1",         0 , 0x5fffL);
	parii[14]= SSItemB(menu, "   7O2",         0 , 0x3fffL);
	SItem(menu, " Protocol",        0 , 0L);
	proti[0] = SSItem(menu, "   None",         0 , 0xeL);
	proti[1] = SSItem(menu, "   Xon/Xoff",     0 , 0xdL);
	proti[2] = SSItem(menu, "   7-Wire",       0 , 0xbL);
	proti[3] = SSItem(menu, "   7-Wire/X",     0 , 0x7L);
	SItem(menu, " Send Break",     'B', 0L);
	SItem(menu, " Hangup",         'H', 0L);
	SItem(menu, " Echo",            0 , 0L);
	echoi[0] = SSItem(menu, "   Off",         '*', 0x2L);
	echoi[1] = SSItem(menu, "   On",          '+', 0x1L);
	SItem(menu, " Strip Bit 8",     0 , 0L);
	strii[0] = SSItem(menu, "   Off",          0 , 0x2L);
	strii[1] = SSItem(menu, "   On",           0 , 0x1L);
	SItem(menu, " Set Break Time",  0 , 0L);
	SItem(menu, " Set Buffer Size", 0 , 0L);
	
	SMenu(menu, "Transfer ");
	
	SItem(menu, " Send File",                  'S', 0L);
	SItem(menu, " Receive File",               'R', 0L);
	SItem(menu, " Change Directory",           'D', 0L);
	SItem(menu, " Protocol",                    0 , 0L);
	xferi[0] = SSItem(menu, "   External",                'T', 0x6L);
	xferi[1] = SSItem(menu, "   Kermit",                  'K', 0x5L);
	xferi[2] = SSItem(menu, "   XMODEM",                  'X', 0x3L);
	SItem(menu, " Kermit Options",              0 , 0L);
	kermi[0] = SSItem(menu, "   Send / Receive",          '-', 0x2L);
	kermi[1] = SSItem(menu, "   Host is Server",          '=', 0x1L);
	kermi[2] = SSItem(menu, "   Kermit Bye",              'Y', 0L);
	SSItem(menu, "   Set Packet Size",          0 , 0L);
	SItem(menu, " XMODEM Options",              0 , 0L);
	xmodi[0] = SSItem(menu, "   Check Sum",               '[', 0xeL);
	xmodi[1] = SSItem(menu, "   Check Sum / 1K Blocks",    0 , 0xdL);
	xmodi[2] = SSItem(menu, "   CRC",                      0 , 0xbL);
	xmodi[3] = SSItem(menu, "   CRC / 1K Blocks",         ']', 0x7L);
	SItem(menu, " External Options",            0 , 0L);
	SItem(menu, " Translate Options",           0 , 0L);
	trani[0] = SSItem(menu, "   No Translation",          ',', 0x2L);
	trani[1] = SSItem(menu, "   CR/LF -> LF",             '.', 0x1L);
	trani[2] = SSItem(menu, "   Downcase File Names",     '/', -1L);
	
	SMenu(menu, "Macros ");
	
	SItem(menu, " ARexx Macro",           'A', 0L);
	SItem(menu, " VLT Macro",             'M', 0L);
	SItem(menu, " Abort VLT Macro  Esc ", (char) -1, 0L);
	SItem(menu, " Keyboard Macros",        0 , 0L);
	SSItem(menu,  " ",                    '1', 0L);
	SSItemB(menu, " ",                    '2', 0L);
	SSItem(menu,  " ",                    '3', 0L);
	SSItemB(menu, " ",                    '4', 0L);
	SSItem(menu,  " ",                    '5', 0L);
	SSItemB(menu, " ",                    '6', 0L);
	SSItem(menu,  " ",                    '7', 0L);
	SSItemB(menu, " ",                    '8', 0L);
	SSItem(menu,  " ",                    '9', 0L);
	SSItemB(menu, " ",                    '0', 0L);
	SItem(menu, " Program Mode",           0 , 0L);
	progi[0] = SSItem(menu, "   On",                 '(', 0x2L);
	progi[1] = SSItem(menu, "   Off",                ')', 0x1L);
	
	SMenu(menu, "Screen ");
	
	SItem(menu, " Clear Screen",        'C', 0L);
	SItem(menu, " Screen to Back",      'J', 0L);
	SItem(menu, " Select Screen Type",   0 , 0L);
	scrni[0] = SSItem(menu, "   Custom Screen",     0 , 0x2L);
	scrni[1] = SSItem(menu, "   Workbench",         0 , 0x1L);
	scrni[2] = SSItem(menu, "   Interlaced",        0 , 0x8L);
	scrni[3] = SSItem(menu, "   Non-Interlaced",    0 , 0x4L);
	SItem(menu, " Number of Colors",     0 , 0L);
	dpthi[0] = SSItem(menu, "   2",                 0 , 0x6L);
	dpthi[1] = SSItem(menu, "   4",                 0 , 0x5L);
	dpthi[2] = SSItem(menu, "   8",                 0 , 0x3L);
	SItem(menu, " Select Colors",       'U', 0L);
	SItem(menu, " Number of Lines",     'L', 0L);
	SItem(menu, " Number of Columns",   'Z', 0L);
	SItem(menu, " Amount of Prescroll",  0 , 0L);
	SItem(menu, " Rendering Mode",       0 , 0L);
	SItem(menu, " Cursor Height",        0 , 0L);
	rendi[0] = SSItem(menu, "   Quick",            'Q', 0x6L);
	rendi[1] = SSItem(menu, "   Normal",           'N', 0x5L);
	rendi[2] = SSItem(menu, "   Color",            'G', 0x3L);
	rendi[3] = SSItem(menu, "   ANSI Color",        0 , -1L);
	
	SMenu(menu, "Operation ");
	
	operi[0] = SItem(menu, "   Wrap",                 'W', -1L);
	operi[1] = SItem(menu, "   Key Repeat",            0 , -1L);
	operi[2] = SItem(menu, "   Numeric Keypad",        0 , -1L);
	operi[3] = SItem(menu, "   Application Cursor",    0 , -1L);
	operi[4] = SItem(menu, "   Swap BS <-> Delete",   'I', -1L);
	operi[5] = SItem(menu, "   Destructive BS",        0 , -1L);
	operi[6] = SItem(menu, "   Help Key = LF",         0 , -1L);
	operi[7] = SItem(menu, "   Shift-Tab = ESC TAB",   0 , -1L);
	operi[8] = SItem(menu, "   Function Key Gadgets", 'P', -1L);
	operi[9] = SItem(menu, "   Mouse Support",        '\\',-1L);
	SItem(menu, "   Beep Volume",           0 , 0L);
	operi[10] = SItem(menu, "   CR Key: Echo CR/LF",    0 , -1L);
	operi[11] = SItem(menu, "   CR Key: Send CR/LF",    0 , -1L);
	operi[12] = SItem(menu, "   Auto-Screen to Back",   0 , -1L);
	
	SMenu(menu, "Graphics ");
	
	grapi[0] = SItem(menu, "   Lock Graphics", ';', -1L);
	SItem(menu, "   To Graphics",   'F', 0L);
	
	AdjustItems(menu);
	
	printf("Stop\n");
	
	if (menu) 
	{
		SetMenuStrip(w, menu);
		WaitForClose(w);
		ClearMenuStrip(w);
		FreeSMenu(menu);
	}
	
	return;
}


/**
 *
 *   Generalized Intuition test routine. Opens a window.
 *
***/

static struct NewWindow nw = 
{
	0,0,
	640, 200,
	0,1,
	CLOSEWINDOW+MOUSEBUTTONS+GADGETUP+GADGETDOWN+INTUITICKS,
	WINDOWDRAG+WINDOWDEPTH+WINDOWCLOSE+ACTIVATE,
	NULL,
	NULL,
	(UBYTE *)"MBRTest",
	NULL,
	NULL,
	5,5,
	700,300,
	WBENCHSCREEN
};

struct IntuitionBase   *IntuitionBase = NULL;
struct GfxBase         *GfxBase = NULL;
struct Library         *ArpBase = NULL;
struct Library         *ExpansionBase = NULL;

void main( int argc, char **argv )
{
	struct Window *w = NULL;
	struct IntuiMessage *message = NULL;
	struct TextFont *font = NULL;
	
	/*
	 *   Libraries
	 */
	IntuitionBase = (struct IntuitionBase *) OpenLibrary("intuition.library", 0L);
	if (IntuitionBase == NULL)
		goto cleanup;
	
	GfxBase = (struct GfxBase *) OpenLibrary("graphics.library", 0L);
	if (GfxBase == NULL)
		goto cleanup;
	
	/*
	 *   Open window and set font
	 */
	w = OpenWindow(&nw);
	if (w == NULL)
		goto cleanup;
	
	font = (struct TextFont *) OpenFont(Topaz80Equiv());
	if (font == NULL)
		goto cleanup;
	SetFont(w->RPort, font);
	/*
	 *   Test begins here.
	 */
	TestRoutine(w);
	
	WaitForClose(w);
	
	cleanup:
	if (font)
		CloseFont(font);
	if (w) 
	{
		while (message = (struct IntuiMessage *)
			GetMsg(w->UserPort)) ReplyMsg((struct Message *) message);
		CloseWindow(w);
	}
	if (GfxBase)
		CloseLibrary((struct Library *) GfxBase);
	if (IntuitionBase)
		CloseLibrary((struct Library *) IntuitionBase);
	
	exit(0);
}


/**
 *
 *   Wait for a close message
 *
**/
BOOL WaitForClose( struct Window *w )
{
	BOOL quit_flag = FALSE;
	ULONG class;
	struct IntuiMessage *message;
	
	while (quit_flag == FALSE) 
	{
		Wait(1L << w->UserPort->mp_SigBit);
		/*
		 *  Handle messages from the window
		 */
		while ( message = (struct IntuiMessage *) GetMsg(w->UserPort) ) 
		{
			class = message->Class;
			ReplyMsg((struct Message *) message);
			
			if (class == CLOSEWINDOW)
				quit_flag = TRUE;
		}
	}
	return(quit_flag);
}


