]> git.mxchange.org Git - flightgear.git/blob - src/GUI/menubar.cxx
Added support for reinit(), to reload the configuration files without
[flightgear.git] / src / GUI / menubar.cxx
1 #include <string.h>
2 #include <iostream>
3 #include <plib/pu.h>
4 #include <simgear/debug/logstream.hxx>
5
6 #include <Main/globals.hxx>
7 #include <Main/fg_props.hxx>
8
9 #include <Input/input.hxx>
10
11 #include "new_gui.hxx"
12 #include "menubar.hxx"
13
14
15 \f
16 ////////////////////////////////////////////////////////////////////////
17 // FIXME!!
18 //
19 // Deprecated wrappers for old menu commands.
20 //
21 // DO NOT ADD TO THESE.  THEY WILL BE DELETED SOON!
22 //
23 // These are defined in gui_funcs.cxx.  They should be replaced with
24 // user-configured dialogs and new commands where necessary.
25 ////////////////////////////////////////////////////////////////////////
26
27 extern void saveFlight (puObject *);
28 static bool
29 do_save_dialog (const SGPropertyNode * arg)
30 {
31     saveFlight(0);
32     return true;
33 }
34
35 extern void loadFlight (puObject *);
36 static bool
37 do_load_dialog (const SGPropertyNode * arg)
38 {
39     loadFlight(0);
40     return true;
41 }
42
43 extern void reInit (puObject *);
44 static bool
45 do_reinit_dialog (const SGPropertyNode * arg)
46 {
47     reInit(0);
48     return true;
49 }
50
51 #if defined(TR_HIRES_SNAP)
52 extern void dumpHiResSnapShot (puObject *);
53 static bool
54 do_hires_snapshot_dialog (const SGPropertyNode * arg)
55 {
56     dumpHiResSnapShot(0);
57     return true;
58 }
59 #endif // TR_HIRES_SNAP
60
61 extern void dumpSnapShot (puObject *);
62 static bool
63 do_snapshot_dialog (const SGPropertyNode * arg)
64 {
65     dumpSnapShot(0);
66     return true;
67 }
68
69
70 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
71 extern void printScreen (puObject *);
72 static bool
73 do_print_dialog (const SGPropertyNode * arg)
74 {
75     printScreen(0);
76     return true;
77 }
78 #endif
79
80 extern void PilotOffsetAdjust (puObject *);
81 static bool
82 do_pilot_offset_dialog (const SGPropertyNode * arg)
83 {
84     PilotOffsetAdjust(0);
85     return true;
86 }
87
88 extern void fgHUDalphaAdjust (puObject *);
89 static bool
90 do_hud_alpha_dialog (const SGPropertyNode * arg)
91 {
92     fgHUDalphaAdjust(0);
93     return true;
94 }
95
96 extern void prop_pickerView (puObject *);
97 static bool
98 do_properties_dialog (const SGPropertyNode * arg)
99 {
100     prop_pickerView(0);
101     return true;
102 }
103
104 extern void fgPresetAirport (puObject *);
105 static bool
106 do_preset_airport_dialog (const SGPropertyNode * arg)
107 {
108     fgPresetAirport(0);
109     return true;
110 }
111
112 extern void fgPresetRunway (puObject *);
113 static bool
114 do_preset_runway_dialog (const SGPropertyNode * arg)
115 {
116     fgPresetRunway(0);
117     return true;
118 }
119
120 extern void fgPresetOffsetDistance (puObject *);
121 static bool
122 do_preset_offset_distance_dialog (const SGPropertyNode * arg)
123 {
124     fgPresetOffsetDistance(0);
125     return true;
126 }
127
128 extern void fgPresetAltitude (puObject *);
129 static bool
130 do_preset_altitude_dialog (const SGPropertyNode * arg)
131 {
132     fgPresetAltitude(0);
133     return true;
134 }
135
136 extern void fgPresetGlideslope (puObject *);
137 static bool
138 do_preset_glideslope_dialog (const SGPropertyNode * arg)
139 {
140     fgPresetGlideslope(0);
141     return true;
142 }
143
144 extern void fgPresetAirspeed (puObject *);
145 static bool
146 do_preset_airspeed_dialog (const SGPropertyNode * arg)
147 {
148     fgPresetAirspeed(0);
149     return true;
150 }
151
152 extern void fgPresetCommit (puObject *);
153 static bool
154 do_preset_commit_dialog (const SGPropertyNode * arg)
155 {
156     fgPresetCommit(0);
157     return true;
158 }
159
160 extern void AddWayPoint (puObject *);
161 static bool
162 do_ap_add_waypoint_dialog (const SGPropertyNode * arg)
163 {
164     AddWayPoint(0);
165     return true;
166 }
167
168 extern void PopWayPoint (puObject *);
169 static bool
170 do_ap_pop_waypoint_dialog (const SGPropertyNode * arg)
171 {
172     PopWayPoint(0);
173     return true;
174 }
175
176 extern void ClearRoute (puObject *);
177 static bool
178 do_ap_clear_route_dialog (const SGPropertyNode * arg)
179 {
180     ClearRoute(0);
181     return true;
182 }
183
184 extern void fgAPAdjust (puObject *);
185 static bool
186 do_ap_adjust_dialog (const SGPropertyNode * arg)
187 {
188     fgAPAdjust(0);
189     return true;
190 }
191
192 extern void fgLatLonFormatToggle (puObject *);
193 static bool
194 do_lat_lon_format_dialog (const SGPropertyNode * arg)
195 {
196     fgLatLonFormatToggle(0);
197     return true;
198 }
199
200 extern void helpCb (puObject *);
201 static bool
202 do_help_dialog (const SGPropertyNode * arg)
203 {
204     helpCb(0);
205     return true;
206 }
207
208 static struct {
209     const char * name;
210     SGCommandMgr::command_t command;
211 } deprecated_dialogs [] = {
212     { "old-save-dialog", do_save_dialog },
213     { "old-load-dialog", do_load_dialog },
214     { "old-reinit_dialog", do_reinit_dialog },
215 #if defined(TR_HIRES_SNAP)
216     { "old-hires-snapshot-dialog", do_hires_snapshot_dialog },
217 #endif
218     { "old-snapshot-dialog", do_snapshot_dialog },
219 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
220     { "old-print-dialog", do_print_dialog },
221 #endif
222     { "old-pilot-offset-dialog", do_pilot_offset_dialog },
223     { "old-hud-alpha-dialog", do_hud_alpha_dialog },
224     { "old-properties-dialog", do_properties_dialog },
225     { "old-preset-airport-dialog", do_preset_airport_dialog },
226     { "old-preset-runway-dialog", do_preset_runway_dialog },
227     { "old-preset-offset-distance-dialog", do_preset_offset_distance_dialog },
228     { "old-preset-altitude-dialog", do_preset_altitude_dialog },
229     { "old-preset-glideslope-dialog", do_preset_glideslope_dialog },
230     { "old-preset-airspeed-dialog", do_preset_airspeed_dialog },
231     { "old-preset-commit-dialog", do_preset_commit_dialog },
232     { "old-ap-add-waypoint-dialog", do_ap_add_waypoint_dialog },
233     { "old-ap-pop-waypoint-dialog", do_ap_pop_waypoint_dialog },
234     { "old-ap-clear-route-dialog", do_ap_clear_route_dialog },
235     { "old-ap-adjust-dialog", do_ap_adjust_dialog },
236     { "old-lat-lon-format-dialog", do_lat_lon_format_dialog },
237     { "old-help-dialog", do_help_dialog },
238     { 0, 0 }
239 };
240
241 static void
242 add_deprecated_dialogs ()
243 {
244   SG_LOG(SG_GENERAL, SG_INFO, "Initializing old dialog commands:");
245   for (int i = 0; deprecated_dialogs[i].name != 0; i++) {
246     SG_LOG(SG_GENERAL, SG_INFO, "  " << deprecated_dialogs[i].name);
247     globals->get_commands()->addCommand(deprecated_dialogs[i].name,
248                                         deprecated_dialogs[i].command);
249   }
250 }
251
252
253 \f
254 ////////////////////////////////////////////////////////////////////////
255 // Static functions.
256 ////////////////////////////////////////////////////////////////////////
257
258
259 static void
260 menu_callback (puObject * object)
261 {
262     NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
263     gui->getMenuBar()->fireItem(object);
264 }
265
266
267 \f
268 ////////////////////////////////////////////////////////////////////////
269 // Implementation of FGMenuBar.
270 ////////////////////////////////////////////////////////////////////////
271
272
273 FGMenuBar::FGMenuBar ()
274     : _visible(false),
275       _menuBar(0)
276 {
277 }
278
279 FGMenuBar::~FGMenuBar ()
280 {
281     hide();
282     puDeleteObject(_menuBar);
283
284     int i;
285
286                                 // Delete all the character arrays
287                                 // we were forced to keep around for
288                                 // plib.
289     std::cerr << "Deleting char arrays\n";
290     for (i = 0; i < _char_arrays.size(); i++) {
291         for (int j = 0; _char_arrays[i][j] != 0; j++)
292             free(_char_arrays[i][j]); // added with strdup
293         delete _char_arrays[i];
294     }
295
296                                 // Delete all the callback arrays
297                                 // we were forced to keep around for
298                                 // plib.
299     std::cerr << "Deleting callback arrays\n";
300     for (i = 0; i < _callback_arrays.size(); i++)
301         delete _callback_arrays[i];
302
303                                 // Delete all those bindings
304     std::cerr << "Deleting bindings\n";
305     map<string,vector<FGBinding *> >::iterator it;
306     it = _bindings.begin();
307     for (it = _bindings.begin(); it != _bindings.end(); it++) {
308         std::cerr << "Deleting bindings for " << it->first << std::endl;
309         for (int i = 0; i < it->second.size(); i++)
310             delete it->second[i];
311     }
312
313     std::cerr << "Done.\n";
314 }
315
316 void
317 FGMenuBar::init ()
318 {
319     if (_menuBar != 0)          // FIXME: check if PUI owns the pointer
320         delete _menuBar;
321     make_menubar();
322
323                                 // FIXME: temporary commands to get at
324                                 // old, hard-coded dialogs.
325     add_deprecated_dialogs();
326 }
327
328 void
329 FGMenuBar::show ()
330 {
331     if (_menuBar != 0)
332         _menuBar->reveal();
333     _visible = true;
334 }
335
336 void
337 FGMenuBar::hide ()
338 {
339     if (_menuBar != 0)
340         _menuBar->hide();
341     _visible = false;
342 }
343
344 bool
345 FGMenuBar::isVisible () const
346 {
347     return _visible;
348 }
349
350 void
351 FGMenuBar::fireItem (puObject * item)
352 {
353     const char * name = item->getLegend();
354     vector<FGBinding *> &bindings = _bindings[name];
355     int nBindings = bindings.size();
356
357     for (int i = 0; i < nBindings; i++)
358         bindings[i]->fire();
359 }
360
361 void
362 FGMenuBar::make_menu (SGPropertyNode_ptr node)
363 {
364     const char * name = strdup(node->getStringValue("label"));
365     vector<SGPropertyNode_ptr> item_nodes = node->getChildren("item");
366
367     int array_size = item_nodes.size();
368
369     char ** items = make_char_array(array_size);
370     puCallback * callbacks = make_callback_array(array_size);
371
372     for (int i = 0, j = item_nodes.size() - 1;
373          i < item_nodes.size();
374          i++, j--) {
375         
376                                 // Set up the PUI entries for this item
377         items[j] = strdup((char *)item_nodes[i]->getStringValue("label"));
378         callbacks[j] = menu_callback;
379
380                                 // Load all the bindings for this item
381         vector<SGPropertyNode_ptr> binding_nodes =
382             item_nodes[i]->getChildren("binding");
383
384         for (int k = 0; k < binding_nodes.size(); k++)
385             _bindings[items[j]].push_back(new FGBinding(binding_nodes[k]));
386     }
387
388     _menuBar->add_submenu(name, items, callbacks);
389 }
390
391 void
392 FGMenuBar::make_menubar ()
393 {
394     _menuBar = new puMenuBar;
395     SGPropertyNode props;
396
397     fgLoadProps("gui/menubar.xml", &props);
398     vector<SGPropertyNode_ptr> menu_nodes = props.getChildren("menu");
399     for (int i = 0; i < menu_nodes.size(); i++)
400         make_menu(menu_nodes[i]);
401
402     _menuBar->close();
403     if (_visible)
404         _menuBar->reveal();
405     else
406         _menuBar->hide();
407 }
408
409 char **
410 FGMenuBar::make_char_array (int size)
411 {
412     char ** list = new char*[size+1];
413     for (int i = 0; i <= size; i++)
414         list[i] = 0;
415     _char_arrays.push_back(list);
416     return list;
417 }
418
419 puCallback *
420 FGMenuBar::make_callback_array (int size)
421 {
422     puCallback * list = new puCallback[size+1];
423     for (int i = 0; i <= size; i++)
424         list[i] = 0;
425     _callback_arrays.push_back(list);
426     return list;
427 }
428
429 // end of menubar.cxx