1 // dialog.cxx: implementation of an XML-configurable dialog box.
3 #include <stdlib.h> // atof()
5 #include <Input/input.hxx>
11 #include "AirportList.hxx"
14 int fgPopup::checkHit(int button, int updown, int x, int y)
16 int result = puPopup::checkHit(button, updown, x, y);
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.
24 if(updown == PU_DOWN && !_dragging) {
28 int hit = getHitObjects(this, x, y);
29 if(hit & (PUCLASS_BUTTON|PUCLASS_ONESHOT|PUCLASS_INPUT))
33 getPosition(&px, &py);
37 } else if(updown == PU_DRAG && _dragging) {
38 setPosition(x + _dX, y + _dY);
45 int fgPopup::getHitObjects(puObject *object, int x, int y)
48 if(object->getType() & PUCLASS_GROUP)
49 for (puObject *obj = ((puGroup *)object)->getFirstChild();
50 obj; obj = obj->getNextObject())
51 type |= getHitObjects(obj, x, y);
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();
63 ////////////////////////////////////////////////////////////////////////
65 ////////////////////////////////////////////////////////////////////////
68 * User data for a GUI object.
72 GUIInfo (FGDialog * d);
76 vector <FGBinding *> bindings;
84 action_callback (puObject * object)
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)
95 gui->setActiveDialog(0);
100 format_callback(puObject *obj, int dx, int dy, void *n)
102 SGPropertyNode *node = (SGPropertyNode *)n;
103 const char *format = node->getStringValue("format"), *f = format;
104 bool number, l = false;
105 // make sure the format matches '[ -+#]?\d*(\.\d*)?l?[fs]'
116 if (*f == ' ' || *f == '+' || *f == '-' || *f == '#')
118 while (*f && isdigit(*f))
122 while (*f && isdigit(*f))
130 else if (*f == 's') {
147 const char *src = obj->getLabel();
150 float value = atof(src);
151 snprintf(buf, 256, format, value);
153 snprintf(buf, 256, format, src);
158 SGPropertyNode *result = node->getNode("formatted", true);
159 result->setStringValue(buf);
160 obj->setLabel(result->getStringValue());
165 ////////////////////////////////////////////////////////////////////////
166 // Static helper functions.
167 ////////////////////////////////////////////////////////////////////////
170 * Copy a property value to a PUI object.
173 copy_to_pui (SGPropertyNode * node, puObject * object)
175 // Treat puText objects specially, so their "values" can be set
177 if(object->getType() & PUCLASS_TEXT) {
178 object->setLabel(node->getStringValue());
182 switch (node->getType()) {
183 case SGPropertyNode::BOOL:
184 case SGPropertyNode::INT:
185 case SGPropertyNode::LONG:
186 object->setValue(node->getIntValue());
188 case SGPropertyNode::FLOAT:
189 case SGPropertyNode::DOUBLE:
190 object->setValue(node->getFloatValue());
193 object->setValue(node->getStringValue());
200 copy_from_pui (puObject * object, SGPropertyNode * node)
202 // puText objects are immutable, so should not be copied out
203 if(object->getType() & PUCLASS_TEXT)
206 switch (node->getType()) {
207 case SGPropertyNode::BOOL:
208 case SGPropertyNode::INT:
209 case SGPropertyNode::LONG:
210 node->setIntValue(object->getIntegerValue());
212 case SGPropertyNode::FLOAT:
213 case SGPropertyNode::DOUBLE:
214 node->setFloatValue(object->getFloatValue());
217 // Special case to handle lists, as getStringValue cannot be overridden
218 if(object->getType() & PUCLASS_LIST)
220 node->setStringValue(((puList *) object)->getListStringValue());
224 node->setStringValue(object->getStringValue());
232 ////////////////////////////////////////////////////////////////////////
233 // Implementation of GUIInfo.
234 ////////////////////////////////////////////////////////////////////////
236 GUIInfo::GUIInfo (FGDialog * d)
243 for (unsigned int i = 0; i < bindings.size(); i++) {
251 ////////////////////////////////////////////////////////////////////////
252 // Implementation of FGDialog.
253 ////////////////////////////////////////////////////////////////////////
255 FGDialog::FGDialog (SGPropertyNode * props)
261 FGDialog::~FGDialog ()
263 puDeleteObject(_object);
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];
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];
285 // Finally, delete the property links.
286 for (i = 0; i < _propertyObjects.size(); i++) {
287 delete _propertyObjects[i];
288 _propertyObjects[i] = 0;
293 FGDialog::updateValue (const char * objectName)
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);
304 FGDialog::applyValue (const char * objectName)
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);
314 FGDialog::updateValues ()
316 for (unsigned int i = 0; i < _propertyObjects.size(); i++)
317 copy_to_pui(_propertyObjects[i]->node, _propertyObjects[i]->object);
321 FGDialog::applyValues ()
323 for (unsigned int i = 0; i < _propertyObjects.size(); i++)
324 copy_from_pui(_propertyObjects[i]->object,
325 _propertyObjects[i]->node);
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())
336 copy_to_pui(_liveObjects[i]->node, obj);
341 FGDialog::display (SGPropertyNode * props)
344 SG_LOG(SG_GENERAL, SG_ALERT, "This widget is already active");
348 int screenw = globals->get_props()->getIntValue("/sim/startup/xsize");
349 int screenh = globals->get_props()->getIntValue("/sim/startup/ysize");
351 bool userx = props->hasValue("x");
352 bool usery = props->hasValue("y");
353 bool userw = props->hasValue("width");
354 bool userh = props->hasValue("height");
356 LayoutWidget wid(props);
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);
366 _object = makeObject(props, screenw, screenh);
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");
378 SG_LOG(SG_GENERAL, SG_ALERT, "Widget "
379 << props->getStringValue("name", "[unnamed]")
380 << " does not contain a proper GUI definition");
385 FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
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);
393 sgVec4 color = {0.8, 0.8, 0.9, 0.85};
394 SGPropertyNode *ncs = props->getNode("color", false);
396 color[0] = ncs->getFloatValue("red", 0.8);
397 color[1] = ncs->getFloatValue("green", 0.8);
398 color[2] = ncs->getFloatValue("blue", 0.9);
399 color[3] = ncs->getFloatValue("alpha", 0.85);
402 string type = props->getName();
406 if (type == "dialog") {
408 if (props->getBoolValue("modal", false))
409 dialog = new puDialogBox(x, y);
411 dialog = new fgPopup(x, y);
412 setupGroup(dialog, props, width, height, color, true);
414 } else if (type == "group") {
415 puGroup * group = new puGroup(x, y);
416 setupGroup(group, props, width, height, color, false);
418 } else if (type == "list") {
419 puList * list = new puList(x, y, x + width, y + height);
420 setupObject(list, props);
422 } else if (type == "airport-list") {
423 AirportList * list = new AirportList(x, y, x + width, y + height);
424 setupObject(list, props);
426 } else if (type == "input") {
427 puInput * input = new puInput(x, y, x + width, y + height);
428 setupObject(input, props);
430 } else if (type == "text") {
431 puText * text = new puText(x, y);
432 setupObject(text, props);
434 if (props->getNode("format")) {
435 SGPropertyNode *live = props->getNode("live");
436 if (live && live->getBoolValue())
437 text->setRenderCallback(format_callback, props);
439 format_callback(text, x, y, props);
441 // Layed-out objects need their size set, and non-layout ones
442 // get a different placement.
443 if(presetSize) text->setSize(width, height);
444 else text->setLabelPlace(PUPLACE_LABEL_DEFAULT);
446 } else if (type == "checkbox") {
448 b = new puButton(x, y, x + width, y + height, PUBUTTON_XCHECK);
449 b->setColourScheme(.8, .7, .7); // matches "PUI input pink"
450 setupObject(b, props);
452 } else if (type == "radio") {
454 b = new puButton(x, y, x + width, y + height, PUBUTTON_CIRCLE);
455 b->setColourScheme(.8, .7, .7); // matches "PUI input pink"
456 setupObject(b, props);
458 } else if (type == "button") {
460 const char * legend = props->getStringValue("legend", "[none]");
461 if (props->getBoolValue("one-shot", true))
462 b = new puOneShot(x, y, legend);
464 b = new puButton(x, y, legend);
466 b->setSize(width, height);
467 setupObject(b, props);
469 } else if (type == "combo") {
470 vector<SGPropertyNode_ptr> value_nodes = props->getChildren("value");
471 char ** entries = make_char_array(value_nodes.size());
472 for (unsigned int i = 0, j = value_nodes.size() - 1;
473 i < value_nodes.size();
475 entries[i] = strdup((char *)value_nodes[i]->getStringValue());
477 new puComboBox(x, y, x + width, y + height, entries,
478 props->getBoolValue("editable", false));
479 setupObject(combo, props);
481 } else if (type == "slider") {
482 bool vertical = props->getBoolValue("vertical", false);
483 puSlider * slider = new puSlider(x, y, (vertical ? height : width));
484 slider->setMinValue(props->getFloatValue("min", 0.0));
485 slider->setMaxValue(props->getFloatValue("max", 1.0));
486 setupObject(slider, props);
488 slider->setSize(width, height);
490 } else if (type == "dial") {
491 puDial * dial = new puDial(x, y, width);
492 dial->setMinValue(props->getFloatValue("min", 0.0));
493 dial->setMaxValue(props->getFloatValue("max", 1.0));
494 dial->setWrap(props->getBoolValue("wrap", true));
495 setupObject(dial, props);
497 } else if (type == "textbox") {
498 int slider_width = props->getIntValue("slider", parentHeight);
499 int wrap = props->getBoolValue("wrap", true)==true;
500 if (slider_width==0) slider_width=20;
501 puLargeInput * puTextBox =
502 new puLargeInput(x, y, x+width, x+height, 2, slider_width, wrap);
503 if (props->hasValue("editable"))
505 if (props->getBoolValue("editable")==false)
506 puTextBox->disableInput();
508 puTextBox->enableInput();
510 setupObject(puTextBox,props);
512 } else if (type == "select") {
513 vector<SGPropertyNode_ptr> value_nodes;
514 SGPropertyNode * selection_node =
515 fgGetNode(props->getChild("selection")->getStringValue(), true);
517 for (int q = 0; q < selection_node->nChildren(); q++)
518 value_nodes.push_back(selection_node->getChild(q));
520 char ** entries = make_char_array(value_nodes.size());
521 for (unsigned int i = 0, j = value_nodes.size() - 1;
522 i < value_nodes.size();
524 entries[i] = strdup((char *)value_nodes[i]->getName());
525 puSelectBox * select =
526 new puSelectBox(x, y, x + width, y + height, entries);
527 setupObject(select, props);
535 FGDialog::setupObject (puObject * object, SGPropertyNode * props)
537 object->setLabelPlace(PUPLACE_CENTERED_RIGHT);
539 if (props->hasValue("legend"))
540 object->setLegend(props->getStringValue("legend"));
542 if (props->hasValue("label"))
543 object->setLabel(props->getStringValue("label"));
545 if (props->hasValue("property")) {
546 const char * name = props->getStringValue("name");
549 const char * propname = props->getStringValue("property");
550 SGPropertyNode_ptr node = fgGetNode(propname, true);
551 copy_to_pui(node, object);
552 PropertyObject* po = new PropertyObject(name, object, node);
553 _propertyObjects.push_back(po);
554 if(props->getBoolValue("live"))
555 _liveObjects.push_back(po);
558 vector<SGPropertyNode_ptr> nodes = props->getChildren("binding");
559 if (nodes.size() > 0) {
560 GUIInfo * info = new GUIInfo(this);
562 for (unsigned int i = 0; i < nodes.size(); i++)
563 info->bindings.push_back(new FGBinding(nodes[i]));
564 object->setCallback(action_callback);
565 object->setUserData(info);
566 _info.push_back(info);
569 object->makeReturnDefault(props->getBoolValue("default"));
573 FGDialog::setupGroup (puGroup * group, SGPropertyNode * props,
574 int width, int height, sgVec4 color, bool makeFrame)
576 setupObject(group, props);
579 puFrame* f = new puFrame(0, 0, width, height);
580 f->setColorScheme(color[0], color[1], color[2], color[3]);
583 int nChildren = props->nChildren();
584 for (int i = 0; i < nChildren; i++)
585 makeObject(props->getChild(i), width, height);
590 FGDialog::make_char_array (int size)
592 char ** list = new char*[size+1];
593 for (int i = 0; i <= size; i++)
595 _char_arrays.push_back(list);
601 ////////////////////////////////////////////////////////////////////////
602 // Implementation of FGDialog::PropertyObject.
603 ////////////////////////////////////////////////////////////////////////
605 FGDialog::PropertyObject::PropertyObject (const char * n,
607 SGPropertyNode_ptr p)