Squashed commit of 'x11-hostlib' branch
[tangerine.git] / workbench / demos / menudemo.c
blob7b42686fc7f10c15eaa74df37223053c8b2048d1
1 #include <intuition/intuition.h>
2 #include <libraries/gadtools.h>
4 #include <proto/exec.h>
5 #include <proto/dos.h>
6 #include <proto/intuition.h>
7 #include <proto/graphics.h>
8 #include <proto/gadtools.h>
9 #include <proto/alib.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <ctype.h>
14 #include <stdlib.h>
15 #include <math.h>
17 struct IntuitionBase *IntuitionBase;
18 struct GfxBase *GfxBase;
19 struct Library *GadToolsBase;
21 static struct Screen *scr;
22 static struct DrawInfo *dri;
23 static struct Window *win;
24 static struct Menu *menus;
25 static APTR vi;
27 static struct NewMenu nm[] =
29 {NM_TITLE, "Menu Title 1" },
30 {NM_ITEM, "Item 1", "A"},
31 {NM_ITEM, "Item 2", "B"},
32 {NM_ITEM, "Item 3", "C"},
33 {NM_ITEM, NM_BARLABEL },
34 {NM_ITEM, "Item 4" },
35 {NM_SUB, "Subitem 1", "!"},
36 {NM_SUB, "Subitem 2", "O"},
37 {NM_SUB, NM_BARLABEL},
38 {NM_SUB, "Subitem 3", "Q"},
39 {NM_ITEM, "Item 5" },
40 {NM_ITEM, NM_BARLABEL},
41 {NM_ITEM, "Quit" },
42 {NM_TITLE, "Menu Title 2"},
43 {NM_ITEM, "One", "1", CHECKIT | CHECKED | MENUTOGGLE},
44 {NM_ITEM, "Two", "2"},
45 {NM_ITEM, "Three", "3"},
46 {NM_TITLE, "Menus with Commandstrings"},
47 {NM_ITEM, "Operation 1", "CTRL 1", NM_COMMANDSTRING},
48 {NM_ITEM, "Operation 3", "CTRL 2", NM_COMMANDSTRING},
49 {NM_ITEM, "Help", "ALT 3", NM_COMMANDSTRING},
50 {NM_ITEM, NM_BARLABEL},
51 {NM_ITEM, "Cool", "4", CHECKIT | CHECKED, 32 + 64 + 128},
52 {NM_ITEM, "Great", "5", CHECKIT, 16 + 64 + 128},
53 {NM_ITEM, "Nice", "6", CHECKIT, 16 + 32 + 128},
54 {NM_ITEM, "Bad", "7", CHECKIT, 16 + 32 + 64},
55 {NM_TITLE, "Disabled", NULL, NM_MENUDISABLED},
56 {NM_ITEM, "Item 1"},
57 {NM_ITEM, "Item 2"},
58 {NM_ITEM, "Item 3"},
59 {NM_ITEM, NM_BARLABEL},
60 {NM_ITEM, "Sub"},
61 {NM_SUB, "Sub 1"},
62 {NM_SUB, "Sub 2"},
63 {NM_SUB, "Sub 3"},
64 {NM_END}
67 static void Cleanup(char *msg)
69 WORD rc;
71 if (msg)
73 printf("MenuDemo: %s\n",msg);
74 rc = RETURN_WARN;
75 } else {
76 rc = RETURN_OK;
79 if (win)
81 ClearMenuStrip(win);
82 CloseWindow(win);
85 if (menus) FreeMenus(menus);
87 if (vi) FreeVisualInfo(vi);
88 if (dri) FreeScreenDrawInfo(scr,dri);
89 if (scr) UnlockPubScreen(0,scr);
91 if (GadToolsBase) CloseLibrary(GadToolsBase);
92 if (GfxBase) CloseLibrary((struct Library *)GfxBase);
93 if (IntuitionBase) CloseLibrary((struct Library *)IntuitionBase);
95 exit (rc);
98 static void OpenLibs(void)
100 if (!(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",39)))
102 Cleanup("Can't open intuition.library V39!");
105 if (!(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",39)))
107 Cleanup("Can't open graphics.library V39!");
110 if (!(GadToolsBase = OpenLibrary("gadtools.library",39)))
112 Cleanup("Can't open gadtools.library V39!");
116 static void GetVisual(void)
118 if (!(scr = LockPubScreen(0)))
120 Cleanup("Can't lock screen!");
123 if (!(dri = GetScreenDrawInfo(scr)))
125 Cleanup("Can't get drawinfo!");
128 if (!(vi = GetVisualInfo(scr,0)))
130 Cleanup("Can't get visual info!");
134 static void MakeMenus(void)
136 menus = CreateMenusA(nm, NULL);
137 if (!menus) Cleanup("Can't create menus!");
139 if (!LayoutMenusA(menus, vi, NULL)) Cleanup("Can't layout menus!");
142 static void MakeWin(void)
144 win = OpenWindowTags(0,WA_Left,scr->MouseX,
145 WA_Top,scr->MouseY,
146 WA_Width,200,
147 WA_Height,scr->WBorTop + scr->Font->ta_YSize + 1,
148 WA_AutoAdjust,TRUE,
149 WA_Title,(IPTR)"MenuDemo",
150 WA_CloseGadget,TRUE,
151 WA_DepthGadget,TRUE,
152 WA_DragBar,TRUE,
153 WA_Activate,TRUE,
154 WA_IDCMP, IDCMP_CLOSEWINDOW | IDCMP_MENUPICK,
155 TAG_DONE);
157 if (!win) Cleanup("Can't open window!");
159 SetMenuStrip(win, menus);
161 ScreenToFront(win->WScreen);
164 static void HandleAll(void)
166 struct IntuiMessage *msg;
167 struct MenuItem *item;
168 UWORD men;
170 BOOL quitme = FALSE;
172 while(!quitme)
174 WaitPort(win->UserPort);
175 while ((msg = (struct IntuiMessage *)GetMsg(win->UserPort)))
177 switch(msg->Class)
179 case IDCMP_CLOSEWINDOW:
180 quitme = TRUE;
181 break;
183 case IDCMP_MENUPICK:
184 printf("\nMENUPICK: ------------------------\n\n");
185 men = msg->Code;
187 while(men != MENUNULL)
189 printf("MENU %d, ITEM %d, SUBITEM %d\n", MENUNUM(men), ITEMNUM(men), SUBNUM(men));
191 if ((item = ItemAddress(menus, men)))
193 men = item->NextSelect;
194 } else {
195 men = MENUNULL;
198 break;
200 } /* switch(msg->Class) */
202 ReplyMsg((struct Message *)msg);
204 } /* while ((msg = (struct IntuiMessage *)GetMsg(win->UserPort))) */
206 } /* while(!quitme) */
209 int main(void)
211 OpenLibs();
212 GetVisual();
213 MakeMenus();
214 MakeWin();
215 HandleAll();
216 Cleanup(0);
217 return 0;