]> git.mxchange.org Git - flightgear.git/blob - src/GUI/dialog.cxx
what about using the right variable?
[flightgear.git] / src / GUI / dialog.cxx
1 // dialog.cxx: implementation of an XML-configurable dialog box.
2
3 #ifdef HAVE_CONFIG_H
4 #  include "config.h"
5 #endif
6
7 #include <stdlib.h>             // atof()
8
9 #include <Input/input.hxx>
10 #include <Scripting/NasalSys.hxx>
11
12 #include "dialog.hxx"
13 #include "new_gui.hxx"
14
15 #include "puList.hxx"
16 #include "AirportList.hxx"
17 #include "layout.hxx"
18
19 ////////////////////////////////////////////////////////////////////////
20 // Implementation of GUIInfo.
21 ////////////////////////////////////////////////////////////////////////
22
23 /**
24  * User data for a GUI object.
25  */
26 struct GUIInfo
27 {
28     GUIInfo (FGDialog * d);
29     virtual ~GUIInfo ();
30
31     FGDialog * dialog;
32     vector <FGBinding *> bindings;
33     int key;
34 };
35
36 GUIInfo::GUIInfo (FGDialog * d)
37     : dialog(d),
38       key(-1)
39 {
40 }
41
42 GUIInfo::~GUIInfo ()
43 {
44     for (unsigned int i = 0; i < bindings.size(); i++) {
45         delete bindings[i];
46         bindings[i] = 0;
47     }
48 }
49
50 \f
51 /**
52  * Key handler.
53  */
54 int fgPopup::checkKey(int key, int updown)
55 {
56     if (updown == PU_UP || !isVisible() || !isActive() || window != puGetWindow())
57         return false;
58
59     puObject *input = getActiveInputField(this);
60     if (input)
61         return input->checkKey(key, updown);
62
63     puObject *object = getKeyObject(this, key);
64     if (!object)
65         return puPopup::checkKey(key, updown);
66
67     // invokeCallback() isn't enough; we need to simulate a mouse button press
68     object->checkHit(PU_LEFT_BUTTON, PU_DOWN,
69             (object->getABox()->min[0] + object->getABox()->max[0]) / 2,
70             (object->getABox()->min[1] + object->getABox()->max[1]) / 2);
71     object->checkHit(PU_LEFT_BUTTON, PU_UP,
72             (object->getABox()->min[0] + object->getABox()->max[0]) / 2,
73             (object->getABox()->min[1] + object->getABox()->max[1]) / 2);
74     return true;
75 }
76
77 puObject *fgPopup::getKeyObject(puObject *object, int key)
78 {
79     puObject *ret;
80     if(object->getType() & PUCLASS_GROUP)
81         for (puObject *obj = ((puGroup *)object)->getFirstChild();
82                 obj; obj = obj->getNextObject())
83             if ((ret = getKeyObject(obj, key)))
84                 return ret;
85
86     GUIInfo *info = (GUIInfo *)object->getUserData();
87     if (info && info->key == key)
88         return object;
89
90     return 0;
91 }
92
93 puObject *fgPopup::getActiveInputField(puObject *object)
94 {
95     puObject *ret;
96     if(object->getType() & PUCLASS_GROUP)
97         for (puObject *obj = ((puGroup *)object)->getFirstChild();
98                 obj; obj = obj->getNextObject())
99             if ((ret = getActiveInputField(obj)))
100                 return ret;
101
102     if (object->getType() & PUCLASS_INPUT && ((puInput *)object)->isAcceptingInput())
103         return object;
104
105     return 0;
106 }
107
108 /**
109  * Mouse handler.
110  */
111 int fgPopup::checkHit(int button, int updown, int x, int y)
112 {
113     int result = puPopup::checkHit(button, updown, x, y);
114
115     if ( !_draggable)
116        return result;
117
118     // This is annoying.  We would really want a true result from the
119     // superclass to indicate "handled by child object", but all it
120     // tells us is that the pointer is inside the dialog.  So do the
121     // intersection test (again) to make sure we don't start a drag
122     // when inside controls.
123
124     if(updown == PU_DOWN && !_dragging) {
125         if(!result)
126             return 0;
127
128         int hit = getHitObjects(this, x, y);
129         if(hit & (PUCLASS_BUTTON|PUCLASS_ONESHOT|PUCLASS_INPUT))
130             return result;
131
132         int px, py;
133         getPosition(&px, &py);
134         _dragging = true;
135         _dX = px - x;
136         _dY = py - y;
137     } else if(updown == PU_DRAG && _dragging) {
138         setPosition(x + _dX, y + _dY);
139     } else {
140         _dragging = false;
141     }
142     return result;
143 }
144
145 int fgPopup::getHitObjects(puObject *object, int x, int y)
146 {
147     int type = 0;
148     if(object->getType() & PUCLASS_GROUP)
149         for (puObject *obj = ((puGroup *)object)->getFirstChild();
150                 obj; obj = obj->getNextObject())
151             type |= getHitObjects(obj, x, y);
152
153     int cx, cy, cw, ch;
154     object->getAbsolutePosition(&cx, &cy);
155     object->getSize(&cw, &ch);
156     if(x >= cx && x < cx + cw && y >= cy && y < cy + ch)
157         type |= object->getType();
158     return type;
159 }
160
161
162 \f
163 ////////////////////////////////////////////////////////////////////////
164 // Callbacks.
165 ////////////////////////////////////////////////////////////////////////
166
167 /**
168  * Action callback.
169  */
170 static void
171 action_callback (puObject * object)
172 {
173     GUIInfo * info = (GUIInfo *)object->getUserData();
174     NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
175     gui->setActiveDialog(info->dialog);
176     int nBindings = info->bindings.size();
177     for (int i = 0; i < nBindings; i++) {
178         info->bindings[i]->fire();
179         if (gui->getActiveDialog() == 0)
180             break;
181     }
182     gui->setActiveDialog(0);
183 }
184
185
186 static void
187 format_callback(puObject *obj, int dx, int dy, void *n)
188 {
189     SGPropertyNode *node = (SGPropertyNode *)n;
190     const char *format = node->getStringValue("format"), *f = format;
191     bool number, l = false;
192     // make sure the format matches '[ -+#]?\d*(\.\d*)?l?[fs]'
193     for (; *f; f++) {
194         if (*f == '%') {
195             if (f[1] == '%')
196                 f++;
197             else
198                 break;
199         }
200     }
201     if (*f++ != '%')
202         return;
203     if (*f == ' ' || *f == '+' || *f == '-' || *f == '#')
204         f++;
205     while (*f && isdigit(*f))
206         f++;
207     if (*f == '.') {
208         f++;
209         while (*f && isdigit(*f))
210             f++;
211     }
212     if (*f == 'l')
213         l = true, f++;
214
215     if (*f == 'f')
216         number = true;
217     else if (*f == 's') {
218         if (l)
219             return;
220         number = false;
221     } else
222         return;
223
224     for (++f; *f; f++) {
225         if (*f == '%') {
226             if (f[1] == '%')
227                 f++;
228             else
229                 return;
230         }
231     }
232
233     char buf[256];
234     const char *src = obj->getLabel();
235
236     if (number) {
237         float value = atof(src);
238         snprintf(buf, 256, format, value);
239     } else {
240         snprintf(buf, 256, format, src);
241     }
242
243     buf[255] = '\0';
244
245     SGPropertyNode *result = node->getNode("formatted", true);
246     result->setStringValue(buf);
247     obj->setLabel(result->getStringValue());
248 }
249
250
251 \f
252 ////////////////////////////////////////////////////////////////////////
253 // Static helper functions.
254 ////////////////////////////////////////////////////////////////////////
255
256 /**
257  * Copy a property value to a PUI object.
258  */
259 static void
260 copy_to_pui (SGPropertyNode * node, puObject * object)
261 {
262     // Treat puText objects specially, so their "values" can be set
263     // from properties.
264     if(object->getType() & PUCLASS_TEXT) {
265         object->setLabel(node->getStringValue());
266         return;
267     }
268
269     switch (node->getType()) {
270     case SGPropertyNode::BOOL:
271     case SGPropertyNode::INT:
272     case SGPropertyNode::LONG:
273         object->setValue(node->getIntValue());
274         break;
275     case SGPropertyNode::FLOAT:
276     case SGPropertyNode::DOUBLE:
277         object->setValue(node->getFloatValue());
278         break;
279     default:
280         object->setValue(node->getStringValue());
281         break;
282     }
283 }
284
285
286 static void
287 copy_from_pui (puObject * object, SGPropertyNode * node)
288 {
289     // puText objects are immutable, so should not be copied out
290     if(object->getType() & PUCLASS_TEXT)
291         return;
292
293     switch (node->getType()) {
294     case SGPropertyNode::BOOL:
295     case SGPropertyNode::INT:
296     case SGPropertyNode::LONG:
297         node->setIntValue(object->getIntegerValue());
298         break;
299     case SGPropertyNode::FLOAT:
300     case SGPropertyNode::DOUBLE:
301         node->setFloatValue(object->getFloatValue());
302         break;
303     default:
304         // Special case to handle lists, as getStringValue cannot be overridden
305         if(object->getType() & PUCLASS_LIST)
306         {
307             const char *s = ((puList *) object)->getListStringValue();
308             if (s)
309                 node->setStringValue(s);
310         }
311         else
312         {
313             node->setStringValue(object->getStringValue());
314         }
315         break;
316     }
317 }
318
319
320 \f
321 ////////////////////////////////////////////////////////////////////////
322 // Implementation of FGDialog.
323 ////////////////////////////////////////////////////////////////////////
324
325 FGDialog::FGDialog (SGPropertyNode * props)
326     : _object(0),
327       _gui((NewGUI *)globals->get_subsystem("gui")),
328       _props(props)
329 {
330     _module = string("__dlg:") + props->getStringValue("name", "[unnamed]");
331     SGPropertyNode *nasal = props->getNode("nasal");
332     if (nasal) {
333         _nasal_close = nasal->getNode("close");
334         SGPropertyNode *open = nasal->getNode("open");
335         if (open) {
336             const char *s = open->getStringValue();
337             FGNasalSys *nas = (FGNasalSys *)globals->get_subsystem("nasal");
338             nas->createModule(_module.c_str(), _module.c_str(), s, strlen(s), props);
339         }
340     }
341     display(props);
342 }
343
344 FGDialog::~FGDialog ()
345 {
346     int x, y;
347     _object->getAbsolutePosition(&x, &y);
348     _props->setIntValue("lastx", x);
349     _props->setIntValue("lasty", y);
350
351     FGNasalSys *nas = (FGNasalSys *)globals->get_subsystem("nasal");
352     if (_nasal_close) {
353         const char *s = _nasal_close->getStringValue();
354         nas->createModule(_module.c_str(), _module.c_str(), s, strlen(s), _props);
355     }
356     nas->deleteModule(_module.c_str());
357
358     puDeleteObject(_object);
359
360     unsigned int i;
361                                 // Delete all the arrays we made
362                                 // and were forced to keep around
363                                 // because PUI won't do its own
364                                 // memory management.
365     for (i = 0; i < _char_arrays.size(); i++)
366         destroy_char_array(_char_arrays[i]);
367
368                                 // Delete all the info objects we
369                                 // were forced to keep around because
370                                 // PUI cannot delete its own user data.
371     for (i = 0; i < _info.size(); i++) {
372         delete (GUIInfo *)_info[i];
373         _info[i] = 0;
374     }
375                                 // Finally, delete the property links.
376     for (i = 0; i < _propertyObjects.size(); i++) {
377         delete _propertyObjects[i];
378         _propertyObjects[i] = 0;
379     }
380 }
381
382 void
383 FGDialog::updateValues (const char * objectName)
384 {
385     for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
386         const string &name = _propertyObjects[i]->name;
387         if (objectName && name != objectName)
388             continue;
389
390         puObject *obj = _propertyObjects[i]->object;
391         copy_to_pui(_propertyObjects[i]->node, obj);
392     }
393 }
394
395 void
396 FGDialog::applyValues (const char * objectName)
397 {
398     for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
399         const string &name = _propertyObjects[i]->name;
400         if (objectName && name != objectName)
401             continue;
402
403         copy_from_pui(_propertyObjects[i]->object,
404                       _propertyObjects[i]->node);
405     }
406 }
407
408 void
409 FGDialog::update ()
410 {
411     for (unsigned int i = 0; i < _liveObjects.size(); i++) {
412         puObject *obj = _liveObjects[i]->object;
413         if (obj->getType() & PUCLASS_INPUT && ((puInput *)obj)->isAcceptingInput())
414             continue;
415
416         copy_to_pui(_liveObjects[i]->node, obj);
417     }
418 }
419
420 void
421 FGDialog::display (SGPropertyNode * props)
422 {
423     if (_object != 0) {
424         SG_LOG(SG_GENERAL, SG_ALERT, "This widget is already active");
425         return;
426     }
427
428     int screenw = globals->get_props()->getIntValue("/sim/startup/xsize");
429     int screenh = globals->get_props()->getIntValue("/sim/startup/ysize");
430
431     bool userx = props->hasValue("x");
432     bool usery = props->hasValue("y");
433     bool userw = props->hasValue("width");
434     bool userh = props->hasValue("height");
435
436     // Let the layout widget work in the same property subtree.
437     LayoutWidget wid(props);
438
439     SGPropertyNode *fontnode = props->getNode("font");
440     if (fontnode) {
441         FGFontCache *fc = _gui->get_fontcache();
442         _font = fc->get(fontnode);
443     } else {
444         _font = _gui->getDefaultFont();
445     }
446     wid.setDefaultFont(_font, int(_font->getPointSize()));
447
448     int pw=0, ph=0;
449     int px, py, savex, savey;
450     if(!userw || !userh)
451         wid.calcPrefSize(&pw, &ph);
452     pw = props->getIntValue("width", pw);
453     ph = props->getIntValue("height", ph);
454     px = savex = props->getIntValue("x", (screenw - pw) / 2);
455     py = savey = props->getIntValue("y", (screenh - ph) / 2);
456
457     // Negative x/y coordinates are interpreted as distance from the top/right
458     // corner rather than bottom/left.
459     if (userx && px < 0)
460         px = screenw - pw + px;
461     if (usery && py < 0)
462         py = screenh - ph + py;
463
464     // Define "x", "y", "width" and/or "height" in the property tree if they
465     // are not specified in the configuration file.
466     wid.layout(px, py, pw, ph);
467
468     // Use the dimension and location properties as specified in the
469     // configuration file or from the layout widget.
470     _object = makeObject(props, screenw, screenh);
471
472     // Remove automatically generated properties, so the layout looks
473     // the same next time around, or restore x and y to preserve negative coords.
474     if(userx)
475         props->setIntValue("x", savex);
476     else
477         props->removeChild("x");
478
479     if(usery)
480         props->setIntValue("y", savey);
481     else
482         props->removeChild("y");
483
484     if(!userw) props->removeChild("width");
485     if(!userh) props->removeChild("height");
486
487     if (_object != 0) {
488         _object->reveal();
489     } else {
490         SG_LOG(SG_GENERAL, SG_ALERT, "Widget "
491                << props->getStringValue("name", "[unnamed]")
492                << " does not contain a proper GUI definition");
493     }
494 }
495
496 puObject *
497 FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
498 {
499     if (props->getBoolValue("hide"))
500         return 0;
501
502     bool presetSize = props->hasValue("width") && props->hasValue("height");
503     int width = props->getIntValue("width", parentWidth);
504     int height = props->getIntValue("height", parentHeight);
505     int x = props->getIntValue("x", (parentWidth - width) / 2);
506     int y = props->getIntValue("y", (parentHeight - height) / 2);
507     string type = props->getName();
508
509     if (type == "")
510         type = "dialog";
511
512     if (type == "dialog") {
513         puPopup * obj;
514         bool draggable = props->getBoolValue("draggable", true);
515         if (props->getBoolValue("modal", false))
516             obj = new puDialogBox(x, y);
517         else
518             obj = new fgPopup(x, y, draggable);
519         setupGroup(obj, props, width, height, true);
520         setColor(obj, props);
521         return obj;
522
523     } else if (type == "group") {
524         puGroup * obj = new puGroup(x, y);
525         setupGroup(obj, props, width, height, false);
526         setColor(obj, props);
527         return obj;
528
529     } else if (type == "frame") {
530         puGroup * obj = new puGroup(x, y);
531         setupGroup(obj, props, width, height, true);
532         setColor(obj, props);
533         return obj;
534
535     } else if (type == "hrule" || type == "vrule") {
536         puFrame * obj = new puFrame(x, y, x + width, y + height);
537         obj->setBorderThickness(0);
538         setColor(obj, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
539         return obj;
540
541     } else if (type == "list") {
542         char ** entries = value_list(props);
543         int slider_width = props->getIntValue("slider", 20);
544         puList * obj = new puList(x, y, x + width, y + height, entries, slider_width);
545         if (presetSize)
546             obj->setSize(width, height);
547         setupObject(obj, props);
548         setColor(obj, props);
549         return obj;
550
551     } else if (type == "airport-list") {
552         AirportList * obj = new AirportList(x, y, x + width, y + height);
553         if (presetSize)
554             obj->setSize(width, height);
555         setupObject(obj, props);
556         setColor(obj, props);
557         return obj;
558
559     } else if (type == "input") {
560         puInput * obj = new puInput(x, y, x + width, y + height);
561         setupObject(obj, props);
562         setColor(obj, props, FOREGROUND|LABEL);
563         return obj;
564
565     } else if (type == "text") {
566         puText * obj = new puText(x, y);
567         setupObject(obj, props);
568
569         if (props->getNode("format")) {
570             SGPropertyNode *live = props->getNode("live");
571             if (live && live->getBoolValue())
572                 obj->setRenderCallback(format_callback, props);
573             else
574                 format_callback(obj, x, y, props);
575         }
576         // Layed-out objects need their size set, and non-layout ones
577         // get a different placement.
578         if (presetSize)
579             obj->setSize(width, height);
580         else
581             obj->setLabelPlace(PUPLACE_LABEL_DEFAULT);
582         setColor(obj, props, LABEL);
583         return obj;
584
585     } else if (type == "checkbox") {
586         puButton * obj;
587         obj = new puButton(x, y, x + width, y + height, PUBUTTON_XCHECK);
588         setupObject(obj, props);
589         setColor(obj, props, FOREGROUND|LABEL);
590         return obj;
591
592     } else if (type == "radio") {
593         puButton * obj;
594         obj = new puButton(x, y, x + width, y + height, PUBUTTON_CIRCLE);
595         setupObject(obj, props);
596         setColor(obj, props, FOREGROUND|LABEL);
597         return obj;
598
599     } else if (type == "button") {
600         puButton * obj;
601         const char * legend = props->getStringValue("legend", "[none]");
602         if (props->getBoolValue("one-shot", true))
603             obj = new puOneShot(x, y, legend);
604         else
605             obj = new puButton(x, y, legend);
606         if (presetSize)
607             obj->setSize(width, height);
608         setupObject(obj, props);
609         setColor(obj, props);
610         return obj;
611
612     } else if (type == "combo") {
613         char ** entries = value_list(props);
614         puComboBox * obj = new puComboBox(x, y, x + width, y + height, entries,
615                            props->getBoolValue("editable", false));
616         setupObject(obj, props);
617 #ifdef PUCOL_EDITFIELD  // plib > 0.8.4
618         setColor(obj, props, EDITFIELD);
619 #else
620         setColor(obj, props, FOREGROUND|LABEL);
621 #endif
622         return obj;
623
624     } else if (type == "slider") {
625         bool vertical = props->getBoolValue("vertical", false);
626         puSlider * obj = new puSlider(x, y, (vertical ? height : width));
627         obj->setMinValue(props->getFloatValue("min", 0.0));
628         obj->setMaxValue(props->getFloatValue("max", 1.0));
629         setupObject(obj, props);
630         if (presetSize)
631             obj->setSize(width, height);
632         setColor(obj, props, FOREGROUND|LABEL);
633         return obj;
634
635     } else if (type == "dial") {
636         puDial * obj = new puDial(x, y, width);
637         obj->setMinValue(props->getFloatValue("min", 0.0));
638         obj->setMaxValue(props->getFloatValue("max", 1.0));
639         obj->setWrap(props->getBoolValue("wrap", true));
640         setupObject(obj, props);
641         setColor(obj, props, FOREGROUND|LABEL);
642         return obj;
643
644     } else if (type == "textbox") {
645         int slider_width = props->getIntValue("slider", 20);
646         int wrap = props->getBoolValue("wrap", true);
647         puLargeInput * obj = new puLargeInput(x, y,
648                 x+width, x+height, 2, slider_width, wrap);
649
650         if (props->hasValue("editable")) {
651             if (props->getBoolValue("editable")==false)
652                 obj->disableInput();
653             else
654                 obj->enableInput();
655         }
656         if (presetSize)
657             obj->setSize(width, height);
658         setupObject(obj, props);
659         setColor(obj, props, FOREGROUND|LABEL);
660         return obj;
661
662     } else if (type == "select") {
663         vector<SGPropertyNode_ptr> value_nodes;
664         SGPropertyNode * selection_node =
665                 fgGetNode(props->getChild("selection")->getStringValue(), true);
666
667         for (int q = 0; q < selection_node->nChildren(); q++)
668             value_nodes.push_back(selection_node->getChild(q));
669
670         char ** entries = make_char_array(value_nodes.size());
671         for (unsigned int i = 0; i < value_nodes.size(); i++)
672             entries[i] = strdup((char *)value_nodes[i]->getName());
673
674         puSelectBox * obj =
675             new puSelectBox(x, y, x + width, y + height, entries);
676         setupObject(obj, props);
677 #ifdef PUCOL_EDITFIELD  // plib > 0.8.4
678         setColor(obj, props, EDITFIELD);
679 #else
680         setColor(obj, props, FOREGROUND|LABEL);
681 #endif
682         return obj;
683     } else {
684         return 0;
685     }
686 }
687
688 void
689 FGDialog::setupObject (puObject * object, SGPropertyNode * props)
690 {
691     string type = props->getName();
692     object->setLabelPlace(PUPLACE_CENTERED_RIGHT);
693
694     if (props->hasValue("legend"))
695         object->setLegend(props->getStringValue("legend"));
696
697     if (props->hasValue("label"))
698         object->setLabel(props->getStringValue("label"));
699
700     if (props->hasValue("border"))
701         object->setBorderThickness( props->getIntValue("border", 2) );
702
703     if ( SGPropertyNode *nft = props->getNode("font", false) ) {
704        FGFontCache *fc = _gui->get_fontcache();
705        puFont *lfnt = fc->get(nft);
706        object->setLabelFont(*lfnt);
707     } else {
708        object->setLabelFont(*_font);
709     }
710
711     if (props->hasValue("property")) {
712         const char * name = props->getStringValue("name");
713         if (name == 0)
714             name = "";
715         const char * propname = props->getStringValue("property");
716         SGPropertyNode_ptr node = fgGetNode(propname, true);
717         copy_to_pui(node, object);
718         PropertyObject* po = new PropertyObject(name, object, node);
719         _propertyObjects.push_back(po);
720         if(props->getBoolValue("live"))
721             _liveObjects.push_back(po);
722     }
723
724     SGPropertyNode * dest = fgGetNode("/sim/bindings/gui", true);
725     vector<SGPropertyNode_ptr> bindings = props->getChildren("binding");
726     if (bindings.size() > 0) {
727         GUIInfo * info = new GUIInfo(this);
728         info->key = props->getIntValue("keynum", -1);
729         if (props->hasValue("key"))
730             info->key = getKeyCode(props->getStringValue("key", ""));
731
732         for (unsigned int i = 0; i < bindings.size(); i++) {
733             unsigned int j = 0;
734             SGPropertyNode *binding;
735             while (dest->getChild("binding", j))
736                 j++;
737
738             const char *cmd = bindings[i]->getStringValue("command");
739             if (!strcmp(cmd, "nasal"))
740                 bindings[i]->setStringValue("module", _module.c_str());
741
742             binding = dest->getChild("binding", j, true);
743             copyProperties(bindings[i], binding);
744             info->bindings.push_back(new FGBinding(binding));
745         }
746         object->setCallback(action_callback);
747
748         if (type == "input" && props->getBoolValue("live"))
749             object->setDownCallback(action_callback);
750
751         object->setUserData(info);
752         _info.push_back(info);
753     }
754
755     object->makeReturnDefault(props->getBoolValue("default"));
756 }
757
758 void
759 FGDialog::setupGroup (puGroup * group, SGPropertyNode * props,
760                     int width, int height, bool makeFrame)
761 {
762     setupObject(group, props);
763
764     if (makeFrame) {
765         puFrame* f = new puFrame(0, 0, width, height);
766         setColor(f, props);
767     }
768
769     int nChildren = props->nChildren();
770     for (int i = 0; i < nChildren; i++)
771         makeObject(props->getChild(i), width, height);
772     group->close();
773 }
774
775 void
776 FGDialog::setColor(puObject * object, SGPropertyNode * props, int which)
777 {
778     string type = props->getName();
779     if (type == "")
780         type = "dialog";
781     if (type == "textbox" && props->getBoolValue("editable"))
782         type += "-editable";
783
784     FGColor *c = new FGColor(_gui->getColor("background"));
785     c->merge(_gui->getColor(type));
786     c->merge(props->getNode("color"));
787     if (c->isValid())
788         object->setColourScheme(c->red(), c->green(), c->blue(), c->alpha());
789
790     const struct {
791         int mask;
792         int id;
793         const char *name;
794         const char *cname;
795     } pucol[] = {
796         { BACKGROUND, PUCOL_BACKGROUND, "background", "color-background" },
797         { FOREGROUND, PUCOL_FOREGROUND, "foreground", "color-foreground" },
798         { HIGHLIGHT,  PUCOL_HIGHLIGHT,  "highlight",  "color-highlight" },
799         { LABEL,      PUCOL_LABEL,      "label",      "color-label" },
800         { LEGEND,     PUCOL_LEGEND,     "legend",     "color-legend" },
801         { MISC,       PUCOL_MISC,       "misc",       "color-misc" },
802 #ifdef PUCOL_EDITFIELD  // plib > 0.8.4
803         { EDITFIELD,  PUCOL_EDITFIELD,  "editfield",  "color-editfield" },
804 #endif
805     };
806
807     const int numcol = sizeof(pucol) / sizeof(pucol[0]);
808
809     for (int i = 0; i < numcol; i++) {
810         bool dirty = false;
811         c->clear();
812         c->setAlpha(1.0);
813
814         dirty |= c->merge(_gui->getColor(type + '-' + pucol[i].name));
815         if (which & pucol[i].mask)
816             dirty |= c->merge(props->getNode("color"));
817
818         if ((pucol[i].mask == LABEL) && !c->isValid())
819             dirty |= c->merge(_gui->getColor("label"));
820
821         dirty |= c->merge(props->getNode(pucol[i].cname));
822
823         if (c->isValid() && dirty)
824             object->setColor(pucol[i].id, c->red(), c->green(), c->blue(), c->alpha());
825     }
826 }
827
828
829 static struct {
830     const char *name;
831     int key;
832 } keymap[] = {
833     {"backspace", 8},
834     {"tab", 9},
835     {"return", 13},
836     {"enter", 13},
837     {"esc", 27},
838     {"escape", 27},
839     {"space", ' '},
840     {"&amp;", '&'},
841     {"and", '&'},
842     {"&lt;", '<'},
843     {"&gt;", '>'},
844     {"f1", PU_KEY_F1},
845     {"f2", PU_KEY_F2},
846     {"f3", PU_KEY_F3},
847     {"f4", PU_KEY_F4},
848     {"f5", PU_KEY_F5},
849     {"f6", PU_KEY_F6},
850     {"f7", PU_KEY_F7},
851     {"f8", PU_KEY_F8},
852     {"f9", PU_KEY_F9},
853     {"f10", PU_KEY_F10},
854     {"f11", PU_KEY_F11},
855     {"f12", PU_KEY_F12},
856     {"left", PU_KEY_LEFT},
857     {"up", PU_KEY_UP},
858     {"right", PU_KEY_RIGHT},
859     {"down", PU_KEY_DOWN},
860     {"pageup", PU_KEY_PAGE_UP},
861     {"pagedn", PU_KEY_PAGE_DOWN},
862     {"home", PU_KEY_HOME},
863     {"end", PU_KEY_END},
864     {"insert", PU_KEY_INSERT},
865     {0, -1},
866 };
867
868 int
869 FGDialog::getKeyCode(const char *str)
870 {
871     enum {
872         CTRL = 0x1,
873         SHIFT = 0x2,
874         ALT = 0x4,
875     };
876
877     while (*str == ' ')
878         str++;
879
880     char *buf = new char[strlen(str) + 1];
881     strcpy(buf, str);
882     char *s = buf + strlen(buf);
883     while (s > str && s[-1] == ' ')
884         s--;
885     *s = 0;
886     s = buf;
887
888     int mod = 0;
889     while (1) {
890         if (!strncmp(s, "Ctrl-", 5) || !strncmp(s, "CTRL-", 5))
891             s += 5, mod |= CTRL;
892         else if (!strncmp(s, "Shift-", 6) || !strncmp(s, "SHIFT-", 6))
893             s += 6, mod |= SHIFT;
894         else if (!strncmp(s, "Alt-", 4) || !strncmp(s, "ALT-", 4))
895             s += 4, mod |= ALT;
896         else
897             break;
898     }
899
900     int key = -1;
901     if (strlen(s) == 1 && isascii(*s)) {
902         key = *s;
903         if (mod & SHIFT)
904             key = toupper(key);
905         if (mod & CTRL)
906             key = toupper(key) - 64;
907         if (mod & ALT)
908             ;   // Alt not propagated to the gui
909     } else {
910         for (char *t = s; *t; t++)
911             *t = tolower(*t);
912         for (int i = 0; keymap[i].name; i++) {
913             if (!strcmp(s, keymap[i].name)) {
914                 key = keymap[i].key;
915                 break;
916             }
917         }
918     }
919     delete[] buf;
920     return key;
921 }
922
923 char **
924 FGDialog::value_list (const SGPropertyNode *props)
925 {
926     vector<SGPropertyNode_ptr> value_nodes = props->getChildren("value");
927     char ** entries = make_char_array(value_nodes.size());
928     for (unsigned int i = 0; i < value_nodes.size(); i++)
929         entries[i] = strdup((char *)value_nodes[i]->getStringValue());
930     return entries;
931 }
932
933 char **
934 FGDialog::make_char_array (int size)
935 {
936     char ** list = new char*[size+1];
937     for (int i = 0; i <= size; i++)
938         list[i] = 0;
939     _char_arrays.push_back(list);
940     return list;
941 }
942
943 void
944 FGDialog::destroy_char_array (char ** array)
945 {
946     for (int i = 0; array[i] != 0; i++)
947         if (array[i])
948             free(array[i]);// added with strdup
949     delete[] array;
950 }
951
952 \f
953 ////////////////////////////////////////////////////////////////////////
954 // Implementation of FGDialog::PropertyObject.
955 ////////////////////////////////////////////////////////////////////////
956
957 FGDialog::PropertyObject::PropertyObject (const char * n,
958                                            puObject * o,
959                                            SGPropertyNode_ptr p)
960     : name(n),
961       object(o),
962       node(p)
963 {
964 }
965
966
967 // end of dialog.cxx