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 puList(minx, miny, maxx, maxy, short(0), 20),
105 GUI_ID(FGCLASS_PROPERTYLIST),
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;
146 int mod_shift = fgGetKeyModifiers() & KEYMOD_SHIFT;
148 if (selected >= 0 && selected < prop_list->_num_entries) {
149 const char *src = prop_list->_entries[selected];
151 if (prop_list->_dot_files && (selected < 2)) {
152 if (!strcmp(src, ".")) {
154 prop_list->toggleFlags();
156 writeProperties(cerr, prop_list->_curr, true);
161 } else if (!strcmp(src, "..")) {
162 SGPropertyNode *parent = prop_list->getCurrent()->getParent();
165 for (; parent->getParent(); parent = parent->getParent())
167 prop_list->setCurrent(parent);
173 // we know we're dealing with a regular entry, so convert
174 // it to an index into children[]
175 if (prop_list->_dot_files)
178 SGPropertyNode_ptr child = prop_list->_children[selected].node;
181 // check if it's a directory
182 if (child->nChildren()) {
183 prop_list->setTopItem(0);
184 prop_list->setCurrent(child);
188 // it is a regular property
189 if (child->getType() == SGPropertyNode::BOOL && mod_ctrl) {
190 child->setBoolValue(!child->getBoolValue());
191 prop_list->update(true);
193 prop_list->publish(child);
196 // the user clicked on blank screen
197 prop_list->update(true);
202 void PropertyList::update(bool restore_pos)
207 _num_entries = (int)_curr->nChildren();
209 // instantiate string objects and add [.] and [..] for subdirs
210 if (!_curr->getParent()) {
211 _entries = new char*[_num_entries + 1];
216 _num_entries += 2; // for . and ..
217 _entries = new char*[_num_entries + 1];
219 _entries[0] = new char[2];
220 strcpy(_entries[0], ".");
222 _entries[1] = new char[3];
223 strcpy(_entries[1], "..");
230 _num_children = _curr->nChildren();
231 _children = new NodeData[_num_children];
232 for (i = 0; i < _num_children; i++)
233 _children[i].node = _curr->getChild(i);
235 qsort(_children, _num_children, sizeof(_children[0]), nodeNameCompare);
237 // Make lists of the children's names, values, etc.
238 for (i = 0; i < _num_children; i++, pi++) {
239 SGPropertyNode *child = _children[i].node;
241 if (child->nChildren() > 0) {
242 stdString name = stdString(child->getDisplayName(true)) + '/';
243 _entries[pi] = new char[name.size() + 1];
244 strcpy(_entries[pi], name.c_str());
247 _entries[pi] = 0; // make it delete-able
248 updateTextForEntry(i);
249 _children[i].setListener(this);
253 _entries[_num_entries] = 0;
255 int top = getTopItem();
262 void PropertyList::updateTextForEntry(int index)
264 assert((index >= 0) && (index < _num_children));
265 SGPropertyNode_ptr node = _children[index].node;
267 stdString name = node->getDisplayName(true);
268 stdString type = getValueTypeString(node);
269 stdString value = node->getStringValue();
271 if (node->getType() == SGPropertyNode::STRING
272 || node->getType() == SGPropertyNode::UNSPECIFIED)
275 stdString line = name + " = '" + value + "' (" + type;
279 if (!node->getAttribute(SGPropertyNode::READ))
281 if (!node->getAttribute(SGPropertyNode::WRITE))
283 if (node->getAttribute(SGPropertyNode::TRACE_READ))
285 if (node->getAttribute(SGPropertyNode::TRACE_WRITE))
287 if (node->getAttribute(SGPropertyNode::ARCHIVE))
289 if (node->getAttribute(SGPropertyNode::USERARCHIVE))
299 if (line.size() >= PUSTRING_MAX)
300 line.resize(PUSTRING_MAX - 1);
305 delete[] _entries[index];
306 _entries[index] = new char[line.size() + 1];
307 strcpy(_entries[index], line.c_str());
311 void PropertyList::valueChanged(SGPropertyNode *nd)
313 for (int i = 0; i < _num_children; i++)
314 if (_children[i].node == nd) {
315 updateTextForEntry(i);
321 int PropertyList::nodeNameCompare(const void *p1, const void *p2)
323 const SGPropertyNode *n1 = (*(const NodeData *)p1).node;
324 const SGPropertyNode *n2 = (*(const NodeData *)p2).node;
326 int diff = strcmp(n1->getName(), n2->getName());
327 return diff ? diff : n1->getIndex() - n2->getIndex();
331 void PropertyList::setValue(const char *s)
334 SGPropertyNode *p = fgGetNode(s, false);
338 throw stdString("node doesn't exist");
339 } catch (const stdString& m) {
340 SG_LOG(SG_GENERAL, SG_DEBUG, "property-list node '" << s << "': " << m);
345 void PropertyList::setCurrent(SGPropertyNode *p)
347 bool same = (_curr == p);