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