Debian Codegraph @

apps/demos/gview/gview.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 // gview - displays a graph view of an Inventor scene graph.
39 //
40
41 #include <cstdlib>
42 #include <unistd.h>
43
44 #include <X11/Intrinsic.h>
45 #include <Xm/CascadeBG.h>
46 #include <Xm/Form.h>
47 #include <Xm/PushBG.h>
48 #include <Xm/RowColumn.h>
49 #include <Xm/SeparatoG.h>
50
51 #include <Inventor/SoDB.h>
52 #include <Inventor/SoInput.h>
53 #include <Inventor/Xt/SoXt.h>
54 #include <Inventor/Xt/viewers/SoXtExaminerViewer.h>
55 #include <Inventor/actions/SoBoxHighlightRenderAction.h>
56 #include <Inventor/actions/SoGetBoundingBoxAction.h>
57 #include <Inventor/actions/SoWriteAction.h>
58 #include <Inventor/nodes/SoCallback.h>
59 #include <Inventor/nodes/SoGroup.h>
60 #include <Inventor/nodes/SoSelection.h>
61 #include <Inventor/nodes/SoSeparator.h>
62 #include <Inventor/nodes/SoShape.h>
63
64 #include "GraphViewer.h"
65 #include "MotifHelp.h"
66 #include "../../samples/common/InventorLogo.h"
67
68 #define SCREEN(w) XScreenNumberOfScreen(XtScreen(w))
69
70 static SoSelection *selector;
71 static SoXtExaminerViewer *examViewer;
72 static GraphViewer *graphViewer;
73
74 static SoSeparator *root;
75
76 ////////////////////////////////////////////////////////////////////////
77 //
78 // Description:
79 // Parses command line arguments.
80 //
81
82 static void
83 parseArgs(int argc, char *argv[], SoInput *in)
84 //
85 ////////////////////////////////////////////////////////////////////////
86 {
87 SbBool ok = TRUE;
88
89 // Check for filename
90 if (argc == 2)
91 ok = in->openFile(argv[1]);
92
93 // Otherwise, we are reading from stdin. Make sure the user knows this!
94 else if (argc == 1) {
95 if (isatty(fileno(stdin)))
96 fprintf(stderr, "Reading from standard input.\n\n");
97 }
98
99 // Too many args!
100 else
101 ok = FALSE;
102
103 if (! ok) {
104 fprintf(stderr, "Usage: %s [filename]\n", argv[0]);
105 fprintf(stderr, "Reads from standard input if no filename is given\n");
106 exit(1);
107 }
108 }
109
110 // Forward references
111 static void graphSelectionCB(void *, SoPath *);
112 static void graphDeselectionCB(void *, SoPath *);
113
114 ////////////////////////////////////////////////////////////////////////
115 //
116 // Description:
117 // Selection callback invoked when an object in the scene graph is
118 // selected. The path is in the scene graph. We need to select the
119 // appropriate icon in the display graph.
120 //
121
122 static void
123 sceneSelectionCB(void *, SoPath *path)
124 //
125 ////////////////////////////////////////////////////////////////////////
126 {
127 // Disable the callbacks so we don't loop
128 graphViewer->removeSelectionCallback(graphSelectionCB, NULL);
129 graphViewer->removeDeselectionCallback(graphDeselectionCB, NULL);
130
131 graphViewer->select(path);
132
133 // Re-enable the callbacks
134 graphViewer->addSelectionCallback(graphSelectionCB, NULL);
135 graphViewer->addDeselectionCallback(graphDeselectionCB, NULL);
136 }
137
138 ////////////////////////////////////////////////////////////////////////
139 //
140 // Description:
141 // Deselection callback invoked when an object in the scene graph
142 // is deselected.
143 //
144
145 static void
146 sceneDeselectionCB(void *, SoPath *)
147 //
148 ////////////////////////////////////////////////////////////////////////
149 {
150 // Disable the callback so we don't loop
151 graphViewer->removeDeselectionCallback(graphDeselectionCB, NULL);
152
153 graphViewer->deselectAll();
154
155 // Re-enable the callback
156 graphViewer->addDeselectionCallback(graphDeselectionCB, NULL);
157 }
158
159 ////////////////////////////////////////////////////////////////////////
160 //
161 // Description:
162 // Selection callback invoked when an icon in the display graph is
163 // selected. The path is in the scene graph. We need to add the
164 // SoSelection node at the top of this path and select the result
165 // in the scene graph.
166 //
167
168 static void
169 graphSelectionCB(void *, SoPath *path)
170 //
171 ////////////////////////////////////////////////////////////////////////
172 {
173 // Create a copy of the path that starts at the SoSelection node
174 SoPath *newPath = new SoPath(selector);
175 int length = path->getLength(), i;
176
177 newPath->ref();
178
179 // Add the root of the old path
180 newPath->append(path->getHead());
181
182 // We want a path that ends in either a shape or a group, since
183 // these are the only things that can be highlighted correctly.
184 // Truncate the path if it does not end in a shape or group.
185 if (! path->getTail()->isOfType(SoGroup::getClassTypeId()) &&
186 ! path->getTail()->isOfType(SoShape::getClassTypeId()))
187 length--;
188
189 // Add all other nodes in the path, named by index
190 for (i = 1; i < length; i++)
191 newPath->append(path->getIndex(i));
192
193 // Disable the callbacks so we don't loop
194 selector->removeSelectionCallback(sceneSelectionCB, NULL);
195 selector->removeDeselectionCallback(sceneDeselectionCB, NULL);
196
197 selector->deselectAll();
198
199 // If the resulting path contains no geometry, we can't highlight
200 // anything, so don't select it. Test this by computing the
201 // bounding box of the path.
202 SoGetBoundingBoxAction bba(examViewer->getViewportRegion());
203 bba.apply(newPath);
204 if (! bba.getBoundingBox().isEmpty())
205 selector->select(newPath);
206
207 // Re-enable the callbacks
208 selector->addSelectionCallback(sceneSelectionCB, NULL);
209 selector->addDeselectionCallback(sceneDeselectionCB, NULL);
210
211 newPath->unref();
212 }
213
214 ////////////////////////////////////////////////////////////////////////
215 //
216 // Description:
217 // Deselection callback invoked when an icon in the display graph
218 // is deselected. The path is in the scene graph.
219 //
220
221 static void
222 graphDeselectionCB(void *, SoPath *)
223 //
224 ////////////////////////////////////////////////////////////////////////
225 {
226 // Disable the callback so we don't loop
227 selector->removeDeselectionCallback(sceneDeselectionCB, NULL);
228
229 selector->deselectAll();
230
231 // Re-enable the callback
232 selector->addDeselectionCallback(sceneDeselectionCB, NULL);
233 }
234
235 ////////////////////////////////////////////////////////////////////////
236 //
237 // Description:
238 // Saves scene graph in file "gview.iv".
239 //
240
241 static void
242 saveCB(Widget, XtPointer, XtPointer)
243 //
244 ////////////////////////////////////////////////////////////////////////
245 {
246 SoWriteAction wa;
247
248 if (! wa.getOutput()->openFile("gview.iv"))
249 return;
250
251 wa.apply(root);
252
253 wa.getOutput()->closeFile();
254 }
255
256 ////////////////////////////////////////////////////////////////////////
257 //
258 // Description:
259 // Saves display graph in file "disp.iv".
260 //
261
262 static void
263 saveDispCB(Widget, XtPointer, XtPointer)
264 //
265 ////////////////////////////////////////////////////////////////////////
266 {
267 SoWriteAction wa;
268
269 if (! wa.getOutput()->openFile("disp.iv"))
270 return;
271
272 wa.apply(graphViewer->getDisplayGraph());
273
274 wa.getOutput()->closeFile();
275 }
276
277 ////////////////////////////////////////////////////////////////////////
278 //
279 // Description:
280 // Quits program.
281 //
282
283 static void
284 quitCB(Widget, XtPointer, XtPointer)
285 //
286 ////////////////////////////////////////////////////////////////////////
287 {
288 exit(0);
289 }
290
291 ////////////////////////////////////////////////////////////////////////
292 //
293 // Description:
294 // Builds the top-bar menu widget.
295 //
296
297 static Widget
298 buildMenu(Widget parent)
299 //
300 ////////////////////////////////////////////////////////////////////////
301 {
302 Widget menu, pulldown, cascade, but;
303 ARG_VARS(20);
304
305 RESET_ARGS();
306 ADD_TOP_FORM(0);
307 ADD_LEFT_FORM(0);
308 ADD_RIGHT_FORM(0);
309 menu = XmCreateMenuBar(parent, "menuBar", ARGS);
310
311 // Create a pulldown menu in the pop-up planes
312 RESET_ARGS();
313 SoXt::getPopupArgs(XtDisplay(menu), SCREEN(menu), args, &argN);
314 pulldown = XmCreatePulldownMenu(menu, "pulldown", ARGS);
315
316 // register callbacks to load/unload the pulldown colormap when the
317 // pulldown menu is posted.
318 SoXt::registerColormapLoad(pulldown, SoXt::getShellWidget(parent));
319
320 // Create a cascade button in the pulldown
321 RESET_ARGS();
322 ADD_ARG(XmNsubMenuId, pulldown);
323 ADD_ARG(XmNlabelString, STRING("gview"));
324 cascade = XmCreateCascadeButtonGadget(menu, "cascade", ARGS);
325 XtManageChild(cascade);
326
327 // Add buttons
328 RESET_ARGS();
329 ADD_ARG(XmNlabelString, STRING("Save in gview.iv"));
330 but = XmCreatePushButtonGadget(pulldown, "Save in gview.iv", ARGS);
331 XtAddCallback(but, XmNactivateCallback, saveCB, (XtPointer) NULL);
332 XtManageChild(but);
333
334 RESET_ARGS();
335 ADD_ARG(XmNlabelString, STRING("Save display graph in disp.iv"));
336 but = XmCreatePushButtonGadget(pulldown,
337 "Save display graph in disp.iv", ARGS);
338 XtAddCallback(but, XmNactivateCallback, saveDispCB, (XtPointer) NULL);
339 XtManageChild(but);
340
341 RESET_ARGS();
342 ADD_ARG(XmNlabelString, STRING("Quit"));
343 but = XmCreatePushButtonGadget(pulldown, "Quit", ARGS);
344 XtAddCallback(but, XmNactivateCallback, quitCB, (XtPointer) NULL);
345 XtManageChild(but);
346
347 return menu;
348 }
349
350 ////////////////////////////////////////////////////////////////////////
351 //
352 // Description:
353 // Callback to set up the logo viewport.
354 //
355
356 static void
357 logoCB(void *, SoAction *action)
358 {
359 if (action->isOfType(SoGLRenderAction::getClassTypeId()))
360 glViewport(0, 0, 80, 80);
361 }
362
363 ////////////////////////////////////////////////////////////////////////
364 //
365 // Description:
366 // Sets up the Inventor logo in the examiner viewer.
367 //
368
369 static void
370 setOverlayLogo(SoXtRenderArea *ra)
371 //
372 ////////////////////////////////////////////////////////////////////////
373 {
374 SoInput in;
375 in.setBuffer((void *) ivLogo, ivLogoSize);
376 SoSeparator *logo = SoDB::readAll(&in);
377 logo->ref();
378
379 // Add a callback node which will set the viewport
380 SoCallback *cb = new SoCallback;
381 cb->setCallback(logoCB);
382 logo->insertChild(cb, 0);
383
384 SbColor col(1, 1, 1);
385 ra->setOverlayColorMap(1, 1, &col);
386 ra->setOverlaySceneGraph(logo);
387 }
388
389 ////////////////////////////////////////////////////////////////////////
390 //
391 // Description:
392 // Mainline.
393 //
394
395 int
396 main(int argc, char **argv)
397 //
398 ////////////////////////////////////////////////////////////////////////
399 {
400 SoInput in;
401 Widget formWidget, menu, examWidget, graphWidget, sepWidget;
402 ARG_VARS(10);
403
404 Widget appWindow = SoXt::init(argv[0]);
405 if (appWindow == NULL)
406 exit(1);
407
408 parseArgs(argc, argv, &in);
409
410 root = SoDB::readAll(&in);
411
412 if (root == NULL) {
413 fprintf(stderr, "Bad data. Bye!\n");
414 exit(1);
415 }
416
417 in.closeFile();
418
419 root->ref();
420
421 // Create a selection node to handle selection
422 selector = new SoSelection;
423 selector->policy = SoSelection::SINGLE;
424 selector->addSelectionCallback(sceneSelectionCB, NULL);
425 selector->addDeselectionCallback(sceneDeselectionCB, NULL);
426
427 // Add root of icon display graph
428 selector->addChild(root);
429
430 // Create an examiner viewer and a graph viewer. Lay them out side
431 // by side in a form widget, with a separator between them and the
432 // menu at the top
433 formWidget = XmCreateForm(appWindow, "form", NULL, 0);
434
435 // Create the top-bar menu
436 menu = buildMenu(formWidget);
437
438 examViewer = new SoXtExaminerViewer(formWidget);
439 examViewer->setSceneGraph(selector);
440 examViewer->setGLRenderAction(new SoBoxHighlightRenderAction);
441 examViewer->redrawOnSelectionChange(selector);
442 setOverlayLogo(examViewer);
443 examWidget = examViewer->getWidget();
444
445 graphViewer = new GraphViewer(formWidget);
446 graphViewer->setSceneGraph(root);
447 graphViewer->addSelectionCallback(graphSelectionCB, NULL);
448 graphViewer->addDeselectionCallback(graphDeselectionCB, NULL);
449 graphWidget = graphViewer->getWidget();
450
451 sepWidget = XmCreateSeparatorGadget(formWidget, "separator", NULL, 0);
452
453 RESET_ARGS();
454 ADD_TOP_FORM(0);
455 ADD_LEFT_FORM(0);
456 ADD_RIGHT_FORM(0);
457 XtSetValues(menu, ARGS);
458
459 RESET_ARGS();
460 ADD_TOP_WIDGET(menu, 6);
461 ADD_BOTTOM_FORM(0);
462 ADD_LEFT_FORM(0);
463 ADD_ARG(XmNrightAttachment, XmATTACH_POSITION);
464 ADD_ARG(XmNrightPosition, 50);
465 XtSetValues(examWidget, ARGS);
466
467 RESET_ARGS();
468 ADD_ARG(XmNorientation, XmVERTICAL);
469 ADD_TOP_WIDGET(menu, 6);
470 ADD_BOTTOM_FORM(0);
471 ADD_LEFT_WIDGET(examWidget, 10);
472 XtSetValues(sepWidget, ARGS);
473
474 RESET_ARGS();
475 ADD_TOP_WIDGET(menu, 6);
476 ADD_BOTTOM_FORM(0);
477 ADD_LEFT_WIDGET(sepWidget, 10);
478 ADD_RIGHT_FORM(0);
479 XtSetValues(graphWidget, ARGS);
480
481 // Show the widgets
482 XtManageChild(formWidget);
483 XtManageChild(menu);
484 XtManageChild(sepWidget);
485 examViewer->show();
486 graphViewer->show();
487
488 SoXt::show(appWindow);
489 SoXt::mainLoop();
490
491 exit(0);
492 }