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