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