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)
47 SGPropertyNode::Type type = node->getType();
48 if (type == SGPropertyNode::UNSPECIFIED)
49 result = "unspecified";
50 else if (type == SGPropertyNode::NONE)
52 else if (type == SGPropertyNode::BOOL)
54 else if (type == SGPropertyNode::INT)
56 else if (type == SGPropertyNode::LONG)
58 else if (type == SGPropertyNode::FLOAT)
60 else if (type == SGPropertyNode::DOUBLE)
62 else if (type == SGPropertyNode::STRING)
69 static void sanitize(stdString& s)
73 for (unsigned i = 0; i < r.size(); i++) {
76 else if (r[i] == '\b')
78 else if (r[i] == '\t')
80 else if (r[i] == '\n')
82 else if (r[i] == '\v')
84 else if (r[i] == '\f')
86 else if (r[i] == '\r')
88 else if (r[i] == '\'')
90 else if (r[i] == '\\')
92 else if (isascii(r[i]))
95 const char *hex = "0123456789abcdef";
97 s += stdString("\\x") + hex[c / 16] + hex[c % 16];
105 PropertyList::PropertyList(int minx, int miny, int maxx, int maxy, SGPropertyNode *start) :
106 puList(minx, miny, maxx, maxy, short(0), 20),
107 GUI_ID(FGCLASS_PROPERTYLIST),
109 _flags(fgGetNode("/sim/gui/dialogs/property-browser/show-flags", true)),
115 _list_box->setUserData(this);
116 _list_box->setCallback(handle_select);
117 _list_box->setValue(0);
122 PropertyList::~PropertyList()
128 void PropertyList::delete_arrays()
133 for (int i = 0; i < _num_entries; i++)
134 delete[] _entries[i];
143 void PropertyList::handle_select(puObject *list_box)
145 PropertyList *prop_list = (PropertyList *)list_box->getUserData();
146 int selected = list_box->getIntegerValue();
147 int mod_ctrl = fgGetKeyModifiers() & KEYMOD_CTRL;
149 if (selected >= 0 && selected < prop_list->_num_entries) {
150 const char *src = prop_list->_entries[selected];
152 if (prop_list->dotFiles && (selected < 2)) {
153 if (!strcmp(src, ".")) {
155 prop_list->toggleFlags();
160 } else if (!strcmp(src, "..")) {
161 SGPropertyNode *parent = prop_list->getCurrent()->getParent();
164 for (; parent->getParent(); parent = parent->getParent())
166 prop_list->setCurrent(parent);
172 // we know we're dealing with a regular entry, so convert
173 // it to an index into children[]
174 if (prop_list->dotFiles)
177 SGPropertyNode_ptr child = prop_list->_children[selected].node;
180 // check if it's a directory
181 if (child->nChildren()) {
182 prop_list->setCurrent(child);
186 // it is a regular property
187 if (child->getType() == SGPropertyNode::BOOL && mod_ctrl) {
188 child->setBoolValue(!child->getBoolValue());
189 prop_list->update(true);
191 prop_list->publish(child);
194 // the user clicked on blank screen
195 prop_list->update(true);
200 void PropertyList::update(bool restore_pos)
205 _num_entries = (int)_curr->nChildren();
207 // instantiate string objects and add [.] and [..] for subdirs
208 if (!_curr->getParent()) {
209 _entries = new char*[_num_entries + 1];
214 _num_entries += 2; // for . and ..
215 _entries = new char*[_num_entries + 1];
217 _entries[0] = new char[2];
218 strcpy(_entries[0], ".");
220 _entries[1] = new char[3];
221 strcpy(_entries[1], "..");
228 _num_children = _curr->nChildren();
229 _children = new NodeData[_num_children];
230 for (i = 0; i < _num_children; i++)
231 _children[i].node = _curr->getChild(i);
233 qsort(_children, _num_children, sizeof(_children[0]), nodeNameCompare);
235 // Make lists of the children's names, values, etc.
236 for (i = 0; i < _num_children; i++, pi++) {
237 SGPropertyNode *child = _children[i].node;
239 if (child->nChildren() > 0) {
240 stdString name = stdString(child->getDisplayName(true)) + '/';
241 _entries[pi] = new char[name.size() + 1];
242 strcpy(_entries[pi], name.c_str());
245 _entries[pi] = 0; // ensure it's 0 before setting intial value
246 updateTextForEntry(i);
247 _children[i].setListener(this);
251 _entries[_num_entries] = 0;
253 int top = getTopItem();
260 void PropertyList::updateTextForEntry(int index)
262 assert((index >= 0) && (index < _num_children));
263 SGPropertyNode_ptr node = _children[index].node;
265 stdString name = node->getDisplayName(true);
266 stdString type = getValueTypeString(node);
267 stdString value = node->getStringValue();
269 if (node->getType() == SGPropertyNode::STRING
270 || node->getType() == SGPropertyNode::UNSPECIFIED)
273 stdString line = name + " = '" + value + "' (" + type;
275 if (_flags->getBoolValue()) {
277 if (!node->getAttribute(SGPropertyNode::READ))
279 if (!node->getAttribute(SGPropertyNode::WRITE))
281 if (node->getAttribute(SGPropertyNode::TRACE_READ))
283 if (node->getAttribute(SGPropertyNode::TRACE_WRITE))
285 if (node->getAttribute(SGPropertyNode::ARCHIVE))
287 if (node->getAttribute(SGPropertyNode::USERARCHIVE))
297 if (line.size() >= PUSTRING_MAX)
298 line.resize(PUSTRING_MAX - 1);
303 delete[] _entries[index];
304 _entries[index] = new char[line.size() + 1];
305 strcpy(_entries[index], line.c_str());
309 void PropertyList::valueChanged(SGPropertyNode *nd)
311 for (int i = 0; i < _num_children; i++)
312 if (_children[i].node == nd) {
313 updateTextForEntry(i);
319 int PropertyList::nodeNameCompare(const void *ppNode1, const void *ppNode2)
321 const SGPropertyNode_ptr pNode1 = (*(const NodeData *)ppNode1).node;
322 const SGPropertyNode_ptr pNode2 = (*(const NodeData *)ppNode2).node;
324 int diff = strcmp(pNode1->getName(), pNode2->getName());
325 return diff ? diff : pNode1->getIndex() - pNode2->getIndex();
329 void PropertyList::setValue(const char *s)
332 SGPropertyNode *p = fgGetNode(s, false);
336 throw stdString("node doesn't exist");
337 } catch (const stdString& m) {
338 SG_LOG(SG_GENERAL, SG_DEBUG, "property-list node '" << s << "': " << m);
343 void PropertyList::setCurrent(SGPropertyNode *p)
345 bool same = (_curr == p);