]> git.mxchange.org Git - flightgear.git/blob - src/GUI/dialog.cxx
apply custom widget fonts also to the legend, so that they show
[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        object->setLegendFont(*lfnt);
719     } else {
720        object->setLabelFont(*_font);
721     }
722
723     if (props->hasValue("property")) {
724         const char * name = props->getStringValue("name");
725         if (name == 0)
726             name = "";
727         const char * propname = props->getStringValue("property");
728         SGPropertyNode_ptr node = fgGetNode(propname, true);
729         copy_to_pui(node, object);
730
731         PropertyObject* po = new PropertyObject(name, object, node);
732         _propertyObjects.push_back(po);
733         if (props->getBoolValue("live"))
734             _liveObjects.push_back(po);
735     }
736
737     SGPropertyNode * dest = fgGetNode("/sim/bindings/gui", true);
738     vector<SGPropertyNode_ptr> bindings = props->getChildren("binding");
739     if (bindings.size() > 0) {
740         info->key = props->getIntValue("keynum", -1);
741         if (props->hasValue("key"))
742             info->key = getKeyCode(props->getStringValue("key", ""));
743
744         for (unsigned int i = 0; i < bindings.size(); i++) {
745             unsigned int j = 0;
746             SGPropertyNode_ptr binding;
747             while (dest->getChild("binding", j))
748                 j++;
749
750             const char *cmd = bindings[i]->getStringValue("command");
751             if (!strcmp(cmd, "nasal"))
752                 bindings[i]->setStringValue("module", _module.c_str());
753
754             binding = dest->getChild("binding", j, true);
755             copyProperties(bindings[i], binding);
756             info->bindings.push_back(new SGBinding(binding, globals->get_props()));
757         }
758         object->setCallback(action_callback);
759     }
760
761     string type = props->getName();
762     if (type == "input" && props->getBoolValue("live"))
763         object->setDownCallback(action_callback);
764
765     if (type == "text") {
766         const char *format = props->getStringValue("format", 0);
767         if (format) {
768             info->fmt_type = validate_format(format);
769             if (info->fmt_type != f_INVALID)
770                 info->format = format;
771             else
772                 SG_LOG(SG_GENERAL, SG_ALERT, "DIALOG: invalid <format> '"
773                         << format << '\'');
774         }
775     }
776 }
777
778 void
779 FGDialog::setupGroup (puGroup * group, SGPropertyNode * props,
780                     int width, int height, bool makeFrame)
781 {
782     setupObject(group, props);
783
784     if (makeFrame) {
785         puFrame* f = new puFrame(0, 0, width, height);
786         setColor(f, props);
787     }
788
789     int nChildren = props->nChildren();
790     for (int i = 0; i < nChildren; i++)
791         makeObject(props->getChild(i), width, height);
792     group->close();
793 }
794
795 void
796 FGDialog::setColor(puObject * object, SGPropertyNode * props, int which)
797 {
798     string type = props->getName();
799     if (type.empty())
800         type = "dialog";
801     if (type == "textbox" && props->getBoolValue("editable"))
802         type += "-editable";
803
804     FGColor c(_gui->getColor("background"));
805     c.merge(_gui->getColor(type));
806     c.merge(props->getNode("color"));
807     if (c.isValid())
808         object->setColourScheme(c.red(), c.green(), c.blue(), c.alpha());
809
810     const struct {
811         int mask;
812         int id;
813         const char *name;
814         const char *cname;
815     } pucol[] = {
816         { BACKGROUND, PUCOL_BACKGROUND, "background", "color-background" },
817         { FOREGROUND, PUCOL_FOREGROUND, "foreground", "color-foreground" },
818         { HIGHLIGHT,  PUCOL_HIGHLIGHT,  "highlight",  "color-highlight" },
819         { LABEL,      PUCOL_LABEL,      "label",      "color-label" },
820         { LEGEND,     PUCOL_LEGEND,     "legend",     "color-legend" },
821         { MISC,       PUCOL_MISC,       "misc",       "color-misc" },
822         { EDITFIELD,  PUCOL_EDITFIELD,  "editfield",  "color-editfield" },
823     };
824
825     const int numcol = sizeof(pucol) / sizeof(pucol[0]);
826
827     for (int i = 0; i < numcol; i++) {
828         bool dirty = false;
829         c.clear();
830         c.setAlpha(1.0);
831
832         dirty |= c.merge(_gui->getColor(type + '-' + pucol[i].name));
833         if (which & pucol[i].mask)
834             dirty |= c.merge(props->getNode("color"));
835
836         if ((pucol[i].mask == LABEL) && !c.isValid())
837             dirty |= c.merge(_gui->getColor("label"));
838
839         dirty |= c.merge(props->getNode(pucol[i].cname));
840
841         if (c.isValid() && dirty)
842             object->setColor(pucol[i].id, c.red(), c.green(), c.blue(), c.alpha());
843     }
844 }
845
846
847 static struct {
848     const char *name;
849     int key;
850 } keymap[] = {
851     {"backspace", 8},
852     {"tab", 9},
853     {"return", 13},
854     {"enter", 13},
855     {"esc", 27},
856     {"escape", 27},
857     {"space", ' '},
858     {"&amp;", '&'},
859     {"and", '&'},
860     {"&lt;", '<'},
861     {"&gt;", '>'},
862     {"f1", PU_KEY_F1},
863     {"f2", PU_KEY_F2},
864     {"f3", PU_KEY_F3},
865     {"f4", PU_KEY_F4},
866     {"f5", PU_KEY_F5},
867     {"f6", PU_KEY_F6},
868     {"f7", PU_KEY_F7},
869     {"f8", PU_KEY_F8},
870     {"f9", PU_KEY_F9},
871     {"f10", PU_KEY_F10},
872     {"f11", PU_KEY_F11},
873     {"f12", PU_KEY_F12},
874     {"left", PU_KEY_LEFT},
875     {"up", PU_KEY_UP},
876     {"right", PU_KEY_RIGHT},
877     {"down", PU_KEY_DOWN},
878     {"pageup", PU_KEY_PAGE_UP},
879     {"pagedn", PU_KEY_PAGE_DOWN},
880     {"home", PU_KEY_HOME},
881     {"end", PU_KEY_END},
882     {"insert", PU_KEY_INSERT},
883     {0, -1},
884 };
885
886 int
887 FGDialog::getKeyCode(const char *str)
888 {
889     enum {
890         CTRL = 0x1,
891         SHIFT = 0x2,
892         ALT = 0x4,
893     };
894
895     while (*str == ' ')
896         str++;
897
898     char *buf = new char[strlen(str) + 1];
899     strcpy(buf, str);
900     char *s = buf + strlen(buf);
901     while (s > str && s[-1] == ' ')
902         s--;
903     *s = 0;
904     s = buf;
905
906     int mod = 0;
907     while (1) {
908         if (!strncmp(s, "Ctrl-", 5) || !strncmp(s, "CTRL-", 5))
909             s += 5, mod |= CTRL;
910         else if (!strncmp(s, "Shift-", 6) || !strncmp(s, "SHIFT-", 6))
911             s += 6, mod |= SHIFT;
912         else if (!strncmp(s, "Alt-", 4) || !strncmp(s, "ALT-", 4))
913             s += 4, mod |= ALT;
914         else
915             break;
916     }
917
918     int key = -1;
919     if (strlen(s) == 1 && isascii(*s)) {
920         key = *s;
921         if (mod & SHIFT)
922             key = toupper(key);
923         if (mod & CTRL)
924             key = toupper(key) - 64;
925         if (mod & ALT)
926             ;   // Alt not propagated to the gui
927     } else {
928         for (char *t = s; *t; t++)
929             *t = tolower(*t);
930         for (int i = 0; keymap[i].name; i++) {
931             if (!strcmp(s, keymap[i].name)) {
932                 key = keymap[i].key;
933                 break;
934             }
935         }
936     }
937     delete[] buf;
938     return key;
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
958 \f
959 ////////////////////////////////////////////////////////////////////////
960 // Implementation of fgValueList and derived pui widgets
961 ////////////////////////////////////////////////////////////////////////
962
963
964 fgValueList::fgValueList(SGPropertyNode *p) :
965     _props(p)
966 {
967     make_list();
968 }
969
970 void
971 fgValueList::update()
972 {
973     destroy_list();
974     make_list();
975 }
976
977 fgValueList::~fgValueList()
978 {
979     destroy_list();
980 }
981
982 void
983 fgValueList::make_list()
984 {
985     vector<SGPropertyNode_ptr> value_nodes = _props->getChildren("value");
986     _list = new char *[value_nodes.size() + 1];
987     unsigned int i;
988     for (i = 0; i < value_nodes.size(); i++)
989         _list[i] = strdup((char *)value_nodes[i]->getStringValue());
990     _list[i] = 0;
991 }
992
993 void
994 fgValueList::destroy_list()
995 {
996     for (int i = 0; _list[i] != 0; i++)
997         if (_list[i])
998             free(_list[i]);
999     delete[] _list;
1000 }
1001
1002
1003
1004 void
1005 fgList::update()
1006 {
1007     fgValueList::update();
1008     int top = getTopItem();
1009     newList(_list);
1010     setTopItem(top);
1011 }
1012
1013 // end of dialog.cxx