]> git.mxchange.org Git - flightgear.git/blob - src/GUI/menubar.cxx
Remove old-preset-* commands; replaced with a general, XML presets
[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 AddWayPoint (puObject *);
96 static bool
97 do_ap_add_waypoint_dialog (const SGPropertyNode * arg)
98 {
99     AddWayPoint(0);
100     return true;
101 }
102
103 extern void PopWayPoint (puObject *);
104 static bool
105 do_ap_pop_waypoint_dialog (const SGPropertyNode * arg)
106 {
107     PopWayPoint(0);
108     return true;
109 }
110
111 extern void ClearRoute (puObject *);
112 static bool
113 do_ap_clear_route_dialog (const SGPropertyNode * arg)
114 {
115     ClearRoute(0);
116     return true;
117 }
118
119 extern void fgAPAdjust (puObject *);
120 static bool
121 do_ap_adjust_dialog (const SGPropertyNode * arg)
122 {
123     fgAPAdjust(0);
124     return true;
125 }
126
127 extern void fgLatLonFormatToggle (puObject *);
128 static bool
129 do_lat_lon_format_dialog (const SGPropertyNode * arg)
130 {
131     fgLatLonFormatToggle(0);
132     return true;
133 }
134
135 extern void helpCb (puObject *);
136 static bool
137 do_help_dialog (const SGPropertyNode * arg)
138 {
139     helpCb(0);
140     return true;
141 }
142
143 static struct {
144     const char * name;
145     SGCommandMgr::command_t command;
146 } deprecated_dialogs [] = {
147     { "old-save-dialog", do_save_dialog },
148     { "old-load-dialog", do_load_dialog },
149     { "old-reinit_dialog", do_reinit_dialog },
150 #if defined(TR_HIRES_SNAP)
151     { "old-hires-snapshot-dialog", do_hires_snapshot_dialog },
152 #endif
153 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
154     { "old-print-dialog", do_print_dialog },
155 #endif
156     { "old-pilot-offset-dialog", do_pilot_offset_dialog },
157     { "old-hud-alpha-dialog", do_hud_alpha_dialog },
158     { "old-properties-dialog", do_properties_dialog },
159     { "old-ap-add-waypoint-dialog", do_ap_add_waypoint_dialog },
160     { "old-ap-pop-waypoint-dialog", do_ap_pop_waypoint_dialog },
161     { "old-ap-clear-route-dialog", do_ap_clear_route_dialog },
162     { "old-ap-adjust-dialog", do_ap_adjust_dialog },
163     { "old-lat-lon-format-dialog", do_lat_lon_format_dialog },
164     { "old-help-dialog", do_help_dialog },
165     { 0, 0 }
166 };
167
168 static void
169 add_deprecated_dialogs ()
170 {
171   SG_LOG(SG_GENERAL, SG_INFO, "Initializing old dialog commands:");
172   for (int i = 0; deprecated_dialogs[i].name != 0; i++) {
173     SG_LOG(SG_GENERAL, SG_INFO, "  " << deprecated_dialogs[i].name);
174     globals->get_commands()->addCommand(deprecated_dialogs[i].name,
175                                         deprecated_dialogs[i].command);
176   }
177 }
178
179
180 \f
181 ////////////////////////////////////////////////////////////////////////
182 // Static functions.
183 ////////////////////////////////////////////////////////////////////////
184
185
186 static void
187 menu_callback (puObject * object)
188 {
189     NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
190     gui->getMenuBar()->fireItem(object);
191 }
192
193
194 \f
195 ////////////////////////////////////////////////////////////////////////
196 // Implementation of FGMenuBar.
197 ////////////////////////////////////////////////////////////////////////
198
199
200 FGMenuBar::FGMenuBar ()
201     : _visible(false),
202       _menuBar(0)
203 {
204 }
205
206 FGMenuBar::~FGMenuBar ()
207 {
208     hide();
209     puDeleteObject(_menuBar);
210
211     int i;
212
213                                 // Delete all the character arrays
214                                 // we were forced to keep around for
215                                 // plib.
216     std::cerr << "Deleting char arrays\n";
217     for (i = 0; i < _char_arrays.size(); i++) {
218         for (int j = 0; _char_arrays[i][j] != 0; j++)
219             free(_char_arrays[i][j]); // added with strdup
220         delete _char_arrays[i];
221     }
222
223                                 // Delete all the callback arrays
224                                 // we were forced to keep around for
225                                 // plib.
226     std::cerr << "Deleting callback arrays\n";
227     for (i = 0; i < _callback_arrays.size(); i++)
228         delete _callback_arrays[i];
229
230                                 // Delete all those bindings
231     std::cerr << "Deleting bindings\n";
232     map<string,vector<FGBinding *> >::iterator it;
233     it = _bindings.begin();
234     for (it = _bindings.begin(); it != _bindings.end(); it++) {
235         std::cerr << "Deleting bindings for " << it->first << std::endl;
236         for (int i = 0; i < it->second.size(); i++)
237             delete it->second[i];
238     }
239
240     std::cerr << "Done.\n";
241 }
242
243 void
244 FGMenuBar::init ()
245 {
246     if (_menuBar != 0)          // FIXME: check if PUI owns the pointer
247         delete _menuBar;
248     make_menubar();
249
250                                 // FIXME: temporary commands to get at
251                                 // old, hard-coded dialogs.
252     add_deprecated_dialogs();
253 }
254
255 void
256 FGMenuBar::show ()
257 {
258     if (_menuBar != 0)
259         _menuBar->reveal();
260     _visible = true;
261 }
262
263 void
264 FGMenuBar::hide ()
265 {
266     if (_menuBar != 0)
267         _menuBar->hide();
268     _visible = false;
269 }
270
271 bool
272 FGMenuBar::isVisible () const
273 {
274     return _visible;
275 }
276
277 void
278 FGMenuBar::fireItem (puObject * item)
279 {
280     const char * name = item->getLegend();
281     vector<FGBinding *> &bindings = _bindings[name];
282     int nBindings = bindings.size();
283
284     for (int i = 0; i < nBindings; i++)
285         bindings[i]->fire();
286 }
287
288 void
289 FGMenuBar::make_menu (SGPropertyNode * node)
290 {
291     const char * name = strdup(node->getStringValue("label"));
292     vector<SGPropertyNode_ptr> item_nodes = node->getChildren("item");
293
294     int array_size = item_nodes.size();
295
296     char ** items = make_char_array(array_size);
297     puCallback * callbacks = make_callback_array(array_size);
298
299     for (int i = 0, j = item_nodes.size() - 1;
300          i < item_nodes.size();
301          i++, j--) {
302         
303                                 // Set up the PUI entries for this item
304         items[j] = strdup((char *)item_nodes[i]->getStringValue("label"));
305         callbacks[j] = menu_callback;
306
307                                 // Load all the bindings for this item
308         vector<SGPropertyNode_ptr> binding_nodes =
309             item_nodes[i]->getChildren("binding");
310
311         for (int k = 0; k < binding_nodes.size(); k++)
312             _bindings[items[j]].push_back(new FGBinding(binding_nodes[k]));
313     }
314
315     _menuBar->add_submenu(name, items, callbacks);
316 }
317
318 void
319 FGMenuBar::make_menubar ()
320 {
321     _menuBar = new puMenuBar;
322     SGPropertyNode props;
323
324     fgLoadProps("gui/menubar.xml", &props);
325     vector<SGPropertyNode_ptr> menu_nodes = props.getChildren("menu");
326     for (int i = 0; i < menu_nodes.size(); i++)
327         make_menu(menu_nodes[i]);
328
329     _menuBar->close();
330     if (_visible)
331         _menuBar->reveal();
332     else
333         _menuBar->hide();
334 }
335
336 char **
337 FGMenuBar::make_char_array (int size)
338 {
339     char ** list = new char*[size+1];
340     for (int i = 0; i <= size; i++)
341         list[i] = 0;
342     _char_arrays.push_back(list);
343     return list;
344 }
345
346 puCallback *
347 FGMenuBar::make_callback_array (int size)
348 {
349     puCallback * list = new puCallback[size+1];
350     for (int i = 0; i <= size; i++)
351         list[i] = 0;
352     _callback_arrays.push_back(list);
353     return list;
354 }
355
356 // end of menubar.cxx