]> git.mxchange.org Git - flightgear.git/blob - src/GUI/dialog.cxx
Add support for emissive layers.
[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       _props(props)
322 {
323     display(props);
324 }
325
326 FGDialog::~FGDialog ()
327 {
328     int x, y;
329     _object->getAbsolutePosition(&x, &y);
330     _props->setIntValue("lastx", x);
331     _props->setIntValue("lasty", y);
332     puDeleteObject(_object);
333
334     unsigned int i;
335
336                                 // Delete all the arrays we made
337                                 // and were forced to keep around
338                                 // because PUI won't do its own
339                                 // memory management.
340     for (i = 0; i < _char_arrays.size(); i++) {
341         for (int j = 0; _char_arrays[i][j] != 0; j++)
342             free(_char_arrays[i][j]); // added with strdup
343         delete[] _char_arrays[i];
344     }
345
346                                 // Delete all the info objects we
347                                 // were forced to keep around because
348                                 // PUI cannot delete its own user data.
349     for (i = 0; i < _info.size(); i++) {
350         delete (GUIInfo *)_info[i];
351         _info[i] = 0;
352     }
353
354                                 // Finally, delete the property links.
355     for (i = 0; i < _propertyObjects.size(); i++) {
356         delete _propertyObjects[i];
357         _propertyObjects[i] = 0;
358     }
359 }
360
361 void
362 FGDialog::updateValue (const char * objectName)
363 {
364     for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
365         const string &name = _propertyObjects[i]->name;
366         if (name == objectName)
367             copy_to_pui(_propertyObjects[i]->node,
368                         _propertyObjects[i]->object);
369     }
370 }
371
372 void
373 FGDialog::applyValue (const char * objectName)
374 {
375     for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
376         if (_propertyObjects[i]->name == objectName)
377             copy_from_pui(_propertyObjects[i]->object,
378                           _propertyObjects[i]->node);
379     }
380 }
381
382 void
383 FGDialog::updateValues ()
384 {
385     for (unsigned int i = 0; i < _propertyObjects.size(); i++)
386         copy_to_pui(_propertyObjects[i]->node, _propertyObjects[i]->object);
387 }
388
389 void
390 FGDialog::applyValues ()
391 {
392     for (unsigned int i = 0; i < _propertyObjects.size(); i++)
393         copy_from_pui(_propertyObjects[i]->object,
394                       _propertyObjects[i]->node);
395 }
396
397 void
398 FGDialog::update ()
399 {
400     for (unsigned int i = 0; i < _liveObjects.size(); i++) {
401         puObject *obj = _liveObjects[i]->object;
402         if (obj->getType() & PUCLASS_INPUT && ((puInput *)obj)->isAcceptingInput())
403             continue;
404
405         copy_to_pui(_liveObjects[i]->node, obj);
406     }
407 }
408
409 void
410 FGDialog::display (SGPropertyNode * props)
411 {
412     if (_object != 0) {
413         SG_LOG(SG_GENERAL, SG_ALERT, "This widget is already active");
414         return;
415     }
416
417     int screenw = globals->get_props()->getIntValue("/sim/startup/xsize");
418     int screenh = globals->get_props()->getIntValue("/sim/startup/ysize");
419
420     bool userx = props->hasValue("x");
421     bool usery = props->hasValue("y");
422     bool userw = props->hasValue("width");
423     bool userh = props->hasValue("height");
424
425     // Let the layout widget work in the same property subtree.
426     LayoutWidget wid(props);
427
428     SGPropertyNode *fontnode = props->getNode("font");
429     if (fontnode) {
430         FGFontCache *fc = _gui->get_fontcache();
431         _font = fc->get(fontnode);
432     } else {
433         _font = _gui->getDefaultFont();
434     }
435     wid.setDefaultFont(_font, int(_font->getPointSize()));
436
437     int pw=0, ph=0;
438     int px, py, savex, savey;
439     if(!userw || !userh)
440         wid.calcPrefSize(&pw, &ph);
441     pw = props->getIntValue("width", pw);
442     ph = props->getIntValue("height", ph);
443     px = savex = props->getIntValue("x", (screenw - pw) / 2);
444     py = savey = props->getIntValue("y", (screenh - ph) / 2);
445
446     // Negative x/y coordinates are interpreted as distance from the top/right
447     // corner rather than bottom/left.
448     if (userx && px < 0)
449         px = screenw - pw + px;
450     if (usery && py < 0)
451         py = screenh - ph + py;
452
453     // Define "x", "y", "width" and/or "height" in the property tree if they
454     // are not specified in the configuration file.
455     wid.layout(px, py, pw, ph);
456
457     // Use the dimension and location properties as specified in the
458     // configuration file or from the layout widget.
459     _object = makeObject(props, screenw, screenh);
460
461     // Remove automatically generated properties, so the layout looks
462     // the same next time around, or restore x and y to preserve negative coords.
463     if(userx)
464         props->setIntValue("x", savex);
465     else
466         props->removeChild("x");
467
468     if(usery)
469         props->setIntValue("y", savey);
470     else
471         props->removeChild("y");
472
473     if(!userw) props->removeChild("width");
474     if(!userh) props->removeChild("height");
475
476     if (_object != 0) {
477         _object->reveal();
478     } else {
479         SG_LOG(SG_GENERAL, SG_ALERT, "Widget "
480                << props->getStringValue("name", "[unnamed]")
481                << " does not contain a proper GUI definition");
482     }
483 }
484
485 puObject *
486 FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
487 {
488     if (props->getBoolValue("hide"))
489         return 0;
490
491     bool presetSize = props->hasValue("width") && props->hasValue("height");
492     int width = props->getIntValue("width", parentWidth);
493     int height = props->getIntValue("height", parentHeight);
494     int x = props->getIntValue("x", (parentWidth - width) / 2);
495     int y = props->getIntValue("y", (parentHeight - height) / 2);
496     string type = props->getName();
497
498     if (type == "")
499         type = "dialog";
500
501     if (type == "dialog") {
502         puPopup * obj;
503         bool draggable = props->getBoolValue("draggable", true);
504         if (props->getBoolValue("modal", false))
505             obj = new puDialogBox(x, y);
506         else
507             obj = new fgPopup(x, y, draggable);
508         setupGroup(obj, props, width, height, true);
509         setColor(obj, props);
510         return obj;
511
512     } else if (type == "group") {
513         puGroup * obj = new puGroup(x, y);
514         setupGroup(obj, props, width, height, false);
515         setColor(obj, props);
516         return obj;
517
518     } else if (type == "frame") {
519         puGroup * obj = new puGroup(x, y);
520         setupGroup(obj, props, width, height, true);
521         setColor(obj, props);
522         return obj;
523
524     } else if (type == "hrule" || type == "vrule") {
525         puFrame * obj = new puFrame(x, y, x + width, y + height);
526         obj->setBorderThickness(0);
527         setColor(obj, props, BACKGROUND|FOREGROUND|HIGHLIGHT);
528         return obj;
529
530     } else if (type == "list") {
531         vector<SGPropertyNode_ptr> value_nodes = props->getChildren("value");
532         char ** entries = make_char_array(value_nodes.size());
533         for (unsigned int i = 0; i < value_nodes.size(); i++)
534             entries[i] = strdup((char *)value_nodes[i]->getStringValue());
535
536         puList * obj = new puList(x, y, x + width, y + height, entries);
537         setupObject(obj, props);
538         setColor(obj, props);
539         return obj;
540
541     } else if (type == "airport-list") {
542         AirportList * obj = new AirportList(x, y, x + width, y + height);
543         setupObject(obj, props);
544         setColor(obj, props);
545         return obj;
546
547     } else if (type == "input") {
548         puInput * obj = new puInput(x, y, x + width, y + height);
549         setupObject(obj, props);
550         setColor(obj, props, FOREGROUND|LABEL);
551         return obj;
552
553     } else if (type == "text") {
554         puText * obj = new puText(x, y);
555         setupObject(obj, props);
556
557         if (props->getNode("format")) {
558             SGPropertyNode *live = props->getNode("live");
559             if (live && live->getBoolValue())
560                 obj->setRenderCallback(format_callback, props);
561             else
562                 format_callback(obj, x, y, props);
563         }
564         // Layed-out objects need their size set, and non-layout ones
565         // get a different placement.
566         if(presetSize) obj->setSize(width, height);
567         else obj->setLabelPlace(PUPLACE_LABEL_DEFAULT);
568         setColor(obj, props, LABEL);
569         return obj;
570
571     } else if (type == "checkbox") {
572         puButton * obj;
573         obj = new puButton(x, y, x + width, y + height, PUBUTTON_XCHECK);
574         setupObject(obj, props);
575         setColor(obj, props, FOREGROUND|LABEL);
576         return obj;
577
578     } else if (type == "radio") {
579         puButton * obj;
580         obj = new puButton(x, y, x + width, y + height, PUBUTTON_CIRCLE);
581         setupObject(obj, props);
582         setColor(obj, props, FOREGROUND|LABEL);
583         return obj;
584
585     } else if (type == "button") {
586         puButton * obj;
587         const char * legend = props->getStringValue("legend", "[none]");
588         if (props->getBoolValue("one-shot", true))
589             obj = new puOneShot(x, y, legend);
590         else
591             obj = new puButton(x, y, legend);
592         if(presetSize)
593             obj->setSize(width, height);
594         setupObject(obj, props);
595         setColor(obj, props);
596         return obj;
597
598     } else if (type == "combo") {
599         vector<SGPropertyNode_ptr> value_nodes = props->getChildren("value");
600         char ** entries = make_char_array(value_nodes.size());
601         for (unsigned int i = 0; i < value_nodes.size(); i++)
602             entries[i] = strdup((char *)value_nodes[i]->getStringValue());
603
604         puComboBox * obj = new puComboBox(x, y, x + width, y + height, entries,
605                            props->getBoolValue("editable", false));
606         setupObject(obj, props);
607 #ifdef PUCOL_EDITFIELD  // plib > 0.8.4
608         setColor(obj, props, EDITFIELD);
609 #else
610         setColor(obj, props, FOREGROUND|LABEL);
611 #endif
612         return obj;
613
614     } else if (type == "slider") {
615         bool vertical = props->getBoolValue("vertical", false);
616         puSlider * obj = new puSlider(x, y, (vertical ? height : width));
617         obj->setMinValue(props->getFloatValue("min", 0.0));
618         obj->setMaxValue(props->getFloatValue("max", 1.0));
619         setupObject(obj, props);
620         if(presetSize)
621             obj->setSize(width, height);
622         setColor(obj, props, FOREGROUND|LABEL);
623         return obj;
624
625     } else if (type == "dial") {
626         puDial * obj = new puDial(x, y, width);
627         obj->setMinValue(props->getFloatValue("min", 0.0));
628         obj->setMaxValue(props->getFloatValue("max", 1.0));
629         obj->setWrap(props->getBoolValue("wrap", true));
630         setupObject(obj, props);
631         setColor(obj, props, FOREGROUND|LABEL);
632         return obj;
633
634     } else if (type == "textbox") {
635         int slider_width = props->getIntValue("slider", parentHeight);
636         int wrap = props->getBoolValue("wrap", true);
637         if (slider_width==0) slider_width=20;
638         puLargeInput * obj = new puLargeInput(x, y,
639                 x+width, x+height, 2, slider_width, wrap);
640
641         if (props->hasValue("editable")) {
642             if (props->getBoolValue("editable")==false)
643                 obj->disableInput();
644             else
645                 obj->enableInput();
646         }
647         setupObject(obj, props);
648         setColor(obj, props, FOREGROUND|LABEL);
649         return obj;
650
651     } else if (type == "select") {
652         vector<SGPropertyNode_ptr> value_nodes;
653         SGPropertyNode * selection_node =
654                 fgGetNode(props->getChild("selection")->getStringValue(), true);
655
656         for (int q = 0; q < selection_node->nChildren(); q++)
657             value_nodes.push_back(selection_node->getChild(q));
658
659         char ** entries = make_char_array(value_nodes.size());
660         for (unsigned int i = 0; i < value_nodes.size(); i++)
661             entries[i] = strdup((char *)value_nodes[i]->getName());
662
663         puSelectBox * obj =
664             new puSelectBox(x, y, x + width, y + height, entries);
665         setupObject(obj, props);
666 #ifdef PUCOL_EDITFIELD  // plib > 0.8.4
667         setColor(obj, props, EDITFIELD);
668 #else
669         setColor(obj, props, FOREGROUND|LABEL);
670 #endif
671         return obj;
672     } else {
673         return 0;
674     }
675 }
676
677 void
678 FGDialog::setupObject (puObject * object, SGPropertyNode * props)
679 {
680     object->setLabelPlace(PUPLACE_CENTERED_RIGHT);
681
682     if (props->hasValue("legend"))
683         object->setLegend(props->getStringValue("legend"));
684
685     if (props->hasValue("label"))
686         object->setLabel(props->getStringValue("label"));
687
688     if (props->hasValue("border"))
689         object->setBorderThickness( props->getIntValue("border", 2) );
690
691     if ( SGPropertyNode *nft = props->getNode("font", false) ) {
692        FGFontCache *fc = _gui->get_fontcache();
693        puFont *lfnt = fc->get(nft);
694        object->setLabelFont(*lfnt);
695     } else {
696        object->setLabelFont(*_font);
697     }
698
699     if (props->hasValue("property")) {
700         const char * name = props->getStringValue("name");
701         if (name == 0)
702             name = "";
703         const char * propname = props->getStringValue("property");
704         SGPropertyNode_ptr node = fgGetNode(propname, true);
705         copy_to_pui(node, object);
706         PropertyObject* po = new PropertyObject(name, object, node);
707         _propertyObjects.push_back(po);
708         if(props->getBoolValue("live"))
709             _liveObjects.push_back(po);
710     }
711
712     SGPropertyNode * dest = fgGetNode("/sim/bindings/gui", true);
713     vector<SGPropertyNode_ptr> bindings = props->getChildren("binding");
714     if (bindings.size() > 0) {
715         GUIInfo * info = new GUIInfo(this);
716         info->key = props->getIntValue("keynum", -1);
717         if (props->hasValue("key"))
718             info->key = getKeyCode(props->getStringValue("key", ""));
719
720         for (unsigned int i = 0; i < bindings.size(); i++) {
721             unsigned int j = 0;
722             SGPropertyNode *binding;
723             while (dest->getChild("binding", j))
724                 j++;
725
726             binding = dest->getChild("binding", j, true);
727             copyProperties(bindings[i], binding);
728             info->bindings.push_back(new FGBinding(binding));
729         }
730         object->setCallback(action_callback);
731         object->setUserData(info);
732         _info.push_back(info);
733     }
734
735     object->makeReturnDefault(props->getBoolValue("default"));
736 }
737
738 void
739 FGDialog::setupGroup (puGroup * group, SGPropertyNode * props,
740                     int width, int height, bool makeFrame)
741 {
742     setupObject(group, props);
743
744     if (makeFrame) {
745         puFrame* f = new puFrame(0, 0, width, height);
746         setColor(f, props);
747     }
748
749     int nChildren = props->nChildren();
750     for (int i = 0; i < nChildren; i++)
751         makeObject(props->getChild(i), width, height);
752     group->close();
753 }
754
755 void
756 FGDialog::setColor(puObject * object, SGPropertyNode * props, int which)
757 {
758     string type = props->getName();
759     if (type == "")
760         type = "dialog";
761     if (type == "textbox" && props->getBoolValue("editable"))
762         type += "-editable";
763
764     FGColor *c = new FGColor(_gui->getColor("background"));
765     c->merge(_gui->getColor(type));
766     c->merge(props->getNode("color"));
767     if (c->isValid())
768         object->setColourScheme(c->red(), c->green(), c->blue(), c->alpha());
769
770     const struct {
771         int mask;
772         int id;
773         const char *name;
774         const char *cname;
775     } pucol[] = {
776         { BACKGROUND, PUCOL_BACKGROUND, "background", "color-background" },
777         { FOREGROUND, PUCOL_FOREGROUND, "foreground", "color-foreground" },
778         { HIGHLIGHT,  PUCOL_HIGHLIGHT,  "highlight",  "color-highlight" },
779         { LABEL,      PUCOL_LABEL,      "label",      "color-label" },
780         { LEGEND,     PUCOL_LEGEND,     "legend",     "color-legend" },
781         { MISC,       PUCOL_MISC,       "misc",       "color-misc" },
782 #ifdef PUCOL_EDITFIELD  // plib > 0.8.4
783         { EDITFIELD,  PUCOL_EDITFIELD,  "editfield",  "color-editfield" },
784 #endif
785     };
786
787     const int numcol = sizeof(pucol) / sizeof(pucol[0]);
788
789     for (int i = 0; i < numcol; i++) {
790         bool dirty = false;
791         c->clear();
792         c->setAlpha(1.0);
793
794         dirty |= c->merge(_gui->getColor(type + '-' + pucol[i].name));
795         if (which & pucol[i].mask)
796             dirty |= c->merge(props->getNode("color"));
797
798         if ((pucol[i].mask == LABEL) && !c->isValid())
799             dirty |= c->merge(_gui->getColor("label"));
800
801         if (c->isValid() && dirty)
802             object->setColor(pucol[i].id, c->red(), c->green(), c->blue(), c->alpha());
803     }
804 }
805
806
807 static struct {
808     const char *name;
809     int key;
810 } keymap[] = {
811     {"tab", 9},
812     {"return", 13},
813     {"enter", 13},
814     {"esc", 27},
815     {"escape", 27},
816     {"space", ' '},
817     {"&amp;", '&'},
818     {"and", '&'},
819     {"&lt;", '<'},
820     {"&gt;", '>'},
821     {"f1", PU_KEY_F1},
822     {"f2", PU_KEY_F2},
823     {"f3", PU_KEY_F3},
824     {"f4", PU_KEY_F4},
825     {"f5", PU_KEY_F5},
826     {"f6", PU_KEY_F6},
827     {"f7", PU_KEY_F7},
828     {"f8", PU_KEY_F8},
829     {"f9", PU_KEY_F9},
830     {"f10", PU_KEY_F10},
831     {"f11", PU_KEY_F11},
832     {"f12", PU_KEY_F12},
833     {"left", PU_KEY_LEFT},
834     {"up", PU_KEY_UP},
835     {"right", PU_KEY_RIGHT},
836     {"down", PU_KEY_DOWN},
837     {"pageup", PU_KEY_PAGE_UP},
838     {"pagedn", PU_KEY_PAGE_DOWN},
839     {"home", PU_KEY_HOME},
840     {"end", PU_KEY_END},
841     {"insert", PU_KEY_INSERT},
842     {0, -1},
843 };
844
845 int
846 FGDialog::getKeyCode(const char *str)
847 {
848     enum {
849         CTRL = 0x1,
850         SHIFT = 0x2,
851         ALT = 0x4,
852     };
853
854     while (*str == ' ')
855         str++;
856
857     char *buf = new char[strlen(str) + 1];
858     strcpy(buf, str);
859     char *s = buf + strlen(buf);
860     while (s > str && s[-1] == ' ')
861         s--;
862     *s = 0;
863     s = buf;
864
865     int mod = 0;
866     while (1) {
867         if (!strncmp(s, "Ctrl-", 5) || !strncmp(s, "CTRL-", 5))
868             s += 5, mod |= CTRL;
869         else if (!strncmp(s, "Shift-", 6) || !strncmp(s, "SHIFT-", 6))
870             s += 6, mod |= SHIFT;
871         else if (!strncmp(s, "Alt-", 4) || !strncmp(s, "ALT-", 4))
872             s += 4, mod |= ALT;
873         else
874             break;
875     }
876
877     int key = -1;
878     if (strlen(s) == 1 && isascii(*s)) {
879         key = *s;
880         if (mod & SHIFT)
881             key = toupper(key);
882         if (mod & CTRL)
883             key = toupper(key) - 64;
884         if (mod & ALT)
885             ;   // Alt not propagated to the gui
886     } else {
887         for (char *t = s; *t; t++)
888             *t = tolower(*t);
889         for (int i = 0; keymap[i].name; i++) {
890             if (!strcmp(s, keymap[i].name)) {
891                 key = keymap[i].key;
892                 break;
893             }
894         }
895     }
896     delete[] buf;
897     return key;
898 }
899
900 char **
901 FGDialog::make_char_array (int size)
902 {
903     char ** list = new char*[size+1];
904     for (int i = 0; i <= size; i++)
905         list[i] = 0;
906     _char_arrays.push_back(list);
907     return list;
908 }
909
910
911 \f
912 ////////////////////////////////////////////////////////////////////////
913 // Implementation of FGDialog::PropertyObject.
914 ////////////////////////////////////////////////////////////////////////
915
916 FGDialog::PropertyObject::PropertyObject (const char * n,
917                                            puObject * o,
918                                            SGPropertyNode_ptr p)
919     : name(n),
920       object(o),
921       node(p)
922 {
923 }
924
925
926 // end of dialog.cxx