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