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