Debian Codegraph @

apps/samples/motif/menu.c++

raw
1 /*
2 *
3 * Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * Further, this software is distributed without any warranty that it is
16 * free of the rightful claim of any third person regarding infringement
17 * or the like. Any license provided herein, whether implied or
18 * otherwise, applies only to this software file. Patent licenses, if
19 * any, provided herein do not apply to combinations of this program with
20 * other software, or any other product whatsoever.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
27 * Mountain View, CA 94043, or:
28 *
29 * http://www.sgi.com
30 *
31 * For further information regarding this notice, see:
32 *
33 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
34 *
35 */
36
37 //
38 // This demonstrates how to set up and use a Motif pulldown menu.
39 //
40 // There are two menus, File and Edit.
41 // The File menu has several items, including a separator line,
42 // and keyboard accelerators for the menu items.
43 // The Edit menu also has several items, but rather than simple push
44 // button items, this menu has a toggle button and three radio buttons.
45 //
46 // A toggle button has an on/off state.
47 // A set of radio buttons has the property that only one can be on
48 // at a time, the others are off.
49 //
50
51 #include <cstdlib>
52 #include <X11/Intrinsic.h>
53 #include <Xm/Xm.h>
54 #include <Xm/CascadeBG.h>
55 #include <Xm/Form.h>
56 #include <Xm/RowColumn.h>
57 #include <Xm/PushB.h>
58 #include <Xm/PushBG.h>
59 #include <Xm/Separator.h>
60 #include <Xm/SeparatoG.h>
61 #include <Xm/ToggleB.h>
62 #include <Xm/ToggleBG.h>
63
64 #include <Inventor/So.h>
65 #include <Inventor/Xt/SoXt.h>
66 #include <Inventor/Xt/SoXtRenderArea.h>
67 #include <Inventor/nodes/SoDirectionalLight.h>
68 #include <Inventor/nodes/SoPerspectiveCamera.h>
69 #include <Inventor/nodes/SoSeparator.h>
70 #include <Inventor/nodes/SoCone.h>
71
72 #define SCREEN(w) XScreenNumberOfScreen(XtScreen(w))
73
74 //
75 // Macros to set Motif toggle buttons on or off
76 //
77 #define TOGGLE_ON(BUTTON) \
78 XmToggleButtonSetState((Widget) BUTTON, TRUE, FALSE)
79 #define TOGGLE_OFF(BUTTON) \
80 XmToggleButtonSetState((Widget) BUTTON, FALSE, FALSE)
81
82 //
83 // Menu item constants - each item in a menu gets a unique id
84 //
85 #define k_OPEN_ITEM 0
86 #define k_SAVE_ITEM 1
87 #define k_PRINT_ITEM 2
88 #define k_QUIT_ITEM 3
89
90 #define k_ONOFF_ITEM 0
91 #define k_ONE_ITEM 1
92 #define k_TWO_ITEM 2
93 #define k_THREE_ITEM 3
94
95 //
96 // The edit menu has radio buttons. Rather than use the radio
97 // buttons which Motif supplies, we handle things ourselves.
98 //
99 typedef struct EditMenuItems {
100 Widget *items; // all the items in this menu
101 int which; // specifies which radio button is on
102 } EditMenuItems;
103
104
105 ////////////////////////////////////////////////////////////////////////
106 //
107 // This is called by Xt when a menu item is picked from the File menu.
108 // The first and last parameters are not used by this routine.
109 // userData is specified when the menu items are created - we set it
110 // to be the unique id of the menu item.
111 //
112 static void
113 fileMenuPick(
114 Widget, // parameter not listed because it's not used
115 void *userData, // userData, specified in buildPulldownMenu
116 XmAnyCallbackStruct *) // parameter not listed because it's not used
117 //
118 ////////////////////////////////////////////////////////////////////////
119 {
120 int which = (long) userData;
121
122 switch (which) {
123 case k_OPEN_ITEM: printf("Open...\n"); break;
124 case k_SAVE_ITEM: printf("Save...\n"); break;
125 case k_PRINT_ITEM: printf("Print...\n"); break;
126 case k_QUIT_ITEM: printf("Quit\n"); exit(0);
127 default: printf("UNKNOWN file menu item!!!\n"); break;
128 }
129 }
130
131 ////////////////////////////////////////////////////////////////////////
132 //
133 // This is called by Xt when a menu item is picked from the Edit menu.
134 // w is the menu item chosen.
135 // cb is the callback data supplied by motif; in this case, toggle
136 // button data
137 // userData is specified when the menu items are created - we set it
138 // to be the unique id of the menu item.
139 //
140 static void
141 editMenuPick(
142 Widget w, // menu item chosen
143 void *userData, // userData, specified in buildPulldownMenu
144 XmAnyCallbackStruct *cb)// callback data
145 //
146 ////////////////////////////////////////////////////////////////////////
147 {
148 XmToggleButtonCallbackStruct *toggle = (XmToggleButtonCallbackStruct *)cb;
149 int which = (long) userData;
150
151 // get the user data for this menu item.
152 // we expect it to be an array of widgets for this menu.
153 Arg args[1];
154 EditMenuItems *menuItems;
155 XtSetArg(args[0], XmNuserData, &menuItems);
156 XtGetValues(w, args, 1);
157
158 switch (which) {
159 case k_ONOFF_ITEM:
160 printf("On off - ");
161 if (toggle->set)
162 printf("ON\n");
163 else printf("OFF\n");
164 break;
165
166 case k_ONE_ITEM:
167 printf("One\n");
168 menuItems->which = k_ONE_ITEM;
169 break;
170
171 case k_TWO_ITEM:
172 printf("Two\n");
173 menuItems->which = k_TWO_ITEM;
174 break;
175
176 case k_THREE_ITEM:
177 printf("Three\n");
178 menuItems->which = k_THREE_ITEM;
179 break;
180
181 default:
182 printf("UNKNOWN edit menu item!!!\n");
183 break;
184 }
185 }
186
187
188 ////////////////////////////////////////////////////////////////////////
189 //
190 // This is called by Xt just before the Edit menu is displayed.
191 // It allows us to update the radio button toggles so the currently
192 // selected button is toggled on.
193 //
194 static void
195 editMenuDisplay(Widget, void *userData, XtPointer)
196 //
197 ////////////////////////////////////////////////////////////////////////
198 {
199 EditMenuItems *menuItems = (EditMenuItems *) userData;
200
201 // toggle all radio buttons off...
202 TOGGLE_OFF(menuItems->items[k_ONE_ITEM]);
203 TOGGLE_OFF(menuItems->items[k_TWO_ITEM]);
204 TOGGLE_OFF(menuItems->items[k_THREE_ITEM]);
205
206 // then turn the current one on...
207 TOGGLE_ON(menuItems->items[menuItems->which]);
208 }
209
210
211 //
212 // These macros assume that 'pulldown' is the variable for the menu,
213 // and 'menuPick' is the callback function. They are merely for convenience.
214 //
215
216 // A push item has no on/off state - it merely gets selected
217 #define PUSH_ITEM(ITEM,NAME,KONST,FUNC) \
218 ITEM = XtCreateManagedWidget(NAME, \
219 xmPushButtonGadgetClass, pulldown, args, n); \
220 XtAddCallback(ITEM, XmNactivateCallback,\
221 (XtCallbackProc) FUNC, \
222 (XtPointer) KONST)
223
224 // A toggle item has on/off state
225 #define TOGGLE_ITEM(ITEM,NAME,KONST,FUNC) \
226 ITEM = XtCreateManagedWidget(NAME, \
227 xmToggleButtonGadgetClass, pulldown, args, n); \
228 XtAddCallback(ITEM, XmNvalueChangedCallback,\
229 (XtCallbackProc) FUNC, \
230 (XtPointer) KONST)
231
232 // A separator draws a line between menu items
233 #define SEP_ITEM(NAME) \
234 (void) XtCreateManagedWidget("separator", \
235 xmSeparatorGadgetClass, pulldown, NULL, 0)
236
237
238 ////////////////////////////////////////////////////////////////////////
239 //
240 // This creates the File menu and all its items.
241 // The items in this menu have accelerators (keyboard shortcuts).
242 //
243 Widget
244 buildFileMenu(Widget menubar)
245 //
246 ////////////////////////////////////////////////////////////////////////
247 {
248 Widget items[4];
249 Widget pulldown;
250 Arg args[4];
251 int n;
252
253
254 // Tell motif to create the menu in the popup plane
255 Arg popupargs[4];
256 int popupn = 0;
257
258 #ifdef MENUS_IN_POPUP
259 SoXt::getPopupArgs(XtDisplay(menubar), SCREEN(menubar),
260 popupargs, &popupn);
261 #endif
262
263 pulldown = XmCreatePulldownMenu(menubar, "fileMenu", popupargs, popupn);
264
265 #ifdef MENUS_IN_POPUP
266 // register callbacks to load/unload the pulldown colormap when the
267 // pulldown menu is posted.
268 SoXt::registerColormapLoad(pulldown, SoXt::getShellWidget(menubar));
269 #endif
270
271 // Accelerators are keyboard shortcuts for the menu items
272 char *openAccel = "Alt <Key> o";
273 char *saveAccel = "Alt <Key> s";
274 char *printAccel = "Alt <Key> p";
275 char *quitAccel = "Alt <Key> q";
276 XmString openAccelText = XmStringCreate("Alt+o", XmSTRING_DEFAULT_CHARSET);
277 XmString saveAccelText = XmStringCreate("Alt+s", XmSTRING_DEFAULT_CHARSET);
278 XmString printAccelText = XmStringCreate("Alt+p", XmSTRING_DEFAULT_CHARSET);
279 XmString quitAccelText = XmStringCreate("Alt+q", XmSTRING_DEFAULT_CHARSET);
280
281 // The "..." in "Open..." signifies that choosing this menu item
282 // will bring up a dialog. (No dialog comes up in this demo,
283 // but one could conceivably bring up a file browser).
284 n = 0;
285 XtSetArg(args[n], XmNaccelerator, openAccel); n++;
286 XtSetArg(args[n], XmNacceleratorText, openAccelText); n++;
287 PUSH_ITEM(items[0], "Open...", k_OPEN_ITEM, fileMenuPick);
288
289 n = 0;
290 XtSetArg(args[n], XmNaccelerator, saveAccel); n++;
291 XtSetArg(args[n], XmNacceleratorText, saveAccelText); n++;
292 PUSH_ITEM(items[1], "Save...", k_SAVE_ITEM, fileMenuPick);
293
294 n = 0;
295 XtSetArg(args[n], XmNaccelerator, printAccel); n++;
296 XtSetArg(args[n], XmNacceleratorText, printAccelText); n++;
297 PUSH_ITEM(items[2], "Print...", k_PRINT_ITEM, fileMenuPick);
298
299 SEP_ITEM("separator");
300
301 // Quit has no "..." like the other menu items, because it
302 // simply executes a command - it does not bring up a dialog.
303 n = 0;
304 XtSetArg(args[n], XmNaccelerator, quitAccel); n++;
305 XtSetArg(args[n], XmNacceleratorText, quitAccelText); n++;
306 PUSH_ITEM(items[3], "Quit", k_QUIT_ITEM, fileMenuPick);
307
308 XtManageChildren(items, 4);
309
310 return pulldown;
311 }
312
313 ////////////////////////////////////////////////////////////////////////
314 //
315 // This creates the Edit menu and all its items.
316 //
317 Widget
318 buildEditMenu(Widget menubar)
319 //
320 ////////////////////////////////////////////////////////////////////////
321 {
322 Widget pulldown;
323 Arg args[4];
324 int n;
325
326 // items is malloced and passed as userData with the pulldown menu items.
327 // this way we can make sure the toggles keep up to date.
328 EditMenuItems *menuItems = new EditMenuItems;
329 menuItems->items = new Widget[4];
330 menuItems->which = k_ONE_ITEM;
331
332 // Tell motif to create the menu in the popup plane
333 Arg popupargs[4];
334 int popupn = 0;
335 #ifdef MENUS_IN_POPUP
336 SoXt::getPopupArgs(XtDisplay(menubar), SCREEN(menubar),
337 popupargs, &popupn);
338 #endif
339 pulldown = XmCreatePulldownMenu(menubar, "editMenu", popupargs, popupn);
340
341 #ifdef MENUS_IN_POPUP
342 // register callbacks to load/unload the pulldown colormap when the
343 // pulldown menu is posted.
344 SoXt::registerColormapLoad(pulldown, SoXt::getShellWidget(menubar));
345 #endif
346
347 // this callback is invoked just before the menu is displayed.
348 // this gives us a chance to update the radio button toggles.
349 XtAddCallback(pulldown, XmNmapCallback,
350 (XtCallbackProc) editMenuDisplay, (XtPointer) menuItems);
351
352 n = 0;
353 XtSetArg(args[n], XmNuserData, menuItems); n++;
354 TOGGLE_ITEM(menuItems->items[0], "On off", k_ONOFF_ITEM, editMenuPick);
355 SEP_ITEM("separator");
356
357 // the next three items are radio buttons - only one of the three
358 // will be toggled on at any time.
359 n = 0;
360 XtSetArg(args[n], XmNuserData, menuItems); n++;
361 XtSetArg(args[n], XmNindicatorType, XmONE_OF_MANY); n++;
362 TOGGLE_ITEM(menuItems->items[1], "One", k_ONE_ITEM, editMenuPick);
363 TOGGLE_ITEM(menuItems->items[2], "Two", k_TWO_ITEM, editMenuPick);
364 TOGGLE_ITEM(menuItems->items[3], "Three", k_THREE_ITEM, editMenuPick);
365
366 XtManageChildren(menuItems->items, 4);
367
368 return pulldown;
369 }
370
371 ////////////////////////////////////////////////////////////////////////
372 //
373 // This creates the pulldown menu bar and its menus.
374 //
375 Widget
376 buildPulldownMenu(Widget parent)
377 //
378 ////////////////////////////////////////////////////////////////////////
379 {
380 Widget menuButtons[2];
381 Widget pulldown1, pulldown2;
382 Arg args[8];
383 int n;
384
385 // menu bar
386 Widget menubar = XmCreateMenuBar(parent, "menuBar", NULL, 0);
387
388 pulldown1 = buildFileMenu(menubar);
389 pulldown2 = buildEditMenu(menubar);
390
391 // the text in the menubar for these menus
392 n = 0;
393 XtSetArg(args[n], XmNsubMenuId, pulldown1); n++;
394 menuButtons[0] = XtCreateManagedWidget("File",
395 xmCascadeButtonGadgetClass,
396 menubar, args, n);
397
398 n = 0;
399 XtSetArg(args[n], XmNsubMenuId, pulldown2); n++;
400 menuButtons[1] = XtCreateManagedWidget("Edit",
401 xmCascadeButtonGadgetClass,
402 menubar, args, n);
403
404 // manage the menu buttons
405 XtManageChildren(menuButtons, 2);
406
407 return menubar;
408 }
409
410 ////////////////////////////////////////////////////////////////////////
411 //
412 // This creates the main window contents. In this case, we have a
413 // menubar at the top of the window, and a render area filling out
414 // the remainder. These widgets are layed out with a Motif form widget.
415 //
416 SoXtRenderArea *
417 buildMainWindow(Widget parent, SoNode *sceneGraph)
418 //
419 ////////////////////////////////////////////////////////////////////////
420 {
421 Widget form;
422 Widget menubar;
423 SoXtRenderArea *renderArea;
424 Arg args[8];
425 int n;
426
427 // build the widgets
428 form = XtCreateWidget("form", xmFormWidgetClass, parent, NULL, 0);
429 menubar = buildPulldownMenu(form);
430
431 renderArea = new SoXtRenderArea(form);
432 renderArea->setSceneGraph(sceneGraph);
433
434 // layout
435 n = 0;
436 XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
437 XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
438 XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
439 XtSetArg(args[n], XmNbottomAttachment, XmATTACH_NONE); n++;
440 XtSetValues(menubar, args, n);
441
442 n = 0;
443 XtSetArg(args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
444 XtSetArg(args[n], XmNtopWidget, menubar); n++;
445 XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
446 XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
447 XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
448 XtSetValues(renderArea->getWidget(), args, n);
449
450 // manage the children
451 XtManageChild(menubar);
452 renderArea->show();
453
454 // manage the form widget
455 XtManageChild(form);
456
457 return renderArea;
458 }
459
460 ////////////////////////////////////////////////////////////////////////
461 //
462 // main - init Inventor and Xt, set up a scene graph and the main window,
463 // display this window, and loop forever...
464 //
465 int
466 main(int /*argc*/, char *argv[])
467 //
468 ////////////////////////////////////////////////////////////////////////
469 {
470 Widget mainWindow;
471
472 // Initialize Inventor and Xt.
473 mainWindow = SoXt::init(argv[0]);
474
475 if (mainWindow != NULL) {
476 // set up a scene graph to display
477 SoSeparator *root = new SoSeparator;
478 SoPerspectiveCamera *camera = new SoPerspectiveCamera;
479 SoDirectionalLight *light = new SoDirectionalLight;
480
481 root->ref();
482 root->addChild(camera);
483 root->addChild(light);
484 root->addChild(new SoCone);
485
486 // set up the pulldown menu with a render area
487 SoXtRenderArea *ra = buildMainWindow(mainWindow, root);
488
489 camera->viewAll(root, ra->getViewportRegion());
490
491 SoXt::show(mainWindow);
492 SoXt::mainLoop();
493 }
494 }
495