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.
8 * Full author contact details are available in file CREDITS.
13 #include "GuiMenubar.h"
16 #include "GuiApplication.h"
17 #include "GuiPopupMenu.h"
20 #include "qt_helpers.h"
22 #include "MenuBackend.h"
24 #include "support/debug.h"
33 // MacOSX specific stuff is at the end.
35 GuiMenubar::GuiMenubar(GuiView * view)
42 void GuiMenubar::init()
44 // Clear all menubar contents before filling it.
45 owner_->menuBar()->clear();
48 // setup special mac specific menu item
53 MenuBackend & menu_backend = guiApp->menuBackend();
54 LYXERR(Debug::GUI, "populating menu bar" << to_utf8(menu_backend.getMenubar().name()));
56 if (menu_backend.getMenubar().size() == 0) {
57 LYXERR(Debug::GUI, "\tERROR: empty menu bar"
58 << to_utf8(menu_backend.getMenubar().name()));
63 LYXERR(Debug::GUI, "menu bar entries "
64 << menu_backend.getMenubar().size());
68 menu_backend.expand(menu_backend.getMenubar(), menu, owner_->buffer());
70 Menu::const_iterator m = menu.begin();
71 Menu::const_iterator end = menu.end();
73 for (; m != end; ++m) {
75 if (m->kind() != MenuItem::Submenu) {
76 LYXERR(Debug::GUI, "\tERROR: not a submenu " << to_utf8(m->label()));
80 LYXERR(Debug::GUI, "menu bar item " << to_utf8(m->label())
81 << " is a submenu named " << to_utf8(m->submenuname()));
83 docstring name = m->submenuname();
84 if (!menu_backend.hasMenu(name)) {
85 LYXERR(Debug::GUI, "\tERROR: " << to_utf8(name)
86 << " submenu has no menu!");
91 menu_backend.expand(menu_backend.getMenubar(), menu, owner_->buffer());
93 GuiPopupMenu * qMenu = new GuiPopupMenu(owner_, *m, true);
94 owner_->menuBar()->addMenu(qMenu);
96 name_map_[toqstr(name)] = qMenu;
101 GuiMenubar::~GuiMenubar() {
107 void GuiMenubar::openByName(QString const & name)
109 if (QMenu * menu = name_map_.value(name))
110 menu->exec(QCursor::pos());
114 /// Some special Qt/Mac support hacks
117 Here is what the Qt documentation says about how a menubar is chosen:
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
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.
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
138 void GuiMenubar::macxMenuBarInit()
140 mac_menubar_ = new QMenuBar;
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)
149 /* The entries of our special mac menu. If we add support for
150 * special entries in MenuBackend, we could imagine something
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)
156 struct MacMenuEntry {
160 QAction::MenuRole role;
163 MacMenuEntry entries[] = {
164 {LFUN_DIALOG_SHOW, "aboutlyx", "About LyX",
166 {LFUN_DIALOG_SHOW, "prefs", "Preferences",
167 QAction::PreferencesRole},
168 {LFUN_RECONFIGURE, "", "Reconfigure",
169 QAction::ApplicationSpecificRole},
170 {LFUN_LYX_QUIT, "", "Quit LyX", QAction::QuitRole}
172 const size_t num_entries = sizeof(entries) / sizeof(MacMenuEntry);
174 // the special menu for MenuBackend.
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),
183 MenuBackend & menu_backend = guiApp->menuBackend();
184 menu_backend.specialMenu(special);
186 // add the entries to a QMenu that will eventually be empty
187 // and therefore invisible.
188 QMenu * qMenu = owner_->menuBar()->addMenu("special");
190 // we do not use 'special' because it is a temporary variable,
191 // whereas MenuBackend::specialMenu points to a persistent
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);
205 } // namespace frontend
208 #include "GuiMenubar_moc.cpp"