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>
37 typedef string stdString; // puObject has a "string" member
39 #include <Main/fg_os.hxx> // fgGetKeyModifiers()
40 #include <Main/fg_props.hxx>
42 #include "property_list.hxx"
45 static string getValueTypeString(const SGPropertyNode *node)
47 using namespace simgear;
50 props::Type type = node->getType();
51 if (type == props::UNSPECIFIED)
52 result = "unspecified";
53 else if (type == props::NONE)
55 else if (type == props::BOOL)
57 else if (type == props::INT)
59 else if (type == props::LONG)
61 else if (type == props::FLOAT)
63 else if (type == props::DOUBLE)
65 else if (type == props::STRING)
67 else if (type == props::VEC3D)
69 else if (type == props::VEC4D)
76 static void dumpProperties(const SGPropertyNode *node)
78 using namespace simgear;
79 cout << node->getPath() << '/' << endl;
80 for (int i = 0; i < node->nChildren(); i++) {
81 const SGPropertyNode *c = node->getChild(i);
82 props::Type type = c->getType();
83 if (type == props::ALIAS || c->nChildren())
86 int index = c->getIndex();
87 cout << std::setw(11) << getValueTypeString(c) << " " << c->getName();
89 cout << '[' << index << ']';
92 switch (c->getType()) {
98 streamsize precision = cout.precision(15);
100 cout.precision(precision);
109 cout << '"' << c->getStringValue() << '"';
114 cout << '\'' << c->getStringValue() << '\'';
122 static void sanitize(stdString& s)
126 for (unsigned i = 0; i < r.size(); i++) {
129 else if (r[i] == '\b')
131 else if (r[i] == '\t')
133 else if (r[i] == '\n')
135 else if (r[i] == '\v')
137 else if (r[i] == '\f')
139 else if (r[i] == '\r')
141 else if (r[i] == '\'')
143 else if (r[i] == '\\')
145 else if (isprint(r[i]))
148 const char *hex = "0123456789abcdef";
150 s += stdString("\\x") + hex[c / 16] + hex[c % 16];
158 PropertyList::PropertyList(int minx, int miny, int maxx, int maxy, SGPropertyNode *start) :
159 puaList(minx, miny, maxx, maxy, short(0), 20),
160 GUI_ID(FGCLASS_PROPERTYLIST),
168 _list_box->setUserData(this);
169 _list_box->setCallback(handle_select);
170 _list_box->setValue(0);
175 PropertyList::~PropertyList()
181 void PropertyList::delete_arrays()
186 for (int i = 0; i < _num_entries; i++)
187 delete[] _entries[i];
196 void PropertyList::handle_select(puObject *list_box)
198 PropertyList *prop_list = (PropertyList *)list_box->getUserData();
199 int selected = list_box->getIntegerValue();
200 int mod_ctrl = fgGetKeyModifiers() & KEYMOD_CTRL;
201 int mod_shift = fgGetKeyModifiers() & KEYMOD_SHIFT;
203 if (selected >= 0 && selected < prop_list->_num_entries) {
204 const char *src = prop_list->_entries[selected];
206 if (prop_list->_dot_files && (selected < 2)) {
207 if (src[0] == '.' && (src[1] == '\0' || src[1] == ' ')) {
208 if (mod_ctrl && mod_shift)
209 prop_list->_curr->fireValueChanged();
211 prop_list->toggleVerbosity();
213 dumpProperties(prop_list->_curr);
218 } else if (!strcmp(src, "..")) {
219 SGPropertyNode *parent = prop_list->getCurrent()->getParent();
222 for (; parent->getParent(); parent = parent->getParent())
224 prop_list->setCurrent(parent);
230 // we know we're dealing with a regular entry, so convert
231 // it to an index into children[]
232 if (prop_list->_dot_files)
235 SGPropertyNode_ptr child = prop_list->_children[selected].node;
237 // check if it's a directory
238 if (child->nChildren()) {
239 prop_list->setTopItem(0);
240 prop_list->setCurrent(child);
244 // it is a regular property
245 if (child->getType() == simgear::props::BOOL && mod_ctrl) {
246 child->setBoolValue(!child->getBoolValue());
247 prop_list->update(true);
249 prop_list->publish(child);
252 // the user clicked on blank screen
253 prop_list->update(true);
258 void PropertyList::update(bool restore_pos)
263 _num_entries = (int)_curr->nChildren();
265 // instantiate string objects and add [.] and [..] for subdirs
266 if (!_curr->getParent()) {
267 _entries = new char*[_num_entries + 1];
272 _num_entries += 2; // for . and ..
273 _entries = new char*[_num_entries + 1];
275 _entries[0] = new char[16];
276 strcpy(_entries[0], _verbose ? ". [verbose]" : ".");
278 _entries[1] = new char[3];
279 strcpy(_entries[1], "..");
286 _num_children = _curr->nChildren();
287 _children = new NodeData[_num_children];
288 for (i = 0; i < _num_children; i++)
289 _children[i].node = _curr->getChild(i);
291 qsort(_children, _num_children, sizeof(_children[0]), nodeNameCompare);
293 // Make lists of the children's names, values, etc.
294 for (i = 0; i < _num_children; i++, pi++) {
295 _children[i].text = &_entries[pi];
296 _entries[pi] = 0; // make it deletable
297 updateTextForEntry(_children[i]);
298 _children[i].setListener(this);
301 _entries[_num_entries] = 0;
303 int top = getTopItem();
310 void PropertyList::updateTextForEntry(NodeData& data)
312 using namespace simgear;
313 SGPropertyNode *node = data.node;
314 stdString name = node->getDisplayName(true);
315 stdString type = getValueTypeString(node);
316 stdString value = node->getStringValue();
318 std::ostringstream line;
321 int children = node->nChildren();
325 if (!children || (_verbose && node->hasValue())) {
326 if (node->getType() == props::STRING
327 || node->getType() == props::UNSPECIFIED)
330 line << " = '" << value << "' (" << type;
334 if (!node->getAttribute(SGPropertyNode::READ))
336 if (!node->getAttribute(SGPropertyNode::WRITE))
338 if (node->getAttribute(SGPropertyNode::TRACE_READ))
340 if (node->getAttribute(SGPropertyNode::TRACE_WRITE))
342 if (node->getAttribute(SGPropertyNode::ARCHIVE))
344 if (node->getAttribute(SGPropertyNode::USERARCHIVE))
352 int num = node->nListeners();
356 line << ", L" << num;
361 stdString out = line.str();
362 if (out.size() >= PUSTRING_MAX)
363 out.resize(PUSTRING_MAX - 1);
366 *data.text = new char[out.size() + 1];
367 strcpy(*data.text, out.c_str());
371 void PropertyList::valueChanged(SGPropertyNode *nd)
373 for (int i = 0; i < _num_children; i++)
374 if (_children[i].node == nd) {
375 updateTextForEntry(_children[i]);
381 int PropertyList::nodeNameCompare(const void *p1, const void *p2)
383 const SGPropertyNode *n1 = (*(const NodeData *)p1).node;
384 const SGPropertyNode *n2 = (*(const NodeData *)p2).node;
386 int diff = strcmp(n1->getName(), n2->getName());
387 return diff ? diff : n1->getIndex() - n2->getIndex();
391 void PropertyList::setValue(const char *s)
394 SGPropertyNode *p = fgGetNode(s, false);
398 throw stdString("node doesn't exist");
399 } catch (const stdString& m) {
400 SG_LOG(SG_GENERAL, SG_DEBUG, "property-list node '" << s << "': " << m);
405 void PropertyList::setCurrent(SGPropertyNode *p)
407 bool same = (_curr == p);