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>
32 typedef string stdString; // puObject has a "string" member
34 #include <Main/fg_os.hxx> // fgGetKeyModifiers()
35 #include <Main/fg_props.hxx>
37 #include "property_list.hxx"
40 static string getValueTypeString(const SGPropertyNode *node)
45 SGPropertyNode::Type type = node->getType();
46 if (type == SGPropertyNode::UNSPECIFIED)
47 result = "unspecified";
48 else if (type == SGPropertyNode::NONE)
50 else if (type == SGPropertyNode::BOOL)
52 else if (type == SGPropertyNode::INT)
54 else if (type == SGPropertyNode::LONG)
56 else if (type == SGPropertyNode::FLOAT)
58 else if (type == SGPropertyNode::DOUBLE)
60 else if (type == SGPropertyNode::STRING)
67 static void sanitize(stdString& s)
71 for (unsigned i = 0; i < r.size(); i++) {
74 else if (r[i] == '\b')
76 else if (r[i] == '\t')
78 else if (r[i] == '\n')
80 else if (r[i] == '\v')
82 else if (r[i] == '\f')
84 else if (r[i] == '\r')
86 else if (r[i] == '\'')
88 else if (r[i] == '\\')
90 else if (isprint(r[i]))
93 const char *hex = "0123456789abcdef";
95 s += stdString("\\x") + hex[c / 16] + hex[c % 16];
103 PropertyList::PropertyList(int minx, int miny, int maxx, int maxy, SGPropertyNode *start) :
104 puaList(minx, miny, maxx, maxy, short(0), 20),
105 GUI_ID(FGCLASS_PROPERTYLIST),
107 _flags(fgGetNode("/sim/gui/dialogs/property-browser/show-flags", true)),
113 _list_box->setUserData(this);
114 _list_box->setCallback(handle_select);
115 _list_box->setValue(0);
120 PropertyList::~PropertyList()
126 void PropertyList::delete_arrays()
131 for (int i = 0; i < _num_entries; i++)
132 delete[] _entries[i];
141 void PropertyList::handle_select(puObject *list_box)
143 PropertyList *prop_list = (PropertyList *)list_box->getUserData();
144 int selected = list_box->getIntegerValue();
145 int mod_ctrl = fgGetKeyModifiers() & KEYMOD_CTRL;
147 if (selected >= 0 && selected < prop_list->_num_entries) {
148 const char *src = prop_list->_entries[selected];
150 if (prop_list->dotFiles && (selected < 2)) {
151 if (!strcmp(src, ".")) {
153 prop_list->toggleFlags();
158 } else if (!strcmp(src, "..")) {
159 SGPropertyNode *parent = prop_list->getCurrent()->getParent();
162 for (; parent->getParent(); parent = parent->getParent())
164 prop_list->setCurrent(parent);
170 // we know we're dealing with a regular entry, so convert
171 // it to an index into children[]
172 if (prop_list->dotFiles)
175 SGPropertyNode_ptr child = prop_list->_children[selected].node;
178 // check if it's a directory
179 if (child->nChildren()) {
180 prop_list->setTopItem(0);
181 prop_list->setCurrent(child);
185 // it is a regular property
186 if (child->getType() == SGPropertyNode::BOOL && mod_ctrl) {
187 child->setBoolValue(!child->getBoolValue());
188 prop_list->update(true);
190 prop_list->publish(child);
193 // the user clicked on blank screen
194 prop_list->update(true);
199 void PropertyList::update(bool restore_pos)
204 _num_entries = (int)_curr->nChildren();
206 // instantiate string objects and add [.] and [..] for subdirs
207 if (!_curr->getParent()) {
208 _entries = new char*[_num_entries + 1];
213 _num_entries += 2; // for . and ..
214 _entries = new char*[_num_entries + 1];
216 _entries[0] = new char[2];
217 strcpy(_entries[0], ".");
219 _entries[1] = new char[3];
220 strcpy(_entries[1], "..");
227 _num_children = _curr->nChildren();
228 _children = new NodeData[_num_children];
229 for (i = 0; i < _num_children; i++)
230 _children[i].node = _curr->getChild(i);
232 qsort(_children, _num_children, sizeof(_children[0]), nodeNameCompare);
234 // Make lists of the children's names, values, etc.
235 for (i = 0; i < _num_children; i++, pi++) {
236 SGPropertyNode *child = _children[i].node;
238 if (child->nChildren() > 0) {
239 stdString name = stdString(child->getDisplayName(true)) + '/';
240 _entries[pi] = new char[name.size() + 1];
241 strcpy(_entries[pi], name.c_str());
244 _entries[pi] = 0; // make it delete-able
245 updateTextForEntry(i);
246 _children[i].setListener(this);
250 _entries[_num_entries] = 0;
252 int top = getTopItem();
259 void PropertyList::updateTextForEntry(int index)
261 assert((index >= 0) && (index < _num_children));
262 SGPropertyNode_ptr node = _children[index].node;
264 stdString name = node->getDisplayName(true);
265 stdString type = getValueTypeString(node);
266 stdString value = node->getStringValue();
268 if (node->getType() == SGPropertyNode::STRING
269 || node->getType() == SGPropertyNode::UNSPECIFIED)
272 stdString line = name + " = '" + value + "' (" + type;
274 if (_flags->getBoolValue()) {
276 if (!node->getAttribute(SGPropertyNode::READ))
278 if (!node->getAttribute(SGPropertyNode::WRITE))
280 if (node->getAttribute(SGPropertyNode::TRACE_READ))
282 if (node->getAttribute(SGPropertyNode::TRACE_WRITE))
284 if (node->getAttribute(SGPropertyNode::ARCHIVE))
286 if (node->getAttribute(SGPropertyNode::USERARCHIVE))
296 if (line.size() >= PUSTRING_MAX)
297 line.resize(PUSTRING_MAX - 1);
302 delete[] _entries[index];
303 _entries[index] = new char[line.size() + 1];
304 strcpy(_entries[index], line.c_str());
308 void PropertyList::valueChanged(SGPropertyNode *nd)
310 for (int i = 0; i < _num_children; i++)
311 if (_children[i].node == nd) {
312 updateTextForEntry(i);
318 int PropertyList::nodeNameCompare(const void *p1, const void *p2)
320 const SGPropertyNode *n1 = (*(const NodeData *)p1).node;
321 const SGPropertyNode *n2 = (*(const NodeData *)p2).node;
323 int diff = strcmp(n1->getName(), n2->getName());
324 return diff ? diff : n1->getIndex() - n2->getIndex();
328 void PropertyList::setValue(const char *s)
331 SGPropertyNode *p = fgGetNode(s, false);
335 throw stdString("node doesn't exist");
336 } catch (const stdString& m) {
337 SG_LOG(SG_GENERAL, SG_DEBUG, "property-list node '" << s << "': " << m);
342 void PropertyList::setCurrent(SGPropertyNode *p)
344 bool same = (_curr == p);