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.
16 #include "GuiApplication.h"
17 #include "GuiPopupMenu.h"
20 #include "qt_helpers.h"
22 #include "support/debug.h"
31 // MacOSX specific stuff is at the end.
33 void Menus::fillMenuBar(GuiView * view)
35 // Clear all menubar contents before filling it.
36 view->menuBar()->clear();
39 // setup special mac specific menu item
40 macxMenuBarInit(view);
43 LYXERR(Debug::GUI, "populating menu bar" << to_utf8(getMenubar().name()));
45 if (getMenubar().size() == 0) {
46 LYXERR(Debug::GUI, "\tERROR: empty menu bar"
47 << to_utf8(getMenubar().name()));
51 LYXERR(Debug::GUI, "menu bar entries "
52 << getMenubar().size());
56 expand(getMenubar(), menu, view->buffer());
58 Menu::const_iterator m = menu.begin();
59 Menu::const_iterator end = menu.end();
61 for (; m != end; ++m) {
63 if (m->kind() != MenuItem::Submenu) {
64 LYXERR(Debug::GUI, "\tERROR: not a submenu " << to_utf8(m->label()));
68 LYXERR(Debug::GUI, "menu bar item " << to_utf8(m->label())
69 << " is a submenu named " << to_utf8(m->submenuname()));
71 docstring name = m->submenuname();
73 LYXERR(Debug::GUI, "\tERROR: " << to_utf8(name)
74 << " submenu has no menu!");
78 GuiPopupMenu * qMenu = new GuiPopupMenu(view, *m, true);
79 view->menuBar()->addMenu(qMenu);
81 name_map_[toqstr(name)] = qMenu;
86 QMenu * Menus::menu(QString const & name)
88 LYXERR(Debug::GUI, "Context menu requested: "
89 << qstring_to_ucs4(name));
90 GuiPopupMenu * menu = name_map_.value(name, 0);
92 LYXERR0("resquested context menu not found: "
93 << qstring_to_ucs4(name));
98 /// Some special Qt/Mac support hacks
101 Here is what the Qt documentation says about how a menubar is chosen:
103 1) If the window has a QMenuBar then it is used. 2) If the window
104 is a modal then its menubar is used. If no menubar is specified
105 then a default menubar is used (as documented below) 3) If the
106 window has no parent then the default menubar is used (as
109 The above 3 steps are applied all the way up the parent window
110 chain until one of the above are satisifed. If all else fails a
111 default menubar will be created, the default menubar on Qt/Mac is
112 an empty menubar, however you can create a different default
113 menubar by creating a parentless QMenuBar, the first one created
114 will thus be designated the default menubar, and will be used
115 whenever a default menubar is needed.
117 Thus, for Qt/Mac, we add the menus to a free standing menubar, so
118 that this menubar will be used also when one of LyX' dialogs has
122 void Menus::macxMenuBarInit(GuiView * view)
124 // The Mac menubar initialisation must be done only once!
125 static bool done = false;
130 /* Since Qt 4.2, the qt/mac menu code has special code for
131 specifying the role of a menu entry. However, it does not
132 work very well with our scheme of creating menus on demand,
133 and therefore we need to put these entries in a special
134 invisible menu. (JMarc)
137 /* The entries of our special mac menu. If we add support for
138 * special entries in MenuBackend, we could imagine something
140 * SpecialItem About " "About LyX" "dialog-show aboutlyx"
141 * and therefore avoid hardcoding. I am not sure it is worth
142 * the hassle, though. (JMarc)
144 struct MacMenuEntry {
148 QAction::MenuRole role;
151 MacMenuEntry entries[] = {
152 {LFUN_DIALOG_SHOW, "aboutlyx", "About LyX",
154 {LFUN_DIALOG_SHOW, "prefs", "Preferences",
155 QAction::PreferencesRole},
156 {LFUN_RECONFIGURE, "", "Reconfigure",
157 QAction::ApplicationSpecificRole},
158 {LFUN_LYX_QUIT, "", "Quit LyX", QAction::QuitRole}
160 const size_t num_entries = sizeof(entries) / sizeof(MacMenuEntry);
162 // the special menu for MenuBackend.
164 for (size_t i = 0 ; i < num_entries ; ++i) {
165 FuncRequest const func(entries[i].action,
166 from_utf8(entries[i].arg));
167 special.add(MenuItem(MenuItem::Command,
168 from_utf8(entries[i].label),
171 specialMenu(special);
173 // add the entries to a QMenu that will eventually be empty
174 // and therefore invisible.
175 QMenu * qMenu = view->menuBar()->addMenu("special");
177 // we do not use 'special' because it is a temporary variable,
178 // whereas MenuBackend::specialMenu points to a persistent
180 Menu::const_iterator cit = specialMenu().begin();
181 Menu::const_iterator end = specialMenu().end();
182 for (size_t i = 0 ; cit != end ; ++cit, ++i) {
183 Action * action = new Action(*view, QIcon(),
184 toqstr(cit->label()),
185 cit->func(), QString());
186 action->setMenuRole(entries[i].role);
187 qMenu->addAction(action);
192 } // namespace frontend
195 #include "Menus_moc.cpp"