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::props;
50 Type type = node->getType();
51 if (type == UNSPECIFIED)
52 result = "unspecified";
53 else if (type == NONE)
55 else if (type == BOOL)
59 else if (type == LONG)
61 else if (type == FLOAT)
63 else if (type == DOUBLE)
65 else if (type == STRING)
72 static void dumpProperties(const SGPropertyNode *node)
74 using namespace simgear::props;
75 cout << node->getPath() << '/' << endl;
76 for (int i = 0; i < node->nChildren(); i++) {
77 const SGPropertyNode *c = node->getChild(i);
78 Type type = c->getType();
79 if (type == ALIAS || c->nChildren())
82 int index = c->getIndex();
83 cout << std::setw(11) << getValueTypeString(c) << " " << c->getName();
85 cout << '[' << index << ']';
88 switch (c->getType()) {
91 cout << std::setprecision(15) << c->getDoubleValue();
95 cout << c->getLongValue();
98 cout << (c->getBoolValue() ? "true" : "false");
101 cout << '"' << c->getStringValue() << '"';
106 cout << '\'' << c->getStringValue() << '\'';
114 static void sanitize(stdString& s)
118 for (unsigned i = 0; i < r.size(); i++) {
121 else if (r[i] == '\b')
123 else if (r[i] == '\t')
125 else if (r[i] == '\n')
127 else if (r[i] == '\v')
129 else if (r[i] == '\f')
131 else if (r[i] == '\r')
133 else if (r[i] == '\'')
135 else if (r[i] == '\\')
137 else if (isprint(r[i]))
140 const char *hex = "0123456789abcdef";
142 s += stdString("\\x") + hex[c / 16] + hex[c % 16];
150 PropertyList::PropertyList(int minx, int miny, int maxx, int maxy, SGPropertyNode *start) :
151 puaList(minx, miny, maxx, maxy, short(0), 20),
152 GUI_ID(FGCLASS_PROPERTYLIST),
160 _list_box->setUserData(this);
161 _list_box->setCallback(handle_select);
162 _list_box->setValue(0);
167 PropertyList::~PropertyList()
173 void PropertyList::delete_arrays()
178 for (int i = 0; i < _num_entries; i++)
179 delete[] _entries[i];
188 void PropertyList::handle_select(puObject *list_box)
190 PropertyList *prop_list = (PropertyList *)list_box->getUserData();
191 int selected = list_box->getIntegerValue();
192 int mod_ctrl = fgGetKeyModifiers() & KEYMOD_CTRL;
193 int mod_shift = fgGetKeyModifiers() & KEYMOD_SHIFT;
195 if (selected >= 0 && selected < prop_list->_num_entries) {
196 const char *src = prop_list->_entries[selected];
198 if (prop_list->_dot_files && (selected < 2)) {
199 if (src[0] == '.' && (src[1] == '\0' || src[1] == ' ')) {
200 if (mod_ctrl && mod_shift)
201 prop_list->_curr->fireValueChanged();
203 prop_list->toggleVerbosity();
205 dumpProperties(prop_list->_curr);
210 } else if (!strcmp(src, "..")) {
211 SGPropertyNode *parent = prop_list->getCurrent()->getParent();
214 for (; parent->getParent(); parent = parent->getParent())
216 prop_list->setCurrent(parent);
222 // we know we're dealing with a regular entry, so convert
223 // it to an index into children[]
224 if (prop_list->_dot_files)
227 SGPropertyNode_ptr child = prop_list->_children[selected].node;
229 // check if it's a directory
230 if (child->nChildren()) {
231 prop_list->setTopItem(0);
232 prop_list->setCurrent(child);
236 // it is a regular property
237 if (child->getType() == simgear::props::BOOL && mod_ctrl) {
238 child->setBoolValue(!child->getBoolValue());
239 prop_list->update(true);
241 prop_list->publish(child);
244 // the user clicked on blank screen
245 prop_list->update(true);
250 void PropertyList::update(bool restore_pos)
255 _num_entries = (int)_curr->nChildren();
257 // instantiate string objects and add [.] and [..] for subdirs
258 if (!_curr->getParent()) {
259 _entries = new char*[_num_entries + 1];
264 _num_entries += 2; // for . and ..
265 _entries = new char*[_num_entries + 1];
267 _entries[0] = new char[16];
268 strcpy(_entries[0], _verbose ? ". [verbose]" : ".");
270 _entries[1] = new char[3];
271 strcpy(_entries[1], "..");
278 _num_children = _curr->nChildren();
279 _children = new NodeData[_num_children];
280 for (i = 0; i < _num_children; i++)
281 _children[i].node = _curr->getChild(i);
283 qsort(_children, _num_children, sizeof(_children[0]), nodeNameCompare);
285 // Make lists of the children's names, values, etc.
286 for (i = 0; i < _num_children; i++, pi++) {
287 _children[i].text = &_entries[pi];
288 _entries[pi] = 0; // make it deletable
289 updateTextForEntry(_children[i]);
290 _children[i].setListener(this);
293 _entries[_num_entries] = 0;
295 int top = getTopItem();
302 void PropertyList::updateTextForEntry(NodeData& data)
304 using namespace simgear::props;
305 SGPropertyNode *node = data.node;
306 stdString name = node->getDisplayName(true);
307 stdString type = getValueTypeString(node);
308 stdString value = node->getStringValue();
310 std::ostringstream line;
313 int children = node->nChildren();
317 if (!children || (_verbose && node->hasValue())) {
318 if (node->getType() == STRING
319 || node->getType() == UNSPECIFIED)
322 line << " = '" << value << "' (" << type;
326 if (!node->getAttribute(SGPropertyNode::READ))
328 if (!node->getAttribute(SGPropertyNode::WRITE))
330 if (node->getAttribute(SGPropertyNode::TRACE_READ))
332 if (node->getAttribute(SGPropertyNode::TRACE_WRITE))
334 if (node->getAttribute(SGPropertyNode::ARCHIVE))
336 if (node->getAttribute(SGPropertyNode::USERARCHIVE))
344 int num = node->nListeners();
348 line << ", L" << num;
353 stdString out = line.str();
354 if (out.size() >= PUSTRING_MAX)
355 out.resize(PUSTRING_MAX - 1);
358 *data.text = new char[out.size() + 1];
359 strcpy(*data.text, out.c_str());
363 void PropertyList::valueChanged(SGPropertyNode *nd)
365 for (int i = 0; i < _num_children; i++)
366 if (_children[i].node == nd) {
367 updateTextForEntry(_children[i]);
373 int PropertyList::nodeNameCompare(const void *p1, const void *p2)
375 const SGPropertyNode *n1 = (*(const NodeData *)p1).node;
376 const SGPropertyNode *n2 = (*(const NodeData *)p2).node;
378 int diff = strcmp(n1->getName(), n2->getName());
379 return diff ? diff : n1->getIndex() - n2->getIndex();
383 void PropertyList::setValue(const char *s)
386 SGPropertyNode *p = fgGetNode(s, false);
390 throw stdString("node doesn't exist");
391 } catch (const stdString& m) {
392 SG_LOG(SG_GENERAL, SG_DEBUG, "property-list node '" << s << "': " << m);
397 void PropertyList::setCurrent(SGPropertyNode *p)
399 bool same = (_curr == p);