Debian Codegraph @

apps/demos/gview/FieldEditor.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 #include <cstdlib>
38 #include <ctype.h>
39 #include <X11/Intrinsic.h>
40
41 #include <Xm/Form.h>
42 #include <Xm/LabelG.h>
43 #include <Xm/PushB.h>
44 #include <Xm/Scale.h>
45 #include <Xm/SeparatoG.h>
46 #include <Xm/Text.h>
47 #include <Xm/ToggleBG.h>
48
49 #include <Inventor/errors/SoReadError.h>
50 #include <Inventor/fields/SoField.h>
51 #include <Inventor/fields/SoFieldData.h>
52 #include <Inventor/nodes/SoNode.h>
53 #include <Inventor/sensors/SoNodeSensor.h>
54
55 #include "Error.h"
56 #include "FieldEditor.h"
57 #include "MotifHelp.h"
58
59 char *FieldEditor::fieldBuf; // Buffer for writing field values
60 int FieldEditor::fieldBufSize = 0; // Size of buffer in bytes
61
62 ////////////////////////////////////////////////////////////////////////
63 //
64 // This class is used to hold info about each individual field widget.
65 // It contains the widget and other information accessible through
66 // callbacks in one neat package.
67 //
68 ////////////////////////////////////////////////////////////////////////
69
70 struct FieldInfo {
71
72 // These are set in initInfo():
73
74 FieldEditor *editor; // Pointer to FieldEditor this is part of
75 SoField *field; // Field being edited
76 int index; // Index of field in node
77 SbName fieldName; // Name of field
78 SbBool setToDefault; // TRUE if user set value to default
79 SbBool isMultiple; // TRUE if field is multiple value
80
81 // These are set in buildFieldWidget():
82
83 Widget widget; // Top-level field editing widget
84 Widget textWidget; // Text editing widget
85 Widget ignoreButton; // Ignore button widget
86 Widget defaultButton; // Set-default button widget
87
88 // These are used only for multiple-value fields:
89
90 Widget numberWidget; // Text widget showing current scale value
91 Widget scrollWidget; // Text widget scrollbar widget
92 };
93
94 ////////////////////////////////////////////////////////////////////////
95 //
96 // Description:
97 // Constructor. Takes pointer to node whose fields we are to edit.
98 //
99
100 FieldEditor::FieldEditor(SoNode *node, Widget parent, const char *name)
101 : SoXtComponent(parent, name)
102 //
103 ////////////////////////////////////////////////////////////////////////
104 {
105 int i;
106
107 nodeToEdit = node;
108 nodeToEdit->ref();
109
110 defNode = (SoNode *) node->getTypeId().createInstance();
111 defNode->ref();
112
113 finishCB = NULL;
114 finishData = NULL;
115
116 errorString = NULL;
117
118 // Set up sensor. Make it priority 0 so we can be called
119 // immediately when a field changes. This way we can update only
120 // the fields that change in the sensor callback.
121 nodeSensor = new SoNodeSensor(&FieldEditor::nodeSensorCB, (void *) this);
122 nodeSensor->setPriority(0);
123 nodeSensor->attach(node);
124
125 // Allocate buffer to write field values into. We have to use
126 // malloc, since we may use realloc on it later
127 if (fieldBufSize == 0)
128 fieldBuf = (char *) malloc((unsigned) (fieldBufSize = 1028));
129
130 // Allocate and initialize field info structures
131 numFields = getNumFields(nodeToEdit);
132 fieldInfos = new FieldInfo[numFields];
133 for (i = 0; i < numFields; i++)
134 initInfo(&fieldInfos[i], i);
135
136 // Set up the class name
137 setClassName("FieldEditor");
138
139 // Build and set the widget
140 setBaseWidget(buildWidget(getParentWidget()));
141 }
142
143 ////////////////////////////////////////////////////////////////////////
144 //
145 // Description:
146 // Destructor.
147 //
148
149 FieldEditor::~FieldEditor()
150 //
151 ////////////////////////////////////////////////////////////////////////
152 {
153 // Widget stuff is handled in SoXtComponent...
154
155 nodeToEdit->unref();
156 defNode->unref();
157 delete nodeSensor;
158
159 delete [] fieldInfos;
160 }
161
162 ////////////////////////////////////////////////////////////////////////
163 //
164 // Description:
165 // Returns number of fields in node being edited. This is static to
166 // allow the check to be made before an editor is constructed.
167 //
168
169 int
170 FieldEditor::getNumFields(SoNode *node)
171 //
172 ////////////////////////////////////////////////////////////////////////
173 {
174 const SoFieldData *fdata = node->getFieldData();
175
176 if (fdata == NULL)
177 return 0;
178
179 return fdata->getNumFields();
180 }
181
182 ////////////////////////////////////////////////////////////////////////
183 //
184 // Description:
185 // Sets a callback to call when editing is finished.
186 //
187
188 void
189 FieldEditor::setFinishCallback(FieldEditorCB *cb, const void *userData)
190 //
191 ////////////////////////////////////////////////////////////////////////
192 {
193 finishCB = cb;
194 finishData = (void *) userData;
195 }
196
197 ////////////////////////////////////////////////////////////////////////
198 //
199 // Description:
200 // Builds and returns top-level editor widget.
201 //
202
203 Widget
204 FieldEditor::buildWidget(Widget parent)
205 //
206 ////////////////////////////////////////////////////////////////////////
207 {
208 Widget topWidget, fieldForm, sep, buttonForm;
209 ARG_VARS(20);
210
211 // If no fields to edit, go away
212 if (numFields == 0)
213 return NULL;
214
215 // The title is the class of the node whose fields we are editing
216 setTitle(nodeToEdit->getTypeId().getName().getString());
217
218 // Create the top level form widget and register it with a class name
219 RESET_ARGS();
220 topWidget = XmCreateForm(parent, (char *) getWidgetName(), ARGS);
221 registerWidget(topWidget);
222
223 // Create buttons at bottom
224 buttonForm = buildButtonWidget(topWidget);
225 RESET_ARGS();
226 ADD_LEFT_FORM(4);
227 ADD_RIGHT_FORM(4);
228 ADD_BOTTOM_FORM(4);
229 XtSetValues(buttonForm, ARGS);
230
231 // Create separator between buttons and field editors
232 RESET_ARGS();
233 ADD_ARG(XmNorientation, XmHORIZONTAL);
234 ADD_LEFT_FORM(0);
235 ADD_RIGHT_FORM(0);
236 ADD_BOTTOM_WIDGET(buttonForm, 4);
237 sep = XmCreateSeparatorGadget(topWidget, "Separator", ARGS);
238
239 // Create widget containing all field widgets
240 fieldForm = buildFieldForm(topWidget);
241 RESET_ARGS();
242 ADD_TOP_FORM(4);
243 ADD_LEFT_FORM(4);
244 ADD_RIGHT_FORM(4);
245 ADD_BOTTOM_WIDGET(sep, 4);
246 XtSetValues(fieldForm, ARGS);
247
248 // Manage the children
249 XtManageChild(fieldForm);
250 XtManageChild(sep);
251 XtManageChild(buttonForm);
252
253 return topWidget;
254 }
255
256 ////////////////////////////////////////////////////////////////////////
257 //
258 // Description:
259 // Creates and returns a widget containing all field-editing widgets.
260 //
261
262 Widget
263 FieldEditor::buildFieldForm(Widget parent)
264 //
265 ////////////////////////////////////////////////////////////////////////
266 {
267 Widget form, sep;
268 FieldInfo *info;
269 int i;
270 ARG_VARS(20);
271
272 // Create form to hold all field widgets
273 RESET_ARGS();
274 form = XmCreateForm(parent, "Form", ARGS);
275
276 // Create widget for each field and add them to form
277 for (i = 0, info = fieldInfos; i < numFields; i++, info++) {
278
279 // Build separator if necessary
280 if (i > 0) {
281 RESET_ARGS();
282 ADD_ARG(XmNorientation, XmHORIZONTAL);
283 ADD_LEFT_FORM(0);
284 ADD_RIGHT_FORM(0);
285 ADD_TOP_WIDGET((info - 1)->widget, 1);
286 sep = XmCreateSeparatorGadget(form, "Separator", ARGS);
287 XtManageChild(sep);
288 }
289
290 // Build field editor widget; sets widget field in info
291 buildFieldWidget(form, info);
292
293 // Set up widget contents from node
294 updateInfo(info);
295
296 RESET_ARGS();
297 ADD_LEFT_FORM(0);
298 ADD_RIGHT_FORM(0);
299 if (i == 0) {
300 ADD_TOP_FORM(0);
301 }
302 else {
303 ADD_TOP_WIDGET(sep, 0);
304 }
305 if (i == numFields - 1) {
306 ADD_BOTTOM_FORM(0);
307 }
308 XtSetValues(info->widget, ARGS);
309 XtManageChild(info->widget);
310 }
311
312 return form;
313 }
314
315 ////////////////////////////////////////////////////////////////////////
316 //
317 // Description:
318 // Creates a widget for editing the i'th field
319 //
320
321 void
322 FieldEditor::buildFieldWidget(Widget parent, FieldInfo *info)
323 //
324 ////////////////////////////////////////////////////////////////////////
325 {
326 Widget form, label, sep1, valueWidget, sep2, butForm, ign, def;
327 ARG_VARS(20);
328
329 // Create a form widget
330 RESET_ARGS();
331 form = XmCreateForm(parent, "Field", ARGS);
332
333 // Create a label widget with the name of the field
334 RESET_ARGS();
335 ADD_ARG(XmNlabelString, STRING((char *) info->fieldName.getString()));
336 ADD_ARG(XmNwidth, 140);
337 ADD_LEFT_FORM(4);
338 ADD_TOP_FORM(4);
339 ADD_BOTTOM_FORM(4);
340 label = XmCreateLabelGadget(form, "Label", ARGS);
341
342 // Create a separator widget
343 RESET_ARGS();
344 ADD_ARG(XmNorientation, XmVERTICAL);
345 ADD_LEFT_WIDGET(label, 4);
346 ADD_TOP_FORM(0);
347 ADD_BOTTOM_FORM(0);
348 sep1 = XmCreateSeparatorGadget(form, "Separator", ARGS);
349
350 // Create a form widget to hold buttons
351 RESET_ARGS();
352 ADD_BOTTOM_FORM(4);
353 ADD_RIGHT_FORM(4);
354 butForm = XmCreateForm(form, "FieldButtons", ARGS);
355
356 // Create a push button to set the field to default value.
357 // (Disable it if the field is already the default value)
358 RESET_ARGS();
359 ADD_ARG(XmNlabelString, STRING("Set To Default"));
360 ADD_ARG(XmNheight, 28);
361 ADD_ARG(XmNhighlightThickness, 0);
362 ADD_RIGHT_FORM(0);
363 ADD_TOP_FORM(0);
364 ADD_BOTTOM_FORM(0);
365 def = XmCreatePushButton(butForm, "defaultButton", ARGS);
366 ADD_CB(def, XmNactivateCallback, &FieldEditor::defaultButtonCB, info);
367
368 // Create a toggle button to set the ignore flag
369 RESET_ARGS();
370 ADD_ARG(XmNlabelString, STRING("Ignore"));
371 ADD_ARG(XmNhighlightThickness, 0);
372 ADD_TOP_FORM(0);
373 ADD_BOTTOM_FORM(0);
374 ADD_LEFT_FORM(0);
375 ADD_RIGHT_WIDGET(def, 4);
376 ign = XmCreateToggleButtonGadget(butForm, "ignoreButton", ARGS);
377
378 // Create a separator widget
379 RESET_ARGS();
380 ADD_ARG(XmNorientation, XmVERTICAL);
381 ADD_RIGHT_WIDGET(butForm, 4);
382 ADD_TOP_FORM(0);
383 ADD_BOTTOM_FORM(0);
384 sep2 = XmCreateSeparatorGadget(form, "Separator", ARGS);
385
386 // Field value editor widget
387 if (info->isMultiple)
388 valueWidget = buildMultipleValueWidget(form, info);
389 else
390 valueWidget = buildSingleValueWidget(form, info);
391 RESET_ARGS();
392 ADD_LEFT_WIDGET(sep1, 4);
393 ADD_RIGHT_WIDGET(sep2, 4);
394 ADD_TOP_FORM(0);
395 ADD_BOTTOM_FORM(0);
396 XtSetValues(valueWidget, ARGS);
397
398 // Manage the children
399 XtManageChild(label);
400 XtManageChild(sep1);
401 XtManageChild(valueWidget);
402 XtManageChild(sep2);
403 XtManageChild(ign);
404 XtManageChild(def);
405 XtManageChild(butForm);
406
407 info->widget = form;
408 info->ignoreButton = ign;
409 info->defaultButton = def;
410 }
411
412 ////////////////////////////////////////////////////////////////////////
413 //
414 // Description:
415 // Creates and returns a widget that does the main work of editing
416 // a multiple-value field.
417 //
418
419 Widget
420 FieldEditor::buildMultipleValueWidget(Widget parent, FieldInfo *info)
421 //
422 ////////////////////////////////////////////////////////////////////////
423 {
424 Widget form, text, number, scroll;
425 char buf[20];
426 ARG_VARS(20);
427
428 //////////////////////////////////////////////////////////////////
429 //
430 // Set up top level form widget
431 //
432
433 RESET_ARGS();
434 form = XmCreateForm(parent, "Form", ARGS);
435
436 //////////////////////////////////////////////////////////////////
437 //
438 // Set up the main scrolled text widget
439 //
440
441 RESET_ARGS();
442 ADD_ARG(XmNeditMode, XmMULTI_LINE_EDIT);
443 ADD_ARG(XmNcolumns, 30);
444 ADD_ARG(XmNrows, 5);
445 ADD_RIGHT_FORM(4);
446 ADD_TOP_FORM(4);
447 ADD_BOTTOM_FORM(4);
448 text = XmCreateScrolledText(form, "text", ARGS);
449
450 // Set up a callback to check changes to text before allowing them
451 ADD_CB(text, XmNmodifyVerifyCallback, &FieldEditor::textChangedCB, info);
452
453 // Get the scroll bar resource from the scrolled window (the
454 // parent widget of the scrolled text)
455 XtVaGetValues(XtParent(text), XmNverticalScrollBar, &scroll, NULL);
456
457 // Set up callbacks on the scroll bar so that the line number
458 // widget scrolls with the other text. Note that we have to add
459 // this to all of the scroll bar callbacks since they are already
460 // set up for the scrolled window.
461 ADD_CB(scroll, XmNvalueChangedCallback, &FieldEditor::scrollCB, info);
462 ADD_CB(scroll, XmNincrementCallback, &FieldEditor::scrollCB, info);
463 ADD_CB(scroll, XmNdecrementCallback, &FieldEditor::scrollCB, info);
464 ADD_CB(scroll, XmNpageIncrementCallback, &FieldEditor::scrollCB, info);
465 ADD_CB(scroll, XmNpageDecrementCallback, &FieldEditor::scrollCB, info);
466 ADD_CB(scroll, XmNtoTopCallback, &FieldEditor::scrollCB, info);
467 ADD_CB(scroll, XmNtoBottomCallback, &FieldEditor::scrollCB, info);
468 ADD_CB(scroll, XmNdragCallback, &FieldEditor::scrollCB, info);
469
470 //////////////////////////////////////////////////////////////////
471 //
472 // Set up the line number text widget
473 //
474
475 RESET_ARGS();
476 ADD_ARG(XmNeditable, False);
477 ADD_ARG(XmNeditMode, XmMULTI_LINE_EDIT);
478 ADD_ARG(XmNvalue, buf);
479 ADD_ARG(XmNcolumns, 5);
480 ADD_ARG(XmNrows, 5);
481 ADD_ARG(XmNhighlightThickness, 0);
482 ADD_RIGHT_WIDGET(text, 0);
483 ADD_TOP_FORM(6);
484 ADD_LEFT_FORM(4);
485 ADD_BOTTOM_FORM(4);
486 number = XmCreateText(form, "numbers", ARGS);
487 XmTextSetString(number, "?"); // Bogus value
488
489 // Set up a callback on the line number widget so that users
490 // cannot scroll the text in there by moving the cursor
491 ADD_CB(number, XmNmotionVerifyCallback, &FieldEditor::lineCB, info);
492
493 XtManageChild(text);
494 XtManageChild(number);
495
496 info->textWidget = text;
497 info->numberWidget = number;
498 info->scrollWidget = scroll;
499
500 return form;
501 }
502
503 ////////////////////////////////////////////////////////////////////////
504 //
505 // Description:
506 // Creates and returns a widget that does the main work of editing
507 // a single-value field.
508 //
509
510 Widget
511 FieldEditor::buildSingleValueWidget(Widget parent, FieldInfo *info)
512 //
513 ////////////////////////////////////////////////////////////////////////
514 {
515 Widget form, text;
516 ARG_VARS(20);
517
518 RESET_ARGS();
519 form = XmCreateForm(parent, "Form", ARGS);
520
521 RESET_ARGS();
522 ADD_ARG(XmNcolumns, 30);
523 ADD_ARG(XmNeditMode, XmSINGLE_LINE_EDIT);
524 ADD_LEFT_FORM(4);
525 ADD_RIGHT_FORM(4);
526 ADD_TOP_FORM(4);
527 ADD_BOTTOM_FORM(4);
528 text = XmCreateText(form, "Text", ARGS);
529
530 ADD_CB(text, XmNmodifyVerifyCallback, &FieldEditor::textChangedCB, info);
531
532 XtManageChild(text);
533
534 info->textWidget = text;
535
536 return form;
537 }
538
539 ////////////////////////////////////////////////////////////////////////
540 //
541 // Description:
542 // Creates and returns a widget containing all buttons.
543 //
544
545 Widget
546 FieldEditor::buildButtonWidget(Widget parent)
547 //
548 ////////////////////////////////////////////////////////////////////////
549 {
550 Widget form, acceptButton, revertButton, cancelButton;
551 ARG_VARS(20);
552
553 // Create a form widget to hold it all
554 RESET_ARGS();
555 form = XmCreateForm(parent, "Field", ARGS);
556
557 // Create "Apply", "Accept", "Revert", and "Cancel" buttons
558 RESET_ARGS();
559 ADD_ARG(XmNlabelString, STRING("Accept"));
560 ADD_ARG(XmNhighlightThickness, 0);
561 ADD_LEFT_FORM(0);
562 ADD_TOP_FORM(0);
563 ADD_BOTTOM_FORM(0);
564 acceptButton = XmCreatePushButton(form, "acceptButton", ARGS);
565 ADD_CB(acceptButton, XmNactivateCallback,
566 &FieldEditor::acceptButtonCB, this);
567
568 RESET_ARGS();
569 ADD_ARG(XmNlabelString, STRING("Apply"));
570 ADD_ARG(XmNhighlightThickness, 0);
571 ADD_TOP_FORM(0);
572 ADD_BOTTOM_FORM(0);
573 ADD_LEFT_WIDGET(acceptButton, 4);
574 applyButton = XmCreatePushButton(form, "applyButton", ARGS);
575 ADD_CB(applyButton, XmNactivateCallback,
576 &FieldEditor::applyButtonCB, this);
577
578 RESET_ARGS();
579 ADD_ARG(XmNlabelString, STRING("Revert"));
580 ADD_ARG(XmNhighlightThickness, 0);
581 ADD_TOP_FORM(0);
582 ADD_BOTTOM_FORM(0);
583 ADD_LEFT_WIDGET(applyButton, 4);
584 revertButton = XmCreatePushButton(form, "revertButton", ARGS);
585 ADD_CB(revertButton, XmNactivateCallback,
586 &FieldEditor::revertButtonCB, this);
587
588 RESET_ARGS();
589 ADD_ARG(XmNlabelString, STRING("Cancel"));
590 ADD_ARG(XmNhighlightThickness, 0);
591 ADD_TOP_FORM(0);
592 ADD_BOTTOM_FORM(0);
593 ADD_LEFT_WIDGET(revertButton, 4);
594 cancelButton = XmCreatePushButton(form, "cancelButton", ARGS);
595 ADD_CB(cancelButton, XmNactivateCallback,
596 &FieldEditor::cancelButtonCB, this);
597
598 // Create an "Override" toggle button to allow user to change
599 // override flag for node
600 RESET_ARGS();
601 ADD_ARG(XmNset, nodeToEdit->isOverride());
602 ADD_ARG(XmNlabelString, STRING("Override"));
603 ADD_ARG(XmNhighlightThickness, 0);
604 ADD_TOP_FORM(0);
605 ADD_BOTTOM_FORM(0);
606 ADD_RIGHT_FORM(0);
607 overrideButton = XmCreateToggleButtonGadget(form, "overrideButton", ARGS);
608
609 XtManageChild(acceptButton);
610 XtManageChild(applyButton);
611 XtManageChild(revertButton);
612 XtManageChild(cancelButton);
613 XtManageChild(overrideButton);
614
615 // Hitting a carriage-return activates the "Apply" button
616 RESET_ARGS();
617 ADD_ARG(XmNdefaultButton, applyButton);
618 XtSetValues(parent, ARGS);
619
620 return form;
621 }
622
623 ////////////////////////////////////////////////////////////////////////
624 //
625 // Description:
626 // Applies changes made in editor to node it is editing.
627 //
628
629 void
630 FieldEditor::apply()
631 //
632 ////////////////////////////////////////////////////////////////////////
633 {
634 FieldInfo *info;
635 int i;
636 SbBool override;
637
638 // Turn off sensor for a minute...
639 nodeSensor->detach();
640
641 // Get all field values and flags and store them back into the node
642 for (i = 0, info = fieldInfos; i < numFields; i++, info++)
643 updateNode(info);
644
645 // Set the override flag if it has changed
646 override = XmToggleButtonGadgetGetState(overrideButton);
647 if (nodeToEdit->isOverride() != override)
648 nodeToEdit->setOverride(override);
649
650 // Make sure the text window reflects the exact state
651 revert();
652
653 // Reattach sensor
654 nodeSensor->attach(nodeToEdit);
655 }
656
657 ////////////////////////////////////////////////////////////////////////
658 //
659 // Description:
660 // Sets field editor based on the values in the node being edited.
661 // If the passed field index is not -1 (the default), only the
662 // value for the specified field is reverted.
663 //
664
665 void
666 FieldEditor::revert(int fieldIndex)
667 //
668 ////////////////////////////////////////////////////////////////////////
669 {
670 FieldInfo *info;
671 int i;
672
673 if (fieldIndex >= 0)
674 updateInfo(&fieldInfos[fieldIndex]);
675
676 // Update all field info from node
677 else
678 for (i = 0, info = fieldInfos; i < numFields; i++, info++)
679 updateInfo(info);
680
681 // Set the override button
682 XmToggleButtonGadgetSetState(overrideButton,
683 nodeToEdit->isOverride(), FALSE);
684 }
685
686 ////////////////////////////////////////////////////////////////////////
687 //
688 // Description:
689 // Initializes given FieldInfo structure based on indexed field.
690 //
691
692 void
693 FieldEditor::initInfo(FieldInfo *info, int i)
694 //
695 ////////////////////////////////////////////////////////////////////////
696 {
697 info->editor = this;
698 info->index = i;
699 info->setToDefault = FALSE;
700
701 // Set the field name and pointer and determine whether it is a
702 // single or multiple value field.
703 const SoFieldData *fdata = nodeToEdit->getFieldData();
704
705 info->field = fdata->getField(nodeToEdit, i);
706 info->fieldName = fdata->getFieldName(i);
707 info->isMultiple = info->field->isOfType(SoMField::getClassTypeId());
708 }
709
710 ////////////////////////////////////////////////////////////////////////
711 //
712 // Description:
713 // Sets up field editor from correct field in node being edited.
714 //
715
716 void
717 FieldEditor::updateInfo(FieldInfo *info)
718 //
719 ////////////////////////////////////////////////////////////////////////
720 {
721 SbString fieldString;
722 ARG_VARS(10);
723
724 // Set text field from current field value(s)
725 showFieldString(info);
726
727 // Set ignore button state
728 RESET_ARGS();
729 ADD_ARG(XmNset, info->field->isIgnored());
730 XtSetValues(info->ignoreButton, ARGS);
731
732 // Turn set-default button on or off
733 info->setToDefault = info->field->isDefault();
734 RESET_ARGS();
735 ADD_ARG(XmNsensitive, ! info->setToDefault);
736 XtSetValues(info->defaultButton, ARGS);
737 }
738
739 ////////////////////////////////////////////////////////////////////////
740 //
741 // Description:
742 // Makes field editor show default value for given field. Does NOT
743 // change the node at all!
744 //
745
746 void
747 FieldEditor::showDefaultValue(FieldInfo *info)
748 //
749 ////////////////////////////////////////////////////////////////////////
750 {
751 SoField *defField, *saveField;
752 ARG_VARS(10);
753
754 // Make sure we know that this was done
755 info->setToDefault = TRUE;
756
757 // Get the appropriate field from a node instance with all default values
758 defField = defNode->getFieldData()->getField(defNode, info->index);
759
760 // Temporarily change the field to the default one
761 saveField = info->field;
762 info->field = defField;
763
764 // Set the string in the text widget
765 showFieldString(info);
766
767 // Restore the real field value
768 info->field = saveField;
769
770 // Disable the default button
771 RESET_ARGS();
772 ADD_ARG(XmNsensitive, FALSE);
773 XtSetValues(info->defaultButton, ARGS);
774 }
775
776 ////////////////////////////////////////////////////////////////////////
777 //
778 // Description:
779 // Sets string(s) in text widget to display field value(s).
780 //
781
782 void
783 FieldEditor::showFieldString(FieldInfo *info) const
784 //
785 ////////////////////////////////////////////////////////////////////////
786 {
787 SbString valueString;
788
789 REM_CB(info->textWidget, XmNmodifyVerifyCallback,
790 &FieldEditor::textChangedCB, info);
791
792 // Get the field value(s) as a string
793 getFieldString(info, valueString);
794
795 // Set the text widget to show the string
796 XmTextSetString(info->textWidget, (char *) valueString.getString());
797
798 // Set up the line number widget for multiple-value fields
799 if (info->isMultiple)
800 updateLineNumbers(info, TRUE);
801
802 // Reconnect the callback
803 ADD_CB(info->textWidget, XmNmodifyVerifyCallback,
804 &FieldEditor::textChangedCB, info);
805 }
806
807 ////////////////////////////////////////////////////////////////////////
808 //
809 // Description:
810 // Updates field value and flags in node from current editor settings.
811 //
812
813 void
814 FieldEditor::updateNode(FieldInfo *info)
815 //
816 ////////////////////////////////////////////////////////////////////////
817 {
818 SbString string;
819 SbBool ignore;
820
821 getEditorString(info, string);
822
823 // Set the read-error callback so that read errors caused by the
824 // following call to SoField::set() can be trapped and displayed
825 // in an error dialog box
826 SoReadError::setHandlerCallback(&FieldEditor::readErrorCB, info);
827
828 if (! nodeToEdit->set(string.getString()))
829 displayReadError();
830
831 // If value was set to default, set flag in the field
832 if (info->setToDefault)
833 info->field->setDefault(TRUE);
834
835 // Make sure ignore flag is correct
836 ignore = XmToggleButtonGadgetGetState(info->ignoreButton);
837 if (info->field->isIgnored() != ignore)
838 info->field->setIgnored(ignore);
839 }
840
841 ////////////////////////////////////////////////////////////////////////
842 //
843 // Description:
844 // Fills in SbString with string representing field value that can
845 // be displayed in editor's text widget.
846 //
847
848 void
849 FieldEditor::getFieldString(FieldInfo *info, SbString &string) const
850 //
851 ////////////////////////////////////////////////////////////////////////
852 {
853 // Get the field value(s) as a string
854 info->field->get(string);
855
856 // If it's a multiple-value string, we have to some extra work
857 if (info->isMultiple) {
858
859 const char *s = string.getString();
860
861 // If first character is an open bracket, we have work to do
862 if (*s == '[') {
863
864 // Skip over the open brace
865 s++;
866
867 // Allocate working space for the string
868 char *buf = new char [strlen(s) + 1];
869
870 char *b = buf;
871 SbBool atNewline = TRUE;
872
873 while (*s != '\0') {
874
875 // Skip over quoted strings - this will have a problem
876 // with quotes nested inside quotes ???
877 if (*s == '\"') {
878 *b++ = *s++;
879 while (*s != '\"')
880 *b++ = *s++;
881 *b++ = *s++; // Closing quote
882 }
883
884 // Change commas into newlines
885 if (*s == ',') {
886 *b++ = '\n';
887 atNewline = TRUE;
888 s++;
889 }
890
891 // Skip over spaces (including extra newlines) after newlines
892 else if (atNewline && isspace(*s))
893 s++;
894
895 else {
896 *b++ = *s++;
897 atNewline = FALSE;
898 }
899 }
900
901 // Remove closing brace and space before it
902 while (*b != ']')
903 b--;
904 while (isspace(*(b - 1)))
905 b--;
906
907 // Terminate string
908 *b = '\0';
909
910 string = buf;
911
912 delete [] buf;
913 }
914 }
915 }
916
917 ////////////////////////////////////////////////////////////////////////
918 //
919 // Description:
920 // Fills in SbString with string containing current field value in
921 // editor's text widget, to store back into node being edited.
922 //
923
924 void
925 FieldEditor::getEditorString(FieldInfo *info, SbString &string) const
926 //
927 ////////////////////////////////////////////////////////////////////////
928 {
929 char *str = XmTextGetString(info->textWidget);
930
931 // Store name of string and a space
932 string = info->fieldName.getString();
933 string += " ";
934
935 // If a single-value field, just append text string from widget
936 if (! info->isMultiple)
937 string += str;
938
939 // If this is a multiple-value field, add extra characters
940 else {
941 char *c;
942
943 string += "[";
944
945 for (c = str; *c != '\0'; c++) {
946
947 // Change all newlines to commas in string first
948 if (*c == '\n') {
949 *c = ',';
950
951 // Remove all whitespace after commas. This also gets
952 // rid of blank lines.
953 c++;
954 while (isspace(*c))
955 c++;
956
957 c--;
958 }
959 }
960 string += str;
961
962 string += "]";
963 }
964
965 XtFree(str);
966 }
967
968 ////////////////////////////////////////////////////////////////////////
969 //
970 // Description:
971 // Redefines this method to clean up when the window is destroyed.
972 //
973
974 void
975 FieldEditor::windowCloseAction()
976 //
977 ////////////////////////////////////////////////////////////////////////
978 {
979 // Alert caller if necessary
980 if (finishCB != NULL)
981 finishCB(finishData, this);
982
983 delete this;
984 }
985
986 ////////////////////////////////////////////////////////////////////////
987 //
988 // Description:
989 // Updates the line-number widget whenever the text area changes or
990 // is scrolled. The textChanged flag indicates why the line number
991 // may be changing.
992 //
993
994 void
995 FieldEditor::updateLineNumbers(FieldInfo *info, SbBool textChanged) const
996 //
997 ////////////////////////////////////////////////////////////////////////
998 {
999 if (textChanged) {
1000
1001 // See if the number of lines in the text area has changed. If
1002 // so, insert or remove line numbers from the widget.
1003
1004 // Count newlines in the text string
1005 char *textString = XmTextGetString(info->textWidget);
1006 int numLines = 0;
1007 char *b;
1008 for (b = textString; *b != '\0'; b++)
1009 if (*b == '\n')
1010 numLines++;
1011 // Last line may not end in a newline?
1012 if (b > textString && *(b-1) != '\n')
1013 numLines++;
1014 XtFree(textString);
1015
1016 // Make sure line number widget has the correct info
1017 char *numberString = XmTextGetString(info->numberWidget);
1018 int numNumbers = strlen(numberString) / 6;
1019 XtFree(numberString);
1020
1021 // Too many line numbers? Remove some.
1022 if (numNumbers > numLines)
1023 XmTextReplace(info->numberWidget,
1024 numLines * 6, numNumbers * 6, NULL);
1025
1026 // Too few line numbers? Add some.
1027 else if (numNumbers < numLines) {
1028 int numNeeded = numLines - numNumbers, i;
1029 char *buf = new char [numNeeded * 6 + 1], *b = buf;
1030 for (i = 0; i < numNeeded; i++) {
1031 sprintf(b, "%5d\n", numNumbers + i);
1032 b += strlen(b);
1033 }
1034 if (numNumbers == 0)
1035 XmTextReplace(info->numberWidget, 0, 1, buf);
1036 else
1037 XmTextInsert(info->numberWidget, numLines * 6, buf);
1038 delete [] buf;
1039 }
1040 }
1041
1042 // Find out the scroll bar value and set the line number position
1043 // based on it
1044 ARG_VARS(1);
1045 int topLine;
1046
1047 RESET_ARGS();
1048 ADD_ARG(XmNvalue, &topLine);
1049 XtGetValues(info->scrollWidget, ARGS);
1050
1051 RESET_ARGS();
1052 ADD_ARG(XmNtopCharacter, topLine * 6);
1053 XtSetValues(info->numberWidget, ARGS);
1054 }
1055
1056 ////////////////////////////////////////////////////////////////////////
1057 //
1058 // Description:
1059 // Callback called when the node we are editing changes. The data
1060 // pointer points to a FieldEditor instance.
1061 //
1062
1063 void
1064 FieldEditor::nodeSensorCB(void *data, SoSensor *sensor)
1065 //
1066 ////////////////////////////////////////////////////////////////////////
1067 {
1068 // Try to update only the field that changed, if there is one
1069
1070 FieldEditor *editor = (FieldEditor *) data;
1071 SoNodeSensor *nodeSensor = (SoNodeSensor *) sensor;
1072 const SoField *trigField = nodeSensor->getTriggerField();
1073 int fieldIndex = -1;
1074
1075 if (trigField != NULL) {
1076 int i;
1077
1078 for (i = 0; i < editor->numFields; i++) {
1079 if (editor->fieldInfos[i].field == trigField) {
1080 fieldIndex = i;
1081 break;
1082 }
1083 }
1084 }
1085
1086 editor->revert(fieldIndex);
1087 }
1088
1089 ////////////////////////////////////////////////////////////////////////
1090 //
1091 // Description:
1092 // Callback called when the "Accept" button in the editor is
1093 // activated. The clientData pointer points to a FieldEditor instance.
1094 //
1095
1096 void
1097 FieldEditor::acceptButtonCB(Widget, XtPointer clientData, XtPointer)
1098 //
1099 ////////////////////////////////////////////////////////////////////////
1100 {
1101 FieldEditor *editor = (FieldEditor *) clientData;
1102
1103 // Make sure latest changes were applied
1104 editor->apply();
1105
1106 // Get rid of window
1107 editor->windowCloseAction();
1108 }
1109
1110 ////////////////////////////////////////////////////////////////////////
1111 //
1112 // Description:
1113 // Callback called when the "Apply" button in the editor is
1114 // activated. The clientData pointer points to a FieldEditor instance.
1115 //
1116
1117 void
1118 FieldEditor::applyButtonCB(Widget, XtPointer clientData, XtPointer)
1119 //
1120 ////////////////////////////////////////////////////////////////////////
1121 {
1122 ((FieldEditor *) clientData)->apply();
1123 }
1124
1125 ////////////////////////////////////////////////////////////////////////
1126 //
1127 // Description:
1128 // Callback called when the "Revert" button in the editor is
1129 // activated. The clientData pointer points to a FieldEditor instance.
1130 //
1131
1132 void
1133 FieldEditor::revertButtonCB(Widget, XtPointer clientData, XtPointer)
1134 //
1135 ////////////////////////////////////////////////////////////////////////
1136 {
1137 ((FieldEditor *) clientData)->revert();
1138 }
1139
1140 ////////////////////////////////////////////////////////////////////////
1141 //
1142 // Description:
1143 // Callback called when the "Cancel" button in the editor is
1144 // activated. The clientData pointer points to a FieldEditor instance.
1145 //
1146
1147 void
1148 FieldEditor::cancelButtonCB(Widget, XtPointer clientData, XtPointer)
1149 //
1150 ////////////////////////////////////////////////////////////////////////
1151 {
1152 FieldEditor *editor = (FieldEditor *) clientData;
1153
1154 // Get rid of window
1155 editor->windowCloseAction();
1156 }
1157
1158 ////////////////////////////////////////////////////////////////////////
1159 //
1160 // Description:
1161 // Callback called when the "Set Default" button in a field editor
1162 // is activated. The clientData pointer points to a FieldInfo
1163 // instance for the field to change.
1164 //
1165
1166 void
1167 FieldEditor::defaultButtonCB(Widget, XtPointer clientData, XtPointer)
1168 //
1169 ////////////////////////////////////////////////////////////////////////
1170 {
1171 FieldInfo *info = (FieldInfo *) clientData;
1172
1173 // Make sure the correct value is displayed in the text widget
1174 info->editor->showDefaultValue(info);
1175 }
1176
1177 ////////////////////////////////////////////////////////////////////////
1178 //
1179 // Description:
1180 // Callback called when the text in a field editor text widget
1181 // is changed. The clientData pointer points to a FieldInfo
1182 // instance.
1183 //
1184
1185 void
1186 FieldEditor::textChangedCB(Widget, XtPointer clientData, XtPointer callData)
1187 //
1188 ////////////////////////////////////////////////////////////////////////
1189 {
1190 FieldInfo *info = (FieldInfo *) clientData;
1191 XmTextVerifyPtr tvp = (XmTextVerifyPtr) callData;
1192
1193 // If we are inserting a text character, see if it is a carriage return
1194 if (tvp->text->length > 0 && *tvp->text->ptr == '\n') {
1195
1196 // We don't want the text to be inserted if it's a
1197 // single-value field - just act as if we hit the "Apply"
1198 // button
1199 if (! info->isMultiple) {
1200 tvp->doit = FALSE;
1201 XtCallActionProc(info->editor->applyButton, "ArmAndActivate",
1202 tvp->event, NULL, 0);
1203 }
1204 }
1205
1206 // Multiple-value fields need to scroll correctly when the text
1207 // changes
1208 if (info->isMultiple)
1209 info->editor->updateLineNumbers(info, TRUE);
1210
1211 // Reset the setToDefault flag to FALSE if necessary
1212 if (info->setToDefault && tvp->doit) {
1213 XtSetSensitive(info->defaultButton, TRUE);
1214 info->setToDefault = FALSE;
1215 }
1216 }
1217
1218 ////////////////////////////////////////////////////////////////////////
1219 //
1220 // Description:
1221 // Callback called when the value of the scrollbar in a
1222 // multiple-value field text scrolling area changes. The clientData
1223 // pointer points to a FieldInfo instance.
1224 //
1225
1226 void
1227 FieldEditor::scrollCB(Widget, XtPointer clientData, XtPointer)
1228 //
1229 ////////////////////////////////////////////////////////////////////////
1230 {
1231 FieldInfo *info = (FieldInfo *) clientData;
1232
1233 // Determine the new top line number from the scroll bar
1234 info->editor->updateLineNumbers(info, FALSE);
1235 }
1236
1237 ////////////////////////////////////////////////////////////////////////
1238 //
1239 // Description:
1240 // Callback used to keep the user from moving the insertion cursor
1241 // within the line-number widget. The clientData pointer points to
1242 // a FieldInfo instance.
1243 //
1244
1245 void
1246 FieldEditor::lineCB(Widget, XtPointer, XtPointer callData)
1247 //
1248 ////////////////////////////////////////////////////////////////////////
1249 {
1250 XmTextVerifyCallbackStruct *cbInfo =
1251 (XmTextVerifyCallbackStruct *) callData;
1252
1253 // Indicate that no cursor motion should occur
1254 cbInfo->doit = False;
1255 }
1256
1257 ////////////////////////////////////////////////////////////////////////
1258 //
1259 // Description:
1260 // Reallocates fieldBuf and sets fieldBufSize.
1261 //
1262
1263 void *
1264 FieldEditor::reallocBuf(void *ptr, size_t newSize)
1265 //
1266 ////////////////////////////////////////////////////////////////////////
1267 {
1268 fieldBufSize = newSize;
1269
1270 fieldBuf = (char *) realloc(ptr, newSize);
1271
1272 return fieldBuf;
1273 }
1274
1275 ////////////////////////////////////////////////////////////////////////
1276 //
1277 // Description:
1278 // Handler for read errors that occur when setting values in the
1279 // field. Saves the error message to be displayed later.
1280 //
1281
1282 void
1283 FieldEditor::readErrorCB(const SoError *error, void *data)
1284 //
1285 ////////////////////////////////////////////////////////////////////////
1286 {
1287 FieldInfo *info = (FieldInfo *) data;
1288
1289 // Save only the first error that is found
1290 if (info->editor->errorString == NULL) {
1291
1292 SbString *err = new SbString;
1293
1294 (*err) = "There was an error reading the values for the field named ";
1295 (*err) += info->fieldName.getString();
1296 (*err) += ":\n\n";
1297 (*err) += error->getDebugString();
1298
1299 info->editor->errorString = err;
1300 }
1301 }
1302
1303 ////////////////////////////////////////////////////////////////////////
1304 //
1305 // Description:
1306 // Displays error message detected during reading.
1307 //
1308
1309 void
1310 FieldEditor::displayReadError()
1311 //
1312 ////////////////////////////////////////////////////////////////////////
1313 {
1314 if (errorString != NULL) {
1315 Error *err = new Error(XtParent(getWidget()),
1316 errorString->getString());
1317 delete errorString;
1318
1319 errorString = NULL;
1320 }
1321 }