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