//
// $VER: InControl.c 1.0 (7.3.97)
//
// Popup Menu example program
//
// ©1996-1997 Henrik Isaksson
// All Rights Reserved.
//
// Click the little button in the top left of your screen!
//

#include <intuition/intuition.h>

#include <clib/intuition_protos.h>
#include <clib/exec_protos.h>
#include <clib/alib_protos.h>

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#include <libraries/pm.h>
#include <proto/pm.h>

struct IntuitionBase	*IntuitionBase;
struct GfxBase		*GfxBase;
struct PopupMenuBase	*PopupMenuBase;

struct Window *w;	// This window is only needed to find out when and where the menu should appear.
			// The font in this window's rastport will be used for the menu.

void main()
{
	BOOL r=TRUE;
	struct IntuiMessage *im,imsg;
	struct PopupMenu *p;

	PopupMenuBase=(struct PopupMenuBase *)OpenLibrary(POPUPMENU_NAME,POPUPMENU_VERSION);			// Open the library
	if(PopupMenuBase) {
		IntuitionBase=(struct IntuitionBase *)PopupMenuBase->pmb_IntuitionBase;	// We let popupmenu.library open the libraries we need
		GfxBase=(struct GfxBase *)PopupMenuBase->pmb_GfxBase;			// They remain valid until the library is closed!

		p=PM_MakeMenu(	// Create a menu. This menu should actually be read from a config file, but it's just demo!
			PMItem("Internet"),
				PMSubMenu("Internet"),
					PMItem("Miami"),		PM_UserData,	20,	End,
					PMItem("YAM"),			PM_UserData,	21,	End,
					PMItem("IBrowse"),		PM_UserData,	22,	End,
					PMItem("AmFTP"),		PM_UserData,	23,	End,
					PMItem("AmIRC"),		PM_UserData,	24,	End,
				End,
			End,
			PMTitleBar,						End,
			PMItem("CEd"),			PM_UserData,	25,	End,
			PMItem("DPaint"),		PM_UserData,	26,	End,
			PMItem("CLI"),			PM_UserData,	27,	End,
			PMItem("DosTrace"),		PM_UserData,	28,	End,
			PMItem("ARTM"),			PM_UserData,	29,	End,
			PMTitleBar,						End,
			PMItem("Exit InControl"),	PM_UserData,	5,	End,
			PMTitleBar,						End,
			PMItem("Reboot"),		PM_UserData,	10,	End,
			End;

		if(p) {
			w=OpenWindowTags(NULL,	WA_IDCMP,	IDCMP_CLOSEWINDOW|IDCMP_MOUSEBUTTONS,	// Open a little window
					WA_RMBTrap,	TRUE,
					WA_DragBar,	TRUE,
					WA_Top,		0,
					WA_Left,	0,
					WA_Width,	19,
					WA_Height,	11,
					WA_Borderless,	TRUE,
					WA_CloseGadget,	TRUE,
					TAG_DONE);
			if(w) {

				w->WScreen->BarHBorder=25;	// This will move the text in the screenbar next time it is redrawn
								// Unfortunately it will also mess up the menus...
								// Mail me if you know a better way to move the screentitle!

				while(r) {
					WaitPort(w->UserPort);						// Wait for a message
					while((im=(struct IntuiMessage *)GetMsg(w->UserPort))) {	// Get the message
						CopyMem(im,&imsg,sizeof(struct IntuiMessage));		// Copy the contents of it
						ReplyMsg((struct Message *)im);				// Reply the message

						switch(imsg.Class) {
							case IDCMP_CLOSEWINDOW:
							case IDCMP_MOUSEBUTTONS:
								r=(BOOL)PM_OpenPopupMenu(w,
										PM_Menu,		p,
										PM_Left,		10,
										PM_Top,			12,
										PM_WideSelectBar,	TRUE,
										PM_RecessSelected,	TRUE,
										PM_Code,		imsg.Code,
										TAG_DONE);
								switch(r) {
									case 5:
										r=0;
										break;
									case 0:
										r=10;
										break;
									case 10:
										break;
									case 20:
										system("RUN <>NIL: COMM:MIAMI/MIAMI");
										break;
									case 21:
										system("RUN <>NIL: COMM:YAM/YAM");
										break;
									case 22:
										system("RUN <>NIL: COMM:IBROWSE/IBROWSE");
										break;
									case 23:
										system("RUN <>NIL: COMM:AMFTP/AMFTP");
										break;
									case 24:
										system("RUN <>NIL: COMM:AMIRC/AMIRC");
										break;
									case 25:
										system("RUN <>NIL: CED");
										break;
									case 26:
										break;
									case 27:
										system("RUN <>NIL: NewCLI");
										break;
									case 28:
										system("RUN <>NIL: DOSTRACE");
										break;
									case 29:
										system("RUN <>NIL: ARTM");
										break;
								}
							break;
						}
					}
				}
				CloseWindow(w);
			} else printf("Window error!\n");
			PM_FreePopupMenu(p);
		} else printf("Menu error!\n");
		CloseLibrary((struct Library *)PopupMenuBase);
	}
}
