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