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