]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiMenubar.cpp
33e7f0b9b753725b302cdafc9d39ddbf55116cae
[lyx.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
103 void GuiMenubar::openByName(QString const & name)
104 {
105         if (QMenu * menu = name_map_.value(name))
106                 menu->exec(QCursor::pos());
107 }
108
109
110 /// Some special Qt/Mac support hacks
111
112 /*
113   Here is what the Qt documentation says about how a menubar is chosen:
114
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
119      documented below).
120
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.
128
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
131   focus. (JMarc)
132 */
133
134 void GuiMenubar::macxMenuBarInit()
135 {
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)
141         */
142
143         /* The entries of our special mac menu. If we add support for
144          * special entries in MenuBackend, we could imagine something
145          * like
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)
149          */
150         struct MacMenuEntry {
151                 kb_action action;
152                 char const * arg;
153                 char const * label;
154                 QAction::MenuRole role;
155         };
156
157         MacMenuEntry entries[] = {
158                 {LFUN_DIALOG_SHOW, "aboutlyx", "About LyX",
159                  QAction::AboutRole},
160                 {LFUN_DIALOG_SHOW, "prefs", "Preferences",
161                  QAction::PreferencesRole},
162                 {LFUN_RECONFIGURE, "", "Reconfigure",
163                  QAction::ApplicationSpecificRole},
164                 {LFUN_LYX_QUIT, "", "Quit LyX", QAction::QuitRole}
165         };
166         const size_t num_entries = sizeof(entries) / sizeof(MacMenuEntry);
167
168         // the special menu for MenuBackend.
169         Menu special;
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),
175                                      func));
176         }
177         MenuBackend & menu_backend = guiApp->menuBackend();
178         menu_backend.specialMenu(special);
179
180         // add the entries to a QMenu that will eventually be empty
181         // and therefore invisible.
182         QMenu * qMenu = owner_->menuBar()->addMenu("special");
183
184         // we do not use 'special' because it is a temporary variable,
185         // whereas MenuBackend::specialMenu points to a persistent
186         // copy.
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);
195
196         }
197 }
198
199 } // namespace frontend
200 } // namespace lyx
201
202 #include "GuiMenubar_moc.cpp"