]> git.mxchange.org Git - flightgear.git/blob - src/GUI/property_list.cxx
- require plib 1.8.5
[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 <sstream>
31 #include STL_IOMANIP
32 #include STL_STRING
33 SG_USING_STD(string);
34 typedef string stdString;      // puObject has a "string" member
35
36 #include <Main/fg_os.hxx>      // fgGetKeyModifiers()
37 #include <Main/fg_props.hxx>
38
39 #include "property_list.hxx"
40
41
42 static string getValueTypeString(const SGPropertyNode *node)
43 {
44     string result;
45
46     SGPropertyNode::Type type = node->getType();
47     if (type == SGPropertyNode::UNSPECIFIED)
48         result = "unspecified";
49     else if (type == SGPropertyNode::NONE)
50         result = "none";
51     else if (type == SGPropertyNode::BOOL)
52         result = "bool";
53     else if (type == SGPropertyNode::INT)
54         result = "int";
55     else if (type == SGPropertyNode::LONG)
56         result = "long";
57     else if (type == SGPropertyNode::FLOAT)
58         result = "float";
59     else if (type == SGPropertyNode::DOUBLE)
60         result = "double";
61     else if (type == SGPropertyNode::STRING)
62         result = "string";
63
64     return result;
65 }
66
67
68 static void dumpProperties(const SGPropertyNode *node)
69 {
70     cout << node->getPath() << '/' << endl;
71     for (int i = 0; i < node->nChildren(); i++) {
72         const SGPropertyNode *c = node->getChild(i);
73         SGPropertyNode::Type type = c->getType();
74         if (type == SGPropertyNode::ALIAS || c->nChildren())
75             continue;
76
77         int index = c->getIndex();
78         cout << std::setw(11) << getValueTypeString(c) << "  " << c->getName();
79         if (index > 0)
80             cout << '[' << index << ']';
81         cout << " = ";
82
83         switch (c->getType()) {
84         case SGPropertyNode::DOUBLE:
85         case SGPropertyNode::FLOAT:
86             cout << std::setprecision(15) << c->getDoubleValue();
87             break;
88         case SGPropertyNode::LONG:
89         case SGPropertyNode::INT:
90             cout << c->getLongValue();
91             break;
92         case SGPropertyNode::BOOL:
93             cout << (c->getBoolValue() ? "true" : "false");
94             break;
95         case SGPropertyNode::STRING:
96             cout << '"' << c->getStringValue() << '"';
97             break;
98         case SGPropertyNode::NONE:
99             break;
100         default:
101             cout << '\'' << c->getStringValue() << '\'';
102         }
103         cout << endl;
104     }
105     cout << endl;
106 }
107
108
109 static void sanitize(stdString& s)
110 {
111     stdString r = s;
112     s = "";
113     for (unsigned i = 0; i < r.size(); i++) {
114         if (r[i] == '\a')
115             s += "\\a";
116         else if (r[i] == '\b')
117             s += "\\b";
118         else if (r[i] == '\t')
119             s += "\\t";
120         else if (r[i] == '\n')
121             s += "\\n";
122         else if (r[i] == '\v')
123             s += "\\v";
124         else if (r[i] == '\f')
125             s += "\\f";
126         else if (r[i] == '\r')
127             s += "\\r";
128         else if (r[i] == '\'')
129             s += "\'";
130         else if (r[i] == '\\')
131             s += "\\";
132         else if (isprint(r[i]))
133             s += r[i];
134         else {
135             const char *hex = "0123456789abcdef";
136             int c = r[i] & 255;
137             s += stdString("\\x") + hex[c / 16] + hex[c % 16];
138         }
139     }
140 }
141
142
143
144
145 PropertyList::PropertyList(int minx, int miny, int maxx, int maxy, SGPropertyNode *start) :
146     puaList(minx, miny, maxx, maxy, short(0), 20),
147     GUI_ID(FGCLASS_PROPERTYLIST),
148     _curr(start),
149     _return(0),
150     _entries(0),
151     _num_entries(0),
152     _verbose(false)
153
154 {
155     _list_box->setUserData(this);
156     _list_box->setCallback(handle_select);
157     _list_box->setValue(0);
158     update();
159 }
160
161
162 PropertyList::~PropertyList()
163 {
164     delete_arrays();
165 }
166
167
168 void PropertyList::delete_arrays()
169 {
170     if (!_entries)
171         return;
172
173     for (int i = 0; i < _num_entries; i++)
174         delete[] _entries[i];
175
176     delete[] _entries;
177     delete[] _children;
178     _entries = 0;
179     _children = 0;
180 }
181
182
183 void PropertyList::handle_select(puObject *list_box)
184 {
185     PropertyList *prop_list = (PropertyList *)list_box->getUserData();
186     int selected = list_box->getIntegerValue();
187     int mod_ctrl = fgGetKeyModifiers() & KEYMOD_CTRL;
188     int mod_shift = fgGetKeyModifiers() & KEYMOD_SHIFT;
189
190     if (selected >= 0 && selected < prop_list->_num_entries) {
191         const char *src = prop_list->_entries[selected];
192
193         if (prop_list->_dot_files && (selected < 2)) {
194             if (!strcmp(src, ".")) {
195                 if (mod_ctrl)
196                     prop_list->toggleVerbosity();
197                 else if (mod_shift)
198                     dumpProperties(prop_list->_curr);
199
200                 prop_list->update();
201                 return;
202
203             } else if (!strcmp(src, "..")) {
204                 SGPropertyNode *parent = prop_list->getCurrent()->getParent();
205                 if (parent) {
206                     if (mod_ctrl)
207                         for (; parent->getParent(); parent = parent->getParent())
208                             ;
209                     prop_list->setCurrent(parent);
210                 }
211                 return;
212             }
213         }
214
215         // we know we're dealing with a regular entry, so convert
216         // it to an index into children[]
217         if (prop_list->_dot_files)
218             selected -= 2;
219
220         SGPropertyNode_ptr child = prop_list->_children[selected].node;
221
222         // check if it's a directory
223         if (child->nChildren()) {
224             prop_list->setTopItem(0);
225             prop_list->setCurrent(child);
226             return;
227         }
228
229         // it is a regular property
230         if (child->getType() == SGPropertyNode::BOOL && mod_ctrl) {
231             child->setBoolValue(!child->getBoolValue());
232             prop_list->update(true);
233         } else
234             prop_list->publish(child);
235
236     } else {
237         // the user clicked on blank screen
238         prop_list->update(true);
239     }
240 }
241
242
243 void PropertyList::update(bool restore_pos)
244 {
245     int pi;
246
247     delete_arrays();
248     _num_entries = (int)_curr->nChildren();
249
250     // instantiate string objects and add [.] and [..] for subdirs
251     if (!_curr->getParent()) {
252         _entries = new char*[_num_entries + 1];
253         pi = 0;
254         _dot_files = false;
255
256     } else {
257         _num_entries += 2;    // for . and ..
258         _entries = new char*[_num_entries + 1];
259
260         _entries[0] = new char[2];
261         strcpy(_entries[0], ".");
262
263         _entries[1] = new char[3];
264         strcpy(_entries[1], "..");
265
266         pi = 2;
267         _dot_files = true;
268     }
269
270     int i;
271     _num_children = _curr->nChildren();
272     _children = new NodeData[_num_children];
273     for (i = 0; i < _num_children; i++)
274         _children[i].node = _curr->getChild(i);
275
276     qsort(_children, _num_children, sizeof(_children[0]), nodeNameCompare);
277
278     // Make lists of the children's names, values, etc.
279     for (i = 0; i < _num_children; i++, pi++) {
280         _children[i].text = &_entries[pi];
281         _entries[pi] = 0;    // make it deletable
282         updateTextForEntry(_children[i]);
283         _children[i].setListener(this);
284     }
285
286     _entries[_num_entries] = 0;
287
288     int top = getTopItem();
289     newList(_entries);
290     if (restore_pos)
291         setTopItem(top);
292 }
293
294
295 void PropertyList::updateTextForEntry(NodeData& data)
296 {
297     SGPropertyNode *node = data.node;
298     stdString name = node->getDisplayName(true);
299     stdString type = getValueTypeString(node);
300     stdString value = node->getStringValue();
301
302     std::ostringstream line;
303     line << name;
304
305     int children = node->nChildren();
306     if (children)
307         line << '/';
308
309     if (!children || (_verbose && node->hasValue())) {
310         if (node->getType() == SGPropertyNode::STRING
311                 || node->getType() == SGPropertyNode::UNSPECIFIED)
312             sanitize(value);
313
314         line << " = '" << value << "' (" << type;
315
316         if (_verbose) {
317             stdString ext;
318             if (!node->getAttribute(SGPropertyNode::READ))
319                 ext += 'r';
320             if (!node->getAttribute(SGPropertyNode::WRITE))
321                 ext += 'w';
322             if (node->getAttribute(SGPropertyNode::TRACE_READ))
323                 ext += 'R';
324             if (node->getAttribute(SGPropertyNode::TRACE_WRITE))
325                 ext += 'W';
326             if (node->getAttribute(SGPropertyNode::ARCHIVE))
327                 ext += 'A';
328             if (node->getAttribute(SGPropertyNode::USERARCHIVE))
329                 ext += 'U';
330             if (node->isTied())
331                 ext += 'T';
332
333             if (!ext.empty())
334                 line << ", " << ext;
335
336             int num = node->nListeners();
337             if (data.listener)
338                 num--;
339             if (num)
340                 line << ", L" << num;
341         }
342         line << ')';
343     }
344
345     stdString out = line.str();
346     if (out.size() >= PUSTRING_MAX)
347         out.resize(PUSTRING_MAX - 1);
348
349     delete[] *data.text;
350     *data.text = new char[out.size() + 1];
351     strcpy(*data.text, out.c_str());
352 }
353
354
355 void PropertyList::valueChanged(SGPropertyNode *nd)
356 {
357     for (int i = 0; i < _num_children; i++)
358         if (_children[i].node == nd) {
359             updateTextForEntry(_children[i]);
360             return;
361         }
362 }
363
364
365 int PropertyList::nodeNameCompare(const void *p1, const void *p2)
366 {
367     const SGPropertyNode *n1 = (*(const NodeData *)p1).node;
368     const SGPropertyNode *n2 = (*(const NodeData *)p2).node;
369
370     int diff = strcmp(n1->getName(), n2->getName());
371     return diff ? diff : n1->getIndex() - n2->getIndex();
372 }
373
374
375 void PropertyList::setValue(const char *s)
376 {
377     try {
378         SGPropertyNode *p = fgGetNode(s, false);
379         if (p)
380             setCurrent(p);
381         else
382             throw stdString("node doesn't exist");
383     } catch (const stdString& m) {
384         SG_LOG(SG_GENERAL, SG_DEBUG, "property-list node '" << s << "': " << m);
385     }
386 }
387
388
389 void PropertyList::setCurrent(SGPropertyNode *p)
390 {
391     bool same = (_curr == p);
392     _return = _curr = p;
393     update(same);
394     if (!same)
395         publish(p);
396 }
397