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() {}
103 void GuiMenubar::openByName(QString const & name)
105 if (QMenu * menu = name_map_.value(name))
106 menu->exec(QCursor::pos());
110 /// Some special Qt/Mac support hacks
113 Here is what the Qt documentation says about how a menubar is chosen:
115 1) If the window has a QMenuBar then it is used. 2) If the window
116 is a modal then its menubar is used. If no menubar is specified
117 then a default menubar is used (as documented below) 3) If the
118 window has no parent then the default menubar is used (as
121 The above 3 steps are applied all the way up the parent window
122 chain until one of the above are satisifed. If all else fails a
123 default menubar will be created, the default menubar on Qt/Mac is
124 an empty menubar, however you can create a different default
125 menubar by creating a parentless QMenuBar, the first one created
126 will thus be designated the default menubar, and will be used
127 whenever a default menubar is needed.
129 Thus, for Qt/Mac, we add the menus to a free standing menubar, so
130 that this menubar will be used also when one of LyX' dialogs has
134 void GuiMenubar::macxMenuBarInit()
136 /* Since Qt 4.2, the qt/mac menu code has special code for
137 specifying the role of a menu entry. However, it does not
138 work very well with our scheme of creating menus on demand,
139 and therefore we need to put these entries in a special
140 invisible menu. (JMarc)
143 /* The entries of our special mac menu. If we add support for
144 * special entries in MenuBackend, we could imagine something
146 * SpecialItem About " "About LyX" "dialog-show aboutlyx"
147 * and therefore avoid hardcoding. I am not sure it is worth
148 * the hassle, though. (JMarc)
150 struct MacMenuEntry {
154 QAction::MenuRole role;
157 MacMenuEntry entries[] = {
158 {LFUN_DIALOG_SHOW, "aboutlyx", "About LyX",
160 {LFUN_DIALOG_SHOW, "prefs", "Preferences",
161 QAction::PreferencesRole},
162 {LFUN_RECONFIGURE, "", "Reconfigure",
163 QAction::ApplicationSpecificRole},
164 {LFUN_LYX_QUIT, "", "Quit LyX", QAction::QuitRole}
166 const size_t num_entries = sizeof(entries) / sizeof(MacMenuEntry);
168 // the special menu for MenuBackend.
170 for (size_t i = 0 ; i < num_entries ; ++i) {
171 FuncRequest const func(entries[i].action,
172 from_utf8(entries[i].arg));
173 special.add(MenuItem(MenuItem::Command,
174 from_utf8(entries[i].label),
177 MenuBackend & menu_backend = guiApp->menuBackend();
178 menu_backend.specialMenu(special);
180 // add the entries to a QMenu that will eventually be empty
181 // and therefore invisible.
182 QMenu * qMenu = owner_->menuBar()->addMenu("special");
184 // we do not use 'special' because it is a temporary variable,
185 // whereas MenuBackend::specialMenu points to a persistent
187 Menu::const_iterator cit = menu_backend.specialMenu().begin();
188 Menu::const_iterator end = menu_backend.specialMenu().end();
189 for (size_t i = 0 ; cit != end ; ++cit, ++i) {
190 Action * action = new Action(*owner_, QIcon(),
191 toqstr(cit->label()),
192 cit->func(), QString());
193 action->setMenuRole(entries[i].role);
194 qMenu->addAction(action);
199 } // namespace frontend
202 #include "GuiMenubar_moc.cpp"