]> git.mxchange.org Git - flightgear.git/blob - src/GUI/dialog.cxx
<input> elements that are <live> always update their input field from the
[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         int slider_width = props->getIntValue("slider", 20);
563         puList * obj = new puList(x, y, x + width, y + height, entries, slider_width);
564         if (presetSize)
565             obj->setSize(width, height);
566         setupObject(obj, props);
567         setColor(obj, props);
568         return obj;
569
570     } else if (type == "airport-list") {
571         AirportList * obj = new AirportList(x, y, x + width, y + height);
572         if (presetSize)
573             obj->setSize(width, height);
574         setupObject(obj, props);
575         setColor(obj, props);
576         return obj;
577
578     } else if (type == "input") {
579         puInput * obj = new puInput(x, y, x + width, y + height);
580         setupObject(obj, props);
581         setColor(obj, props, FOREGROUND|LABEL);
582         return obj;
583
584     } else if (type == "text") {
585         puText * obj = new puText(x, y);
586         setupObject(obj, props);
587
588         if (props->getNode("format")) {
589             SGPropertyNode *live = props->getNode("live");
590             if (live && live->getBoolValue())
591                 obj->setRenderCallback(format_callback, props);
592             else
593                 format_callback(obj, x, y, props);
594         }
595         // Layed-out objects need their size set, and non-layout ones
596         // get a different placement.
597         if (presetSize)
598             obj->setSize(width, height);
599         else
600             obj->setLabelPlace(PUPLACE_LABEL_DEFAULT);
601         setColor(obj, props, LABEL);
602         return obj;
603
604     } else if (type == "checkbox") {
605         puButton * obj;
606         obj = new puButton(x, y, x + width, y + height, PUBUTTON_XCHECK);
607         setupObject(obj, props);
608         setColor(obj, props, FOREGROUND|LABEL);
609         return obj;
610
611     } else if (type == "radio") {
612         puButton * obj;
613         obj = new puButton(x, y, x + width, y + height, PUBUTTON_CIRCLE);
614         setupObject(obj, props);
615         setColor(obj, props, FOREGROUND|LABEL);
616         return obj;
617
618     } else if (type == "button") {
619         puButton * obj;
620         const char * legend = props->getStringValue("legend", "[none]");
621         if (props->getBoolValue("one-shot", true))
622             obj = new puOneShot(x, y, legend);
623         else
624             obj = new puButton(x, y, legend);
625         if (presetSize)
626             obj->setSize(width, height);
627         setupObject(obj, props);
628         setColor(obj, props);
629         return obj;
630
631     } else if (type == "combo") {
632         vector<SGPropertyNode_ptr> value_nodes = props->getChildren("value");
633         char ** entries = make_char_array(value_nodes.size());
634         for (unsigned int i = 0; i < value_nodes.size(); i++)
635             entries[i] = strdup((char *)value_nodes[i]->getStringValue());
636
637         puComboBox * obj = new puComboBox(x, y, x + width, y + height, entries,
638                            props->getBoolValue("editable", false));
639         setupObject(obj, props);
640 #ifdef PUCOL_EDITFIELD  // plib > 0.8.4
641         setColor(obj, props, EDITFIELD);
642 #else
643         setColor(obj, props, FOREGROUND|LABEL);
644 #endif
645         return obj;
646
647     } else if (type == "slider") {
648         bool vertical = props->getBoolValue("vertical", false);
649         puSlider * obj = new puSlider(x, y, (vertical ? height : width));
650         obj->setMinValue(props->getFloatValue("min", 0.0));
651         obj->setMaxValue(props->getFloatValue("max", 1.0));
652         setupObject(obj, props);
653         if (presetSize)
654             obj->setSize(width, height);
655         setColor(obj, props, FOREGROUND|LABEL);
656         return obj;
657
658     } else if (type == "dial") {
659         puDial * obj = new puDial(x, y, width);
660         obj->setMinValue(props->getFloatValue("min", 0.0));
661         obj->setMaxValue(props->getFloatValue("max", 1.0));
662         obj->setWrap(props->getBoolValue("wrap", true));
663         setupObject(obj, props);
664         setColor(obj, props, FOREGROUND|LABEL);
665         return obj;
666
667     } else if (type == "textbox") {
668         int slider_width = props->getIntValue("slider", 20);
669         int wrap = props->getBoolValue("wrap", true);
670         puLargeInput * obj = new puLargeInput(x, y,
671                 x+width, x+height, 2, slider_width, wrap);
672
673         if (props->hasValue("editable")) {
674             if (props->getBoolValue("editable")==false)
675                 obj->disableInput();
676             else
677                 obj->enableInput();
678         }
679         if (presetSize)
680             obj->setSize(width, height);
681         setupObject(obj, props);
682         setColor(obj, props, FOREGROUND|LABEL);
683         return obj;
684
685     } else if (type == "select") {
686         vector<SGPropertyNode_ptr> value_nodes;
687         SGPropertyNode * selection_node =
688                 fgGetNode(props->getChild("selection")->getStringValue(), true);
689
690         for (int q = 0; q < selection_node->nChildren(); q++)
691             value_nodes.push_back(selection_node->getChild(q));
692
693         char ** entries = make_char_array(value_nodes.size());
694         for (unsigned int i = 0; i < value_nodes.size(); i++)
695             entries[i] = strdup((char *)value_nodes[i]->getName());
696
697         puSelectBox * obj =
698             new puSelectBox(x, y, x + width, y + height, entries);
699         setupObject(obj, props);
700 #ifdef PUCOL_EDITFIELD  // plib > 0.8.4
701         setColor(obj, props, EDITFIELD);
702 #else
703         setColor(obj, props, FOREGROUND|LABEL);
704 #endif
705         return obj;
706     } else {
707         return 0;
708     }
709 }
710
711 void
712 FGDialog::setupObject (puObject * object, SGPropertyNode * props)
713 {
714     string type = props->getName();
715     object->setLabelPlace(PUPLACE_CENTERED_RIGHT);
716
717     if (props->hasValue("legend"))
718         object->setLegend(props->getStringValue("legend"));
719
720     if (props->hasValue("label"))
721         object->setLabel(props->getStringValue("label"));
722
723     if (props->hasValue("border"))
724         object->setBorderThickness( props->getIntValue("border", 2) );
725
726     if ( SGPropertyNode *nft = props->getNode("font", false) ) {
727        FGFontCache *fc = _gui->get_fontcache();
728        puFont *lfnt = fc->get(nft);
729        object->setLabelFont(*lfnt);
730     } else {
731        object->setLabelFont(*_font);
732     }
733
734     if (props->hasValue("property")) {
735         const char * name = props->getStringValue("name");
736         if (name == 0)
737             name = "";
738         const char * propname = props->getStringValue("property");
739         SGPropertyNode_ptr node = fgGetNode(propname, true);
740         copy_to_pui(node, object);
741         PropertyObject* po = new PropertyObject(name, object, node);
742         _propertyObjects.push_back(po);
743         if(props->getBoolValue("live"))
744             _liveObjects.push_back(po);
745     }
746
747     SGPropertyNode * dest = fgGetNode("/sim/bindings/gui", true);
748     vector<SGPropertyNode_ptr> bindings = props->getChildren("binding");
749     if (bindings.size() > 0) {
750         GUIInfo * info = new GUIInfo(this);
751         info->key = props->getIntValue("keynum", -1);
752         if (props->hasValue("key"))
753             info->key = getKeyCode(props->getStringValue("key", ""));
754
755         for (unsigned int i = 0; i < bindings.size(); i++) {
756             unsigned int j = 0;
757             SGPropertyNode *binding;
758             while (dest->getChild("binding", j))
759                 j++;
760
761             const char *cmd = bindings[i]->getStringValue("command");
762             if (!strcmp(cmd, "nasal"))
763                 bindings[i]->setStringValue("module", _module.c_str());
764
765             binding = dest->getChild("binding", j, true);
766             copyProperties(bindings[i], binding);
767             info->bindings.push_back(new FGBinding(binding));
768         }
769         object->setCallback(action_callback);
770
771         if (type == "input" && props->getBoolValue("live"))
772             object->setDownCallback(action_callback);
773
774         object->setUserData(info);
775         _info.push_back(info);
776     }
777
778     object->makeReturnDefault(props->getBoolValue("default"));
779 }
780
781 void
782 FGDialog::setupGroup (puGroup * group, SGPropertyNode * props,
783                     int width, int height, bool makeFrame)
784 {
785     setupObject(group, props);
786
787     if (makeFrame) {
788         puFrame* f = new puFrame(0, 0, width, height);
789         setColor(f, props);
790     }
791
792     int nChildren = props->nChildren();
793     for (int i = 0; i < nChildren; i++)
794         makeObject(props->getChild(i), width, height);
795     group->close();
796 }
797
798 void
799 FGDialog::setColor(puObject * object, SGPropertyNode * props, int which)
800 {
801     string type = props->getName();
802     if (type == "")
803         type = "dialog";
804     if (type == "textbox" && props->getBoolValue("editable"))
805         type += "-editable";
806
807     FGColor *c = new FGColor(_gui->getColor("background"));
808     c->merge(_gui->getColor(type));
809     c->merge(props->getNode("color"));
810     if (c->isValid())
811         object->setColourScheme(c->red(), c->green(), c->blue(), c->alpha());
812
813     const struct {
814         int mask;
815         int id;
816         const char *name;
817         const char *cname;
818     } pucol[] = {
819         { BACKGROUND, PUCOL_BACKGROUND, "background", "color-background" },
820         { FOREGROUND, PUCOL_FOREGROUND, "foreground", "color-foreground" },
821         { HIGHLIGHT,  PUCOL_HIGHLIGHT,  "highlight",  "color-highlight" },
822         { LABEL,      PUCOL_LABEL,      "label",      "color-label" },
823         { LEGEND,     PUCOL_LEGEND,     "legend",     "color-legend" },
824         { MISC,       PUCOL_MISC,       "misc",       "color-misc" },
825 #ifdef PUCOL_EDITFIELD  // plib > 0.8.4
826         { EDITFIELD,  PUCOL_EDITFIELD,  "editfield",  "color-editfield" },
827 #endif
828     };
829
830     const int numcol = sizeof(pucol) / sizeof(pucol[0]);
831
832     for (int i = 0; i < numcol; i++) {
833         bool dirty = false;
834         c->clear();
835         c->setAlpha(1.0);
836
837         dirty |= c->merge(_gui->getColor(type + '-' + pucol[i].name));
838         if (which & pucol[i].mask)
839             dirty |= c->merge(props->getNode("color"));
840
841         if ((pucol[i].mask == LABEL) && !c->isValid())
842             dirty |= c->merge(_gui->getColor("label"));
843
844         if (c->isValid() && dirty)
845             object->setColor(pucol[i].id, c->red(), c->green(), c->blue(), c->alpha());
846     }
847 }
848
849
850 static struct {
851     const char *name;
852     int key;
853 } keymap[] = {
854     {"backspace", 8},
855     {"tab", 9},
856     {"return", 13},
857     {"enter", 13},
858     {"esc", 27},
859     {"escape", 27},
860     {"space", ' '},
861     {"&amp;", '&'},
862     {"and", '&'},
863     {"&lt;", '<'},
864     {"&gt;", '>'},
865     {"f1", PU_KEY_F1},
866     {"f2", PU_KEY_F2},
867     {"f3", PU_KEY_F3},
868     {"f4", PU_KEY_F4},
869     {"f5", PU_KEY_F5},
870     {"f6", PU_KEY_F6},
871     {"f7", PU_KEY_F7},
872     {"f8", PU_KEY_F8},
873     {"f9", PU_KEY_F9},
874     {"f10", PU_KEY_F10},
875     {"f11", PU_KEY_F11},
876     {"f12", PU_KEY_F12},
877     {"left", PU_KEY_LEFT},
878     {"up", PU_KEY_UP},
879     {"right", PU_KEY_RIGHT},
880     {"down", PU_KEY_DOWN},
881     {"pageup", PU_KEY_PAGE_UP},
882     {"pagedn", PU_KEY_PAGE_DOWN},
883     {"home", PU_KEY_HOME},
884     {"end", PU_KEY_END},
885     {"insert", PU_KEY_INSERT},
886     {0, -1},
887 };
888
889 int
890 FGDialog::getKeyCode(const char *str)
891 {
892     enum {
893         CTRL = 0x1,
894         SHIFT = 0x2,
895         ALT = 0x4,
896     };
897
898     while (*str == ' ')
899         str++;
900
901     char *buf = new char[strlen(str) + 1];
902     strcpy(buf, str);
903     char *s = buf + strlen(buf);
904     while (s > str && s[-1] == ' ')
905         s--;
906     *s = 0;
907     s = buf;
908
909     int mod = 0;
910     while (1) {
911         if (!strncmp(s, "Ctrl-", 5) || !strncmp(s, "CTRL-", 5))
912             s += 5, mod |= CTRL;
913         else if (!strncmp(s, "Shift-", 6) || !strncmp(s, "SHIFT-", 6))
914             s += 6, mod |= SHIFT;
915         else if (!strncmp(s, "Alt-", 4) || !strncmp(s, "ALT-", 4))
916             s += 4, mod |= ALT;
917         else
918             break;
919     }
920
921     int key = -1;
922     if (strlen(s) == 1 && isascii(*s)) {
923         key = *s;
924         if (mod & SHIFT)
925             key = toupper(key);
926         if (mod & CTRL)
927             key = toupper(key) - 64;
928         if (mod & ALT)
929             ;   // Alt not propagated to the gui
930     } else {
931         for (char *t = s; *t; t++)
932             *t = tolower(*t);
933         for (int i = 0; keymap[i].name; i++) {
934             if (!strcmp(s, keymap[i].name)) {
935                 key = keymap[i].key;
936                 break;
937             }
938         }
939     }
940     delete[] buf;
941     return key;
942 }
943
944 char **
945 FGDialog::make_char_array (int size)
946 {
947     char ** list = new char*[size+1];
948     for (int i = 0; i <= size; i++)
949         list[i] = 0;
950     _char_arrays.push_back(list);
951     return list;
952 }
953
954
955 \f
956 ////////////////////////////////////////////////////////////////////////
957 // Implementation of FGDialog::PropertyObject.
958 ////////////////////////////////////////////////////////////////////////
959
960 FGDialog::PropertyObject::PropertyObject (const char * n,
961                                            puObject * o,
962                                            SGPropertyNode_ptr p)
963     : name(n),
964       object(o),
965       node(p)
966 {
967 }
968
969
970 // end of dialog.cxx