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