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