Debian Codegraph @

apps/demos/revo/revo.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 // Surface of revolution program
39 //
40
41 #include <cstdio>
42 #include <cmath>
43 #include <cstdlib>
44 #include <unistd.h>
45
46 #include <Inventor/SoDB.h>
47 #include <Inventor/Xt/SoXt.h>
48 #include <Inventor/Xt/SoXtRenderArea.h>
49 #include <Inventor/Xt/SoXtClipboard.h>
50 #include <Inventor/Xt/viewers/SoXtExaminerViewer.h>
51 #include <Inventor/nodes/SoNode.h>
52 #include <Inventor/nodes/SoCallback.h>
53 #include <Inventor/nodes/SoSeparator.h>
54
55 #include <Xm/Xm.h>
56 #include <Xm/Text.h>
57 #include <Xm/Form.h>
58 #include <Xm/Frame.h>
59 #include <Xm/ScrollBar.h>
60 #include <Xm/PushBG.h>
61 #include <Xm/LabelG.h>
62 #include <Xm/ToggleBG.h>
63 #include <Xm/RowColumn.h>
64
65 #include "../../samples/common/InventorLogo.h"
66 #include "LineManip.h"
67 #include "RevClass.h"
68
69 //
70 // These are defined in profile.c++
71 //
72 extern SoNode *createProfileGraph(Widget, RevolutionSurface *);
73 extern void clearPoints();
74
75 //
76 // Callback for 'About...' button
77 //
78 void
79 showAboutDialog(Widget, XtPointer, XtPointer)
80 {
81 if (access(IVDEMOBINDIR "/revo.about.pdf", R_OK) != 0)
82 {
83 system("xmessage 'Sorry, could not find "
84 IVDEMOBINDIR "/revo.about.pdf' > /dev/null");
85 return;
86 }
87
88 char command[2*PATH_MAX+100];
89 sprintf(command, "which " PDFVIEWER " >& /dev/null");
90
91 int err = system(command);
92 if (err)
93 {
94 system("xmessage 'You must install " PDFVIEWER
95 " for this function to work' > /dev/null");
96 return;
97 }
98
99 sprintf(command, PDFVIEWER " " IVDEMOBINDIR "/revo.about.pdf &");
100 system(command);
101 }
102
103 //
104 // Called by the quit button
105 //
106 static void
107 quitCallback(Widget, XtPointer, XtPointer)
108 {
109 exit(0);
110 }
111
112 //
113 // Callback for the text widget (called when the user hits return).
114 //
115 void
116 changeNumSides(Widget textWidget, XtPointer data, XtPointer)
117 {
118 static char t[4];
119 char *str = XmTextGetString((Widget)textWidget);
120 int NumSides = atoi(str) + 1;
121 if (NumSides < 4) NumSides = 4;
122
123 sprintf(t, "%d", (NumSides-1) % 1000);
124 XmTextSetString((Widget)textWidget, t);
125
126 XtFree(str);
127
128 RevolutionSurface *s = (RevolutionSurface *)data;
129 s->changeNumSides(NumSides);
130 }
131
132 //
133 // Callback for the copy button. Copies the surface of revolution to
134 // the clipboard.
135 //
136 static void
137 copyCallback(Widget, XtPointer vwr, XtPointer cbstruct)
138 {
139 static SoXtClipboard *theClipboard = NULL;
140
141 SoXtExaminerViewer *v = (SoXtExaminerViewer *)vwr;
142
143 if (theClipboard == NULL)
144 {
145 theClipboard = new SoXtClipboard(v->getWidget());
146 }
147 SoNode *root = v->getSceneGraph();
148 theClipboard->copy(root,
149 ((XmAnyCallbackStruct *)cbstruct)->event->xbutton.time);
150 }
151
152 static void
153 clearCallback(Widget, XtPointer, XtPointer)
154 {
155 clearPoints();
156 }
157 ////////////////////////////////////////////////////////////////////////
158 //
159 // Draw the Inventor logo in the overlays.
160 //
161 ////////////////////////////////////////////////////////////////////////
162
163 static void
164 logoCB(void *, SoAction *action)
165 {
166 if (action->isOfType(SoGLRenderAction::getClassTypeId()))
167 glViewport(0, 0, 80, 80); // See Dave Mott for details!
168 }
169 static void
170 setOverlayLogo(SoXtRenderArea *ra)
171 {
172 static SoSeparator *logo = NULL;
173 if (logo == NULL) {
174 SoInput in;
175 in.setBuffer((void *)ivLogo, ivLogoSize); // common directory
176 logo = SoDB::readAll(&in);
177 logo->ref();
178
179 SoCallback *cb = new SoCallback; // set the GL viewport
180 cb->setCallback(logoCB);
181 logo->insertChild(cb, 0);
182 }
183 SbColor col(1, 1, 1);
184 ra->setOverlayColorMap(1, 1, &col);
185 ra->setOverlaySceneGraph(logo);
186 }
187
188
189 ////////////////////////////////////////////////////////////////////////
190 //
191 // main
192 //
193 ////////////////////////////////////////////////////////////////////////
194
195 int
196 main(int, char **argv)
197 {
198 Widget w = SoXt::init(argv[0]);
199 if (w == NULL) exit(1);
200
201 LineManip2::initClass();
202
203 RevolutionSurface *revSurf = new RevolutionSurface();
204
205 Arg resources[20];
206 int n = 0;
207 XtSetArg(resources[n], "width", 800); n++;
208 XtSetArg(resources[n], "height", 400); n++;
209 Widget form = XmCreateForm(w, "form", resources, n); n = 0;
210
211 //
212 // This RowColumn widget holds all the stuff at the bottom
213 // of the screen
214 //
215 XtSetArg(resources[n], XmNorientation, XmHORIZONTAL); ++n;
216 XtSetArg(resources[n], XmNleftAttachment, XmATTACH_FORM); n++;
217 XtSetArg(resources[n], XmNadjustLast, FALSE); ++n;
218 XtSetArg(resources[n], XmNrightAttachment, XmATTACH_FORM); n++;
219 XtSetArg(resources[n], XmNbottomAttachment, XmATTACH_FORM); n++;
220 Widget rc = XmCreateRowColumn(form, "bottomStuff", resources, n);
221 n = 0;
222
223 //
224 // Add the viewer to view the shaded geometry
225 //
226 XtSetArg(resources[n], XmNtopAttachment, XmATTACH_FORM); n++;
227 XtSetArg(resources[n], XmNleftAttachment, XmATTACH_POSITION); n++;
228 XtSetArg(resources[n], XmNleftPosition, 50); n++;
229 XtSetArg(resources[n], XmNrightAttachment, XmATTACH_FORM); n++;
230 XtSetArg(resources[n], XmNbottomAttachment, XmATTACH_WIDGET); n++;
231 XtSetArg(resources[n], XmNbottomWidget, rc); n++;
232 Widget frame2 = XmCreateFrame(form, "renderAreaFrame", resources, n);
233 n = 0;
234
235 SoXtExaminerViewer *examViewer = new SoXtExaminerViewer(frame2);
236 examViewer->setSceneGraph(revSurf->getSceneGraph());
237 examViewer->setBorder(FALSE);
238 examViewer->setDecoration(FALSE);
239 setOverlayLogo( examViewer ); // Add Inventor logo to overlays
240 examViewer->show();
241 XtManageChild(frame2);
242
243 //
244 // Add viewer to edit the revolution profile
245 //
246 XtSetArg(resources[n], XmNtopAttachment, XmATTACH_FORM); n++;
247 XtSetArg(resources[n], XmNleftAttachment, XmATTACH_FORM); n++;
248 XtSetArg(resources[n], XmNrightAttachment, XmATTACH_POSITION); n++;
249 XtSetArg(resources[n], XmNrightPosition, 50); n++;
250 XtSetArg(resources[n], XmNbottomAttachment, XmATTACH_WIDGET); n++;
251 XtSetArg(resources[n], XmNbottomWidget, rc); n++;
252 Widget frame1 = XmCreateFrame(form, "renderAreaFrame", resources, n);
253 n = 0;
254
255 SoXtRenderArea *ra = new SoXtRenderArea(frame1);
256 ra->setSceneGraph(createProfileGraph(ra->getWidget(), revSurf));
257 ra->setBorder(FALSE);
258 ra->show();
259 XtManageChild(frame1);
260
261 #define STRING(a) XmStringCreate(a,XmSTRING_DEFAULT_CHARSET)
262
263 XtSetArg(resources[n], XmNlabelString, STRING("# sides:")); ++n;
264 Widget nsidesLabel = XmCreateLabelGadget(
265 rc, "nsidesLabel", resources, n);
266 n = 0;
267 XtManageChild(nsidesLabel);
268
269 XtSetArg(resources[n], XmNvalue, "20"); ++n;
270 XtSetArg(resources[n], XmNcolumns, 3); ++n;
271 XtSetArg(resources[n], XmNeditMode, XmSINGLE_LINE_EDIT); ++n;
272 Widget nsidesEdit = XmCreateText(
273 rc, "nsidesEdit", resources, n);
274 n = 0;
275 XtAddCallback(nsidesEdit, XmNactivateCallback,
276 changeNumSides, (XtPointer)revSurf);
277 XtManageChild(nsidesEdit);
278
279 XtSetArg(resources[n], XmNlabelString, STRING("Copy")); ++n;
280 Widget copyButton = XmCreatePushButtonGadget(
281 rc, "copy", resources, n);
282 n = 0;
283 XtAddCallback(copyButton, XmNactivateCallback,
284 copyCallback, (XtPointer)examViewer);
285 XtManageChild(copyButton);
286
287 XtSetArg(resources[n], XmNlabelString, STRING("Clear")); ++n;
288 Widget clearButton = XmCreatePushButtonGadget(
289 rc, "clear", resources, n);
290 n = 0;
291 XtAddCallback(clearButton, XmNactivateCallback,
292 clearCallback, (XtPointer)examViewer);
293 XtManageChild(clearButton);
294
295 XtSetArg(resources[n], XmNlabelString, STRING("About...")); ++n;
296 Widget aboutButton = XmCreatePushButtonGadget(
297 rc, "about", resources, n);
298 n = 0;
299 XtAddCallback(aboutButton, XmNactivateCallback,
300 showAboutDialog, NULL);
301 XtManageChild(aboutButton);
302
303 XtSetArg(resources[n], XmNlabelString, STRING("Quit")); ++n;
304 Widget quitButton = XmCreatePushButtonGadget(
305 rc, "quit", resources, n);
306 n = 0;
307 XtAddCallback(quitButton, XmNactivateCallback,
308 quitCallback, NULL);
309 XtManageChild(quitButton);
310
311 XtManageChild(rc);
312 XtManageChild(form);
313
314 examViewer->viewAll();
315 SoXt::show(w);
316 SoXt::mainLoop();
317
318 return 0; // Keep C++ from complaining...
319 }