]> 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];
145     const char *src = obj->getLabel();
146
147     if (number) {
148         float value = atof(src);
149         snprintf(buf, 256, format, value);
150     } else {
151         snprintf(buf, 256, format, src);
152     }
153
154     buf[255] = '\0';
155
156     SGPropertyNode *result = node->getNode("formatted", true);
157     result->setStringValue(buf);
158     obj->setLabel(result->getStringValue());
159 }
160
161
162 \f
163 ////////////////////////////////////////////////////////////////////////
164 // Static helper functions.
165 ////////////////////////////////////////////////////////////////////////
166
167 /**
168  * Copy a property value to a PUI object.
169  */
170 static void
171 copy_to_pui (SGPropertyNode * node, puObject * object)
172 {
173     // Treat puText objects specially, so their "values" can be set
174     // from properties.
175     if(object->getType() & PUCLASS_TEXT) {
176         object->setLabel(node->getStringValue());
177         return;
178     }
179
180     switch (node->getType()) {
181     case SGPropertyNode::BOOL:
182     case SGPropertyNode::INT:
183     case SGPropertyNode::LONG:
184         object->setValue(node->getIntValue());
185         break;
186     case SGPropertyNode::FLOAT:
187     case SGPropertyNode::DOUBLE:
188         object->setValue(node->getFloatValue());
189         break;
190     default:
191         object->setValue(node->getStringValue());
192         break;
193     }
194 }
195
196
197 static void
198 copy_from_pui (puObject * object, SGPropertyNode * node)
199 {
200     // puText objects are immutable, so should not be copied out
201     if(object->getType() & PUCLASS_TEXT)
202         return;
203
204     switch (node->getType()) {
205     case SGPropertyNode::BOOL:
206     case SGPropertyNode::INT:
207     case SGPropertyNode::LONG:
208         node->setIntValue(object->getIntegerValue());
209         break;
210     case SGPropertyNode::FLOAT:
211     case SGPropertyNode::DOUBLE:
212         node->setFloatValue(object->getFloatValue());
213         break;
214     default:
215         // Special case to handle lists, as getStringValue cannot be overridden
216         if(object->getType() & PUCLASS_LIST)
217         {
218             node->setStringValue(((puList *) object)->getListStringValue());
219         }
220         else
221         {
222             node->setStringValue(object->getStringValue());
223         }
224         break;
225     }
226 }
227
228
229 \f
230 ////////////////////////////////////////////////////////////////////////
231 // Implementation of GUIInfo.
232 ////////////////////////////////////////////////////////////////////////
233
234 GUIInfo::GUIInfo (FGDialog * d)
235     : dialog(d)
236 {
237 }
238
239 GUIInfo::~GUIInfo ()
240 {
241     for (unsigned int i = 0; i < bindings.size(); i++) {
242         delete bindings[i];
243         bindings[i] = 0;
244     }
245 }
246
247
248 \f
249 ////////////////////////////////////////////////////////////////////////
250 // Implementation of FGDialog.
251 ////////////////////////////////////////////////////////////////////////
252
253 FGDialog::FGDialog (SGPropertyNode * props)
254     : _object(0)
255 {
256     display(props);
257 }
258
259 FGDialog::~FGDialog ()
260 {
261     puDeleteObject(_object);
262
263     unsigned int i;
264
265                                 // Delete all the arrays we made
266                                 // and were forced to keep around
267                                 // because PUI won't do its own
268                                 // memory management.
269     for (i = 0; i < _char_arrays.size(); i++) {
270         for (int j = 0; _char_arrays[i][j] != 0; j++)
271             free(_char_arrays[i][j]); // added with strdup
272         delete[] _char_arrays[i];
273     }
274
275                                 // Delete all the info objects we
276                                 // were forced to keep around because
277                                 // PUI cannot delete its own user data.
278     for (i = 0; i < _info.size(); i++) {
279         delete (GUIInfo *)_info[i];
280         _info[i] = 0;
281     }
282
283                                 // Finally, delete the property links.
284     for (i = 0; i < _propertyObjects.size(); i++) {
285         delete _propertyObjects[i];
286         _propertyObjects[i] = 0;
287     }
288 }
289
290 void
291 FGDialog::updateValue (const char * objectName)
292 {
293     for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
294         const string &name = _propertyObjects[i]->name;
295         if (name == objectName)
296             copy_to_pui(_propertyObjects[i]->node,
297                         _propertyObjects[i]->object);
298     }
299 }
300
301 void
302 FGDialog::applyValue (const char * objectName)
303 {
304     for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
305         if (_propertyObjects[i]->name == objectName)
306             copy_from_pui(_propertyObjects[i]->object,
307                           _propertyObjects[i]->node);
308     }
309 }
310
311 void
312 FGDialog::updateValues ()
313 {
314     for (unsigned int i = 0; i < _propertyObjects.size(); i++)
315         copy_to_pui(_propertyObjects[i]->node, _propertyObjects[i]->object);
316 }
317
318 void
319 FGDialog::applyValues ()
320 {
321     for (unsigned int i = 0; i < _propertyObjects.size(); i++)
322         copy_from_pui(_propertyObjects[i]->object,
323                       _propertyObjects[i]->node);
324 }
325
326 void
327 FGDialog::update ()
328 {
329     for (unsigned int i = 0; i < _liveObjects.size(); i++) {
330         puObject *obj = _liveObjects[i]->object;
331         if (obj->getType() & PUCLASS_INPUT && ((puInput *)obj)->isAcceptingInput())
332             continue;
333
334         copy_to_pui(_liveObjects[i]->node, obj);
335     }
336 }
337
338 void
339 FGDialog::display (SGPropertyNode * props)
340 {
341     if (_object != 0) {
342         SG_LOG(SG_GENERAL, SG_ALERT, "This widget is already active");
343         return;
344     }
345
346     int screenw = globals->get_props()->getIntValue("/sim/startup/xsize");
347     int screenh = globals->get_props()->getIntValue("/sim/startup/ysize");
348
349     bool userx = props->hasValue("x");
350     bool usery = props->hasValue("y");
351     bool userw = props->hasValue("width");
352     bool userh = props->hasValue("height");
353
354     LayoutWidget wid(props);
355     int pw=0, ph=0;
356     if(!userw || !userh)
357         wid.calcPrefSize(&pw, &ph);
358     pw = props->getIntValue("width", pw);
359     ph = props->getIntValue("height", ph);
360     int px = props->getIntValue("x", (screenw - pw) / 2);
361     int py = props->getIntValue("y", (screenh - ph) / 2);
362     wid.layout(px, py, pw, ph);
363
364     _object = makeObject(props, screenw, screenh);
365
366     // Remove automatically generated properties, so the layout looks
367     // the same next time around.
368     if(!userx) props->removeChild("x");
369     if(!usery) props->removeChild("y");
370     if(!userw) props->removeChild("width");
371     if(!userh) props->removeChild("height");
372
373     if (_object != 0) {
374         _object->reveal();
375     } else {
376         SG_LOG(SG_GENERAL, SG_ALERT, "Widget "
377                << props->getStringValue("name", "[unnamed]")
378                << " does not contain a proper GUI definition");
379     }
380 }
381
382 puObject *
383 FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
384 {
385     bool presetSize = props->hasValue("width") && props->hasValue("height");
386     int width = props->getIntValue("width", parentWidth);
387     int height = props->getIntValue("height", parentHeight);
388     int x = props->getIntValue("x", (parentWidth - width) / 2);
389     int y = props->getIntValue("y", (parentHeight - height) / 2);
390
391     string type = props->getName();
392     if (type == "")
393         type = "dialog";
394
395     if (type == "dialog") {
396         puPopup * dialog;
397         if (props->getBoolValue("modal", false))
398             dialog = new puDialogBox(x, y);
399         else
400             dialog = new fgPopup(x, y);
401         setupGroup(dialog, props, width, height, true);
402         return dialog;
403     } else if (type == "group") {
404         puGroup * group = new puGroup(x, y);
405         setupGroup(group, props, width, height, false);
406         return group;
407     } else if (type == "list") {
408         puList * list = new puList(x, y, x + width, y + height);
409         setupObject(list, props);
410         return list;
411     } else if (type == "airport-list") {
412         AirportList * list = new AirportList(x, y, x + width, y + height);
413         setupObject(list, props);
414         return list;
415     } else if (type == "input") {
416         puInput * input = new puInput(x, y, x + width, y + height);
417         setupObject(input, props);
418         return input;
419     } else if (type == "text") {
420         puText * text = new puText(x, y);
421         setupObject(text, props);
422
423         if (props->getNode("format"))
424             text->setRenderCallback(format_callback, props);
425         // Layed-out objects need their size set, and non-layout ones
426         // get a different placement.
427         if(presetSize) text->setSize(width, height);
428         else text->setLabelPlace(PUPLACE_LABEL_DEFAULT);
429         return text;
430     } else if (type == "checkbox") {
431         puButton * b;
432         b = new puButton(x, y, x + width, y + height, PUBUTTON_XCHECK);
433         b->setColourScheme(.8, .7, .7); // matches "PUI input pink"
434         setupObject(b, props);
435         return b;
436     } else if (type == "radio") {
437         puButton * b;
438         b = new puButton(x, y, x + width, y + height, PUBUTTON_CIRCLE);
439         b->setColourScheme(.8, .7, .7); // matches "PUI input pink"
440         setupObject(b, props);
441         return b;
442     } else if (type == "button") {
443         puButton * b;
444         const char * legend = props->getStringValue("legend", "[none]");
445         if (props->getBoolValue("one-shot", true))
446             b = new puOneShot(x, y, legend);
447         else
448             b = new puButton(x, y, legend);
449         if(presetSize)
450             b->setSize(width, height);
451         setupObject(b, props);
452         return b;
453     } else if (type == "combo") {
454         vector<SGPropertyNode_ptr> value_nodes = props->getChildren("value");
455         char ** entries = make_char_array(value_nodes.size());
456         for (unsigned int i = 0, j = value_nodes.size() - 1;
457              i < value_nodes.size();
458              i++, j--)
459             entries[i] = strdup((char *)value_nodes[i]->getStringValue());
460         puComboBox * combo =
461             new puComboBox(x, y, x + width, y + height, entries,
462                            props->getBoolValue("editable", false));
463         setupObject(combo, props);
464         return combo;
465     } else if (type == "slider") {
466         bool vertical = props->getBoolValue("vertical", false);
467         puSlider * slider = new puSlider(x, y, (vertical ? height : width));
468         slider->setMinValue(props->getFloatValue("min", 0.0));
469         slider->setMaxValue(props->getFloatValue("max", 1.0));
470         setupObject(slider, props);
471         if(presetSize)
472             slider->setSize(width, height);
473         return slider;
474     } else if (type == "dial") {
475         puDial * dial = new puDial(x, y, width);
476         dial->setMinValue(props->getFloatValue("min", 0.0));
477         dial->setMaxValue(props->getFloatValue("max", 1.0));
478         dial->setWrap(props->getBoolValue("wrap", true));
479         setupObject(dial, props);
480         return dial;
481     } else if (type == "textbox") {
482        int slider_width = props->getIntValue("slider", parentHeight);
483        if (slider_width==0) slider_width=20;
484        puLargeInput * puTextBox =
485                      new puLargeInput(x, y, x+width, x+height, 2, slider_width);
486        if  (props->hasValue("editable"))
487        {
488           if (props->getBoolValue("editable")==false)
489              puTextBox->disableInput();
490           else
491              puTextBox->enableInput();
492        }
493        setupObject(puTextBox,props);
494        return puTextBox;
495     } else if (type == "select") {
496         vector<SGPropertyNode_ptr> value_nodes;
497         SGPropertyNode * selection_node =
498                 fgGetNode(props->getChild("selection")->getStringValue(), true);
499
500         for (int q = 0; q < selection_node->nChildren(); q++)
501             value_nodes.push_back(selection_node->getChild(q));
502
503         char ** entries = make_char_array(value_nodes.size());
504         for (unsigned int i = 0, j = value_nodes.size() - 1;
505              i < value_nodes.size();
506              i++, j--)
507             entries[i] = strdup((char *)value_nodes[i]->getName());
508         puSelectBox * select =
509             new puSelectBox(x, y, x + width, y + height, entries);
510         setupObject(select, props);
511         return select;
512     } else {
513         return 0;
514     }
515 }
516
517 void
518 FGDialog::setupObject (puObject * object, SGPropertyNode * props)
519 {
520     object->setLabelPlace(PUPLACE_CENTERED_RIGHT);
521
522     if (props->hasValue("legend"))
523         object->setLegend(props->getStringValue("legend"));
524
525     if (props->hasValue("label"))
526         object->setLabel(props->getStringValue("label"));
527
528     if (props->hasValue("property")) {
529         const char * name = props->getStringValue("name");
530         if (name == 0)
531             name = "";
532         const char * propname = props->getStringValue("property");
533         SGPropertyNode_ptr node = fgGetNode(propname, true);
534         copy_to_pui(node, object);
535         PropertyObject* po = new PropertyObject(name, object, node);
536         _propertyObjects.push_back(po);
537         if(props->getBoolValue("live"))
538             _liveObjects.push_back(po);
539     }
540
541     vector<SGPropertyNode_ptr> nodes = props->getChildren("binding");
542     if (nodes.size() > 0) {
543         GUIInfo * info = new GUIInfo(this);
544
545         for (unsigned int i = 0; i < nodes.size(); i++)
546             info->bindings.push_back(new FGBinding(nodes[i]));
547         object->setCallback(action_callback);
548         object->setUserData(info);
549         _info.push_back(info);
550     }
551
552     object->makeReturnDefault(props->getBoolValue("default"));
553 }
554
555 void
556 FGDialog::setupGroup (puGroup * group, SGPropertyNode * props,
557                     int width, int height, bool makeFrame)
558 {
559     setupObject(group, props);
560
561     if (makeFrame) {
562         puFrame* f = new puFrame(0, 0, width, height);
563         f->setColorScheme(0.8, 0.8, 0.9, 0.85);
564     }
565
566     int nChildren = props->nChildren();
567     for (int i = 0; i < nChildren; i++)
568         makeObject(props->getChild(i), width, height);
569     group->close();
570 }
571
572 char **
573 FGDialog::make_char_array (int size)
574 {
575     char ** list = new char*[size+1];
576     for (int i = 0; i <= size; i++)
577         list[i] = 0;
578     _char_arrays.push_back(list);
579     return list;
580 }
581
582
583 \f
584 ////////////////////////////////////////////////////////////////////////
585 // Implementation of FGDialog::PropertyObject.
586 ////////////////////////////////////////////////////////////////////////
587
588 FGDialog::PropertyObject::PropertyObject (const char * n,
589                                            puObject * o,
590                                            SGPropertyNode_ptr p)
591     : name(n),
592       object(o),
593       node(p)
594 {
595 }
596
597
598 // end of dialog.cxx