1 // dialog.cxx: implementation of an XML-configurable dialog box.
3 #include <Input/input.hxx>
9 #include "AirportList.hxx"
12 int fgPopup::checkHit(int button, int updown, int x, int y)
14 int result = puPopup::checkHit(button, updown, x, y);
16 // This is annoying. We would really want a true result from the
17 // superclass to indicate "handled by child object", but all it
18 // tells us is that the pointer is inside the dialog. So do the
19 // intersection test (again) to make sure we don't start a drag
20 // when inside controls.
21 if(!result) return result;
22 puObject* child = getFirstChild();
23 if(child) child = child->getNextObject(); // Skip the puFrame
26 child->getAbsolutePosition(&cx, &cy);
27 child->getSize(&cw, &ch);
28 if(x >= cx && x < cx + cw && y >= cy && y < cy + ch)
30 child = child->getNextObject();
33 // Finally, handle the mouse event
34 if(updown == PU_DOWN) {
36 getPosition(&px, &py);
40 } else if(updown == PU_DRAG && _dragging) {
41 setPosition(x + _dX, y + _dY);
49 ////////////////////////////////////////////////////////////////////////
51 ////////////////////////////////////////////////////////////////////////
54 * User data for a GUI object.
58 GUIInfo (FGDialog * d);
62 vector <FGBinding *> bindings;
70 action_callback (puObject * object)
72 GUIInfo * info = (GUIInfo *)object->getUserData();
73 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
74 gui->setActiveDialog(info->dialog);
75 int nBindings = info->bindings.size();
76 for (int i = 0; i < nBindings; i++) {
77 info->bindings[i]->fire();
78 if (gui->getActiveDialog() == 0)
81 gui->setActiveDialog(0);
86 ////////////////////////////////////////////////////////////////////////
87 // Static helper functions.
88 ////////////////////////////////////////////////////////////////////////
91 * Copy a property value to a PUI object.
94 copy_to_pui (SGPropertyNode * node, puObject * object)
96 switch (node->getType()) {
97 case SGPropertyNode::BOOL:
98 case SGPropertyNode::INT:
99 case SGPropertyNode::LONG:
100 object->setValue(node->getIntValue());
102 case SGPropertyNode::FLOAT:
103 case SGPropertyNode::DOUBLE:
104 object->setValue(node->getFloatValue());
107 object->setValue(node->getStringValue());
111 // Treat puText objects specially, so their "values" can be set
113 if(object->getType() & PUCLASS_TEXT)
114 object->setLabel(node->getStringValue());
119 copy_from_pui (puObject * object, SGPropertyNode * node)
121 switch (node->getType()) {
122 case SGPropertyNode::BOOL:
123 case SGPropertyNode::INT:
124 case SGPropertyNode::LONG:
125 node->setIntValue(object->getIntegerValue());
127 case SGPropertyNode::FLOAT:
128 case SGPropertyNode::DOUBLE:
129 node->setFloatValue(object->getFloatValue());
132 node->setStringValue(object->getStringValue());
139 ////////////////////////////////////////////////////////////////////////
140 // Implementation of GUIInfo.
141 ////////////////////////////////////////////////////////////////////////
143 GUIInfo::GUIInfo (FGDialog * d)
150 for (unsigned int i = 0; i < bindings.size(); i++) {
158 ////////////////////////////////////////////////////////////////////////
159 // Implementation of FGDialog.
160 ////////////////////////////////////////////////////////////////////////
162 FGDialog::FGDialog (SGPropertyNode * props)
168 FGDialog::~FGDialog ()
170 puDeleteObject(_object);
174 // Delete all the arrays we made
175 // and were forced to keep around
176 // because PUI won't do its own
177 // memory management.
178 for (i = 0; i < _char_arrays.size(); i++) {
179 for (int j = 0; _char_arrays[i][j] != 0; j++)
180 free(_char_arrays[i][j]); // added with strdup
181 delete[] _char_arrays[i];
184 // Delete all the info objects we
185 // were forced to keep around because
186 // PUI cannot delete its own user data.
187 for (i = 0; i < _info.size(); i++) {
188 delete (GUIInfo *)_info[i];
192 // Finally, delete the property links.
193 for (i = 0; i < _propertyObjects.size(); i++) {
194 delete _propertyObjects[i];
195 _propertyObjects[i] = 0;
200 FGDialog::updateValue (const char * objectName)
202 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
203 const string &name = _propertyObjects[i]->name;
204 if (name == objectName)
205 copy_to_pui(_propertyObjects[i]->node,
206 _propertyObjects[i]->object);
211 FGDialog::applyValue (const char * objectName)
213 for (unsigned int i = 0; i < _propertyObjects.size(); i++) {
214 if (_propertyObjects[i]->name == objectName)
215 copy_from_pui(_propertyObjects[i]->object,
216 _propertyObjects[i]->node);
221 FGDialog::updateValues ()
223 for (unsigned int i = 0; i < _propertyObjects.size(); i++)
224 copy_to_pui(_propertyObjects[i]->node, _propertyObjects[i]->object);
228 FGDialog::applyValues ()
230 for (unsigned int i = 0; i < _propertyObjects.size(); i++)
231 copy_from_pui(_propertyObjects[i]->object,
232 _propertyObjects[i]->node);
238 for (unsigned int i = 0; i < _liveObjects.size(); i++)
239 copy_to_pui(_liveObjects[i]->node, _liveObjects[i]->object);
243 FGDialog::display (SGPropertyNode * props)
246 SG_LOG(SG_GENERAL, SG_ALERT, "This widget is already active");
250 int screenw = globals->get_props()->getIntValue("/sim/startup/xsize");
251 int screenh = globals->get_props()->getIntValue("/sim/startup/ysize");
253 bool userx = props->hasValue("x");
254 bool usery = props->hasValue("y");
255 bool userw = props->hasValue("width");
256 bool userh = props->hasValue("height");
258 LayoutWidget wid(props);
261 wid.calcPrefSize(&pw, &ph);
262 pw = props->getIntValue("width", pw);
263 ph = props->getIntValue("height", ph);
264 int px = props->getIntValue("x", (screenw - pw) / 2);
265 int py = props->getIntValue("y", (screenh - ph) / 2);
266 wid.layout(px, py, pw, ph);
268 _object = makeObject(props, screenw, screenh);
270 // Remove automatically generated properties, so the layout looks
271 // the same next time around.
272 if(!userx) props->removeChild("x");
273 if(!usery) props->removeChild("y");
274 if(!userw) props->removeChild("width");
275 if(!userh) props->removeChild("height");
280 SG_LOG(SG_GENERAL, SG_ALERT, "Widget "
281 << props->getStringValue("name", "[unnamed]")
282 << " does not contain a proper GUI definition");
287 FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
289 bool presetSize = props->hasValue("width") && props->hasValue("height");
290 int width = props->getIntValue("width", parentWidth);
291 int height = props->getIntValue("height", parentHeight);
292 int x = props->getIntValue("x", (parentWidth - width) / 2);
293 int y = props->getIntValue("y", (parentHeight - height) / 2);
295 string type = props->getName();
299 if (type == "dialog") {
301 if (props->getBoolValue("modal", false))
302 dialog = new puDialogBox(x, y);
304 dialog = new fgPopup(x, y);
305 setupGroup(dialog, props, width, height, true);
307 } else if (type == "group") {
308 puGroup * group = new puGroup(x, y);
309 setupGroup(group, props, width, height, false);
311 } else if (type == "list") {
312 puList * list = new puList(x, y, x + width, y + height);
313 setupObject(list, props);
315 } else if (type == "airport-list") {
316 AirportList * list = new AirportList(x, y, x + width, y + height);
317 setupObject(list, props);
319 } else if (type == "input") {
320 puInput * input = new puInput(x, y, x + width, y + height);
321 setupObject(input, props);
323 } else if (type == "text") {
324 puText * text = new puText(x, y);
325 setupObject(text, props);
326 // Layed-out objects need their size set, and non-layout ones
327 // get a different placement.
328 if(presetSize) text->setSize(width, height);
329 else text->setLabelPlace(PUPLACE_LABEL_DEFAULT);
331 } else if (type == "checkbox") {
333 b = new puButton(x, y, x + width, y + height, PUBUTTON_XCHECK);
334 b->setColourScheme(.8, .7, .7); // matches "PUI input pink"
335 setupObject(b, props);
337 } else if (type == "radio") {
339 b = new puButton(x, y, x + width, y + height, PUBUTTON_CIRCLE);
340 b->setColourScheme(.8, .7, .7); // matches "PUI input pink"
341 setupObject(b, props);
343 } else if (type == "button") {
345 const char * legend = props->getStringValue("legend", "[none]");
346 if (props->getBoolValue("one-shot", true))
347 b = new puOneShot(x, y, legend);
349 b = new puButton(x, y, legend);
351 b->setSize(width, height);
352 setupObject(b, props);
354 } else if (type == "combo") {
355 vector<SGPropertyNode_ptr> value_nodes = props->getChildren("value");
356 char ** entries = make_char_array(value_nodes.size());
357 for (unsigned int i = 0, j = value_nodes.size() - 1;
358 i < value_nodes.size();
360 entries[i] = strdup((char *)value_nodes[i]->getStringValue());
362 new puComboBox(x, y, x + width, y + height, entries,
363 props->getBoolValue("editable", false));
364 setupObject(combo, props);
366 } else if (type == "slider") {
367 bool vertical = props->getBoolValue("vertical", false);
368 puSlider * slider = new puSlider(x, y, (vertical ? height : width));
369 slider->setMinValue(props->getFloatValue("min", 0.0));
370 slider->setMaxValue(props->getFloatValue("max", 1.0));
371 setupObject(slider, props);
373 slider->setSize(width, height);
375 } else if (type == "dial") {
376 puDial * dial = new puDial(x, y, width);
377 dial->setMinValue(props->getFloatValue("min", 0.0));
378 dial->setMaxValue(props->getFloatValue("max", 1.0));
379 dial->setWrap(props->getBoolValue("wrap", true));
380 setupObject(dial, props);
382 } else if (type == "select") {
383 vector<SGPropertyNode_ptr> value_nodes;
384 SGPropertyNode * selection_node =
385 fgGetNode(props->getChild("selection")->getStringValue(), true);
387 for (int q = 0; q < selection_node->nChildren(); q++)
388 value_nodes.push_back(selection_node->getChild(q));
390 char ** entries = make_char_array(value_nodes.size());
391 for (unsigned int i = 0, j = value_nodes.size() - 1;
392 i < value_nodes.size();
394 entries[i] = strdup((char *)value_nodes[i]->getName());
395 puSelectBox * select =
396 new puSelectBox(x, y, x + width, y + height, entries);
397 setupObject(select, props);
405 FGDialog::setupObject (puObject * object, SGPropertyNode * props)
407 object->setLabelPlace(PUPLACE_CENTERED_RIGHT);
409 if (props->hasValue("legend"))
410 object->setLegend(props->getStringValue("legend"));
412 if (props->hasValue("label"))
413 object->setLabel(props->getStringValue("label"));
415 if (props->hasValue("property")) {
416 const char * name = props->getStringValue("name");
419 const char * propname = props->getStringValue("property");
420 SGPropertyNode_ptr node = fgGetNode(propname, true);
421 copy_to_pui(node, object);
422 PropertyObject* po = new PropertyObject(name, object, node);
423 _propertyObjects.push_back(po);
424 if(props->getBoolValue("live"))
425 _liveObjects.push_back(po);
428 vector<SGPropertyNode_ptr> nodes = props->getChildren("binding");
429 if (nodes.size() > 0) {
430 GUIInfo * info = new GUIInfo(this);
432 for (unsigned int i = 0; i < nodes.size(); i++)
433 info->bindings.push_back(new FGBinding(nodes[i]));
434 object->setCallback(action_callback);
435 object->setUserData(info);
436 _info.push_back(info);
439 object->makeReturnDefault(props->getBoolValue("default"));
443 FGDialog::setupGroup (puGroup * group, SGPropertyNode * props,
444 int width, int height, bool makeFrame)
446 setupObject(group, props);
449 puFrame* f = new puFrame(0, 0, width, height);
450 f->setColorScheme(0.8, 0.8, 0.9, 0.85);
453 int nChildren = props->nChildren();
454 for (int i = 0; i < nChildren; i++)
455 makeObject(props->getChild(i), width, height);
460 FGDialog::make_char_array (int size)
462 char ** list = new char*[size+1];
463 for (int i = 0; i <= size; i++)
465 _char_arrays.push_back(list);
471 ////////////////////////////////////////////////////////////////////////
472 // Implementation of FGDialog::PropertyObject.
473 ////////////////////////////////////////////////////////////////////////
475 FGDialog::PropertyObject::PropertyObject (const char * n,
477 SGPropertyNode_ptr p)