]> git.mxchange.org Git - flightgear.git/blob - src/GUI/property_list.cxx
s/isascii/isprint/
[flightgear.git] / src / GUI / property_list.cxx
1 // Implementation of the <property-list> widget.
2 //
3 // Copyright (C) 2001  Steve BAKER
4 // Copyright (C) 2001  Jim WILSON
5 // Copyright (C) 2006  Melchior FRANZ
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #include <simgear/compiler.h>
29
30 #include STL_STRING
31 SG_USING_STD(string);
32 typedef string stdString;      // puObject has a "string" member
33
34 #include <Main/fg_os.hxx>      // fgGetKeyModifiers()
35 #include <Main/fg_props.hxx>
36
37 #include "property_list.hxx"
38
39
40 static string getValueTypeString(const SGPropertyNode *node)
41 {
42     string result;
43     assert(node);
44
45     SGPropertyNode::Type type = node->getType();
46     if (type == SGPropertyNode::UNSPECIFIED)
47         result = "unspecified";
48     else if (type == SGPropertyNode::NONE)
49         result = "none";
50     else if (type == SGPropertyNode::BOOL)
51         result = "bool";
52     else if (type == SGPropertyNode::INT)
53         result = "int";
54     else if (type == SGPropertyNode::LONG)
55         result = "long";
56     else if (type == SGPropertyNode::FLOAT)
57         result = "float";
58     else if (type == SGPropertyNode::DOUBLE)
59         result = "double";
60     else if (type == SGPropertyNode::STRING)
61         result = "string";
62
63     return result;
64 }
65
66
67 static void sanitize(stdString& s)
68 {
69     stdString r = s;
70     s = "";
71     for (unsigned i = 0; i < r.size(); i++) {
72         if (r[i] == '\a')
73             s += "\\a";
74         else if (r[i] == '\b')
75             s += "\\b";
76         else if (r[i] == '\t')
77             s += "\\t";
78         else if (r[i] == '\n')
79             s += "\\n";
80         else if (r[i] == '\v')
81             s += "\\v";
82         else if (r[i] == '\f')
83             s += "\\f";
84         else if (r[i] == '\r')
85             s += "\\r";
86         else if (r[i] == '\'')
87             s += "\'";
88         else if (r[i] == '\\')
89             s += "\\";
90         else if (isprint(r[i]))
91             s += r[i];
92         else {
93             const char *hex = "0123456789abcdef";
94             int c = r[i] & 255;
95             s += stdString("\\x") + hex[c / 16] + hex[c % 16];
96         }
97     }
98 }
99
100
101
102
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),
106     _curr(start),
107     _flags(fgGetNode("/sim/gui/dialogs/property-browser/show-flags", true)),
108     _return(0),
109     _entries(0),
110     _num_entries(0)
111
112 {
113     _list_box->setUserData(this);
114     _list_box->setCallback(handle_select);
115     _list_box->setValue(0);
116     update();
117 }
118
119
120 PropertyList::~PropertyList()
121 {
122     delete_arrays();
123 }
124
125
126 void PropertyList::delete_arrays()
127 {
128     if (!_entries)
129         return;
130
131     for (int i = 0; i < _num_entries; i++)
132         delete[] _entries[i];
133
134     delete[] _entries;
135     delete[] _children;
136     _entries = 0;
137     _children = 0;
138 }
139
140
141 void PropertyList::handle_select(puObject *list_box)
142 {
143     PropertyList *prop_list = (PropertyList *)list_box->getUserData();
144     int selected = list_box->getIntegerValue();
145     int mod_ctrl = fgGetKeyModifiers() & KEYMOD_CTRL;
146
147     if (selected >= 0 && selected < prop_list->_num_entries) {
148         const char *src = prop_list->_entries[selected];
149
150         if (prop_list->dotFiles && (selected < 2)) {
151             if (!strcmp(src, ".")) {
152                 if (mod_ctrl)
153                     prop_list->toggleFlags();
154
155                 prop_list->update();
156                 return;
157
158             } else if (!strcmp(src, "..")) {
159                 SGPropertyNode *parent = prop_list->getCurrent()->getParent();
160                 if (parent) {
161                     if (mod_ctrl)
162                         for (; parent->getParent(); parent = parent->getParent())
163                             ;
164                     prop_list->setCurrent(parent);
165                 }
166                 return;
167             }
168         }
169
170         // we know we're dealing with a regular entry, so convert
171         // it to an index into children[]
172         if (prop_list->dotFiles)
173             selected -= 2;
174
175         SGPropertyNode_ptr child = prop_list->_children[selected].node;
176         assert(child);
177
178         // check if it's a directory
179         if (child->nChildren()) {
180             prop_list->setTopItem(0);
181             prop_list->setCurrent(child);
182             return;
183         }
184
185         // it is a regular property
186         if (child->getType() == SGPropertyNode::BOOL && mod_ctrl) {
187             child->setBoolValue(!child->getBoolValue());
188             prop_list->update(true);
189         } else
190             prop_list->publish(child);
191
192     } else {
193         // the user clicked on blank screen
194         prop_list->update(true);
195     }
196 }
197
198
199 void PropertyList::update(bool restore_pos)
200 {
201     int pi;
202
203     delete_arrays();
204     _num_entries = (int)_curr->nChildren();
205
206     // instantiate string objects and add [.] and [..] for subdirs
207     if (!_curr->getParent()) {
208         _entries = new char*[_num_entries + 1];
209         pi = 0;
210         dotFiles = false;
211
212     } else {
213         _num_entries += 2;    // for . and ..
214         _entries = new char*[_num_entries + 1];
215
216         _entries[0] = new char[2];
217         strcpy(_entries[0], ".");
218
219         _entries[1] = new char[3];
220         strcpy(_entries[1], "..");
221
222         pi = 2;
223         dotFiles = true;
224     }
225
226     int i;
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);
231
232     qsort(_children, _num_children, sizeof(_children[0]), nodeNameCompare);
233
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;
237
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());
242
243         } else {
244             _entries[pi] = 0;       // make it delete-able
245             updateTextForEntry(i);
246             _children[i].setListener(this);
247         }
248     }
249
250     _entries[_num_entries] = 0;
251
252     int top = getTopItem();
253     newList(_entries);
254     if (restore_pos)
255         setTopItem(top);
256 }
257
258
259 void PropertyList::updateTextForEntry(int index)
260 {
261     assert((index >= 0) && (index < _num_children));
262     SGPropertyNode_ptr node = _children[index].node;
263
264     stdString name = node->getDisplayName(true);
265     stdString type = getValueTypeString(node);
266     stdString value = node->getStringValue();
267
268     if (node->getType() == SGPropertyNode::STRING
269             || node->getType() == SGPropertyNode::UNSPECIFIED)
270         sanitize(value);
271
272     stdString line = name + " = '" + value + "' (" + type;
273
274     if (_flags->getBoolValue()) {
275         stdString ext;
276         if (!node->getAttribute(SGPropertyNode::READ))
277             ext += 'r';
278         if (!node->getAttribute(SGPropertyNode::WRITE))
279             ext += 'w';
280         if (node->getAttribute(SGPropertyNode::TRACE_READ))
281             ext += 'R';
282         if (node->getAttribute(SGPropertyNode::TRACE_WRITE))
283             ext += 'W';
284         if (node->getAttribute(SGPropertyNode::ARCHIVE))
285             ext += 'A';
286         if (node->getAttribute(SGPropertyNode::USERARCHIVE))
287             ext += 'U';
288         if (node->isTied())
289             ext += 'T';
290         if (ext.size())
291             line += ", " + ext;
292     }
293
294     line += ')';
295
296     if (line.size() >= PUSTRING_MAX)
297         line.resize(PUSTRING_MAX - 1);
298
299     if (dotFiles)
300         index += 2;
301
302     delete[] _entries[index];
303     _entries[index] = new char[line.size() + 1];
304     strcpy(_entries[index], line.c_str());
305 }
306
307
308 void PropertyList::valueChanged(SGPropertyNode *nd)
309 {
310     for (int i = 0; i < _num_children; i++)
311         if (_children[i].node == nd) {
312             updateTextForEntry(i);
313             return;
314         }
315 }
316
317
318 int PropertyList::nodeNameCompare(const void *p1, const void *p2)
319 {
320     const SGPropertyNode *n1 = (*(const NodeData *)p1).node;
321     const SGPropertyNode *n2 = (*(const NodeData *)p2).node;
322
323     int diff = strcmp(n1->getName(), n2->getName());
324     return diff ? diff : n1->getIndex() - n2->getIndex();
325 }
326
327
328 void PropertyList::setValue(const char *s)
329 {
330     try {
331         SGPropertyNode *p = fgGetNode(s, false);
332         if (p)
333             setCurrent(p);
334         else
335             throw stdString("node doesn't exist");
336     } catch (const stdString& m) {
337         SG_LOG(SG_GENERAL, SG_DEBUG, "property-list node '" << s << "': " << m);
338     }
339 }
340
341
342 void PropertyList::setCurrent(SGPropertyNode *p)
343 {
344     bool same = (_curr == p);
345     _return = _curr = p;
346     update(same);
347     if (!same)
348         publish(p);
349 }
350