]> git.mxchange.org Git - flightgear.git/blob - src/GUI/dialog.cxx
Oops, I didn't test compile the latest code and this is what happens.
[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     char* envp = ::getenv( "FG_FONTS" );
262     if ( envp != NULL ) {
263         _font_path.set( envp );
264     } else {
265         _font_path.set( globals->get_fg_root() );
266         _font_path.append( "Fonts" );
267     }
268
269     display(props);
270 }
271
272 FGDialog::~FGDialog ()
273 {
274     puDeleteObject(_object);
275
276     unsigned int i;
277
278                                 // Delete all the arrays we made
279                                 // and were forced to keep around
280                                 // because PUI won't do its own
281                                 // memory management.
282     for (i = 0; i < _char_arrays.size(); i++) {
283         for (int j = 0; _char_arrays[i][j] != 0; j++)
284             free(_char_arrays[i][j]); // added with strdup
285         delete[] _char_arrays[i];
286     }
287
288                                 // Delete all the info objects we
289                                 // were forced to keep around because
290                                 // PUI cannot delete its own user data.
291     for (i = 0; i < _info.size(); i++) {
292         delete (GUIInfo *)_info[i];
293         _info[i] = 0;
294     }
295
296                                 // Finally, delete the property links.
297     for (i = 0; i < _propertyObjects.size(); i++) {
298         delete _propertyObjects[i];
299         _propertyObjects[i] = 0;
300     }
301 }
302
303 void
304 FGDialog::updateValue (const char * objectName)
305 {
306     for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
307         const string &name = _propertyObjects[i]->name;
308         if (name == objectName)
309             copy_to_pui(_propertyObjects[i]->node,
310                         _propertyObjects[i]->object);
311     }
312 }
313
314 void
315 FGDialog::applyValue (const char * objectName)
316 {
317     for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
318         if (_propertyObjects[i]->name == objectName)
319             copy_from_pui(_propertyObjects[i]->object,
320                           _propertyObjects[i]->node);
321     }
322 }
323
324 void
325 FGDialog::updateValues ()
326 {
327     for (unsigned int i = 0; i < _propertyObjects.size(); i++)
328         copy_to_pui(_propertyObjects[i]->node, _propertyObjects[i]->object);
329 }
330
331 void
332 FGDialog::applyValues ()
333 {
334     for (unsigned int i = 0; i < _propertyObjects.size(); i++)
335         copy_from_pui(_propertyObjects[i]->object,
336                       _propertyObjects[i]->node);
337 }
338
339 void
340 FGDialog::update ()
341 {
342     for (unsigned int i = 0; i < _liveObjects.size(); i++) {
343         puObject *obj = _liveObjects[i]->object;
344         if (obj->getType() & PUCLASS_INPUT && ((puInput *)obj)->isAcceptingInput())
345             continue;
346
347         copy_to_pui(_liveObjects[i]->node, obj);
348     }
349 }
350
351 void
352 FGDialog::display (SGPropertyNode * props)
353 {
354     if (_object != 0) {
355         SG_LOG(SG_GENERAL, SG_ALERT, "This widget is already active");
356         return;
357     }
358
359     int screenw = globals->get_props()->getIntValue("/sim/startup/xsize");
360     int screenh = globals->get_props()->getIntValue("/sim/startup/ysize");
361
362     // bool userx = props->hasValue("x");
363     // bool usery = props->hasValue("y");
364     bool userw = props->hasValue("width");
365     bool userh = props->hasValue("height");
366
367     LayoutWidget wid(props);
368     int pw=0, ph=0;
369     if(!userw || !userh)
370         wid.calcPrefSize(&pw, &ph);
371     pw = props->getIntValue("width", pw);
372     ph = props->getIntValue("height", ph);
373     int px = props->getIntValue("x", (screenw - pw) / 2);
374     int py = props->getIntValue("y", (screenh - ph) / 2);
375     wid.layout(px, py, pw, ph);
376
377     _object = makeObject(props, screenw, screenh);
378
379     // Remove automatically generated properties, so the layout looks
380     // EMH - this isn't needed anymore since the layout remains in
381     //       the poprty tree now.
382     // the same next time around.
383     // if(!userx) props->removeChild("x");
384     // if(!usery) props->removeChild("y");
385     // if(!userw) props->removeChild("width");
386     // if(!userh) props->removeChild("height");
387
388     if (_object != 0) {
389         _object->reveal();
390     } else {
391         SG_LOG(SG_GENERAL, SG_ALERT, "Widget "
392                << props->getStringValue("name", "[unnamed]")
393                << " does not contain a proper GUI definition");
394     }
395 }
396
397 puObject *
398 FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
399 {
400     bool presetSize = props->hasValue("width") && props->hasValue("height");
401     int width = props->getIntValue("width", parentWidth);
402     int height = props->getIntValue("height", parentHeight);
403     int x = props->getIntValue("x", (parentWidth - width) / 2);
404     int y = props->getIntValue("y", (parentHeight - height) / 2);
405
406     sgVec4 color = {0.8, 0.8, 0.9, 0.85};
407     SGPropertyNode *ncs = props->getNode("color", false);
408     if ( ncs ) {
409        color[0] = ncs->getFloatValue("red", 0.8);
410        color[1] = ncs->getFloatValue("green", 0.8);
411        color[2] = ncs->getFloatValue("blue", 0.9);
412        color[3] = ncs->getFloatValue("alpha", 0.85);
413     }
414
415     string type = props->getName();
416     if (type == "")
417         type = "dialog";
418
419     if (type == "dialog") {
420         puPopup * dialog;
421         bool draggable = props->getBoolValue("draggable", true);
422         if (props->getBoolValue("modal", false))
423             dialog = new puDialogBox(x, y);
424         else
425             dialog = new fgPopup(x, y, draggable);
426         setupGroup(dialog, props, width, height, color, true);
427         return dialog;
428     } else if (type == "group") {
429         puGroup * group = new puGroup(x, y);
430         setupGroup(group, props, width, height, color, false);
431         return group;
432     } else if (type == "frame") {
433         puFrame * frame = new puFrame(x, y,  width, height);
434         frame->setBorderThickness(1);
435         frame->setColorScheme(color[0], color[1], color[2], color[3]);
436         puGroup * group = new puGroup(x, y);
437         setupGroup(group, props, width, height, color, false);
438         return group;
439     } else if (type == "list") {
440         puList * list = new puList(x, y, x + width, y + height);
441         setupObject(list, props);
442         return list;
443     } else if (type == "airport-list") {
444         AirportList * list = new AirportList(x, y, x + width, y + height);
445         setupObject(list, props);
446         return list;
447     } else if (type == "input") {
448         puInput * input = new puInput(x, y, x + width, y + height);
449         setupObject(input, props);
450         return input;
451     } else if (type == "text") {
452         puText * text = new puText(x, y);
453         setupObject(text, props);
454
455         if (props->getNode("format")) {
456             SGPropertyNode *live = props->getNode("live");
457             if (live && live->getBoolValue())
458                 text->setRenderCallback(format_callback, props);
459             else
460                 format_callback(text, x, y, props);
461         }
462         // Layed-out objects need their size set, and non-layout ones
463         // get a different placement.
464         if(presetSize) text->setSize(width, height);
465         else text->setLabelPlace(PUPLACE_LABEL_DEFAULT);
466         return text;
467     } else if (type == "checkbox") {
468         puButton * b;
469         b = new puButton(x, y, x + width, y + height, PUBUTTON_XCHECK);
470         b->setColourScheme(.8, .7, .7); // matches "PUI input pink"
471         setupObject(b, props);
472         return b;
473     } else if (type == "radio") {
474         puButton * b;
475         b = new puButton(x, y, x + width, y + height, PUBUTTON_CIRCLE);
476         b->setColourScheme(.8, .7, .7); // matches "PUI input pink"
477         setupObject(b, props);
478         return b;
479     } else if (type == "button") {
480         puButton * b;
481         const char * legend = props->getStringValue("legend", "[none]");
482         if (props->getBoolValue("one-shot", true))
483             b = new puOneShot(x, y, legend);
484         else
485             b = new puButton(x, y, legend);
486         if(presetSize)
487             b->setSize(width, height);
488         setupObject(b, props);
489         return b;
490     } else if (type == "combo") {
491         vector<SGPropertyNode_ptr> value_nodes = props->getChildren("value");
492         char ** entries = make_char_array(value_nodes.size());
493         for (unsigned int i = 0, j = value_nodes.size() - 1;
494              i < value_nodes.size();
495              i++, j--)
496             entries[i] = strdup((char *)value_nodes[i]->getStringValue());
497         puComboBox * combo =
498             new puComboBox(x, y, x + width, y + height, entries,
499                            props->getBoolValue("editable", false));
500         setupObject(combo, props);
501         return combo;
502     } else if (type == "slider") {
503         bool vertical = props->getBoolValue("vertical", false);
504         puSlider * slider = new puSlider(x, y, (vertical ? height : width));
505         slider->setMinValue(props->getFloatValue("min", 0.0));
506         slider->setMaxValue(props->getFloatValue("max", 1.0));
507         setupObject(slider, props);
508         if(presetSize)
509             slider->setSize(width, height);
510         return slider;
511     } else if (type == "dial") {
512         puDial * dial = new puDial(x, y, width);
513         dial->setMinValue(props->getFloatValue("min", 0.0));
514         dial->setMaxValue(props->getFloatValue("max", 1.0));
515         dial->setWrap(props->getBoolValue("wrap", true));
516         setupObject(dial, props);
517         return dial;
518     } else if (type == "textbox") {
519        int slider_width = props->getIntValue("slider", parentHeight);
520        int wrap = props->getBoolValue("wrap", true)==true;
521        if (slider_width==0) slider_width=20;
522        puLargeInput * puTextBox =
523                      new puLargeInput(x, y, x+width, x+height, 2, slider_width, wrap);
524        if  (props->hasValue("editable"))
525        {
526           if (props->getBoolValue("editable")==false)
527              puTextBox->disableInput();
528           else
529              puTextBox->enableInput();
530        }
531        setupObject(puTextBox,props);
532        return puTextBox;
533     } else if (type == "select") {
534         vector<SGPropertyNode_ptr> value_nodes;
535         SGPropertyNode * selection_node =
536                 fgGetNode(props->getChild("selection")->getStringValue(), true);
537
538         for (int q = 0; q < selection_node->nChildren(); q++)
539             value_nodes.push_back(selection_node->getChild(q));
540
541         char ** entries = make_char_array(value_nodes.size());
542         for (unsigned int i = 0, j = value_nodes.size() - 1;
543              i < value_nodes.size();
544              i++, j--)
545             entries[i] = strdup((char *)value_nodes[i]->getName());
546         puSelectBox * select =
547             new puSelectBox(x, y, x + width, y + height, entries);
548         setupObject(select, props);
549         return select;
550     } else {
551         return 0;
552     }
553 }
554
555 void
556 FGDialog::setupObject (puObject * object, SGPropertyNode * props)
557 {
558     object->setLabelPlace(PUPLACE_CENTERED_RIGHT);
559
560     if (props->hasValue("legend"))
561         object->setLegend(props->getStringValue("legend"));
562
563     if (props->hasValue("label"))
564         object->setLabel(props->getStringValue("label"));
565
566     if (props->hasValue("border"))
567         object->setBorderThickness( props->getIntValue("border", 2) );
568
569     if ( SGPropertyNode *nft = props->getNode("font", false) ) {
570        SGPath path( _font_path );
571        const char *name = nft->getStringValue("name", "default");
572        float size = nft->getFloatValue("size", 13.0);
573        float slant = nft->getFloatValue("slant", 0.0);
574        path.append( name );
575        path.concat( ".txf" );
576
577        fntFont *font = new fntTexFont;
578        font->load( (char *)path.c_str() );
579
580        puFont lfnt(font, size, slant);
581        object->setLabelFont( lfnt );
582     }
583
584     if ( SGPropertyNode *ncs = props->getNode("color", false) ) {
585        sgVec4 color;
586        color[0] = ncs->getFloatValue("red", 0.0);
587        color[1] = ncs->getFloatValue("green", 0.0);
588        color[2] = ncs->getFloatValue("blue", 0.0);
589        color[3] = ncs->getFloatValue("alpha", 1.0);
590        object->setColor(PUCOL_LABEL, color[0], color[1], color[2], color[3]);
591     }
592
593     if (props->hasValue("property")) {
594         const char * name = props->getStringValue("name");
595         if (name == 0)
596             name = "";
597         const char * propname = props->getStringValue("property");
598         SGPropertyNode_ptr node = fgGetNode(propname, true);
599         copy_to_pui(node, object);
600         PropertyObject* po = new PropertyObject(name, object, node);
601         _propertyObjects.push_back(po);
602         if(props->getBoolValue("live"))
603             _liveObjects.push_back(po);
604     }
605
606     vector<SGPropertyNode_ptr> nodes = props->getChildren("binding");
607     if (nodes.size() > 0) {
608         GUIInfo * info = new GUIInfo(this);
609
610         for (unsigned int i = 0; i < nodes.size(); i++)
611             info->bindings.push_back(new FGBinding(nodes[i]));
612         object->setCallback(action_callback);
613         object->setUserData(info);
614         _info.push_back(info);
615     }
616
617     object->makeReturnDefault(props->getBoolValue("default"));
618 }
619
620 void
621 FGDialog::setupGroup (puGroup * group, SGPropertyNode * props,
622                     int width, int height, sgVec4 color, bool makeFrame)
623 {
624     setupObject(group, props);
625
626     if (makeFrame) {
627         puFrame* f = new puFrame(0, 0, width, height);
628         f->setColorScheme(color[0], color[1], color[2], color[3]);
629     }
630
631     int nChildren = props->nChildren();
632     for (int i = 0; i < nChildren; i++)
633         makeObject(props->getChild(i), width, height);
634     group->close();
635 }
636
637 char **
638 FGDialog::make_char_array (int size)
639 {
640     char ** list = new char*[size+1];
641     for (int i = 0; i <= size; i++)
642         list[i] = 0;
643     _char_arrays.push_back(list);
644     return list;
645 }
646
647
648 \f
649 ////////////////////////////////////////////////////////////////////////
650 // Implementation of FGDialog::PropertyObject.
651 ////////////////////////////////////////////////////////////////////////
652
653 FGDialog::PropertyObject::PropertyObject (const char * n,
654                                            puObject * o,
655                                            SGPropertyNode_ptr p)
656     : name(n),
657       object(o),
658       node(p)
659 {
660 }
661
662
663 // end of dialog.cxx