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