]> git.lyx.org Git - features.git/blob - src/frontends/qt4/GuiMenubar.cpp
Don't pretend we have multiple menu backends: transfer the singleton to GuiApplication.
[features.git] / src / frontends / qt4 / GuiMenubar.cpp
1 /**
2  * \file qt4/GuiMenubar.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "GuiMenubar.h"
14
15 #include "Action.h"
16 #include "GuiApplication.h"
17 #include "GuiPopupMenu.h"
18 #include "GuiView.h"
19
20 #include "qt_helpers.h"
21
22 #include "MenuBackend.h"
23
24 #include "support/debug.h"
25
26 #include <QCursor>
27 #include <QMenuBar>
28
29
30 namespace lyx {
31 namespace frontend {
32
33 // MacOSX specific stuff is at the end.
34
35 GuiMenubar::GuiMenubar(GuiView * view)
36         : owner_(view)
37 {
38         init();
39 }
40
41
42 void GuiMenubar::init()
43 {
44         // Clear all menubar contents before filling it.
45         owner_->menuBar()->clear();
46         
47 #ifdef Q_WS_MACX
48         // setup special mac specific menu item
49         macxMenuBarInit();
50 #endif
51
52         /// menu controller
53         MenuBackend & menu_backend = guiApp->menuBackend();
54         LYXERR(Debug::GUI, "populating menu bar" << to_utf8(menu_backend.getMenubar().name()));
55
56         if (menu_backend.getMenubar().size() == 0) {
57                 LYXERR(Debug::GUI, "\tERROR: empty menu bar"
58                         << to_utf8(menu_backend.getMenubar().name()));
59                 return;
60                 //                      continue;
61         }
62         else {
63                 LYXERR(Debug::GUI, "menu bar entries "
64                         << menu_backend.getMenubar().size());
65         }
66
67         Menu menu;
68         menu_backend.expand(menu_backend.getMenubar(), menu, owner_->buffer());
69
70         Menu::const_iterator m = menu.begin();
71         Menu::const_iterator end = menu.end();
72
73         for (; m != end; ++m) {
74
75                 if (m->kind() != MenuItem::Submenu) {
76                         LYXERR(Debug::GUI, "\tERROR: not a submenu " << to_utf8(m->label()));
77                         continue;
78                 }
79
80                 LYXERR(Debug::GUI, "menu bar item " << to_utf8(m->label())
81                         << " is a submenu named " << to_utf8(m->submenuname()));
82
83                 docstring name = m->submenuname();
84                 if (!menu_backend.hasMenu(name)) {
85                         LYXERR(Debug::GUI, "\tERROR: " << to_utf8(name)
86                                 << " submenu has no menu!");
87                         continue;
88                 }
89
90                 Menu menu;
91                 menu_backend.expand(menu_backend.getMenubar(), menu, owner_->buffer());
92
93                 GuiPopupMenu * qMenu = new GuiPopupMenu(owner_, *m, true);
94                 owner_->menuBar()->addMenu(qMenu);
95
96                 name_map_[toqstr(name)] = qMenu;
97         }
98 }
99
100
101 GuiMenubar::~GuiMenubar() {
102 #ifdef Q_WS_MACX
103         delete mac_menubar_;
104 #endif
105 }
106
107 void GuiMenubar::openByName(QString const & name)
108 {
109         if (QMenu * menu = name_map_.value(name))
110                 menu->exec(QCursor::pos());
111 }
112
113
114 /// Some special Qt/Mac support hacks
115
116 /*
117   Here is what the Qt documentation says about how a menubar is chosen:
118
119      1) If the window has a QMenuBar then it is used. 2) If the window
120      is a modal then its menubar is used. If no menubar is specified
121      then a default menubar is used (as documented below) 3) If the
122      window has no parent then the default menubar is used (as
123      documented below).
124
125      The above 3 steps are applied all the way up the parent window
126      chain until one of the above are satisifed. If all else fails a
127      default menubar will be created, the default menubar on Qt/Mac is
128      an empty menubar, however you can create a different default
129      menubar by creating a parentless QMenuBar, the first one created
130      will thus be designated the default menubar, and will be used
131      whenever a default menubar is needed.
132
133   Thus, for Qt/Mac, we add the menus to a free standing menubar, so
134   that this menubar will be used also when one of LyX' dialogs has
135   focus. (JMarc)
136 */
137
138 void GuiMenubar::macxMenuBarInit()
139 {
140         mac_menubar_ = new QMenuBar;
141
142         /* Since Qt 4.2, the qt/mac menu code has special code for
143            specifying the role of a menu entry. However, it does not
144            work very well with our scheme of creating menus on demand,
145            and therefore we need to put these entries in a special
146            invisible menu. (JMarc)
147         */
148
149         /* The entries of our special mac menu. If we add support for
150          * special entries in MenuBackend, we could imagine something
151          * like
152          *    SpecialItem About " "About LyX" "dialog-show aboutlyx"
153          * and therefore avoid hardcoding. I am not sure it is worth
154          * the hassle, though. (JMarc)
155          */
156         struct MacMenuEntry {
157                 kb_action action;
158                 char const * arg;
159                 char const * label;
160                 QAction::MenuRole role;
161         };
162
163         MacMenuEntry entries[] = {
164                 {LFUN_DIALOG_SHOW, "aboutlyx", "About LyX",
165                  QAction::AboutRole},
166                 {LFUN_DIALOG_SHOW, "prefs", "Preferences",
167                  QAction::PreferencesRole},
168                 {LFUN_RECONFIGURE, "", "Reconfigure",
169                  QAction::ApplicationSpecificRole},
170                 {LFUN_LYX_QUIT, "", "Quit LyX", QAction::QuitRole}
171         };
172         const size_t num_entries = sizeof(entries) / sizeof(MacMenuEntry);
173
174         // the special menu for MenuBackend.
175         Menu special;
176         for (size_t i = 0 ; i < num_entries ; ++i) {
177                 FuncRequest const func(entries[i].action,
178                                        from_utf8(entries[i].arg));
179                 special.add(MenuItem(MenuItem::Command,
180                                      from_utf8(entries[i].label),
181                                      func));
182         }
183         MenuBackend & menu_backend = guiApp->menuBackend();
184         menu_backend.specialMenu(special);
185
186         // add the entries to a QMenu that will eventually be empty
187         // and therefore invisible.
188         QMenu * qMenu = owner_->menuBar()->addMenu("special");
189
190         // we do not use 'special' because it is a temporary variable,
191         // whereas MenuBackend::specialMenu points to a persistent
192         // copy.
193         Menu::const_iterator cit = menu_backend.specialMenu().begin();
194         Menu::const_iterator end = menu_backend.specialMenu().end();
195         for (size_t i = 0 ; cit != end ; ++cit, ++i) {
196                 Action * action = new Action(*owner_, QIcon(), 
197                                              toqstr(cit->label()),
198                                              cit->func(), QString());
199                 action->setMenuRole(entries[i].role);
200                 qMenu->addAction(action);
201
202         }
203 }
204
205 } // namespace frontend
206 } // namespace lyx
207
208 #include "GuiMenubar_moc.cpp"