]> git.mxchange.org Git - flightgear.git/blob - src/GUI/menubar.cxx
remove obsolete "lon/lat format" commands that have been labeled "deprecated"
[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 <Input/input.hxx>
11 #include <Main/globals.hxx>
12
13 #include "new_gui.hxx"
14 #include "menubar.hxx"
15
16
17 \f
18 ////////////////////////////////////////////////////////////////////////
19 // FIXME!!
20 //
21 // Deprecated wrappers for old menu commands.
22 //
23 // DO NOT ADD TO THESE.  THEY WILL BE DELETED SOON!
24 //
25 // These are defined in gui_funcs.cxx.  They should be replaced with
26 // user-configured dialogs and new commands where necessary.
27 ////////////////////////////////////////////////////////////////////////
28
29 extern void reInit (puObject *);
30 static bool
31 do_reinit_dialog (const SGPropertyNode * arg)
32 {
33     reInit(0);
34     return true;
35 }
36
37 #if defined(TR_HIRES_SNAP)
38 extern void dumpHiResSnapShot (puObject *);
39 static bool
40 do_hires_snapshot_dialog (const SGPropertyNode * arg)
41 {
42     dumpHiResSnapShot(0);
43     return true;
44 }
45 #endif // TR_HIRES_SNAP
46
47 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
48 extern void printScreen (puObject *);
49 static bool
50 do_print_dialog (const SGPropertyNode * arg)
51 {
52     printScreen(0);
53     return true;
54 }
55 #endif
56
57 extern void prop_pickerView (puObject *);
58 static bool
59 do_properties_dialog (const SGPropertyNode * arg)
60 {
61     prop_pickerView(0);
62     return true;
63 }
64
65 extern void helpCb (puObject *);
66 static bool
67 do_help_dialog (const SGPropertyNode * arg)
68 {
69     helpCb(0);
70     return true;
71 }
72
73 static struct {
74     const char * name;
75     SGCommandMgr::command_t command;
76 } deprecated_dialogs [] = {
77     { "old-reinit-dialog", do_reinit_dialog },
78 #if defined(TR_HIRES_SNAP)
79     { "old-hires-snapshot-dialog", do_hires_snapshot_dialog },
80 #endif
81 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
82     { "old-print-dialog", do_print_dialog },
83 #endif
84     { "old-properties-dialog", do_properties_dialog },
85     { "old-help-dialog", do_help_dialog },
86     { 0, 0 }
87 };
88
89 static void
90 add_deprecated_dialogs ()
91 {
92   SG_LOG(SG_GENERAL, SG_INFO, "Initializing old dialog commands:");
93   for (int i = 0; deprecated_dialogs[i].name != 0; i++) {
94     SG_LOG(SG_GENERAL, SG_INFO, "  " << deprecated_dialogs[i].name);
95     globals->get_commands()->addCommand(deprecated_dialogs[i].name,
96                                         deprecated_dialogs[i].command);
97   }
98 }
99
100
101 \f
102 ////////////////////////////////////////////////////////////////////////
103 // Static functions.
104 ////////////////////////////////////////////////////////////////////////
105
106
107 static void
108 menu_callback (puObject * object)
109 {
110     NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
111     gui->getMenuBar()->fireItem(object);
112 }
113
114
115 \f
116 ////////////////////////////////////////////////////////////////////////
117 // Implementation of FGMenuBar.
118 ////////////////////////////////////////////////////////////////////////
119
120
121 FGMenuBar::FGMenuBar ()
122     : _visible(false),
123       _menuBar(0)
124 {
125 }
126
127 FGMenuBar::~FGMenuBar ()
128 {
129     destroy_menubar();
130 }
131
132 void
133 FGMenuBar::init ()
134 {
135     delete _menuBar;            // FIXME: check if PUI owns the pointer
136     make_menubar();
137                                 // FIXME: temporary commands to get at
138                                 // old, hard-coded dialogs.
139     add_deprecated_dialogs();
140 }
141
142 void
143 FGMenuBar::show ()
144 {
145     if (_menuBar != 0)
146         _menuBar->reveal();
147     _visible = true;
148 }
149
150 void
151 FGMenuBar::hide ()
152 {
153     if (_menuBar != 0)
154         _menuBar->hide();
155     _visible = false;
156 }
157
158 bool
159 FGMenuBar::isVisible () const
160 {
161     return _visible;
162 }
163
164 void
165 FGMenuBar::fireItem (puObject * item)
166 {
167     const char * name = item->getLegend();
168     vector<FGBinding *> &bindings = _bindings[name];
169     int nBindings = bindings.size();
170
171     for (int i = 0; i < nBindings; i++)
172         bindings[i]->fire();
173 }
174
175 void
176 FGMenuBar::make_menu (SGPropertyNode * node)
177 {
178     const char * name = strdup(node->getStringValue("label"));
179     vector<SGPropertyNode_ptr> item_nodes = node->getChildren("item");
180
181     int array_size = item_nodes.size();
182
183     char ** items = make_char_array(array_size);
184     puCallback * callbacks = make_callback_array(array_size);
185
186     for (unsigned int i = 0, j = item_nodes.size() - 1;
187          i < item_nodes.size();
188          i++, j--) {
189
190                                 // Set up the PUI entries for this item
191         items[j] = strdup((char *)item_nodes[i]->getStringValue("label"));
192         callbacks[j] = menu_callback;
193
194                                 // Load all the bindings for this item
195         vector<SGPropertyNode_ptr> bindings = item_nodes[i]->getChildren("binding");
196         SGPropertyNode * dest = fgGetNode("/sim/bindings/menu", true);
197
198         for (unsigned int k = 0; k < bindings.size(); k++) {
199             unsigned int m = 0;
200             SGPropertyNode *binding;
201             while (dest->getChild("binding", m))
202                 m++;
203
204             binding = dest->getChild("binding", m, true);
205             copyProperties(bindings[k], binding);
206             _bindings[items[j]].push_back(new FGBinding(binding));
207         }
208     }
209
210     _menuBar->add_submenu(name, items, callbacks);
211 }
212
213 void
214 FGMenuBar::make_menubar ()
215 {
216     SGPropertyNode *targetpath;
217    
218     targetpath = fgGetNode("/sim/menubar/default",true);
219     // fgLoadProps("gui/menubar.xml", targetpath);
220     
221     /* NOTE: there is no check to see whether there's any usable data at all
222      *
223      * This would also have the advantage of being able to create some kind of
224      * 'fallback' menu - just in case that either menubar.xml is empty OR that
225      * its XML data is not valid, that way we would avoid displaying an
226      * unusable menubar without any functionality - if we decided to add another
227      * char * element to the commands structure in
228      *  $FG_SRC/src/Main/fgcommands.cxx 
229      * we could additionally save each function's (short) description and use
230      * this as label for the fallback PUI menubar item labels - as a workaround
231      * one might simply use the internal fgcommands and put them into the 
232      * fallback menu, so that the user is at least able to re-init the menu
233      * loading - just in case there was some malformed XML in it
234      * (it happend to me ...)
235      */
236     
237     make_menubar(targetpath);
238 }
239
240 /* WARNING: We aren't yet doing any validation of what's found - but since
241  * this isn't done with menubar.xml either, it should not really matter
242  * right now. Although one should later on consider to validate the
243  * contents, whether they are representing a 'legal' menubar structure.
244  */
245 void
246 FGMenuBar::make_menubar(SGPropertyNode * props) 
247 {    
248     // Just in case.
249     destroy_menubar();
250     _menuBar = new puMenuBar;
251
252     vector<SGPropertyNode_ptr> menu_nodes = props->getChildren("menu");
253     for (unsigned int i = 0; i < menu_nodes.size(); i++)
254         make_menu(menu_nodes[i]);
255
256     _menuBar->close();
257     make_object_map(props);
258
259     if (_visible)
260         _menuBar->reveal();
261     else
262         _menuBar->hide();
263 }
264
265 void
266 FGMenuBar::destroy_menubar ()
267 {
268     if ( _menuBar == 0 )
269         return;
270
271     hide();
272     puDeleteObject(_menuBar);
273
274     unsigned int i;
275
276                                 // Delete all the character arrays
277                                 // we were forced to keep around for
278                                 // plib.
279     SG_LOG(SG_GENERAL, SG_INFO, "Deleting char arrays");
280     for (i = 0; i < _char_arrays.size(); i++) {
281         for (int j = 0; _char_arrays[i][j] != 0; j++)
282             free(_char_arrays[i][j]); // added with strdup
283         delete[] _char_arrays[i];
284     }
285
286                                 // Delete all the callback arrays
287                                 // we were forced to keep around for
288                                 // plib.
289     SG_LOG(SG_GENERAL, SG_INFO, "Deleting callback arrays");
290     for (i = 0; i < _callback_arrays.size(); i++)
291         delete[] _callback_arrays[i];
292
293                                 // Delete all those bindings
294     SG_LOG(SG_GENERAL, SG_INFO, "Deleting bindings");
295     map<string,vector<FGBinding *> >::iterator it;
296     it = _bindings.begin();
297     for (it = _bindings.begin(); it != _bindings.end(); it++) {
298         SG_LOG(SG_GENERAL, SG_INFO, "Deleting bindings for " << it->first);
299         for ( i = 0; i < it->second.size(); i++ )
300             delete it->second[i];
301     }
302
303     SG_LOG(SG_GENERAL, SG_INFO, "Done.");
304 }
305
306 void
307 FGMenuBar::make_object_map(SGPropertyNode * node)
308 {
309     unsigned int menu_index = 0;
310     vector<SGPropertyNode_ptr> menus = node->getChildren("menu");
311     for (puObject *obj = ((puGroup *)_menuBar)->getFirstChild();
312             obj; obj = obj->getNextObject()) {
313
314         // skip puPopupMenus. They are also children of _menuBar,
315         // but we access them via getUserData()  (see below)
316         if (!(obj->getType() & PUCLASS_ONESHOT))
317             continue;
318
319         if (menu_index >= menus.size()) {
320             SG_LOG(SG_GENERAL, SG_WARN, "'menu' object without node: "
321                     << node->getPath() << "/menu[" << menu_index << ']');
322             return;
323         }
324
325         SGPropertyNode *menu = menus.at(menu_index);
326         _objects[menu->getPath()] = obj;
327         add_enabled_listener(menu);
328
329         puGroup *popup = (puGroup *)obj->getUserData();
330         if (!popup)
331             continue;
332
333         // the entries are for some reason reversed (last first), and we
334         // don't know yet how many there will be; so we collect first
335         vector<puObject *> e;
336         for (puObject *me = popup->getFirstChild(); me; me = me->getNextObject())
337             e.push_back(me);
338
339         vector<SGPropertyNode_ptr> items = menu->getChildren("item");
340         for (unsigned int i = 0; i < e.size(); i++) {
341             if (i >= items.size()) {
342                 SG_LOG(SG_GENERAL, SG_WARN, "'item' object without node: "
343                         << menu->getPath() << "/item[" << i << ']');
344                 break;
345             }
346             SGPropertyNode *item = items.at(e.size() - i - 1);
347             _objects[item->getPath()] = e[i];
348             add_enabled_listener(item);
349         }
350         menu_index++;
351     }
352 }
353
354 struct EnabledListener : SGPropertyChangeListener {
355     void valueChanged(SGPropertyNode *node) {
356         NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
357         if (!gui)
358             return;
359         FGMenuBar *menubar = gui->getMenuBar();
360         if (menubar)
361             menubar->enable_item(node->getParent(), node->getBoolValue());
362     }
363 };
364
365 void
366 FGMenuBar::add_enabled_listener(SGPropertyNode * node)
367 {
368     if (!node->hasValue("enabled"))
369         node->setBoolValue("enabled", true);
370
371     enable_item(node, node->getBoolValue("enabled"));
372     node->getNode("enabled")->addChangeListener(new EnabledListener());
373 }
374
375 bool
376 FGMenuBar::enable_item(const SGPropertyNode * node, bool state)
377 {
378     const char *path = node->getPath();
379     if (_objects.find(path) == _objects.end()) {
380         SG_LOG(SG_GENERAL, SG_ALERT, "Trying to enable/disable "
381             "non-existent menu item for node `" << path << '\'');
382         return false;
383     }
384     puObject *object = _objects[path];
385     if (state)
386         object->activate();
387     else
388         object->greyOut();
389
390     return true;
391 }
392
393 char **
394 FGMenuBar::make_char_array (int size)
395 {
396     char ** list = new char*[size+1];
397     for (int i = 0; i <= size; i++)
398         list[i] = 0;
399     _char_arrays.push_back(list);
400     return list;
401 }
402
403 puCallback *
404 FGMenuBar::make_callback_array (int size)
405 {
406     puCallback * list = new puCallback[size+1];
407     for (int i = 0; i <= size; i++)
408         list[i] = 0;
409     _callback_arrays.push_back(list);
410     return list;
411 }
412
413 // end of menubar.cxx