]> git.mxchange.org Git - flightgear.git/blob - src/GUI/menubar.cxx
b79894cd0c599c2c4ee565d8625cc34d88de5a4f
[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     delete _menuBar;            // FIXME: check if PUI owns the pointer
283
284                                 // Delete all those bindings
285     map<string,vector<FGBinding *>*>::iterator it;
286     it = _bindings.begin();
287     while (it != _bindings.end()) {
288         for (int i = 0; i < it->second->size(); i++)
289             delete (*it->second)[i];
290         delete it->second;
291     }
292 }
293
294 void
295 FGMenuBar::init ()
296 {
297     if (_menuBar != 0)          // FIXME: check if PUI owns the pointer
298         delete _menuBar;
299     make_menubar();
300
301                                 // FIXME: temporary commands to get at
302                                 // old, hard-coded dialogs.
303     add_deprecated_dialogs();
304 }
305
306 void
307 FGMenuBar::show ()
308 {
309     if (_menuBar != 0)
310         _menuBar->reveal();
311     _visible = true;
312 }
313
314 void
315 FGMenuBar::hide ()
316 {
317     if (_menuBar != 0)
318         _menuBar->hide();
319     _visible = false;
320 }
321
322 bool
323 FGMenuBar::isVisible () const
324 {
325     return _visible;
326 }
327
328 void
329 FGMenuBar::fireItem (puObject * item)
330 {
331     const char * name = item->getLegend();
332     vector<FGBinding *> * bindings = _bindings[name];
333
334     for (int i = 0; i < bindings->size(); i++)
335         (*bindings)[i]->fire();
336 }
337
338 void
339 FGMenuBar::make_menu (SGPropertyNode_ptr node)
340 {
341     const char * name = strdup(node->getStringValue("label"));
342     vector<SGPropertyNode_ptr> item_nodes = node->getChildren("item");
343
344     int array_size = item_nodes.size() + 1;
345
346     char ** items = new char*[array_size];
347     puCallback * callbacks = new puCallback[array_size];
348
349     for (int i = 0, j = item_nodes.size() - 1;
350          i < item_nodes.size();
351          i++, j--) {
352         
353                                 // Set up the PUI entries for this item
354         items[j] = strdup((char *)item_nodes[i]->getStringValue("label"));
355         callbacks[j] = menu_callback;
356
357                                 // Load all the bindings for this item
358         vector<SGPropertyNode_ptr> binding_nodes =
359             item_nodes[i]->getChildren("binding");
360
361         if (_bindings[items[j]] == 0)
362             _bindings[items[j]] = new vector<FGBinding *>;
363         for (int k = 0; k < binding_nodes.size(); k++)
364             _bindings[items[j]]->push_back(new FGBinding(binding_nodes[k]));
365     }
366
367     items[item_nodes.size()] = 0;
368     callbacks[item_nodes.size()] = 0;
369
370     _menuBar->add_submenu(name, items, callbacks);
371 }
372
373 void
374 FGMenuBar::make_menubar ()
375 {
376     _menuBar = new puMenuBar;
377     SGPropertyNode props;
378
379     fgLoadProps("gui/menubar.xml", &props);
380     vector<SGPropertyNode_ptr> menu_nodes = props.getChildren("menu");
381     for (int i = 0; i < menu_nodes.size(); i++)
382         make_menu(menu_nodes[i]);
383
384     _menuBar->close();
385     if (_visible)
386         _menuBar->reveal();
387     else
388         _menuBar->hide();
389 }
390
391 // end of menubar.cxx