1 // dialog.cxx: implementation of an XML-configurable dialog box.
3 #include <Input/input.hxx>
10 ////////////////////////////////////////////////////////////////////////
12 ////////////////////////////////////////////////////////////////////////
18 action_callback (puObject * object)
20 GUIInfo * info = (GUIInfo *)object->getUserData();
21 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
22 gui->setCurrentWidget(info->widget);
23 int nBindings = info->bindings.size();
24 for (int i = 0; i < nBindings; i++) {
25 info->bindings[i]->fire();
26 if (gui->getCurrentWidget() == 0)
29 gui->setCurrentWidget(0);
34 ////////////////////////////////////////////////////////////////////////
35 // Static helper functions.
36 ////////////////////////////////////////////////////////////////////////
39 * Copy a property value to a PUI object.
42 copy_to_pui (SGPropertyNode * node, puObject * object)
44 switch (node->getType()) {
45 case SGPropertyNode::BOOL:
46 case SGPropertyNode::INT:
47 case SGPropertyNode::LONG:
48 object->setValue(node->getIntValue());
50 case SGPropertyNode::FLOAT:
51 case SGPropertyNode::DOUBLE:
52 object->setValue(node->getFloatValue());
55 object->setValue(node->getStringValue());
62 copy_from_pui (puObject * object, SGPropertyNode * node)
64 switch (node->getType()) {
65 case SGPropertyNode::BOOL:
66 case SGPropertyNode::INT:
67 case SGPropertyNode::LONG:
68 node->setIntValue(object->getIntegerValue());
70 case SGPropertyNode::FLOAT:
71 case SGPropertyNode::DOUBLE:
72 node->setFloatValue(object->getFloatValue());
75 node->setStringValue(object->getStringValue());
82 ////////////////////////////////////////////////////////////////////////
83 // Implementation of GUIInfo.
84 ////////////////////////////////////////////////////////////////////////
86 GUIInfo::GUIInfo (FGDialog * w)
93 for (int i = 0; i < bindings.size(); i++) {
101 ////////////////////////////////////////////////////////////////////////
102 // Implementation of FGDialog.
103 ////////////////////////////////////////////////////////////////////////
105 FGDialog::FGDialog (SGPropertyNode_ptr props)
111 FGDialog::~FGDialog ()
116 for (i = 0; i < _info.size(); i++) {
121 for (i = 0; i < _propertyObjects.size(); i++) {
122 delete _propertyObjects[i];
123 _propertyObjects[i] = 0;
128 FGDialog::updateValue (const char * objectName)
130 for (int i = 0; i < _propertyObjects.size(); i++) {
131 const string &name = _propertyObjects[i]->name;
132 if (name == objectName)
133 copy_to_pui(_propertyObjects[i]->node,
134 _propertyObjects[i]->object);
139 FGDialog::applyValue (const char * objectName)
141 for (int i = 0; i < _propertyObjects.size(); i++) {
142 if (_propertyObjects[i]->name == objectName)
143 copy_from_pui(_propertyObjects[i]->object,
144 _propertyObjects[i]->node);
149 FGDialog::updateValues ()
151 for (int i = 0; i < _propertyObjects.size(); i++)
152 copy_to_pui(_propertyObjects[i]->node, _propertyObjects[i]->object);
156 FGDialog::applyValues ()
158 for (int i = 0; i < _propertyObjects.size(); i++)
159 copy_from_pui(_propertyObjects[i]->object,
160 _propertyObjects[i]->node);
164 FGDialog::display (SGPropertyNode_ptr props)
167 SG_LOG(SG_GENERAL, SG_ALERT, "This widget is already active");
171 _object = makeObject(props, 1024, 768);
176 SG_LOG(SG_GENERAL, SG_ALERT, "Widget "
177 << props->getStringValue("name", "[unnamed]")
178 << " does not contain a proper GUI definition");
183 FGDialog::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
185 int width = props->getIntValue("width", parentWidth);
186 int height = props->getIntValue("height", parentHeight);
188 int x = props->getIntValue("x", (parentWidth - width) / 2);
189 int y = props->getIntValue("y", (parentHeight - height) / 2);
191 string type = props->getName();
193 type = props->getStringValue("type");
195 SG_LOG(SG_GENERAL, SG_ALERT, "No type specified for GUI object");
199 if (type == "dialog") {
201 if (props->getBoolValue("modal", false))
202 dialog = new puDialogBox(x, y);
204 dialog = new puPopup(x, y);
205 setupGroup(dialog, props, width, height, true);
207 } else if (type == "group") {
208 puGroup * group = new puGroup(x, y);
209 setupGroup(group, props, width, height, false);
211 } else if (type == "input") {
212 puInput * input = new puInput(x, y, x + width, y + height);
213 setupObject(input, props);
215 } else if (type == "text") {
216 puText * text = new puText(x, y);
217 setupObject(text, props);
219 } else if (type == "checkbox") {
221 b = new puButton(x, y, x + width, y + height, PUBUTTON_CIRCLE);
222 setupObject(b, props);
224 } else if (type == "button") {
226 const char * legend = props->getStringValue("legend", "[none]");
227 if (props->getBoolValue("one-shot", true))
228 b = new puOneShot(x, y, legend);
230 b = new puButton(x, y, legend);
231 setupObject(b, props);
233 } else if (type == "combo") {
234 vector<SGPropertyNode_ptr> value_nodes = props->getChildren("value");
235 char ** entries = new char*[value_nodes.size()+1];
236 for (int i = 0, j = value_nodes.size() - 1;
237 i < value_nodes.size();
239 entries[i] = (char *)value_nodes[i]->getStringValue();
240 entries[value_nodes.size()] = 0;
242 new puComboBox(x, y, x + width, y + height, entries,
243 props->getBoolValue("editable", false));
245 setupObject(combo, props);
253 FGDialog::setupObject (puObject * object, SGPropertyNode * props)
255 if (props->hasValue("legend"))
256 object->setLegend(props->getStringValue("legend"));
258 if (props->hasValue("label"))
259 object->setLabel(props->getStringValue("label"));
261 if (props->hasValue("property")) {
262 const char * name = props->getStringValue("name");
265 const char * propname = props->getStringValue("property");
266 SGPropertyNode_ptr node = fgGetNode(propname, true);
267 copy_to_pui(node, object);
269 _propertyObjects.push_back(new PropertyObject(name, object, node));
272 vector<SGPropertyNode_ptr> nodes = props->getChildren("binding");
273 if (nodes.size() > 0) {
274 GUIInfo * info = new GUIInfo(this);
276 for (int i = 0; i < nodes.size(); i++)
277 info->bindings.push_back(new FGBinding(nodes[i]));
278 object->setCallback(action_callback);
279 object->setUserData(info);
280 _info.push_back(info);
283 object->makeReturnDefault(props->getBoolValue("default"));
287 FGDialog::setupGroup (puGroup * group, SGPropertyNode * props,
288 int width, int height, bool makeFrame)
290 setupObject(group, props);
293 new puFrame(0, 0, width, height);
295 int nChildren = props->nChildren();
296 for (int i = 0; i < nChildren; i++)
297 makeObject(props->getChild(i), width, height);
303 ////////////////////////////////////////////////////////////////////////
304 // Implementation of FGDialog::PropertyObject.
305 ////////////////////////////////////////////////////////////////////////
307 FGDialog::PropertyObject::PropertyObject (const char * n,
309 SGPropertyNode_ptr p)