]> git.mxchange.org Git - flightgear.git/blob - src/GUI/menubar.cxx
Add in some temporary commands to get at the old, hard-coded 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 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 NewAltitude (puObject *);
161 static bool
162 do_ap_altitude_dialog (const SGPropertyNode * arg)
163 {
164     NewAltitude(0);
165     return true;
166 }
167
168 extern void NewHeading (puObject *);
169 static bool
170 do_ap_heading_dialog (const SGPropertyNode * arg)
171 {
172     NewHeading(0);
173     return true;
174 }
175
176 extern void AddWayPoint (puObject *);
177 static bool
178 do_ap_add_waypoint_dialog (const SGPropertyNode * arg)
179 {
180     AddWayPoint(0);
181     return true;
182 }
183
184 extern void PopWayPoint (puObject *);
185 static bool
186 do_ap_pop_waypoint_dialog (const SGPropertyNode * arg)
187 {
188     PopWayPoint(0);
189     return true;
190 }
191
192 extern void ClearRoute (puObject *);
193 static bool
194 do_ap_clear_route_dialog (const SGPropertyNode * arg)
195 {
196     ClearRoute(0);
197     return true;
198 }
199
200 extern void fgAPAdjust (puObject *);
201 static bool
202 do_ap_adjust_dialog (const SGPropertyNode * arg)
203 {
204     fgAPAdjust(0);
205     return true;
206 }
207
208 extern void fgLatLonFormatToggle (puObject *);
209 static bool
210 do_lat_lon_format_dialog (const SGPropertyNode * arg)
211 {
212     fgLatLonFormatToggle(0);
213     return true;
214 }
215
216 extern void helpCb (puObject *);
217 static bool
218 do_help_dialog (const SGPropertyNode * arg)
219 {
220     helpCb(0);
221     return true;
222 }
223
224 static struct {
225     const char * name;
226     SGCommandMgr::command_t command;
227 } deprecated_dialogs [] = {
228     { "old-save-dialog", do_save_dialog },
229     { "old-load-dialog", do_load_dialog },
230     { "old-reinit_dialog", do_reinit_dialog },
231 #if defined(TR_HIRES_SNAP)
232     { "old-hires-snapshot-dialog", do_hires_snapshot_dialog },
233 #endif
234     { "old-snapshot-dialog", do_snapshot_dialog },
235 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
236     { "old-print-dialog", do_print_dialog },
237 #endif
238     { "old-pilot-offset-dialog", do_pilot_offset_dialog },
239     { "old-hud-alpha-dialog", do_hud_alpha_dialog },
240     { "old-properties-dialog", do_properties_dialog },
241     { "old-preset-airport-dialog", do_preset_airport_dialog },
242     { "old-preset-runway-dialog", do_preset_runway_dialog },
243     { "old-preset-offset-distance-dialog", do_preset_offset_distance_dialog },
244     { "old-preset-altitude-dialog", do_preset_altitude_dialog },
245     { "old-preset-glideslope-dialog", do_preset_glideslope_dialog },
246     { "old-preset-airspeed-dialog", do_preset_airspeed_dialog },
247     { "old-preset-commit-dialog", do_preset_commit_dialog },
248     { "old-ap-altitude-dialog", do_ap_altitude_dialog },
249     { "old-ap-heading-dialog", do_ap_heading_dialog },
250     { "old-ap-add-waypoint-dialog", do_ap_add_waypoint_dialog },
251     { "old-ap-pop-waypoint-dialog", do_ap_pop_waypoint_dialog },
252     { "old-ap-clear-route-dialog", do_ap_clear_route_dialog },
253     { "old-ap-adjust-dialog", do_ap_adjust_dialog },
254     { "old-lat-lon-format-dialog", do_lat_lon_format_dialog },
255     { "old-help-dialog", do_help_dialog },
256     { 0, 0 }
257 };
258
259 static void
260 add_deprecated_dialogs ()
261 {
262   SG_LOG(SG_GENERAL, SG_INFO, "Initializing old dialog commands:");
263   for (int i = 0; deprecated_dialogs[i].name != 0; i++) {
264     SG_LOG(SG_GENERAL, SG_INFO, "  " << deprecated_dialogs[i].name);
265     globals->get_commands()->addCommand(deprecated_dialogs[i].name,
266                                         deprecated_dialogs[i].command);
267   }
268 }
269
270
271 \f
272 ////////////////////////////////////////////////////////////////////////
273 // Static functions.
274 ////////////////////////////////////////////////////////////////////////
275
276
277 static void
278 menu_callback (puObject * object)
279 {
280     NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
281     gui->getMenuBar()->fireItem(object);
282 }
283
284
285 \f
286 ////////////////////////////////////////////////////////////////////////
287 // Implementation of FGMenuBar.
288 ////////////////////////////////////////////////////////////////////////
289
290
291 FGMenuBar::FGMenuBar ()
292     : _visible(false),
293       _menuBar(0)
294 {
295 }
296
297 FGMenuBar::~FGMenuBar ()
298 {
299     hide();
300     delete _menuBar;            // FIXME: check if PUI owns the pointer
301
302                                 // Delete all those bindings
303     map<string,vector<FGBinding *> >::iterator it;
304     it = _bindings.begin();
305     while (it != _bindings.end()) {
306         for (int i = 0; i < it->second.size(); i++)
307             delete it->second[i];
308     }
309 }
310
311 void
312 FGMenuBar::init ()
313 {
314     if (_menuBar != 0)          // FIXME: check if PUI owns the pointer
315         delete _menuBar;
316     make_menubar();
317
318                                 // FIXME: temporary commands to get at
319                                 // old, hard-coded dialogs.
320     add_deprecated_dialogs();
321 }
322
323 void
324 FGMenuBar::show ()
325 {
326     if (_menuBar != 0) {
327         _menuBar->reveal();
328         _visible = true;
329     } else {
330         SG_LOG(SG_GENERAL, SG_ALERT, "No menu bar to show");
331         _visible = false;
332     }
333 }
334
335 void
336 FGMenuBar::hide ()
337 {
338     if (_menuBar != 0) {
339         _menuBar->hide();
340     } else {
341         SG_LOG(SG_GENERAL, SG_ALERT, "No menu bar to show");
342     }
343     _visible = false;
344 }
345
346 bool
347 FGMenuBar::isVisible () const
348 {
349     return _visible;
350 }
351
352 bool
353 FGMenuBar::fireItem (puObject * item)
354 {
355     const char * name = item->getLegend();
356     vector<FGBinding *> &bindings = _bindings[name];
357
358     for (int i = 0; i < bindings.size(); i++)
359         bindings[i]->fire();
360 }
361
362 void
363 FGMenuBar::make_menu (SGPropertyNode_ptr node)
364 {
365     const char * name = strdup(node->getStringValue("label"));
366     vector<SGPropertyNode_ptr> item_nodes = node->getChildren("item");
367
368     int array_size = item_nodes.size() + 1;
369
370     char ** items = new char*[array_size];
371     puCallback * callbacks = new puCallback[array_size];
372
373     for (int i = 0, j = item_nodes.size() - 1;
374          i < item_nodes.size();
375          i++, j--) {
376         
377                                 // Set up the PUI entries for this item
378         items[j] = strdup((char *)item_nodes[i]->getStringValue("label"));
379         callbacks[j] = menu_callback;
380
381                                 // Load all the bindings for this item
382         vector<SGPropertyNode_ptr> binding_nodes =
383             item_nodes[i]->getChildren("binding");
384         for (int k = 0; k < binding_nodes.size(); k++)
385             _bindings[items[j]].push_back(new FGBinding(binding_nodes[k]));
386     }
387
388     items[item_nodes.size()] = 0;
389     callbacks[item_nodes.size()] = 0;
390
391     _menuBar->add_submenu(name, items, callbacks);
392 }
393
394 void
395 FGMenuBar::make_menubar ()
396 {
397     _menuBar = new puMenuBar;
398     SGPropertyNode props;
399
400     fgLoadProps("gui/menubar.xml", &props);
401     vector<SGPropertyNode_ptr> menu_nodes = props.getChildren("menu");
402     for (int i = 0; i < menu_nodes.size(); i++)
403         make_menu(menu_nodes[i]);
404
405     _menuBar->close();
406 }
407
408 // end of menubar.cxx