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