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