2 * \file qt4/QLMenubar.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 // Qt defines a macro 'signals' that clashes with a boost namespace.
14 // All is well if the namespace is visible first.
18 #include "QLMenubar.h"
19 #include "QLPopupMenu.h"
21 #include "qt_helpers.h"
22 #include "support/lstrings.h"
24 #include "MenuBackend.h"
38 // MacOSX specific stuff is at the end.
40 QLMenubar::QLMenubar(LyXView * view, MenuBackend & mbe)
41 : owner_(static_cast<GuiView*>(view)), menubackend_(mbe)
45 LYXERR(Debug::GUI) << "populating menu bar" << to_utf8(menubackend_.getMenubar().name()) << endl;
47 if (menubackend_.getMenubar().size() == 0) {
48 LYXERR(Debug::GUI) << "\tERROR: empty menu bar"
49 << to_utf8(menubackend_.getMenubar().name()) << endl;
54 LYXERR(Debug::GUI) << "menu bar entries "
55 << menubackend_.getMenubar().size();
59 menubackend_.expand(menubackend_.getMenubar(), menu, owner_->buffer());
61 Menu::const_iterator m = menu.begin();
62 Menu::const_iterator end = menu.end();
64 for (; m != end; ++m) {
66 if (m->kind() != MenuItem::Submenu) {
67 LYXERR(Debug::GUI) << "\tERROR: not a submenu "
68 << to_utf8(m->label()) << endl;
72 LYXERR(Debug::GUI) << "menu bar item " << to_utf8(m->label())
73 << " is a submenu named " << to_utf8(m->submenuname()) << endl;
75 docstring name = m->submenuname();
76 if (!menubackend_.hasMenu(name)) {
77 LYXERR(Debug::GUI) << "\tERROR: " << to_utf8(name)
78 << " submenu has no menu!" << endl;
83 menubackend_.expand(menubackend_.getMenubar(), menu, owner_->buffer());
85 QLPopupMenu * qMenu = new QLPopupMenu(this, *m, true);
86 owner_->menuBar()->addMenu(qMenu);
88 pair<NameMap::iterator, bool> I = name_map_.insert(make_pair(name, qMenu));
90 LYXERR(Debug::GUI) << "\tERROR: " << to_utf8(name)
91 << " submenu is already there!" << endl;
94 QObject::connect(qMenu, SIGNAL(aboutToShow()), this, SLOT(update()));
95 QObject::connect(qMenu, SIGNAL(triggered(QAction *)), this, SLOT(update()));
96 QObject::connect(qMenu->menuAction(), SIGNAL(triggered()), this, SLOT(update()));
99 //QObject::connect(owner_->menuBar(), SIGNAL(triggered()), this, SLOT(update()));
102 void QLMenubar::openByName(docstring const & name)
104 NameMap::const_iterator const cit = name_map_.find(name);
105 if (cit == name_map_.end())
108 // I (Abdel) don't understand this comment:
109 // this will have to do I'm afraid.
110 cit->second->exec(QCursor::pos());
114 void QLMenubar::update()
117 GuiView * QLMenubar::view()
123 MenuBackend const & QLMenubar::backend()
129 /// Some special Qt/Mac support hacks
132 Here is what the Qt documentation says about how a menubar is chosen:
134 1) If the window has a QMenuBar then it is used. 2) If the window
135 is a modal then its menubar is used. If no menubar is specified
136 then a default menubar is used (as documented below) 3) If the
137 window has no parent then the default menubar is used (as
140 The above 3 steps are applied all the way up the parent window
141 chain until one of the above are satisifed. If all else fails a
142 default menubar will be created, the default menubar on Qt/Mac is
143 an empty menubar, however you can create a different default
144 menubar by creating a parentless QMenuBar, the first one created
145 will thus be designated the default menubar, and will be used
146 whenever a default menubar is needed.
148 Thus, for Qt/Mac, we add the menus to a free standing menubar, so
149 that this menubar will be used also when one of LyX' dialogs has
152 QMenuBar * QLMenubar::menuBar() const
155 return mac_menubar_.get();
157 return owner_->menuBar();
162 extern void qt_mac_set_menubar_merge(bool b);
165 void QLMenubar::macxMenuBarInit()
168 mac_menubar_.reset(new QMenuBar);
170 # if QT_VERSION >= 0x040200
171 /* Since Qt 4.2, the qt/mac menu code has special code for
172 specifying the role of a menu entry. However, it does not
173 work very well with our scheme of creating menus on demand,
174 and therefore we need to put these entries in a special
175 invisible menu. (JMarc)
178 /* The entries of our special mac menu. If we add support for
179 * special entries in MenuBackend, we could imagine something
181 * SpecialItem About " "About LyX" "dialog-show aboutlyx"
182 * and therefore avoid hardcoding. I am not sure it is worth
183 * the hassle, though. (JMarc)
185 struct MacMenuEntry {
189 QAction::MenuRole role;
192 MacMenuEntry entries[] = {
193 {LFUN_DIALOG_SHOW, "aboutlyx", "About LyX",
195 {LFUN_DIALOG_SHOW, "prefs", "Preferences",
196 QAction::PreferencesRole},
197 {LFUN_RECONFIGURE, "", "Reconfigure",
198 QAction::ApplicationSpecificRole},
199 {LFUN_LYX_QUIT, "", "Quit LyX", QAction::QuitRole}
201 const size_t num_entries = sizeof(entries) / sizeof(MacMenuEntry);
203 // the special menu for MenuBackend.
205 for (size_t i = 0 ; i < num_entries ; ++i) {
206 FuncRequest const func(entries[i].action,
207 from_utf8(entries[i].arg));
208 special.add(MenuItem(MenuItem::Command,
209 from_utf8(entries[i].label),
212 menubackend_.specialMenu(special);
214 // add the entries to a QMenu that will eventually be empty
215 // and therefore invisible.
216 QMenu * qMenu = owner_->menuBar()->addMenu("special");
218 // we do not use 'special' because it is a temporary variable,
219 // whereas MenuBackend::specialMenu points to a persistent
221 Menu::const_iterator cit = menubackend_.specialMenu().begin();
222 Menu::const_iterator end = menubackend_.specialMenu().end();
223 for (size_t i = 0 ; cit != end ; ++cit, ++i) {
224 Action * action = new Action(*owner_, cit->label(),
226 action->setMenuRole(entries[i].role);
227 qMenu->addAction(action);
231 qt_mac_set_menubar_merge(false);
232 # endif // QT_VERSION >= 0x040200
236 } // namespace frontend
239 #include "QLMenubar_moc.cpp"