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