]> git.mxchange.org Git - flightgear.git/blob - src/GUI/FGPUIMenuBar.cxx
Code cleanups, code updates and fix at least on (possible) devide-by-zero
[flightgear.git] / src / GUI / FGPUIMenuBar.cxx
1 #ifdef HAVE_CONFIG_H
2 #  include <config.h>
3 #endif
4
5 #include <string.h>
6 #include <iostream>
7 #include <plib/pu.h>
8 #include <simgear/debug/logstream.hxx>
9 #include <simgear/structure/SGBinding.hxx>
10 #include <simgear/props/props_io.hxx>
11
12 #include <Main/globals.hxx>
13 #include <Main/locale.hxx>
14 #include <Main/fg_props.hxx>
15
16 #include "new_gui.hxx"
17 #include "FGPUIMenuBar.hxx"
18
19 using std::vector;
20 using std::string;
21 using std::map;\f
22 ////////////////////////////////////////////////////////////////////////
23 // FIXME!!
24 //
25 // Deprecated wrappers for old menu commands.
26 //
27 // DO NOT ADD TO THESE.  THEY WILL BE DELETED SOON!
28 //
29 // These are defined in gui_funcs.cxx.  They should be replaced with
30 // user-configured dialogs and new commands where necessary.
31 ////////////////////////////////////////////////////////////////////////
32
33 #if defined(TR_HIRES_SNAP)
34 extern void dumpHiResSnapShot ();
35 static bool
36 do_hires_snapshot_dialog (const SGPropertyNode * arg)
37 {
38     dumpHiResSnapShot();
39     return true;
40 }
41 #endif // TR_HIRES_SNAP
42
43 static struct {
44     const char * name;
45     SGCommandMgr::command_t command;
46 } deprecated_dialogs [] = {
47 #if defined(TR_HIRES_SNAP)
48     { "old-hires-snapshot-dialog", do_hires_snapshot_dialog },
49 #endif
50     { 0, 0 }
51 };
52
53 static void
54 add_deprecated_dialogs ()
55 {
56   SG_LOG(SG_GENERAL, SG_INFO, "Initializing old dialog commands:");
57   for (int i = 0; deprecated_dialogs[i].name != 0; i++) {
58     SG_LOG(SG_GENERAL, SG_INFO, "  " << deprecated_dialogs[i].name);
59     globals->get_commands()->addCommand(deprecated_dialogs[i].name,
60                                         deprecated_dialogs[i].command);
61   }
62 }
63
64
65 \f
66 ////////////////////////////////////////////////////////////////////////
67 // Static functions.
68 ////////////////////////////////////////////////////////////////////////
69
70
71 static void
72 menu_callback (puObject * object)
73 {
74     NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
75     FGPUIMenuBar* mb = static_cast<FGPUIMenuBar*>(gui->getMenuBar());
76     mb->fireItem(object);
77 }
78
79 ////////////////////////////////////////////////////////////////////////
80 // Implementation of FGPUIMenuBar.
81 ////////////////////////////////////////////////////////////////////////
82
83
84 FGPUIMenuBar::FGPUIMenuBar ()
85     : _visible(false),
86       _menuBar(0)
87 {
88 }
89
90 FGPUIMenuBar::~FGPUIMenuBar ()
91 {
92     destroy_menubar();
93 }
94
95 void
96 FGPUIMenuBar::init ()
97 {
98     bool visible = _visible;
99     destroy_menubar();
100
101     make_menubar();
102                                 // FIXME: temporary commands to get at
103                                 // old, hard-coded dialogs.
104     add_deprecated_dialogs();
105
106     // Keep menu visible during gui-redraw
107     if( visible )
108       show();
109 }
110
111 void
112 FGPUIMenuBar::show ()
113 {
114     if (_menuBar != 0)
115         _menuBar->reveal();
116     _visible = true;
117 }
118
119 void
120 FGPUIMenuBar::hide ()
121 {
122     if (_menuBar != 0)
123         _menuBar->hide();
124     _visible = false;
125 }
126
127 bool
128 FGPUIMenuBar::isVisible () const
129 {
130     return _visible;
131 }
132
133 void
134 FGPUIMenuBar::fireItem (puObject * item)
135 {
136     const char * name = item->getLegend();
137     vector<SGBinding *> &bindings = _bindings[name];
138     int nBindings = bindings.size();
139
140     for (int i = 0; i < nBindings; i++)
141         bindings[i]->fire();
142 }
143
144 void
145 FGPUIMenuBar::make_menu (SGPropertyNode * node)
146 {
147     string s = getLocalizedLabel(node);
148
149     // hack: map at least some UTF-8 characters to Latin1, since FG fonts are
150     // Latin1 (or plain ASCII, which is a subset). This hack can be removed once
151     // the PLIB/OSG port is complete (OSG has full UTF-8 support! :) ).
152     FGLocale::utf8toLatin1(s);
153
154     const char* name = strdup(s.c_str());
155
156     vector<SGPropertyNode_ptr> item_nodes = node->getChildren("item");
157
158     int array_size = item_nodes.size();
159
160     char ** items = make_char_array(array_size);
161     puCallback * callbacks = make_callback_array(array_size);
162
163     for (unsigned int i = 0, j = item_nodes.size() - 1;
164          i < item_nodes.size();
165          i++, j--) {
166
167                                 // Set up the PUI entries for this item
168         string label = getLocalizedLabel(item_nodes[i]);
169         FGLocale::utf8toLatin1(label);
170
171         // append the keyboard hint to the menu entry
172         const char* key = item_nodes[i]->getStringValue("key", 0);
173         if (key)
174         {
175             label.append("           <");
176             label.append(key);
177             label.append(">");
178         }
179         items[j] = strdup(label.c_str());
180         callbacks[j] = menu_callback;
181
182                                 // Load all the bindings for this item
183         vector<SGPropertyNode_ptr> bindings = item_nodes[i]->getChildren("binding");
184         SGPropertyNode * dest = fgGetNode("/sim/bindings/menu", true);
185       
186         for (unsigned int k = 0; k < bindings.size(); k++) {
187             unsigned int m = 0;
188             SGPropertyNode_ptr binding;
189             while (dest->getChild("binding", m))
190                 m++;
191
192             binding = dest->getChild("binding", m, true);
193             copyProperties(bindings[k], binding);
194             _bindings[items[j]].push_back(new SGBinding(binding, globals->get_props()));
195         }
196     }
197
198     _menuBar->add_submenu(name, items, callbacks);
199 }
200
201 void
202 FGPUIMenuBar::make_menubar ()
203 {
204     SGPropertyNode *targetpath;
205    
206     targetpath = fgGetNode("/sim/menubar/default",true);
207     // fgLoadProps("gui/menubar.xml", targetpath);
208     
209     /* NOTE: there is no check to see whether there's any usable data at all
210      *
211      * This would also have the advantage of being able to create some kind of
212      * 'fallback' menu - just in case that either menubar.xml is empty OR that
213      * its XML data is not valid, that way we would avoid displaying an
214      * unusable menubar without any functionality - if we decided to add another
215      * char * element to the commands structure in
216      *  $FG_SRC/src/Main/fgcommands.cxx 
217      * we could additionally save each function's (short) description and use
218      * this as label for the fallback PUI menubar item labels - as a workaround
219      * one might simply use the internal fgcommands and put them into the 
220      * fallback menu, so that the user is at least able to re-init the menu
221      * loading - just in case there was some malformed XML in it
222      * (it happend to me ...)
223      */
224     
225     make_menubar(targetpath);
226 }
227
228 /* WARNING: We aren't yet doing any validation of what's found - but since
229  * this isn't done with menubar.xml either, it should not really matter
230  * right now. Although one should later on consider to validate the
231  * contents, whether they are representing a 'legal' menubar structure.
232  */
233 void
234 FGPUIMenuBar::make_menubar(SGPropertyNode * props) 
235 {    
236     // Just in case.
237     destroy_menubar();
238     _menuBar = new puMenuBar;
239
240     vector<SGPropertyNode_ptr> menu_nodes = props->getChildren("menu");
241     for (unsigned int i = 0; i < menu_nodes.size(); i++)
242         make_menu(menu_nodes[i]);
243
244     _menuBar->close();
245     make_object_map(props);
246
247     if (_visible)
248         _menuBar->reveal();
249     else
250         _menuBar->hide();
251 }
252
253 void
254 FGPUIMenuBar::destroy_menubar ()
255 {
256     if ( _menuBar == 0 )
257         return;
258
259     hide();
260     puDeleteObject(_menuBar);
261
262     unsigned int i;
263
264                                 // Delete all the character arrays
265                                 // we were forced to keep around for
266                                 // plib.
267     SG_LOG(SG_GENERAL, SG_BULK, "Deleting char arrays");
268     for (i = 0; i < _char_arrays.size(); i++) {
269         for (int j = 0; _char_arrays[i][j] != 0; j++) {
270             free(_char_arrays[i][j]); // added with strdup
271             _char_arrays[i][j] = 0;
272         }
273         delete[] _char_arrays[i];
274         _char_arrays[i] = 0;
275     }
276   
277                                 // Delete all the callback arrays
278                                 // we were forced to keep around for
279                                 // plib.
280     SG_LOG(SG_GENERAL, SG_BULK, "Deleting callback arrays");
281     for (i = 0; i < _callback_arrays.size(); i++)
282         delete[] _callback_arrays[i];
283
284                                 // Delete all those bindings
285     SG_LOG(SG_GENERAL, SG_BULK, "Deleting bindings");
286     map<string,vector<SGBinding *> >::iterator it;
287     for (it = _bindings.begin(); it != _bindings.end(); it++) {
288         SG_LOG(SG_GENERAL, SG_BULK, "Deleting bindings for " << it->first);
289         for ( i = 0; i < it->second.size(); i++ )
290             delete it->second[i];
291     }
292
293     _menuBar = NULL;
294     _bindings.clear();
295     _char_arrays.clear();
296     _callback_arrays.clear();
297     SG_LOG(SG_GENERAL, SG_BULK, "Done.");
298 }
299
300 void
301 FGPUIMenuBar::make_object_map(SGPropertyNode * node)
302 {
303     unsigned int menu_index = 0;
304     vector<SGPropertyNode_ptr> menus = node->getChildren("menu");
305     for (puObject *obj = ((puGroup *)_menuBar)->getFirstChild();
306             obj; obj = obj->getNextObject()) {
307
308         // skip puPopupMenus. They are also children of _menuBar,
309         // but we access them via getUserData()  (see below)
310         if (!(obj->getType() & PUCLASS_ONESHOT))
311             continue;
312
313         if (menu_index >= menus.size()) {
314             SG_LOG(SG_GENERAL, SG_WARN, "'menu' object without node: "
315                     << node->getPath() << "/menu[" << menu_index << ']');
316             return;
317         }
318
319         SGPropertyNode *menu = menus.at(menu_index);
320         _objects[menu->getPath()] = obj;
321         add_enabled_listener(menu);
322
323         puGroup *popup = (puGroup *)obj->getUserData();
324         if (!popup)
325             continue;
326
327         // the entries are for some reason reversed (last first), and we
328         // don't know yet how many there will be; so we collect first
329         vector<puObject *> e;
330         for (puObject *me = popup->getFirstChild(); me; me = me->getNextObject())
331             e.push_back(me);
332
333         vector<SGPropertyNode_ptr> items = menu->getChildren("item");
334         for (unsigned int i = 0; i < e.size(); i++) {
335             if (i >= items.size()) {
336                 SG_LOG(SG_GENERAL, SG_WARN, "'item' object without node: "
337                         << menu->getPath() << "/item[" << i << ']');
338                 break;
339             }
340             SGPropertyNode *item = items.at(e.size() - i - 1);
341             _objects[item->getPath()] = e[i];
342             add_enabled_listener(item);
343         }
344         menu_index++;
345     }
346 }
347
348 namespace { 
349   
350   struct EnabledListener : SGPropertyChangeListener {
351     void valueChanged(SGPropertyNode *node) {
352         NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
353         if (!gui)
354             return;
355         FGPUIMenuBar* menubar = static_cast<FGPUIMenuBar*>(gui->getMenuBar());
356         if (menubar)
357             menubar->enable_item(node->getParent(), node->getBoolValue());
358     }
359 };
360
361 } // of anonymous namespace
362
363 void
364 FGPUIMenuBar::add_enabled_listener(SGPropertyNode * node)
365 {
366     if (!node->hasValue("enabled"))
367         node->setBoolValue("enabled", true);
368
369     enable_item(node, node->getBoolValue("enabled"));
370     node->getNode("enabled")->addChangeListener(new EnabledListener());
371 }
372
373 bool
374 FGPUIMenuBar::enable_item(const SGPropertyNode * node, bool state)
375 {
376     string path = node->getPath();
377     if (_objects.find(path) == _objects.end()) {
378         SG_LOG(SG_GENERAL, SG_ALERT, "Trying to enable/disable "
379             "non-existent menu item for node `" << path << '\'');
380         return false;
381     }
382     puObject *object = _objects[path];
383     if (state)
384         object->activate();
385     else
386         object->greyOut();
387
388     return true;
389 }
390
391 char **
392 FGPUIMenuBar::make_char_array (int size)
393 {
394     char ** list = new char*[size+1];
395     for (int i = 0; i <= size; i++)
396         list[i] = 0;
397     _char_arrays.push_back(list);
398     return list;
399 }
400
401 puCallback *
402 FGPUIMenuBar::make_callback_array (int size)
403 {
404     puCallback * list = new puCallback[size+1];
405     for (int i = 0; i <= size; i++)
406         list[i] = 0;
407     _callback_arrays.push_back(list);
408     return list;
409 }
410
411 // end of menubar.cxx