1 // Implementation of the <property-list> widget.
3 // Copyright (C) 2001 Steve BAKER
4 // Copyright (C) 2001 Jim WILSON
5 // Copyright (C) 2006 Melchior FRANZ
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 #include <simgear/compiler.h>
34 typedef string stdString; // puObject has a "string" member
36 #include <Main/fg_os.hxx> // fgGetKeyModifiers()
37 #include <Main/fg_props.hxx>
39 #include "property_list.hxx"
42 static string getValueTypeString(const SGPropertyNode *node)
46 SGPropertyNode::Type type = node->getType();
47 if (type == SGPropertyNode::UNSPECIFIED)
48 result = "unspecified";
49 else if (type == SGPropertyNode::NONE)
51 else if (type == SGPropertyNode::BOOL)
53 else if (type == SGPropertyNode::INT)
55 else if (type == SGPropertyNode::LONG)
57 else if (type == SGPropertyNode::FLOAT)
59 else if (type == SGPropertyNode::DOUBLE)
61 else if (type == SGPropertyNode::STRING)
68 static void dumpProperties(const SGPropertyNode *node)
70 cout << node->getPath() << '/' << endl;
71 for (int i = 0; i < node->nChildren(); i++) {
72 const SGPropertyNode *c = node->getChild(i);
73 SGPropertyNode::Type type = c->getType();
74 if (type == SGPropertyNode::ALIAS || c->nChildren())
77 int index = c->getIndex();
78 cout << std::setw(11) << getValueTypeString(c) << " " << c->getName();
80 cout << '[' << index << ']';
83 switch (c->getType()) {
84 case SGPropertyNode::DOUBLE:
85 case SGPropertyNode::FLOAT:
86 cout << std::setprecision(15) << c->getDoubleValue();
88 case SGPropertyNode::LONG:
89 case SGPropertyNode::INT:
90 cout << c->getLongValue();
92 case SGPropertyNode::BOOL:
93 cout << (c->getBoolValue() ? "true" : "false");
95 case SGPropertyNode::STRING:
96 cout << '"' << c->getStringValue() << '"';
98 case SGPropertyNode::NONE:
101 cout << '\'' << c->getStringValue() << '\'';
109 static void sanitize(stdString& s)
113 for (unsigned i = 0; i < r.size(); i++) {
116 else if (r[i] == '\b')
118 else if (r[i] == '\t')
120 else if (r[i] == '\n')
122 else if (r[i] == '\v')
124 else if (r[i] == '\f')
126 else if (r[i] == '\r')
128 else if (r[i] == '\'')
130 else if (r[i] == '\\')
132 else if (isprint(r[i]))
135 const char *hex = "0123456789abcdef";
137 s += stdString("\\x") + hex[c / 16] + hex[c % 16];
145 PropertyList::PropertyList(int minx, int miny, int maxx, int maxy, SGPropertyNode *start) :
146 puList(minx, miny, maxx, maxy, short(0), 20),
147 GUI_ID(FGCLASS_PROPERTYLIST),
155 _list_box->setUserData(this);
156 _list_box->setCallback(handle_select);
157 _list_box->setValue(0);
162 PropertyList::~PropertyList()
168 void PropertyList::delete_arrays()
173 for (int i = 0; i < _num_entries; i++)
174 delete[] _entries[i];
183 void PropertyList::handle_select(puObject *list_box)
185 PropertyList *prop_list = (PropertyList *)list_box->getUserData();
186 int selected = list_box->getIntegerValue();
187 int mod_ctrl = fgGetKeyModifiers() & KEYMOD_CTRL;
188 int mod_shift = fgGetKeyModifiers() & KEYMOD_SHIFT;
190 if (selected >= 0 && selected < prop_list->_num_entries) {
191 const char *src = prop_list->_entries[selected];
193 if (prop_list->_dot_files && (selected < 2)) {
194 if (!strcmp(src, ".")) {
196 prop_list->toggleVerbosity();
198 dumpProperties(prop_list->_curr);
203 } else if (!strcmp(src, "..")) {
204 SGPropertyNode *parent = prop_list->getCurrent()->getParent();
207 for (; parent->getParent(); parent = parent->getParent())
209 prop_list->setCurrent(parent);
215 // we know we're dealing with a regular entry, so convert
216 // it to an index into children[]
217 if (prop_list->_dot_files)
220 SGPropertyNode_ptr child = prop_list->_children[selected].node;
222 // check if it's a directory
223 if (child->nChildren()) {
224 prop_list->setTopItem(0);
225 prop_list->setCurrent(child);
229 // it is a regular property
230 if (child->getType() == SGPropertyNode::BOOL && mod_ctrl) {
231 child->setBoolValue(!child->getBoolValue());
232 prop_list->update(true);
234 prop_list->publish(child);
237 // the user clicked on blank screen
238 prop_list->update(true);
243 void PropertyList::update(bool restore_pos)
248 _num_entries = (int)_curr->nChildren();
250 // instantiate string objects and add [.] and [..] for subdirs
251 if (!_curr->getParent()) {
252 _entries = new char*[_num_entries + 1];
257 _num_entries += 2; // for . and ..
258 _entries = new char*[_num_entries + 1];
260 _entries[0] = new char[2];
261 strcpy(_entries[0], ".");
263 _entries[1] = new char[3];
264 strcpy(_entries[1], "..");
271 _num_children = _curr->nChildren();
272 _children = new NodeData[_num_children];
273 for (i = 0; i < _num_children; i++)
274 _children[i].node = _curr->getChild(i);
276 qsort(_children, _num_children, sizeof(_children[0]), nodeNameCompare);
278 // Make lists of the children's names, values, etc.
279 for (i = 0; i < _num_children; i++, pi++) {
280 _children[i].text = &_entries[pi];
281 _entries[pi] = 0; // make it deletable
282 updateTextForEntry(_children[i]);
283 _children[i].setListener(this);
286 _entries[_num_entries] = 0;
288 int top = getTopItem();
295 void PropertyList::updateTextForEntry(NodeData& data)
297 SGPropertyNode *node = data.node;
298 stdString name = node->getDisplayName(true);
299 stdString type = getValueTypeString(node);
300 stdString value = node->getStringValue();
302 std::ostringstream line;
305 int children = node->nChildren();
309 if (!children || (_verbose && node->hasValue())) {
310 if (node->getType() == SGPropertyNode::STRING
311 || node->getType() == SGPropertyNode::UNSPECIFIED)
314 line << " = '" << value << "' (" << type;
318 if (!node->getAttribute(SGPropertyNode::READ))
320 if (!node->getAttribute(SGPropertyNode::WRITE))
322 if (node->getAttribute(SGPropertyNode::TRACE_READ))
324 if (node->getAttribute(SGPropertyNode::TRACE_WRITE))
326 if (node->getAttribute(SGPropertyNode::ARCHIVE))
328 if (node->getAttribute(SGPropertyNode::USERARCHIVE))
336 int num = node->nListeners();
340 line << ", L" << num;
345 stdString out = line.str();
346 if (out.size() >= PUSTRING_MAX)
347 out.resize(PUSTRING_MAX - 1);
350 *data.text = new char[out.size() + 1];
351 strcpy(*data.text, out.c_str());
355 void PropertyList::valueChanged(SGPropertyNode *nd)
357 for (int i = 0; i < _num_children; i++)
358 if (_children[i].node == nd) {
359 updateTextForEntry(_children[i]);
365 int PropertyList::nodeNameCompare(const void *p1, const void *p2)
367 const SGPropertyNode *n1 = (*(const NodeData *)p1).node;
368 const SGPropertyNode *n2 = (*(const NodeData *)p2).node;
370 int diff = strcmp(n1->getName(), n2->getName());
371 return diff ? diff : n1->getIndex() - n2->getIndex();
375 void PropertyList::setValue(const char *s)
378 SGPropertyNode *p = fgGetNode(s, false);
382 throw stdString("node doesn't exist");
383 } catch (const stdString& m) {
384 SG_LOG(SG_GENERAL, SG_DEBUG, "property-list node '" << s << "': " << m);
389 void PropertyList::setCurrent(SGPropertyNode *p)
391 bool same = (_curr == p);