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