]> git.mxchange.org Git - flightgear.git/blob - src/GUI/property_list.cxx
ba25849c88885040726ca1e8397d4285afc6d5fd
[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 int nodeNameCompare(const void *ppNode1, const void *ppNode2)
41 {
42     const SGPropertyNode_ptr pNode1 = *(const SGPropertyNode_ptr *)ppNode1;
43     const SGPropertyNode_ptr pNode2 = *(const SGPropertyNode_ptr *)ppNode2;
44
45     int diff = strcmp(pNode1->getName(), pNode2->getName());
46     return diff ? diff : pNode1->getIndex() - pNode2->getIndex();
47 }
48
49
50 static string getValueTypeString(const SGPropertyNode *node)
51 {
52     string result;
53
54     if (!node)
55         return "unknown";
56
57     SGPropertyNode::Type type = node->getType();
58     if (type == SGPropertyNode::UNSPECIFIED)
59         result = "unspecified";
60     else if (type == SGPropertyNode::NONE)
61         result = "none";
62     else if (type == SGPropertyNode::BOOL)
63         result = "bool";
64     else if (type == SGPropertyNode::INT)
65         result = "int";
66     else if (type == SGPropertyNode::LONG)
67         result = "long";
68     else if (type == SGPropertyNode::FLOAT)
69         result = "float";
70     else if (type == SGPropertyNode::DOUBLE)
71         result = "double";
72     else if (type == SGPropertyNode::STRING)
73         result = "string";
74
75     return result;
76 }
77
78
79
80
81 PropertyList::PropertyList(int minx, int miny, int maxx, int maxy, SGPropertyNode *start) :
82     puList(minx, miny, maxx, maxy, short(0), 20),
83     GUI_ID(FGCLASS_PROPERTYLIST),
84     _curr(start),
85     _flags(fgGetNode("/sim/gui/dialogs/property-browser/show-flags", true)),
86     _return(0),
87     _entries(0),
88     _num_entries(0)
89
90 {
91     _list_box->setUserData(this);
92     _list_box->setCallback(handle_select);
93     _list_box->setValue(0);
94     update();
95 }
96
97
98 PropertyList::~PropertyList()
99 {
100     delete_arrays();
101 }
102
103
104 void PropertyList::delete_arrays()
105 {
106     if (!_entries)
107         return;
108
109     for (int i = 0; i < _num_entries; i++)
110         delete[] _entries[i];
111
112     for (int j = 0; j < _num_children; j++) {
113         if (!_children[j]->nChildren())
114             _children[j]->removeChangeListener(this);
115     }
116
117     delete[] _entries;
118     delete[] _children;
119     _entries = 0;
120     _children = 0;
121 }
122
123
124 void PropertyList::handle_select(puObject *list_box)
125 {
126     PropertyList *prop_list = (PropertyList *)list_box->getUserData();
127     int selected = list_box->getIntegerValue();
128     bool mod_ctrl = fgGetKeyModifiers() & KEYMOD_CTRL;
129
130     if (selected >= 0 && selected < prop_list->_num_entries) {
131         const char *src = prop_list->_entries[selected];
132
133         if (prop_list->dotFiles && (selected < 2)) {
134             if (!strcmp(src, ".")) {
135                 if (mod_ctrl)
136                     prop_list->toggleFlags();
137
138                 prop_list->update();
139                 return;
140
141             } else if (!strcmp(src, "..")) {
142                 SGPropertyNode *parent = prop_list->getCurrent()->getParent();
143                 if (parent) {
144                     if (mod_ctrl)
145                         for (; parent->getParent(); parent = parent->getParent())
146                             ;
147                     prop_list->setCurrent(parent);
148                 }
149                 return;
150             }
151         }
152
153         // we know we're dealing with a regular entry, so convert
154         // it to an index into children[]
155         if (prop_list->dotFiles)
156             selected -= 2;
157
158         SGPropertyNode_ptr child = prop_list->_children[selected];
159         assert(child);
160
161         // check if it's a directory
162         if (child->nChildren()) {
163             prop_list->setCurrent(child);
164             return;
165         }
166
167         // it is a regular property
168         if (child->getType() == SGPropertyNode::BOOL && mod_ctrl) {
169             child->setBoolValue(!child->getBoolValue());
170             prop_list->update();
171         } else
172             prop_list->publish(child);
173
174     } else {
175         // the user clicked on blank screen
176         prop_list->update();
177     }
178 }
179
180
181 void PropertyList::update(bool restore_pos)
182 {
183     int pi;
184     int i;
185
186     delete_arrays();
187     _num_entries = (int)_curr->nChildren();
188
189     // instantiate string objects and add [.] and [..] for subdirs
190     if (!_curr->getParent()) {
191         _entries = new char*[_num_entries + 1];
192         pi = 0;
193         dotFiles = false;
194
195     } else {
196         _num_entries += 2;    // for . and ..
197         _entries = new char*[_num_entries + 1];
198
199         _entries[0] = new char[2];
200         strcpy(_entries[0], ".");
201
202         _entries[1] = new char[3];
203         strcpy(_entries[1], "..");
204
205         pi = 2;
206         dotFiles = true;
207     }
208
209     _num_children = _curr->nChildren();
210     _children = new SGPropertyNode_ptr[_num_children];
211     for (i = 0; i < _num_children; i++)
212         _children[i] = _curr->getChild(i);
213
214     qsort(_children, _num_children, sizeof(_children[0]), nodeNameCompare);
215
216     // Make lists of the children's names, values, etc.
217     for (i = 0; i < _num_children; i++, pi++) {
218         SGPropertyNode *child = _children[i];
219
220         if (child->nChildren() > 0) {
221             stdString name = stdString(child->getDisplayName(true)) + '/';
222             _entries[pi] = new char[name.size() + 1];
223             strcpy(_entries[pi], name.c_str());
224
225         } else {
226             _entries[pi] = 0;       // ensure it's 0 before setting intial value
227             updateTextForEntry(i);
228             child->addChangeListener(this);
229         }
230     }
231
232     _entries[_num_entries] = 0;
233
234     int top = getTopItem();
235     newList(_entries);
236     if (restore_pos)
237         setTopItem(top);
238 }
239
240
241 void PropertyList::updateTextForEntry(int index)
242 {
243     assert((index >= 0) && (index < _num_children));
244     SGPropertyNode_ptr node = _children[index];
245
246     stdString name = node->getDisplayName(true);
247     stdString type = getValueTypeString(node);
248     stdString value = node->getStringValue();
249
250     stdString line = name + " = '" + value + "' (" + type;
251
252     if (_flags->getBoolValue()) {
253         stdString ext;
254         if (!node->getAttribute(SGPropertyNode::READ))
255             ext += 'r';
256         if (!node->getAttribute(SGPropertyNode::WRITE))
257             ext += 'w';
258         if (node->getAttribute(SGPropertyNode::TRACE_READ))
259             ext += 'R';
260         if (node->getAttribute(SGPropertyNode::TRACE_WRITE))
261             ext += 'W';
262         if (node->getAttribute(SGPropertyNode::ARCHIVE))
263             ext += 'A';
264         if (node->getAttribute(SGPropertyNode::USERARCHIVE))
265             ext += 'U';
266         if (node->isTied())
267             ext += 'T';
268         if (ext.size())
269             line += ", " + ext;
270     }
271
272     line += ')';
273
274     if (line.size() >= PUSTRING_MAX)
275         line.resize(PUSTRING_MAX - 1);
276
277     if (dotFiles)
278         index += 2;
279
280     delete[] _entries[index];
281     _entries[index] = new char[line.size() + 1];
282     strcpy(_entries[index], line.c_str());
283 }
284
285
286 void PropertyList::valueChanged(SGPropertyNode *nd)
287 {
288     for (int i = 0; i < _num_children; i++)
289         if (_children[i] == nd) {
290             updateTextForEntry(i);
291             return;
292         }
293 }
294
295
296 void PropertyList::setValue(const char *s)
297 {
298     SGPropertyNode *p = fgGetNode(s, false);
299     if (p)
300         setCurrent(p);
301 }
302
303