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