]> git.mxchange.org Git - flightgear.git/blob - src/GUI/dialog.cxx
empty names aren't names
[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     if (objectName && !objectName[0])
386         objectName = 0;
387
388     for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
389         const string &name = _propertyObjects[i]->name;
390         if (objectName && name != objectName)
391             continue;
392
393         puObject *obj = _propertyObjects[i]->object;
394         copy_to_pui(_propertyObjects[i]->node, obj);
395     }
396 }
397
398 void
399 FGDialog::applyValues (const char * objectName)
400 {
401     if (objectName && !objectName[0])
402         objectName = 0;
403
404     for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
405         const string &name = _propertyObjects[i]->name;
406         if (objectName && name != objectName)
407             continue;
408
409         copy_from_pui(_propertyObjects[i]->object,
410                       _propertyObjects[i]->node);
411     }
412 }
413
414 void
415 FGDialog::update ()
416 {
417     for (unsigned int i = 0; i < _liveObjects.size(); i++) {
418         puObject *obj = _liveObjects[i]->object;
419         if (obj->getType() & PUCLASS_INPUT && ((puInput *)obj)->isAcceptingInput())
420             continue;
421
422         copy_to_pui(_liveObjects[i]->node, obj);
423     }
424 }
425
426 void
427 FGDialog::display (SGPropertyNode * props)
428 {
429     if (_object != 0) {
430         SG_LOG(SG_GENERAL, SG_ALERT, "This widget is already active");
431         return;
432     }
433
434     int screenw = globals->get_props()->getIntValue("/sim/startup/xsize");
435     int screenh = globals->get_props()->getIntValue("/sim/startup/ysize");
436
437     bool userx = props->hasValue("x");
438     bool usery = props->hasValue("y");
439     bool userw = props->hasValue("width");
440     bool userh = props->hasValue("height");
441
442     // Let the layout widget work in the same property subtree.
443     LayoutWidget wid(props);
444
445     SGPropertyNode *fontnode = props->getNode("font");
446     if (fontnode) {
447         FGFontCache *fc = _gui->get_fontcache();
448         _font = fc->get(fontnode);
449     } else {
450         _font = _gui->getDefaultFont();
451     }
452     wid.setDefaultFont(_font, int(_font->getPointSize()));
453
454     int pw=0, ph=0;
455     int px, py, savex, savey;
456     if(!userw || !userh)
457         wid.calcPrefSize(&pw, &ph);
458     pw = props->getIntValue("width", pw);
459     ph = props->getIntValue("height", ph);
460     px = savex = props->getIntValue("x", (screenw - pw) / 2);
461     py = savey = props->getIntValue("y", (screenh - ph) / 2);
462
463     // Negative x/y coordinates are interpreted as distance from the top/right
464     // corner rather than bottom/left.
465     if (userx && px < 0)
466         px = screenw - pw + px;
467     if (usery && py < 0)
468         py = screenh - ph + py;
469
470     // Define "x", "y", "width" and/or "height" in the property tree if they
471     // are not specified in the configuration file.
472     wid.layout(px, py, pw, ph);
473
474     // Use the dimension and location properties as specified in the
475     // configuration file or from the layout widget.
476     _object = makeObject(props, screenw, screenh);
477
478     // Remove automatically generated properties, so the layout looks
479     // the same next time around, or restore x and y to preserve negative coords.
480     if(userx)
481         props->setIntValue("x", savex);
482     else
483         props->removeChild("x");
484
485     if(usery)
486         props->setIntValue("y", savey);
487     else
488         props->removeChild("y");
489
490     if(!userw) props->removeChild("width");
491     if(!userh) props->removeChild("height");
492
493     if (_object != 0) {
494         _object->reveal();
495     } else {
496         SG_LOG(SG_GENERAL, SG_ALERT, "Widget "
497                << props->getStringValue("name", "[unnamed]")
498                << " does not contain a proper GUI definition");
499     }
500 }
501
502 puObject *
503 FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
504 {
505     if (props->getBoolValue("hide"))
506         return 0;
507
508     bool presetSize = props->hasValue("width") && props->hasValue("height");
509     int width = props->getIntValue("width", parentWidth);
510     int height = props->getIntValue("height", parentHeight);
511     int x = props->getIntValue("x", (parentWidth - width) / 2);
512     int y = props->getIntValue("y", (parentHeight - height) / 2);
513     string type = props->getName();
514
515     if (type == "")
516         type = "dialog";
517
518     if (type == "dialog") {
519         puPopup * obj;
520         bool draggable = props->getBoolValue("draggable", true);
521         if (props->getBoolValue("modal", false))
522             obj = new puDialogBox(x, y);
523         else
524             obj = new fgPopup(x, y, draggable);
525         setupGroup(obj, props, width, height, true);
526         setColor(obj, props);
527         return obj;
528
529     } else if (type == "group") {
530         puGroup * obj = new puGroup(x, y);
531         setupGroup(obj, props, width, height, false);
532         setColor(obj, props);
533         return obj;
534
535     } else if (type == "frame") {
536         puGroup * obj = new puGroup(x, y);
537         setupGroup(obj, props, width, height, true);
538         setColor(obj, props);
539         return obj;
540
541     } else if (type == "hrule" || type == "vrule") {
542         puFrame * obj = new puFrame(x, y, x + width, y + height);
543         obj->setBorderThickness(0);
544         setColor(obj, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
545         return obj;
546
547     } else if (type == "list") {
548         char ** entries = value_list(props);
549         int slider_width = props->getIntValue("slider", 20);
550         puList * obj = new puList(x, y, x + width, y + height, entries, slider_width);
551         if (presetSize)
552             obj->setSize(width, height);
553         setupObject(obj, props);
554         setColor(obj, props);
555         return obj;
556
557     } else if (type == "airport-list") {
558         AirportList * obj = new AirportList(x, y, x + width, y + height);
559         if (presetSize)
560             obj->setSize(width, height);
561         setupObject(obj, props);
562         setColor(obj, props);
563         return obj;
564
565     } else if (type == "input") {
566         puInput * obj = new puInput(x, y, x + width, y + height);
567         setupObject(obj, props);
568         setColor(obj, props, FOREGROUND|LABEL);
569         return obj;
570
571     } else if (type == "text") {
572         puText * obj = new puText(x, y);
573         setupObject(obj, props);
574
575         if (props->getNode("format")) {
576             SGPropertyNode *live = props->getNode("live");
577             if (live && live->getBoolValue())
578                 obj->setRenderCallback(format_callback, props);
579             else
580                 format_callback(obj, x, y, props);
581         }
582         // Layed-out objects need their size set, and non-layout ones
583         // get a different placement.
584         if (presetSize)
585             obj->setSize(width, height);
586         else
587             obj->setLabelPlace(PUPLACE_LABEL_DEFAULT);
588         setColor(obj, props, LABEL);
589         return obj;
590
591     } else if (type == "checkbox") {
592         puButton * obj;
593         obj = new puButton(x, y, x + width, y + height, PUBUTTON_XCHECK);
594         setupObject(obj, props);
595         setColor(obj, props, FOREGROUND|LABEL);
596         return obj;
597
598     } else if (type == "radio") {
599         puButton * obj;
600         obj = new puButton(x, y, x + width, y + height, PUBUTTON_CIRCLE);
601         setupObject(obj, props);
602         setColor(obj, props, FOREGROUND|LABEL);
603         return obj;
604
605     } else if (type == "button") {
606         puButton * obj;
607         const char * legend = props->getStringValue("legend", "[none]");
608         if (props->getBoolValue("one-shot", true))
609             obj = new puOneShot(x, y, legend);
610         else
611             obj = new puButton(x, y, legend);
612         if (presetSize)
613             obj->setSize(width, height);
614         setupObject(obj, props);
615         setColor(obj, props);
616         return obj;
617
618     } else if (type == "combo") {
619         char ** entries = value_list(props);
620         puComboBox * obj = new puComboBox(x, y, x + width, y + height, entries,
621                            props->getBoolValue("editable", false));
622         setupObject(obj, props);
623 #ifdef PUCOL_EDITFIELD  // plib > 0.8.4
624         setColor(obj, props, EDITFIELD);
625 #else
626         setColor(obj, props, FOREGROUND|LABEL);
627 #endif
628         return obj;
629
630     } else if (type == "slider") {
631         bool vertical = props->getBoolValue("vertical", false);
632         puSlider * obj = new puSlider(x, y, (vertical ? height : width));
633         obj->setMinValue(props->getFloatValue("min", 0.0));
634         obj->setMaxValue(props->getFloatValue("max", 1.0));
635         setupObject(obj, props);
636         if (presetSize)
637             obj->setSize(width, height);
638         setColor(obj, props, FOREGROUND|LABEL);
639         return obj;
640
641     } else if (type == "dial") {
642         puDial * obj = new puDial(x, y, width);
643         obj->setMinValue(props->getFloatValue("min", 0.0));
644         obj->setMaxValue(props->getFloatValue("max", 1.0));
645         obj->setWrap(props->getBoolValue("wrap", true));
646         setupObject(obj, props);
647         setColor(obj, props, FOREGROUND|LABEL);
648         return obj;
649
650     } else if (type == "textbox") {
651         int slider_width = props->getIntValue("slider", 20);
652         int wrap = props->getBoolValue("wrap", true);
653         puLargeInput * obj = new puLargeInput(x, y,
654                 x+width, x+height, 2, slider_width, wrap);
655
656         if (props->hasValue("editable")) {
657             if (props->getBoolValue("editable")==false)
658                 obj->disableInput();
659             else
660                 obj->enableInput();
661         }
662         if (presetSize)
663             obj->setSize(width, height);
664         setupObject(obj, props);
665         setColor(obj, props, FOREGROUND|LABEL);
666         return obj;
667
668     } else if (type == "select") {
669         vector<SGPropertyNode_ptr> value_nodes;
670         SGPropertyNode * selection_node =
671                 fgGetNode(props->getChild("selection")->getStringValue(), true);
672
673         for (int q = 0; q < selection_node->nChildren(); q++)
674             value_nodes.push_back(selection_node->getChild(q));
675
676         char ** entries = make_char_array(value_nodes.size());
677         for (unsigned int i = 0; i < value_nodes.size(); i++)
678             entries[i] = strdup((char *)value_nodes[i]->getName());
679
680         puSelectBox * obj =
681             new puSelectBox(x, y, x + width, y + height, entries);
682         setupObject(obj, props);
683 #ifdef PUCOL_EDITFIELD  // plib > 0.8.4
684         setColor(obj, props, EDITFIELD);
685 #else
686         setColor(obj, props, FOREGROUND|LABEL);
687 #endif
688         return obj;
689     } else {
690         return 0;
691     }
692 }
693
694 void
695 FGDialog::setupObject (puObject * object, SGPropertyNode * props)
696 {
697     string type = props->getName();
698     object->setLabelPlace(PUPLACE_CENTERED_RIGHT);
699
700     if (props->hasValue("legend"))
701         object->setLegend(props->getStringValue("legend"));
702
703     if (props->hasValue("label"))
704         object->setLabel(props->getStringValue("label"));
705
706     if (props->hasValue("border"))
707         object->setBorderThickness( props->getIntValue("border", 2) );
708
709     if ( SGPropertyNode *nft = props->getNode("font", false) ) {
710        FGFontCache *fc = _gui->get_fontcache();
711        puFont *lfnt = fc->get(nft);
712        object->setLabelFont(*lfnt);
713     } else {
714        object->setLabelFont(*_font);
715     }
716
717     if (props->hasValue("property")) {
718         const char * name = props->getStringValue("name");
719         if (name == 0)
720             name = "";
721         const char * propname = props->getStringValue("property");
722         SGPropertyNode_ptr node = fgGetNode(propname, true);
723         copy_to_pui(node, object);
724         PropertyObject* po = new PropertyObject(name, object, node);
725         _propertyObjects.push_back(po);
726         if(props->getBoolValue("live"))
727             _liveObjects.push_back(po);
728     }
729
730     SGPropertyNode * dest = fgGetNode("/sim/bindings/gui", true);
731     vector<SGPropertyNode_ptr> bindings = props->getChildren("binding");
732     if (bindings.size() > 0) {
733         GUIInfo * info = new GUIInfo(this);
734         info->key = props->getIntValue("keynum", -1);
735         if (props->hasValue("key"))
736             info->key = getKeyCode(props->getStringValue("key", ""));
737
738         for (unsigned int i = 0; i < bindings.size(); i++) {
739             unsigned int j = 0;
740             SGPropertyNode *binding;
741             while (dest->getChild("binding", j))
742                 j++;
743
744             const char *cmd = bindings[i]->getStringValue("command");
745             if (!strcmp(cmd, "nasal"))
746                 bindings[i]->setStringValue("module", _module.c_str());
747
748             binding = dest->getChild("binding", j, true);
749             copyProperties(bindings[i], binding);
750             info->bindings.push_back(new FGBinding(binding));
751         }
752         object->setCallback(action_callback);
753
754         if (type == "input" && props->getBoolValue("live"))
755             object->setDownCallback(action_callback);
756
757         object->setUserData(info);
758         _info.push_back(info);
759     }
760
761     object->makeReturnDefault(props->getBoolValue("default"));
762 }
763
764 void
765 FGDialog::setupGroup (puGroup * group, SGPropertyNode * props,
766                     int width, int height, bool makeFrame)
767 {
768     setupObject(group, props);
769
770     if (makeFrame) {
771         puFrame* f = new puFrame(0, 0, width, height);
772         setColor(f, props);
773     }
774
775     int nChildren = props->nChildren();
776     for (int i = 0; i < nChildren; i++)
777         makeObject(props->getChild(i), width, height);
778     group->close();
779 }
780
781 void
782 FGDialog::setColor(puObject * object, SGPropertyNode * props, int which)
783 {
784     string type = props->getName();
785     if (type == "")
786         type = "dialog";
787     if (type == "textbox" && props->getBoolValue("editable"))
788         type += "-editable";
789
790     FGColor *c = new FGColor(_gui->getColor("background"));
791     c->merge(_gui->getColor(type));
792     c->merge(props->getNode("color"));
793     if (c->isValid())
794         object->setColourScheme(c->red(), c->green(), c->blue(), c->alpha());
795
796     const struct {
797         int mask;
798         int id;
799         const char *name;
800         const char *cname;
801     } pucol[] = {
802         { BACKGROUND, PUCOL_BACKGROUND, "background", "color-background" },
803         { FOREGROUND, PUCOL_FOREGROUND, "foreground", "color-foreground" },
804         { HIGHLIGHT,  PUCOL_HIGHLIGHT,  "highlight",  "color-highlight" },
805         { LABEL,      PUCOL_LABEL,      "label",      "color-label" },
806         { LEGEND,     PUCOL_LEGEND,     "legend",     "color-legend" },
807         { MISC,       PUCOL_MISC,       "misc",       "color-misc" },
808 #ifdef PUCOL_EDITFIELD  // plib > 0.8.4
809         { EDITFIELD,  PUCOL_EDITFIELD,  "editfield",  "color-editfield" },
810 #endif
811     };
812
813     const int numcol = sizeof(pucol) / sizeof(pucol[0]);
814
815     for (int i = 0; i < numcol; i++) {
816         bool dirty = false;
817         c->clear();
818         c->setAlpha(1.0);
819
820         dirty |= c->merge(_gui->getColor(type + '-' + pucol[i].name));
821         if (which & pucol[i].mask)
822             dirty |= c->merge(props->getNode("color"));
823
824         if ((pucol[i].mask == LABEL) && !c->isValid())
825             dirty |= c->merge(_gui->getColor("label"));
826
827         dirty |= c->merge(props->getNode(pucol[i].cname));
828
829         if (c->isValid() && dirty)
830             object->setColor(pucol[i].id, c->red(), c->green(), c->blue(), c->alpha());
831     }
832 }
833
834
835 static struct {
836     const char *name;
837     int key;
838 } keymap[] = {
839     {"backspace", 8},
840     {"tab", 9},
841     {"return", 13},
842     {"enter", 13},
843     {"esc", 27},
844     {"escape", 27},
845     {"space", ' '},
846     {"&amp;", '&'},
847     {"and", '&'},
848     {"&lt;", '<'},
849     {"&gt;", '>'},
850     {"f1", PU_KEY_F1},
851     {"f2", PU_KEY_F2},
852     {"f3", PU_KEY_F3},
853     {"f4", PU_KEY_F4},
854     {"f5", PU_KEY_F5},
855     {"f6", PU_KEY_F6},
856     {"f7", PU_KEY_F7},
857     {"f8", PU_KEY_F8},
858     {"f9", PU_KEY_F9},
859     {"f10", PU_KEY_F10},
860     {"f11", PU_KEY_F11},
861     {"f12", PU_KEY_F12},
862     {"left", PU_KEY_LEFT},
863     {"up", PU_KEY_UP},
864     {"right", PU_KEY_RIGHT},
865     {"down", PU_KEY_DOWN},
866     {"pageup", PU_KEY_PAGE_UP},
867     {"pagedn", PU_KEY_PAGE_DOWN},
868     {"home", PU_KEY_HOME},
869     {"end", PU_KEY_END},
870     {"insert", PU_KEY_INSERT},
871     {0, -1},
872 };
873
874 int
875 FGDialog::getKeyCode(const char *str)
876 {
877     enum {
878         CTRL = 0x1,
879         SHIFT = 0x2,
880         ALT = 0x4,
881     };
882
883     while (*str == ' ')
884         str++;
885
886     char *buf = new char[strlen(str) + 1];
887     strcpy(buf, str);
888     char *s = buf + strlen(buf);
889     while (s > str && s[-1] == ' ')
890         s--;
891     *s = 0;
892     s = buf;
893
894     int mod = 0;
895     while (1) {
896         if (!strncmp(s, "Ctrl-", 5) || !strncmp(s, "CTRL-", 5))
897             s += 5, mod |= CTRL;
898         else if (!strncmp(s, "Shift-", 6) || !strncmp(s, "SHIFT-", 6))
899             s += 6, mod |= SHIFT;
900         else if (!strncmp(s, "Alt-", 4) || !strncmp(s, "ALT-", 4))
901             s += 4, mod |= ALT;
902         else
903             break;
904     }
905
906     int key = -1;
907     if (strlen(s) == 1 && isascii(*s)) {
908         key = *s;
909         if (mod & SHIFT)
910             key = toupper(key);
911         if (mod & CTRL)
912             key = toupper(key) - 64;
913         if (mod & ALT)
914             ;   // Alt not propagated to the gui
915     } else {
916         for (char *t = s; *t; t++)
917             *t = tolower(*t);
918         for (int i = 0; keymap[i].name; i++) {
919             if (!strcmp(s, keymap[i].name)) {
920                 key = keymap[i].key;
921                 break;
922             }
923         }
924     }
925     delete[] buf;
926     return key;
927 }
928
929 char **
930 FGDialog::value_list (const SGPropertyNode *props)
931 {
932     vector<SGPropertyNode_ptr> value_nodes = props->getChildren("value");
933     char ** entries = make_char_array(value_nodes.size());
934     for (unsigned int i = 0; i < value_nodes.size(); i++)
935         entries[i] = strdup((char *)value_nodes[i]->getStringValue());
936     return entries;
937 }
938
939 char **
940 FGDialog::make_char_array (int size)
941 {
942     char ** list = new char*[size+1];
943     for (int i = 0; i <= size; i++)
944         list[i] = 0;
945     _char_arrays.push_back(list);
946     return list;
947 }
948
949 void
950 FGDialog::destroy_char_array (char ** array)
951 {
952     for (int i = 0; array[i] != 0; i++)
953         if (array[i])
954             free(array[i]);// added with strdup
955     delete[] array;
956 }
957
958 \f
959 ////////////////////////////////////////////////////////////////////////
960 // Implementation of FGDialog::PropertyObject.
961 ////////////////////////////////////////////////////////////////////////
962
963 FGDialog::PropertyObject::PropertyObject (const char * n,
964                                            puObject * o,
965                                            SGPropertyNode_ptr p)
966     : name(n),
967       object(o),
968       node(p)
969 {
970 }
971
972
973 // end of dialog.cxx