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