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