Debian Codegraph @

apps/demos/textomatic/TextGraph.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 // Routines for handling the text scene graph
39 //
40 // The scene graph consists of a Profile2, Font, and Translation node,
41 // and then a series of separators, each of which represents a
42 // paragraph. The paragraphs consist of a Separator, under which
43 // there is a Translation, Material, and a Text3 node (underneath
44 // another separator which is used to cache the polygons).
45 //
46 // The whole thing is underneath a root Separator.
47 //
48
49 #include <cstdio>
50 #include <cstring>
51 #include <cmath>
52
53 #include <Inventor/Xt/SoXt.h>
54 #include <Inventor/Xt/SoXtClipboard.h>
55 #include <Inventor/Xt/SoXtMaterialEditor.h>
56 #include <Inventor/SoPath.h>
57 #include <Inventor/nodes/SoFont.h>
58 #include <Inventor/nodes/SoLinearProfile.h>
59 #include <Inventor/nodes/SoMaterial.h>
60 #include <Inventor/nodes/SoMaterialBinding.h>
61 #include <Inventor/nodes/SoPerspectiveCamera.h>
62 #include <Inventor/nodes/SoProfileCoordinate2.h>
63 #include <Inventor/nodes/SoSeparator.h>
64 #include <Inventor/nodes/SoText3.h>
65 #include <Inventor/nodes/SoTranslation.h>
66 #include <Inventor/sensors/SoAlarmSensor.h>
67 #include <Inventor/sensors/SoSensor.h>
68
69 #include <Xm/Xm.h>
70 #include <X11/Shell.h>
71 #include <Xm/Text.h>
72 #include <Xm/Form.h>
73 #include <Xm/RowColumn.h>
74 #include <Xm/ToggleBG.h>
75 #include <Xm/LabelG.h>
76
77 #include "TextGraph.h"
78 #include "TextWrapper.h"
79 #include "labels.h"
80
81 //
82 // Global variables. Evil, but I'm in a hurry.
83 //
84 SoProfileCoordinate2 *profileCoords;
85 SoLinearProfile *profile;
86
87 // Global for resources...
88 extern Labels labels;
89
90
91 //
92 // Variables local to this file. Also evil (not as evil as globals),
93 // but I'm not going to bother to encapsulate all this into a class,
94 // since I don't really plan on reusing it.
95 //
96
97 static SoSeparator *root;
98
99 struct TextThing {
100 SoSeparator *top;
101 SoTranslation *translation;
102 SoSeparator *cache;
103 SoText3 *text;
104 };
105
106 static TextThing *paragraphs;
107 static SoFont *TheFont;
108 static SoTranslation *TheTranslation;
109 static SoMaterial *TheMaterial;
110 static int numParagraphs = 0;
111 static const float spacing = 1.0; // Space between lines
112 static const float paraSpacing = 0.5; // Extra space between paragraphs
113
114 // Which parts material editor is editing
115 static int whichMatEditing = SoText3::ALL;
116 // Which parts (front/sides/back) of the text are visible
117 static int whichPartsOn = SoText3::FRONT;
118
119 //
120 // These two callbacks are called when the text in the Motif text
121 // widget changes. delayUpdateText is called while typing is
122 // happening; it, in turn, just schedules the delayCB to be called one
123 // second later. While typing, that sensor will be constantly
124 // rescheduled, so delayCB is not called until one second after typing
125 // has ceased. This is a pretty good compromise between the ideal
126 // immediate feedback and some sort of explicit gesture (like an OK
127 // button) that the user would have to use.
128 //
129 void
130 delayCB(void *textwidget, SoSensor *)
131 {
132 updateText(NULL, (XtPointer)textwidget, NULL);
133 }
134 void
135 delayUpdateText(Widget, XtPointer textwidget, XtPointer)
136 {
137 static SoAlarmSensor *alarmSensor = NULL;
138 if (alarmSensor == NULL) {
139 alarmSensor = new SoAlarmSensor;
140 alarmSensor->setFunction(delayCB);
141 }
142 else {
143 alarmSensor->unschedule();
144 }
145 alarmSensor->setData(textwidget);
146 alarmSensor->setTimeFromNow(SbTime(1.0)); // Wait a second...
147 alarmSensor->schedule();
148 }
149 //
150 // Actually change the scene graph based on the text entered into the
151 // Motif text widget
152 //
153 void
154 updateText(Widget, XtPointer textwidget, XtPointer)
155 {
156 char *str = XmTextGetString((Widget)textwidget);
157 //
158 // The TextWrapper word-wraps the text string. Here, we tell it
159 // to format the string for 30 character lines.
160 //
161 TextWrapper twrap(str, 30);
162 XtFree(str);
163
164 //
165 // Next, get rid of old text in scene graph and add the new text
166 // (with appropriate translation to move the text apart)
167 //
168 int i;
169 for (i = numParagraphs-1; i >= 0; i--) {
170 root->removeChild(paragraphs[i].top);
171 }
172 if (numParagraphs != 0) {
173 delete[] paragraphs;
174 }
175
176 numParagraphs = twrap.numParagraphs();
177
178 if (numParagraphs != 0) {
179 paragraphs = new TextThing[numParagraphs];
180 }
181 float currentY = 0.0;
182 for (i = 0; i < numParagraphs; i++) {
183 SoSeparator *top = new SoSeparator;
184 paragraphs[i].top = top;
185 root->addChild(top);
186
187 paragraphs[i].translation = new SoTranslation;
188 top->addChild(paragraphs[i].translation);
189 paragraphs[i].translation->
190 translation.setValue(0.0, currentY, 0.0);
191
192 paragraphs[i].cache = new SoSeparator;
193 top->addChild(paragraphs[i].cache);
194 paragraphs[i].cache->renderCaching = SoSeparator::ON;
195 paragraphs[i].cache->boundingBoxCaching = SoSeparator::ON;
196
197 SoText3 *tnode = new SoText3;
198 paragraphs[i].text = tnode;
199 paragraphs[i].cache->addChild(tnode);
200 tnode->justification = SoText3::CENTER;
201 tnode->parts = whichPartsOn;
202
203 for (int j = 0; j < twrap.numLines(i); j++) {
204 tnode->string.set1Value(j, twrap.getLine(i, j));
205 currentY -= spacing;
206 }
207 currentY -= paraSpacing;
208 }
209 //
210 // This centers the entire text vertically
211 //
212 float total_d = (-currentY - paraSpacing - spacing);
213 TheTranslation->translation.setValue(0.0, total_d/2.0, 0.0);
214 }
215
216 //
217 // Copy the text scene graph to the clipboard
218 //
219 void
220 copyText(Widget, XtPointer w, XtPointer cbstruct)
221 {
222 static SoXtClipboard *theClipboard = NULL;
223
224 if (theClipboard == NULL) {
225 theClipboard = new SoXtClipboard((Widget)w);
226 }
227 theClipboard->copy(root,
228 ((XmAnyCallbackStruct *)cbstruct)->event->xbutton.time);
229 }
230
231 //
232 // Turn the given part on or off.
233 //
234 static void
235 togglePart(Widget, XtPointer mydata, XtPointer cbstruct)
236 {
237 int flag = ((XmToggleButtonCallbackStruct *)cbstruct)->set;
238 int whichPart = (long)mydata;
239
240 if (flag) { // Turn part on
241 whichPartsOn |= whichPart;
242 }
243 else {
244 whichPartsOn &= ~whichPart;
245 }
246 for (int i = 0; i < numParagraphs; i++) {
247 paragraphs[i].text->parts.setValue(whichPartsOn);
248 }
249 }
250
251 //
252 // And cause the material editor to edit/not edit the given part.
253 //
254 static void
255 editToggle(Widget, XtPointer mydata, XtPointer cbstruct)
256 {
257 int flag = ((XmToggleButtonCallbackStruct *)cbstruct)->set;
258 int whichPart = (long)mydata;
259
260 if (flag) { // Edit the part
261 whichMatEditing |= whichPart;
262 }
263 else { // Don't edit it
264 whichMatEditing &= (~whichPart);
265 }
266 }
267
268 //
269 // The material editor calls this function whenever its material
270 // changes. We have to copy its material into the right place in the
271 // scene graph.
272 //
273 static void
274 matEditCB(void *, const SoMaterial *mtl)
275 {
276 if (whichMatEditing & SoText3::FRONT) {
277 TheMaterial->ambientColor.set1Value(0, mtl->ambientColor[0]);
278 TheMaterial->diffuseColor.set1Value(0, mtl->diffuseColor[0]);
279 TheMaterial->specularColor.set1Value(0, mtl->specularColor[0]);
280 TheMaterial->emissiveColor.set1Value(0, mtl->emissiveColor[0]);
281 TheMaterial->shininess.set1Value(0, mtl->shininess[0]);
282 TheMaterial->transparency.set1Value(0, mtl->transparency[0]);
283 }
284 if (whichMatEditing & SoText3::SIDES) {
285 TheMaterial->ambientColor.set1Value(1, mtl->ambientColor[0]);
286 TheMaterial->diffuseColor.set1Value(1, mtl->diffuseColor[0]);
287 TheMaterial->specularColor.set1Value(1, mtl->specularColor[0]);
288 TheMaterial->emissiveColor.set1Value(1, mtl->emissiveColor[0]);
289 TheMaterial->shininess.set1Value(1, mtl->shininess[0]);
290 TheMaterial->transparency.set1Value(1, mtl->transparency[0]);
291 }
292 if (whichMatEditing & SoText3::BACK) {
293 TheMaterial->ambientColor.set1Value(2, mtl->ambientColor[0]);
294 TheMaterial->diffuseColor.set1Value(2, mtl->diffuseColor[0]);
295 TheMaterial->specularColor.set1Value(2, mtl->specularColor[0]);
296 TheMaterial->emissiveColor.set1Value(2, mtl->emissiveColor[0]);
297 TheMaterial->shininess.set1Value(2, mtl->shininess[0]);
298 TheMaterial->transparency.set1Value(2, mtl->transparency[0]);
299 }
300 }
301
302 //
303 // This is called when the user presses the 'Edit Parts' button. It
304 // creates the parts editor (if necessary) and then manages it.
305 //
306 void
307 createPartEditor(Widget w, XtPointer, XtPointer)
308 {
309 static Widget shell = NULL;
310
311 if (shell == NULL) {
312 // Create 3 labels and 6 togglebuttons, and one material
313 // editor
314 Arg resources[20];
315 int n;
316
317 // change the XmNdeleteResponse (when use closes the window) from
318 // XmDESTROY to XmUNMAP since we don't want to delete the material
319 // editor widgets (we would also have to delete the material editor
320 // class since we can rebuild them independently). We could use
321 // XmCreateFormDialog() but some some strange reason the material
322 // editor has problems building inside one of them.
323 n = 0;
324 XtSetArg(resources[n], XmNdeleteResponse, XmUNMAP); ++n;
325 shell = XtCreatePopupShell("editParts", topLevelShellWidgetClass,
326 SoXt::getShellWidget(w), resources, n);
327
328 Widget form = XmCreateForm(shell, NULL, NULL, 0);
329
330 SoXtMaterialEditor *medit = new SoXtMaterialEditor(form);
331 medit->addMaterialChangedCallback(matEditCB, NULL);
332 XtSetArg(resources[n], XmNtopAttachment, XmATTACH_FORM); ++n;
333 XtSetArg(resources[n], XmNleftAttachment, XmATTACH_FORM); ++n;
334 XtSetArg(resources[n], XmNrightAttachment, XmATTACH_FORM); ++n;
335 XtSetValues(medit->getWidget(), resources, n);
336
337 // Create three columns (row/column widgets) containing 3
338 // buttons each, for a total of 12 widgets/gadgets.
339 Widget w[12]; // Used for XtManageChildren() call
340
341 n = 0;
342 XtSetArg(resources[n], XmNnumColumns, 1); ++n;
343 XtSetArg(resources[n], XmNorientation, XmVERTICAL); ++n;
344 XtSetArg(resources[n], XmNpacking, XmPACK_COLUMN); ++n;
345 XtSetArg(resources[n], XmNisAligned, TRUE); ++n;
346 XtSetArg(resources[n], XmNentryAlignment, XmALIGNMENT_END); ++n;
347 XtSetArg(resources[n], XmNadjustLast, FALSE); ++n;
348 XtSetArg(resources[n], XmNtopAttachment, XmATTACH_WIDGET); ++n;
349 XtSetArg(resources[n], XmNtopWidget, medit->getWidget()); ++n;
350 XtSetArg(resources[n], XmNbottomAttachment, XmATTACH_FORM); ++n;
351
352 // All the previous resources are the same for the three
353 // column widgets...
354 int nn = n;
355 XtSetArg(resources[n], XmNleftAttachment, XmATTACH_FORM); ++n;
356 XtSetArg(resources[n], XmNrightAttachment, XmATTACH_POSITION); ++n;
357 XtSetArg(resources[n], XmNrightPosition, 32); ++n;
358 w[0] = XmCreateRowColumn(form, "PartsLabels", resources, n);
359
360 n = nn;
361 XtSetArg(resources[n], XmNleftAttachment, XmATTACH_POSITION); ++n;
362 XtSetArg(resources[n], XmNleftPosition, 34); ++n;
363 XtSetArg(resources[n], XmNrightAttachment, XmATTACH_POSITION); ++n;
364 XtSetArg(resources[n], XmNrightPosition, 66); ++n;
365 w[1] = XmCreateRowColumn(form, "PartsOnOff", resources, n);
366
367 n = nn;
368 XtSetArg(resources[n], XmNleftAttachment, XmATTACH_POSITION); ++n;
369 XtSetArg(resources[n], XmNleftPosition, 67); ++n;
370 XtSetArg(resources[n], XmNrightAttachment, XmATTACH_FORM); ++n;
371 w[2] = XmCreateRowColumn(form, "PartsEdit", resources, n);
372 n = 0;
373
374 #define STRING(a) XmStringCreateLocalized(a)
375
376 XtSetArg(resources[n], XmNlabelString, STRING(labels.front)); ++n;
377 w[3] = XmCreateLabelGadget(w[0], "frontLabel", resources, n); n = 0;
378 XtSetArg(resources[n], XmNlabelString, STRING(labels.sides)); ++n;
379 w[4] = XmCreateLabelGadget(w[0], "sidesLabel", resources, n); n = 0;
380 XtSetArg(resources[n], XmNlabelString, STRING(labels.back)); ++n;
381 w[5] = XmCreateLabelGadget(w[0], "backLabel", resources, n); n = 0;
382
383 // The three parts toggles
384 XtSetArg(resources[n], XmNlabelString, STRING(labels.on)); ++n;
385 XtSetArg(resources[n], XmNset, 1); ++n;
386 w[6] = XmCreateToggleButtonGadget(w[1], "frontOnOff", resources, n);
387 XtAddCallback(w[6], XmNvalueChangedCallback,
388 togglePart, (XtPointer)SoText3::FRONT);
389 n = 0;
390
391 XtSetArg(resources[n], XmNlabelString, STRING(labels.on)); ++n;
392 if (whichPartsOn & SoText3::SIDES) {
393 XtSetArg(resources[n], XmNset, 1); ++n;
394 } else {
395 XtSetArg(resources[n], XmNset, 0); ++n;
396 }
397 w[7] = XmCreateToggleButtonGadget(w[1], "sidesOnOff", resources, n);
398 XtAddCallback(w[7], XmNvalueChangedCallback,
399 togglePart, (XtPointer)SoText3::SIDES);
400 n = 0;
401
402 XtSetArg(resources[n], XmNlabelString, STRING(labels.on)); ++n;
403 if (whichPartsOn & SoText3::BACK) {
404 XtSetArg(resources[n], XmNset, 1); ++n;
405 } else {
406 XtSetArg(resources[n], XmNset, 0); ++n;
407 }
408 w[8] = XmCreateToggleButtonGadget(w[1], "backOnOff", resources, n);
409 XtAddCallback(w[8], XmNvalueChangedCallback,
410 togglePart, (XtPointer)SoText3::BACK);
411 n = 0;
412
413 // And the three material toggles
414 XtSetArg(resources[n], XmNlabelString, STRING(labels.edit)); ++n;
415 XtSetArg(resources[n], XmNset, 1); ++n;
416 w[9] = XmCreateToggleButtonGadget(w[2], "frontEdit", resources, n);
417 XtAddCallback(w[9], XmNvalueChangedCallback,
418 editToggle, (XtPointer)SoText3::FRONT);
419
420 w[10] = XmCreateToggleButtonGadget(w[2], "sidesEdit", resources, n);
421 XtAddCallback(w[10], XmNvalueChangedCallback,
422 editToggle, (XtPointer)SoText3::SIDES);
423
424 w[11] = XmCreateToggleButtonGadget(w[2], "backEdit", resources, n);
425 XtAddCallback(w[11], XmNvalueChangedCallback,
426 editToggle, (XtPointer)SoText3::BACK);
427
428 XtManageChildren(w+3, 3); // labels/buttons
429 XtManageChildren(w+6, 3);
430 XtManageChildren(w+9, 3);
431 medit->show();
432 XtManageChildren(w, 3); // row-columns..
433 XtManageChild(form);
434 }
435 SoXt::show(shell);
436 }
437
438 //
439 // This function is called when the user goes into or out of the
440 // profile editor window. I could give the line manipulator
441 // start/edit/finish callbacks, and have those start and finish
442 // callbacks turn off/on render caching, but I think it is neat to be
443 // able to demonstrate the change in speed of caching vs. no caching
444 // by moving the mouse between windows while the text spins.
445 //
446 void
447 setTextCaching(int flag)
448 {
449 for (int i = 0; i < numParagraphs; i++) {
450 if (flag) {
451 paragraphs[i].cache->renderCaching = SoSeparator::ON;
452 }
453 else {
454 paragraphs[i].cache->renderCaching = SoSeparator::OFF;
455 }
456 }
457 }
458
459 //
460 // Create the main scene graph (the scene graph viewed by the
461 // Examiner).
462 //
463 SoSeparator *
464 createTextSceneGraph()
465 {
466 SoSeparator *veryTop = new SoSeparator;
467 veryTop->ref();
468
469 SoPerspectiveCamera *camera = new SoPerspectiveCamera;
470 veryTop->addChild(camera);
471 camera->heightAngle = 28.0 * M_PI/180.0;
472 camera->aspectRatio = 1.0;
473 camera->position.setValue(0.0, 0.0, 25.0);
474 camera->nearDistance = 8.0;
475 camera->farDistance = 50.0;
476 camera->focalDistance = 25.0;
477
478 root = new SoSeparator;
479 veryTop->addChild(root);
480
481 TheFont = new SoFont;
482 TheFont->name = labels.sofontlist;
483 TheFont->size = 1;
484 root->addChild(TheFont);
485
486 TheMaterial = new SoMaterial;
487 root->addChild(TheMaterial);
488 SoMaterial *defaultMaterial = new SoMaterial;
489 defaultMaterial->ref();
490 // Make all three materials (for front/back/sides) the same:
491 matEditCB(NULL, defaultMaterial);
492 defaultMaterial->unref();
493
494 SoMaterialBinding *mb = new SoMaterialBinding;
495 root->addChild(mb);
496 mb->value = SoMaterialBinding::PER_PART;
497
498 TheTranslation = new SoTranslation;
499 root->addChild(TheTranslation);
500
501 profileCoords = new SoProfileCoordinate2;
502 root->addChild(profileCoords);
503
504 profile = new SoLinearProfile;
505 root->addChild(profile);
506
507 return veryTop;
508 }
509