//
// $VER: Demo.c 1.1 (23.3.97)
//
// Popup Menu example program
//
// ©1996-1997 Henrik Isaksson
// All Rights Reserved.
//
// Run and click the mouse in the window and over the dragbar!
// (two different menus)
//

#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.

BOOL HandleReturnCodes(ULONG rc)
{
	BOOL running=TRUE;

	switch(rc) {
		case 10:
			system("RUN <>NIL: Disable");
			break;
		case 15:
			system("RUN <>NIL: StartMenu");
			break;
		case 20:
			system("RUN <>NIL: SimpleMenu");
			break;
		case 25:
			system("RUN <>NIL: OldStyle");
			break;
		case 5:
			running=FALSE;
			break;
	}

	return running;
}

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

	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!

		p1=PMMenu("Run Demos"),
			PMItem("Disable"),	PM_UserData,	10,	End,
			PMItem("StartMenu"),	PM_UserData,	15,	End,
			PMItem("SimpleMenu"),	PM_UserData,	20,	End,
			PMItem("OldStyle"),	PM_UserData,	25,	End,
			End;

		p2=PMMenu("PopupMenu Demo"),
			PMItem("About..."),				End,
			PMTitleBar,					End,
			PMItem("Quit"),		PM_UserData,	5,	End,
			End;

		if(p1 && p2) {
			w=OpenWindowTags(NULL,	WA_IDCMP,	IDCMP_CLOSEWINDOW|IDCMP_MOUSEBUTTONS,	// Open a little window
					WA_RMBTrap,	TRUE,
					WA_DragBar,	TRUE,
					WA_Left,	150,
					WA_Top,		100,
					WA_Width,	150,
					WA_Height,	100,
					WA_SizeGadget,	TRUE,
					WA_DepthGadget,	TRUE,
					WA_MinWidth,	150,
					WA_MaxWidth,	1280,
					WA_MinHeight,	100,
					WA_MaxHeight,	1024,
					WA_Title,	"Popup Menu Demo",
					WA_CloseGadget,	TRUE,
					TAG_DONE);
			if(w) {

				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: r=FALSE; break;
							case IDCMP_MOUSEBUTTONS:			// The user has hit a mousebutton - time to open the menu!
								if(w->MouseY>w->BorderTop) {
									r=HandleReturnCodes(PM_OpenPopupMenu(w,
										PM_Menu,		p1,
										PM_Code,		imsg.Code,
										TAG_DONE));
								} else {
									r=HandleReturnCodes(PM_OpenPopupMenu(w,
										PM_Menu,		p2,
										PM_Code,		imsg.Code,
										TAG_DONE));
								}
							break;
						}
					}
				}
				CloseWindow(w);
			} else printf("Window error!\n");
		} else printf("Menu error!\n");
		if(p1) PM_FreePopupMenu(p1);
		if(p2) PM_FreePopupMenu(p2);
		CloseLibrary((struct Library *)PopupMenuBase);
	}
}
