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