]> git.mxchange.org Git - flightgear.git/blob - src/GUI/dialog.cxx
Allow for defining the label color.
[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 int fgPopup::checkHit(int button, int updown, int x, int y)
15 {
16     int result = puPopup::checkHit(button, updown, x, y);
17
18     if ( !_draggable)
19        return result;
20
21     // This is annoying.  We would really want a true result from the
22     // superclass to indicate "handled by child object", but all it
23     // tells us is that the pointer is inside the dialog.  So do the
24     // intersection test (again) to make sure we don't start a drag
25     // when inside controls.
26
27     if(updown == PU_DOWN && !_dragging) {
28         if(!result)
29             return 0;
30
31         int hit = getHitObjects(this, x, y);
32         if(hit & (PUCLASS_BUTTON|PUCLASS_ONESHOT|PUCLASS_INPUT))
33             return result;
34
35         int px, py;
36         getPosition(&px, &py);
37         _dragging = true;
38         _dX = px - x;
39         _dY = py - y;
40     } else if(updown == PU_DRAG && _dragging) {
41         setPosition(x + _dX, y + _dY);
42     } else {
43         _dragging = false;
44     }
45     return result;
46 }
47
48 int fgPopup::getHitObjects(puObject *object, int x, int y)
49 {
50     int type = 0;
51     if(object->getType() & PUCLASS_GROUP)
52         for (puObject *obj = ((puGroup *)object)->getFirstChild();
53                 obj; obj = obj->getNextObject())
54             type |= getHitObjects(obj, x, y);
55
56     int cx, cy, cw, ch;
57     object->getAbsolutePosition(&cx, &cy);
58     object->getSize(&cw, &ch);
59     if(x >= cx && x < cx + cw && y >= cy && y < cy + ch)
60         type |= object->getType();
61     return type;
62 }
63
64
65 \f
66 ////////////////////////////////////////////////////////////////////////
67 // Callbacks.
68 ////////////////////////////////////////////////////////////////////////
69
70 /**
71  * User data for a GUI object.
72  */
73 struct GUIInfo
74 {
75     GUIInfo (FGDialog * d);
76     virtual ~GUIInfo ();
77
78     FGDialog * dialog;
79     vector <FGBinding *> bindings;
80 };
81
82
83 /**
84  * Action callback.
85  */
86 static void
87 action_callback (puObject * object)
88 {
89     GUIInfo * info = (GUIInfo *)object->getUserData();
90     NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
91     gui->setActiveDialog(info->dialog);
92     int nBindings = info->bindings.size();
93     for (int i = 0; i < nBindings; i++) {
94         info->bindings[i]->fire();
95         if (gui->getActiveDialog() == 0)
96             break;
97     }
98     gui->setActiveDialog(0);
99 }
100
101
102 static void
103 format_callback(puObject *obj, int dx, int dy, void *n)
104 {
105     SGPropertyNode *node = (SGPropertyNode *)n;
106     const char *format = node->getStringValue("format"), *f = format;
107     bool number, l = false;
108     // make sure the format matches '[ -+#]?\d*(\.\d*)?l?[fs]'
109     for (; *f; f++) {
110         if (*f == '%') {
111             if (f[1] == '%')
112                 f++;
113             else
114                 break;
115         }
116     }
117     if (*f++ != '%')
118         return;
119     if (*f == ' ' || *f == '+' || *f == '-' || *f == '#')
120         f++;
121     while (*f && isdigit(*f))
122         f++;
123     if (*f == '.') {
124         f++;
125         while (*f && isdigit(*f))
126             f++;
127     }
128     if (*f == 'l')
129         l = true, f++;
130
131     if (*f == 'f')
132         number = true;
133     else if (*f == 's') {
134         if (l)
135             return;
136         number = false;
137     } else
138         return;
139
140     for (++f; *f; f++) {
141         if (*f == '%') {
142             if (f[1] == '%')
143                 f++;
144             else
145                 return;
146         }
147     }
148
149     char buf[256];
150     const char *src = obj->getLabel();
151
152     if (number) {
153         float value = atof(src);
154         snprintf(buf, 256, format, value);
155     } else {
156         snprintf(buf, 256, format, src);
157     }
158
159     buf[255] = '\0';
160
161     SGPropertyNode *result = node->getNode("formatted", true);
162     result->setStringValue(buf);
163     obj->setLabel(result->getStringValue());
164 }
165
166
167 \f
168 ////////////////////////////////////////////////////////////////////////
169 // Static helper functions.
170 ////////////////////////////////////////////////////////////////////////
171
172 /**
173  * Copy a property value to a PUI object.
174  */
175 static void
176 copy_to_pui (SGPropertyNode * node, puObject * object)
177 {
178     // Treat puText objects specially, so their "values" can be set
179     // from properties.
180     if(object->getType() & PUCLASS_TEXT) {
181         object->setLabel(node->getStringValue());
182         return;
183     }
184
185     switch (node->getType()) {
186     case SGPropertyNode::BOOL:
187     case SGPropertyNode::INT:
188     case SGPropertyNode::LONG:
189         object->setValue(node->getIntValue());
190         break;
191     case SGPropertyNode::FLOAT:
192     case SGPropertyNode::DOUBLE:
193         object->setValue(node->getFloatValue());
194         break;
195     default:
196         object->setValue(node->getStringValue());
197         break;
198     }
199 }
200
201
202 static void
203 copy_from_pui (puObject * object, SGPropertyNode * node)
204 {
205     // puText objects are immutable, so should not be copied out
206     if(object->getType() & PUCLASS_TEXT)
207         return;
208
209     switch (node->getType()) {
210     case SGPropertyNode::BOOL:
211     case SGPropertyNode::INT:
212     case SGPropertyNode::LONG:
213         node->setIntValue(object->getIntegerValue());
214         break;
215     case SGPropertyNode::FLOAT:
216     case SGPropertyNode::DOUBLE:
217         node->setFloatValue(object->getFloatValue());
218         break;
219     default:
220         // Special case to handle lists, as getStringValue cannot be overridden
221         if(object->getType() & PUCLASS_LIST)
222         {
223             node->setStringValue(((puList *) object)->getListStringValue());
224         }
225         else
226         {
227             node->setStringValue(object->getStringValue());
228         }
229         break;
230     }
231 }
232
233
234 \f
235 ////////////////////////////////////////////////////////////////////////
236 // Implementation of GUIInfo.
237 ////////////////////////////////////////////////////////////////////////
238
239 GUIInfo::GUIInfo (FGDialog * d)
240     : dialog(d)
241 {
242 }
243
244 GUIInfo::~GUIInfo ()
245 {
246     for (unsigned int i = 0; i < bindings.size(); i++) {
247         delete bindings[i];
248         bindings[i] = 0;
249     }
250 }
251
252
253 \f
254 ////////////////////////////////////////////////////////////////////////
255 // Implementation of FGDialog.
256 ////////////////////////////////////////////////////////////////////////
257
258 FGDialog::FGDialog (SGPropertyNode * props)
259     : _object(0)
260 {
261     display(props);
262 }
263
264 FGDialog::~FGDialog ()
265 {
266     puDeleteObject(_object);
267
268     unsigned int i;
269
270                                 // Delete all the arrays we made
271                                 // and were forced to keep around
272                                 // because PUI won't do its own
273                                 // memory management.
274     for (i = 0; i < _char_arrays.size(); i++) {
275         for (int j = 0; _char_arrays[i][j] != 0; j++)
276             free(_char_arrays[i][j]); // added with strdup
277         delete[] _char_arrays[i];
278     }
279
280                                 // Delete all the info objects we
281                                 // were forced to keep around because
282                                 // PUI cannot delete its own user data.
283     for (i = 0; i < _info.size(); i++) {
284         delete (GUIInfo *)_info[i];
285         _info[i] = 0;
286     }
287
288                                 // Finally, delete the property links.
289     for (i = 0; i < _propertyObjects.size(); i++) {
290         delete _propertyObjects[i];
291         _propertyObjects[i] = 0;
292     }
293 }
294
295 void
296 FGDialog::updateValue (const char * objectName)
297 {
298     for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
299         const string &name = _propertyObjects[i]->name;
300         if (name == objectName)
301             copy_to_pui(_propertyObjects[i]->node,
302                         _propertyObjects[i]->object);
303     }
304 }
305
306 void
307 FGDialog::applyValue (const char * objectName)
308 {
309     for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
310         if (_propertyObjects[i]->name == objectName)
311             copy_from_pui(_propertyObjects[i]->object,
312                           _propertyObjects[i]->node);
313     }
314 }
315
316 void
317 FGDialog::updateValues ()
318 {
319     for (unsigned int i = 0; i < _propertyObjects.size(); i++)
320         copy_to_pui(_propertyObjects[i]->node, _propertyObjects[i]->object);
321 }
322
323 void
324 FGDialog::applyValues ()
325 {
326     for (unsigned int i = 0; i < _propertyObjects.size(); i++)
327         copy_from_pui(_propertyObjects[i]->object,
328                       _propertyObjects[i]->node);
329 }
330
331 void
332 FGDialog::update ()
333 {
334     for (unsigned int i = 0; i < _liveObjects.size(); i++) {
335         puObject *obj = _liveObjects[i]->object;
336         if (obj->getType() & PUCLASS_INPUT && ((puInput *)obj)->isAcceptingInput())
337             continue;
338
339         copy_to_pui(_liveObjects[i]->node, obj);
340     }
341 }
342
343 void
344 FGDialog::display (SGPropertyNode * props)
345 {
346     if (_object != 0) {
347         SG_LOG(SG_GENERAL, SG_ALERT, "This widget is already active");
348         return;
349     }
350
351     int screenw = globals->get_props()->getIntValue("/sim/startup/xsize");
352     int screenh = globals->get_props()->getIntValue("/sim/startup/ysize");
353
354     bool userx = props->hasValue("x");
355     bool usery = props->hasValue("y");
356     bool userw = props->hasValue("width");
357     bool userh = props->hasValue("height");
358
359     LayoutWidget wid(props);
360     int pw=0, ph=0;
361     if(!userw || !userh)
362         wid.calcPrefSize(&pw, &ph);
363     pw = props->getIntValue("width", pw);
364     ph = props->getIntValue("height", ph);
365     int px = props->getIntValue("x", (screenw - pw) / 2);
366     int py = props->getIntValue("y", (screenh - ph) / 2);
367     wid.layout(px, py, pw, ph);
368
369     _object = makeObject(props, screenw, screenh);
370
371     // Remove automatically generated properties, so the layout looks
372     // the same next time around.
373     if(!userx) props->removeChild("x");
374     if(!usery) props->removeChild("y");
375     if(!userw) props->removeChild("width");
376     if(!userh) props->removeChild("height");
377
378     if (_object != 0) {
379         _object->reveal();
380     } else {
381         SG_LOG(SG_GENERAL, SG_ALERT, "Widget "
382                << props->getStringValue("name", "[unnamed]")
383                << " does not contain a proper GUI definition");
384     }
385 }
386
387 puObject *
388 FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
389 {
390     bool presetSize = props->hasValue("width") && props->hasValue("height");
391     int width = props->getIntValue("width", parentWidth);
392     int height = props->getIntValue("height", parentHeight);
393     int x = props->getIntValue("x", (parentWidth - width) / 2);
394     int y = props->getIntValue("y", (parentHeight - height) / 2);
395
396     sgVec4 color = {0.8, 0.8, 0.9, 0.85};
397     SGPropertyNode *ncs = props->getNode("color", false);
398     if ( ncs ) {
399        color[0] = ncs->getFloatValue("red", 0.8);
400        color[1] = ncs->getFloatValue("green", 0.8);
401        color[2] = ncs->getFloatValue("blue", 0.9);
402        color[3] = ncs->getFloatValue("alpha", 0.85);
403     }
404
405     string type = props->getName();
406     if (type == "")
407         type = "dialog";
408
409     if (type == "dialog") {
410         puPopup * dialog;
411         bool draggable = props->getBoolValue("draggable", true);
412         if (props->getBoolValue("modal", false))
413             dialog = new puDialogBox(x, y);
414         else
415             dialog = new fgPopup(x, y, draggable);
416         setupGroup(dialog, props, width, height, color, true);
417         return dialog;
418     } else if (type == "group") {
419         puGroup * group = new puGroup(x, y);
420         setupGroup(group, props, width, height, color, false);
421         return group;
422     } else if (type == "list") {
423         puList * list = new puList(x, y, x + width, y + height);
424         setupObject(list, props);
425         return list;
426     } else if (type == "airport-list") {
427         AirportList * list = new AirportList(x, y, x + width, y + height);
428         setupObject(list, props);
429         return list;
430     } else if (type == "input") {
431         puInput * input = new puInput(x, y, x + width, y + height);
432         setupObject(input, props);
433         return input;
434     } else if (type == "text") {
435         puText * text = new puText(x, y);
436         setupObject(text, props);
437
438         if (props->getNode("format")) {
439             SGPropertyNode *live = props->getNode("live");
440             if (live && live->getBoolValue())
441                 text->setRenderCallback(format_callback, props);
442             else
443                 format_callback(text, x, y, props);
444         }
445         // Layed-out objects need their size set, and non-layout ones
446         // get a different placement.
447         if(presetSize) text->setSize(width, height);
448         else text->setLabelPlace(PUPLACE_LABEL_DEFAULT);
449         return text;
450     } else if (type == "checkbox") {
451         puButton * b;
452         b = new puButton(x, y, x + width, y + height, PUBUTTON_XCHECK);
453         b->setColourScheme(.8, .7, .7); // matches "PUI input pink"
454         setupObject(b, props);
455         return b;
456     } else if (type == "radio") {
457         puButton * b;
458         b = new puButton(x, y, x + width, y + height, PUBUTTON_CIRCLE);
459         b->setColourScheme(.8, .7, .7); // matches "PUI input pink"
460         setupObject(b, props);
461         return b;
462     } else if (type == "button") {
463         puButton * b;
464         const char * legend = props->getStringValue("legend", "[none]");
465         if (props->getBoolValue("one-shot", true))
466             b = new puOneShot(x, y, legend);
467         else
468             b = new puButton(x, y, legend);
469         if(presetSize)
470             b->setSize(width, height);
471         setupObject(b, props);
472         return b;
473     } else if (type == "combo") {
474         vector<SGPropertyNode_ptr> value_nodes = props->getChildren("value");
475         char ** entries = make_char_array(value_nodes.size());
476         for (unsigned int i = 0, j = value_nodes.size() - 1;
477              i < value_nodes.size();
478              i++, j--)
479             entries[i] = strdup((char *)value_nodes[i]->getStringValue());
480         puComboBox * combo =
481             new puComboBox(x, y, x + width, y + height, entries,
482                            props->getBoolValue("editable", false));
483         setupObject(combo, props);
484         return combo;
485     } else if (type == "slider") {
486         bool vertical = props->getBoolValue("vertical", false);
487         puSlider * slider = new puSlider(x, y, (vertical ? height : width));
488         slider->setMinValue(props->getFloatValue("min", 0.0));
489         slider->setMaxValue(props->getFloatValue("max", 1.0));
490         setupObject(slider, props);
491         if(presetSize)
492             slider->setSize(width, height);
493         return slider;
494     } else if (type == "dial") {
495         puDial * dial = new puDial(x, y, width);
496         dial->setMinValue(props->getFloatValue("min", 0.0));
497         dial->setMaxValue(props->getFloatValue("max", 1.0));
498         dial->setWrap(props->getBoolValue("wrap", true));
499         setupObject(dial, props);
500         return dial;
501     } else if (type == "textbox") {
502        int slider_width = props->getIntValue("slider", parentHeight);
503        int wrap = props->getBoolValue("wrap", true)==true;
504        if (slider_width==0) slider_width=20;
505        puLargeInput * puTextBox =
506                      new puLargeInput(x, y, x+width, x+height, 2, slider_width, wrap);
507        if  (props->hasValue("editable"))
508        {
509           if (props->getBoolValue("editable")==false)
510              puTextBox->disableInput();
511           else
512              puTextBox->enableInput();
513        }
514        setupObject(puTextBox,props);
515        return puTextBox;
516     } else if (type == "select") {
517         vector<SGPropertyNode_ptr> value_nodes;
518         SGPropertyNode * selection_node =
519                 fgGetNode(props->getChild("selection")->getStringValue(), true);
520
521         for (int q = 0; q < selection_node->nChildren(); q++)
522             value_nodes.push_back(selection_node->getChild(q));
523
524         char ** entries = make_char_array(value_nodes.size());
525         for (unsigned int i = 0, j = value_nodes.size() - 1;
526              i < value_nodes.size();
527              i++, j--)
528             entries[i] = strdup((char *)value_nodes[i]->getName());
529         puSelectBox * select =
530             new puSelectBox(x, y, x + width, y + height, entries);
531         setupObject(select, props);
532         return select;
533     } else {
534         return 0;
535     }
536 }
537
538 void
539 FGDialog::setupObject (puObject * object, SGPropertyNode * props)
540 {
541     object->setLabelPlace(PUPLACE_CENTERED_RIGHT);
542
543     if (props->hasValue("legend"))
544         object->setLegend(props->getStringValue("legend"));
545
546     if (props->hasValue("label"))
547         object->setLabel(props->getStringValue("label"));
548
549 #if 0
550     if (props->hasValue("style")) {
551        int style = PUSTYLE_DEFAULT;
552        string s = props->getStringValue("style");
553        if (s == "underline")            style = PUSTYLE_SPECIAL_UNDERLINED;
554        else if (s == "small-bevelled")  style = PUSTYLE_SMALL_BEVELLED;
555        else if (s == "small-shaded")    style = PUSTYLE_SMALL_SHADED;
556        else if (s == "boxed")           style = PUSTYLE_BOXED;
557        else if (s == "bevelled")        style = PUSTYLE_BEVELLED;
558        else if (s == "shaded")          style = PUSTYLE_SHADED;
559        else if (s == "dropshadow")      style = PUSTYLE_DROPSHADOW;
560        object->setStyle(style);
561     }
562 #endif
563
564     if ( SGPropertyNode *ncs = props->getNode("color", false) ) {
565        sgVec4 color;
566        color[0] = ncs->getFloatValue("red", 0.0);
567        color[1] = ncs->getFloatValue("green", 0.0);
568        color[2] = ncs->getFloatValue("blue", 0.0);
569        color[3] = ncs->getFloatValue("alpha", 1.0);
570        object->setColor(PUCOL_LABEL, color[0], color[1], color[2], color[3]);
571     }
572
573     if (props->hasValue("property")) {
574         const char * name = props->getStringValue("name");
575         if (name == 0)
576             name = "";
577         const char * propname = props->getStringValue("property");
578         SGPropertyNode_ptr node = fgGetNode(propname, true);
579         copy_to_pui(node, object);
580         PropertyObject* po = new PropertyObject(name, object, node);
581         _propertyObjects.push_back(po);
582         if(props->getBoolValue("live"))
583             _liveObjects.push_back(po);
584     }
585
586     vector<SGPropertyNode_ptr> nodes = props->getChildren("binding");
587     if (nodes.size() > 0) {
588         GUIInfo * info = new GUIInfo(this);
589
590         for (unsigned int i = 0; i < nodes.size(); i++)
591             info->bindings.push_back(new FGBinding(nodes[i]));
592         object->setCallback(action_callback);
593         object->setUserData(info);
594         _info.push_back(info);
595     }
596
597     object->makeReturnDefault(props->getBoolValue("default"));
598 }
599
600 void
601 FGDialog::setupGroup (puGroup * group, SGPropertyNode * props,
602                     int width, int height, sgVec4 color, bool makeFrame)
603 {
604     setupObject(group, props);
605
606     if (makeFrame) {
607         puFrame* f = new puFrame(0, 0, width, height);
608         f->setColorScheme(color[0], color[1], color[2], color[3]);
609     }
610
611     int nChildren = props->nChildren();
612     for (int i = 0; i < nChildren; i++)
613         makeObject(props->getChild(i), width, height);
614     group->close();
615 }
616
617 char **
618 FGDialog::make_char_array (int size)
619 {
620     char ** list = new char*[size+1];
621     for (int i = 0; i <= size; i++)
622         list[i] = 0;
623     _char_arrays.push_back(list);
624     return list;
625 }
626
627
628 \f
629 ////////////////////////////////////////////////////////////////////////
630 // Implementation of FGDialog::PropertyObject.
631 ////////////////////////////////////////////////////////////////////////
632
633 FGDialog::PropertyObject::PropertyObject (const char * n,
634                                            puObject * o,
635                                            SGPropertyNode_ptr p)
636     : name(n),
637       object(o),
638       node(p)
639 {
640 }
641
642
643 // end of dialog.cxx