]> git.mxchange.org Git - flightgear.git/blob - src/GUI/FGPUIMenuBar.cxx
Add a node mask bit for permanent lights (needed by Rembrandt) and select it in the...
[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 \f
81 ////////////////////////////////////////////////////////////////////////
82 // Implementation of FGPUIMenuBar.
83 ////////////////////////////////////////////////////////////////////////
84
85
86 FGPUIMenuBar::FGPUIMenuBar ()
87     : _visible(false),
88       _menuBar(0)
89 {
90 }
91
92 FGPUIMenuBar::~FGPUIMenuBar ()
93 {
94     destroy_menubar();
95 }
96
97 void
98 FGPUIMenuBar::init ()
99 {
100     delete _menuBar;            // FIXME: check if PUI owns the pointer
101     make_menubar();
102                                 // FIXME: temporary commands to get at
103                                 // old, hard-coded dialogs.
104     add_deprecated_dialogs();
105 }
106
107 void
108 FGPUIMenuBar::show ()
109 {
110     if (_menuBar != 0)
111         _menuBar->reveal();
112     _visible = true;
113 }
114
115 void
116 FGPUIMenuBar::hide ()
117 {
118     if (_menuBar != 0)
119         _menuBar->hide();
120     _visible = false;
121 }
122
123 bool
124 FGPUIMenuBar::isVisible () const
125 {
126     return _visible;
127 }
128
129 void
130 FGPUIMenuBar::fireItem (puObject * item)
131 {
132     const char * name = item->getLegend();
133     vector<SGBinding *> &bindings = _bindings[name];
134     int nBindings = bindings.size();
135
136     for (int i = 0; i < nBindings; i++)
137         bindings[i]->fire();
138 }
139
140 void
141 FGPUIMenuBar::make_menu (SGPropertyNode * node)
142 {
143     string s = getLocalizedLabel(node);
144
145     // hack: map at least some UTF-8 characters to Latin1, since FG fonts are
146     // Latin1 (or plain ASCII, which is a subset). This hack can be removed once
147     // the PLIB/OSG port is complete (OSG has full UTF-8 support! :) ).
148     FGLocale::utf8toLatin1(s);
149
150     const char* name = strdup(s.c_str());
151
152     vector<SGPropertyNode_ptr> item_nodes = node->getChildren("item");
153
154     int array_size = item_nodes.size();
155
156     char ** items = make_char_array(array_size);
157     puCallback * callbacks = make_callback_array(array_size);
158
159     for (unsigned int i = 0, j = item_nodes.size() - 1;
160          i < item_nodes.size();
161          i++, j--) {
162
163                                 // Set up the PUI entries for this item
164         string label = getLocalizedLabel(item_nodes[i]);
165         FGLocale::utf8toLatin1(label);
166
167         // append the keyboard hint to the menu entry
168         const char* key = item_nodes[i]->getStringValue("key", 0);
169         if (key)
170         {
171             label.append("           <");
172             label.append(key);
173             label.append(">");
174         }
175         items[j] = strdup(label.c_str());
176         callbacks[j] = menu_callback;
177
178                                 // Load all the bindings for this item
179         vector<SGPropertyNode_ptr> bindings = item_nodes[i]->getChildren("binding");
180         SGPropertyNode * dest = fgGetNode("/sim/bindings/menu", true);
181
182         for (unsigned int k = 0; k < bindings.size(); k++) {
183             unsigned int m = 0;
184             SGPropertyNode_ptr binding;
185             while (dest->getChild("binding", m))
186                 m++;
187
188             binding = dest->getChild("binding", m, true);
189             copyProperties(bindings[k], binding);
190             _bindings[items[j]].push_back(new SGBinding(binding, globals->get_props()));
191         }
192     }
193
194     _menuBar->add_submenu(name, items, callbacks);
195 }
196
197 void
198 FGPUIMenuBar::make_menubar ()
199 {
200     SGPropertyNode *targetpath;
201    
202     targetpath = fgGetNode("/sim/menubar/default",true);
203     // fgLoadProps("gui/menubar.xml", targetpath);
204     
205     /* NOTE: there is no check to see whether there's any usable data at all
206      *
207      * This would also have the advantage of being able to create some kind of
208      * 'fallback' menu - just in case that either menubar.xml is empty OR that
209      * its XML data is not valid, that way we would avoid displaying an
210      * unusable menubar without any functionality - if we decided to add another
211      * char * element to the commands structure in
212      *  $FG_SRC/src/Main/fgcommands.cxx 
213      * we could additionally save each function's (short) description and use
214      * this as label for the fallback PUI menubar item labels - as a workaround
215      * one might simply use the internal fgcommands and put them into the 
216      * fallback menu, so that the user is at least able to re-init the menu
217      * loading - just in case there was some malformed XML in it
218      * (it happend to me ...)
219      */
220     
221     make_menubar(targetpath);
222 }
223
224 /* WARNING: We aren't yet doing any validation of what's found - but since
225  * this isn't done with menubar.xml either, it should not really matter
226  * right now. Although one should later on consider to validate the
227  * contents, whether they are representing a 'legal' menubar structure.
228  */
229 void
230 FGPUIMenuBar::make_menubar(SGPropertyNode * props) 
231 {    
232     // Just in case.
233     destroy_menubar();
234     _menuBar = new puMenuBar;
235
236     vector<SGPropertyNode_ptr> menu_nodes = props->getChildren("menu");
237     for (unsigned int i = 0; i < menu_nodes.size(); i++)
238         make_menu(menu_nodes[i]);
239
240     _menuBar->close();
241     make_object_map(props);
242
243     if (_visible)
244         _menuBar->reveal();
245     else
246         _menuBar->hide();
247 }
248
249 void
250 FGPUIMenuBar::destroy_menubar ()
251 {
252     if ( _menuBar == 0 )
253         return;
254
255     hide();
256     puDeleteObject(_menuBar);
257
258     unsigned int i;
259
260                                 // Delete all the character arrays
261                                 // we were forced to keep around for
262                                 // plib.
263     SG_LOG(SG_GENERAL, SG_BULK, "Deleting char arrays");
264     for (i = 0; i < _char_arrays.size(); i++) {
265         for (int j = 0; _char_arrays[i][j] != 0; j++) {
266             free(_char_arrays[i][j]); // added with strdup
267             _char_arrays[i][j] = 0;
268         }
269         delete[] _char_arrays[i];
270         _char_arrays[i] = 0;
271     }
272
273                                 // Delete all the callback arrays
274                                 // we were forced to keep around for
275                                 // plib.
276     SG_LOG(SG_GENERAL, SG_BULK, "Deleting callback arrays");
277     for (i = 0; i < _callback_arrays.size(); i++)
278         delete[] _callback_arrays[i];
279
280                                 // Delete all those bindings
281     SG_LOG(SG_GENERAL, SG_BULK, "Deleting bindings");
282     map<string,vector<SGBinding *> >::iterator it;
283     for (it = _bindings.begin(); it != _bindings.end(); it++) {
284         SG_LOG(SG_GENERAL, SG_BULK, "Deleting bindings for " << it->first);
285         for ( i = 0; i < it->second.size(); i++ )
286             delete it->second[i];
287     }
288
289     SG_LOG(SG_GENERAL, SG_BULK, "Done.");
290 }
291
292 void
293 FGPUIMenuBar::make_object_map(SGPropertyNode * node)
294 {
295     unsigned int menu_index = 0;
296     vector<SGPropertyNode_ptr> menus = node->getChildren("menu");
297     for (puObject *obj = ((puGroup *)_menuBar)->getFirstChild();
298             obj; obj = obj->getNextObject()) {
299
300         // skip puPopupMenus. They are also children of _menuBar,
301         // but we access them via getUserData()  (see below)
302         if (!(obj->getType() & PUCLASS_ONESHOT))
303             continue;
304
305         if (menu_index >= menus.size()) {
306             SG_LOG(SG_GENERAL, SG_WARN, "'menu' object without node: "
307                     << node->getPath() << "/menu[" << menu_index << ']');
308             return;
309         }
310
311         SGPropertyNode *menu = menus.at(menu_index);
312         _objects[menu->getPath()] = obj;
313         add_enabled_listener(menu);
314
315         puGroup *popup = (puGroup *)obj->getUserData();
316         if (!popup)
317             continue;
318
319         // the entries are for some reason reversed (last first), and we
320         // don't know yet how many there will be; so we collect first
321         vector<puObject *> e;
322         for (puObject *me = popup->getFirstChild(); me; me = me->getNextObject())
323             e.push_back(me);
324
325         vector<SGPropertyNode_ptr> items = menu->getChildren("item");
326         for (unsigned int i = 0; i < e.size(); i++) {
327             if (i >= items.size()) {
328                 SG_LOG(SG_GENERAL, SG_WARN, "'item' object without node: "
329                         << menu->getPath() << "/item[" << i << ']');
330                 break;
331             }
332             SGPropertyNode *item = items.at(e.size() - i - 1);
333             _objects[item->getPath()] = e[i];
334             add_enabled_listener(item);
335         }
336         menu_index++;
337     }
338 }
339
340 namespace { 
341   
342   struct EnabledListener : SGPropertyChangeListener {
343     void valueChanged(SGPropertyNode *node) {
344         NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
345         if (!gui)
346             return;
347         FGPUIMenuBar* menubar = static_cast<FGPUIMenuBar*>(gui->getMenuBar());
348         if (menubar)
349             menubar->enable_item(node->getParent(), node->getBoolValue());
350     }
351 };
352
353 } // of anonymous namespace
354
355 void
356 FGPUIMenuBar::add_enabled_listener(SGPropertyNode * node)
357 {
358     if (!node->hasValue("enabled"))
359         node->setBoolValue("enabled", true);
360
361     enable_item(node, node->getBoolValue("enabled"));
362     node->getNode("enabled")->addChangeListener(new EnabledListener());
363 }
364
365 bool
366 FGPUIMenuBar::enable_item(const SGPropertyNode * node, bool state)
367 {
368     string path = node->getPath();
369     if (_objects.find(path) == _objects.end()) {
370         SG_LOG(SG_GENERAL, SG_ALERT, "Trying to enable/disable "
371             "non-existent menu item for node `" << path << '\'');
372         return false;
373     }
374     puObject *object = _objects[path];
375     if (state)
376         object->activate();
377     else
378         object->greyOut();
379
380     return true;
381 }
382
383 char **
384 FGPUIMenuBar::make_char_array (int size)
385 {
386     char ** list = new char*[size+1];
387     for (int i = 0; i <= size; i++)
388         list[i] = 0;
389     _char_arrays.push_back(list);
390     return list;
391 }
392
393 puCallback *
394 FGPUIMenuBar::make_callback_array (int size)
395 {
396     puCallback * list = new puCallback[size+1];
397     for (int i = 0; i <= size; i++)
398         list[i] = 0;
399     _callback_arrays.push_back(list);
400     return list;
401 }
402
403 // end of menubar.cxx