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