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