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