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