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