home *** CD-ROM | disk | FTP | other *** search
- /* test.c - program to demonstrate/test the PopMenu Package */
- /* by Paul T. Miller */
- /* (Link with popmenu_pak.o and graphics_pak.o) */
-
- #include <intuition/intuition.h>
- #include "graphics_pak.h"
- #include "popmenu_pak.h"
-
- #define WIN_X 20
- #define WIN_Y 40
- #define WIN_W 320
- #define WIN_H 150
-
- struct NewWindow nw;
-
- struct Window *window = NULL;
- struct PMenu *pmenu = NULL; /* make a pointer to a PMenu for each one */
-
- /* Initialize your IntuiText and PMenuItem structures like you would any
- other menu (of course, you could allocate them dynamically) */
- struct IntuiText pitemtext[] = {
- {1, 0, JAM1, 0, 0, NULL, (UBYTE *)"Broad Sword", NULL},
- {1, 0, JAM1, 0, 0, NULL, (UBYTE *)"Club", NULL},
- {1, 0, JAM1, 0, 0, NULL, (UBYTE *)"Dagger", NULL},
- {1, 0, JAM1, 0, 0, NULL, (UBYTE *)"Javelin", NULL},
- {1, 0, JAM1, 0, 0, NULL, (UBYTE *)"Long Sword", NULL},
- {1, 0, JAM1, 0, 0, NULL, (UBYTE *)"Mace", NULL},
- {1, 0, JAM1, 0, 0, NULL, (UBYTE *)"Short Sword", NULL}
- };
-
- /* Notice the CENTERD flag: This centers the menu item text in the menu.
- Make sure you specified the ITEMTEXT flag, no other PopMenu rendering is
- supported yet. Also, absence of ITEMENABLED does not yet ghost the item,
- but you can't select it either. */
-
- struct PMenuItem pitems[] = {
- {&pitems[1],CENTERED|ITEMTEXT|ITEMENABLED,NULL,(APTR)&pitemtext[0],NULL},
- {&pitems[2],CENTERED|ITEMTEXT|ITEMENABLED,NULL,(APTR)&pitemtext[1],NULL},
- {&pitems[3],CENTERED|ITEMTEXT|ITEMENABLED,NULL,(APTR)&pitemtext[2],NULL},
- {&pitems[4],CENTERED|ITEMTEXT|ITEMENABLED,NULL,(APTR)&pitemtext[3],NULL},
- {&pitems[5],CENTERED|ITEMTEXT|ITEMENABLED,NULL,(APTR)&pitemtext[4],NULL},
- {&pitems[6],CENTERED|ITEMTEXT|ITEMENABLED,NULL,(APTR)&pitemtext[5],NULL},
- {NULL, CENTERED|ITEMTEXT|ITEMENABLED,NULL,(APTR)&pitemtext[6],NULL}
- };
-
- void main(void);
- void abort(char *);
- void setup(void);
- void setup_display(void);
- void closedown(void);
- void close_display(void);
- void handle_input(void);
-
- void main()
- {
- setup();
- handle_input();
- }
-
- void abort(txt)
- char *txt;
- {
- puts(txt);
- closedown();
- exit(0);
- }
-
- void setup()
- {
- OpenLibraries(GFXBASE|INTUITIONBASE); /* graphics_pak.o */
- setup_display();
- }
-
- void closedown()
- {
- close_display();
- CloseLibraries(); /* graphics_pak.o */
- }
-
- void setup_display()
- {
- ULONG flags, IDCMPflags;
-
- setmem(&nw, sizeof(struct NewWindow), 0L);
-
- /* you don't have to include the GADGETDOWN flag in your IDCMP, but
- InitPMenu() turns this flag on for your window anyway (as PopMenus
- are activated with GADGETDOWN messages) */
-
- IDCMPflags = RAWKEY | GADGETUP | GADGETDOWN | CLOSEWINDOW;
- flags = WINDOWDRAG | WINDOWDEPTH | SMART_REFRESH | WINDOWCLOSE | ACTIVATE;
-
- /* set up your list-window like any other window. */
- /* make sure your window is tall enough for all of the menu items, and
- that they won't run off the edge of the window */
-
- nw.LeftEdge = WIN_X;
- nw.TopEdge = WIN_Y;
- nw.Width = WIN_W;
- nw.Height = WIN_H;
- nw.DetailPen = -1;
- nw.BlockPen = -1;
- nw.IDCMPFlags = IDCMPflags;
- nw.Flags = flags;
- nw.Title = "A Window with a PopMenu";
- nw.FirstGadget = NULL;
- nw.Screen = NULL;
- nw.Type = WBENCHSCREEN;
-
- /* open your window - PopMenus must be attached to windows */
-
- window = (struct Window *)OpenWindow(&nw);
- if (!window)
- abort("Can't open PopMenu Window.");
-
- /* allocate your PopMenu - use type COMMPMENU, LISTPMENUs are not
- yet supported. Also, make the first item the active (visible) one.
- The NULL means we don't want a default title - we just show the
- selected item name in the menu box. We'll use 100 for this PopMenu ID.
- Use this ID to find out which PopMenu was selected (DO NOT USE ANY
- GADGETS IN YOUR PROGRAM WITH THE SAME ID AS ANY POPMENUs). */
-
- pmenu = BuildPMenu(&pitems[0], COMMPMENU|SHADOWED, 1, NULL, 100);
- if (!pmenu)
- abort("Can't allocate the PopMenu.");
-
- /* you can make constant changes to the width and height of the PopMenu
- before you initialize it with InitPMenu(), otherwise the width/height
- will be derrived from the window's RastPort and the longest item name
- length. The separation of the menu items is derrived from the PMenu
- height as well, so don't use values that are too large.
-
- i.e.: "pmenu->Width = 150;"
- "pmenu->Height = 10;" */
-
- /* Now initialize and attach your PopMenu to your window, with the
- upper-left corner at 10, 20. */
-
- InitPMenu(window, pmenu, 50, 45);
- }
-
- void close_display()
- {
- if (pmenu)
- {
- RemovePMenu(pmenu); /* remove the PopMenu from the window */
- FreePMenu(pmenu); /* deallocate the PopMenu */
- }
- if (window) CloseWindow(window);
- }
-
- void handle_input(void)
- {
- struct IntuiMessage *message;
- ULONG class;
- USHORT code, qualifier;
- struct Gadget *gadget;
- struct Window *win;
- USHORT item;
- struct PMenu *popmenu;
-
- /* receive and handle Intui-events normally */
-
- while (1)
- {
- WaitPort(window->UserPort);
- while (message = (struct IntuiMessage *)GetMsg(window->UserPort))
- {
- class = message->Class;
- code = message->Code;
- qualifier = message->Qualifier;
- win = message->IDCMPWindow;
- gadget = (struct Gadget *)message->IAddress;
-
- ReplyMsg((struct Message *)message);
-
- switch (class)
- {
- case CLOSEWINDOW:
- abort("");
- break;
-
- /* when you get a GADGETDOWN message, look at the GadgetID of the
- returned gadget. If it matches one of your PopMenu ID's, pass
- a pointer to the gadget's UserData field (typecast to type
- (struct PMenu *) first) to the HandlePMenu() function, which
- will draw the menu and let the user select (or not) an item */
-
- case GADGETDOWN:
- if (gadget->GadgetID == 100) /* test to see if a PopMenu */
- {
- popmenu = (struct PopMenu *)gadget->UserData;
- if (!popmenu) break; /* just to be sure... */
-
- item = HandlePMenu(popmenu); /* get the item number */
-
- if (item == 0)
- printf("No Item selected.\n");
- else
- printf("Item #%d Selected: %s\n", item,
- (char *)pitemtext[item-1].IText);
- }
- break;
- case MENUPICK:
- break;
- case RAWKEY:
- break;
- }
- }
- }
- }
-