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