]> git.lyx.org Git - features.git/blob - src/frontends/qt4/GuiMenubar.cpp
9926ea0caf0faf3534eb950860943c30f632ff73
[features.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 // Qt defines a macro 'signals' that clashes with a boost namespace.
14 // All is well if the namespace is visible first.
15 #include "GuiView.h"
16
17 #include "Action.h"
18 #include "GuiMenubar.h"
19 #include "GuiPopupMenu.h"
20
21 #include "qt_helpers.h"
22
23 #include "MenuBackend.h"
24
25 #include "support/debug.h"
26
27 #include <QCursor>
28 #include <QMenuBar>
29
30
31 namespace lyx {
32 namespace frontend {
33
34 // MacOSX specific stuff is at the end.
35
36 GuiMenubar::GuiMenubar(GuiView * view, MenuBackend & mbe)
37         : owner_(view), menubackend_(mbe)
38 {
39         init();
40 }
41
42
43 void GuiMenubar::init()
44 {
45         // Clear all menubar contents before filling it.
46         owner_->menuBar()->clear();
47         
48 #ifdef Q_WS_MACX
49         // setup special mac specific menu item
50         macxMenuBarInit();
51 #endif
52
53         LYXERR(Debug::GUI, "populating menu bar" << to_utf8(menubackend_.getMenubar().name()));
54
55         if (menubackend_.getMenubar().size() == 0) {
56                 LYXERR(Debug::GUI, "\tERROR: empty menu bar"
57                         << to_utf8(menubackend_.getMenubar().name()));
58                 return;
59                 //                      continue;
60         }
61         else {
62                 LYXERR(Debug::GUI, "menu bar entries "
63                         << menubackend_.getMenubar().size());
64         }
65
66         Menu menu;
67         menubackend_.expand(menubackend_.getMenubar(), menu, owner_->buffer());
68
69         Menu::const_iterator m = menu.begin();
70         Menu::const_iterator end = menu.end();
71
72         for (; m != end; ++m) {
73
74                 if (m->kind() != MenuItem::Submenu) {
75                         LYXERR(Debug::GUI, "\tERROR: not a submenu " << to_utf8(m->label()));
76                         continue;
77                 }
78
79                 LYXERR(Debug::GUI, "menu bar item " << to_utf8(m->label())
80                         << " is a submenu named " << to_utf8(m->submenuname()));
81
82                 docstring name = m->submenuname();
83                 if (!menubackend_.hasMenu(name)) {
84                         LYXERR(Debug::GUI, "\tERROR: " << to_utf8(name)
85                                 << " submenu has no menu!");
86                         continue;
87                 }
88
89                 Menu menu;
90                 menubackend_.expand(menubackend_.getMenubar(), menu, owner_->buffer());
91
92                 GuiPopupMenu * qMenu = new GuiPopupMenu(owner_, *m, true);
93                 owner_->menuBar()->addMenu(qMenu);
94
95                 name_map_[toqstr(name)] = qMenu;
96 /*
97                 QObject::connect(qMenu, SIGNAL(aboutToShow()), this, SLOT(update()));
98                 QObject::connect(qMenu, SIGNAL(triggered(QAction *)), this, SLOT(update()));
99                 QObject::connect(qMenu->menuAction(), SIGNAL(triggered()), this, SLOT(update()));
100 */
101         }
102         //QObject::connect(owner_->menuBar(), SIGNAL(triggered()), this, SLOT(update()));
103 }
104
105
106 GuiMenubar::~GuiMenubar() {
107 #ifdef Q_WS_MACX
108         delete mac_menubar_;
109 #endif
110 }
111
112 void GuiMenubar::openByName(QString const & name)
113 {
114         if (QMenu * menu = name_map_.value(name))
115                 menu->exec(QCursor::pos());
116 }
117
118
119 /// Some special Qt/Mac support hacks
120
121 /*
122   Here is what the Qt documentation says about how a menubar is chosen:
123
124      1) If the window has a QMenuBar then it is used. 2) If the window
125      is a modal then its menubar is used. If no menubar is specified
126      then a default menubar is used (as documented below) 3) If the
127      window has no parent then the default menubar is used (as
128      documented below).
129
130      The above 3 steps are applied all the way up the parent window
131      chain until one of the above are satisifed. If all else fails a
132      default menubar will be created, the default menubar on Qt/Mac is
133      an empty menubar, however you can create a different default
134      menubar by creating a parentless QMenuBar, the first one created
135      will thus be designated the default menubar, and will be used
136      whenever a default menubar is needed.
137
138   Thus, for Qt/Mac, we add the menus to a free standing menubar, so
139   that this menubar will be used also when one of LyX' dialogs has
140   focus. (JMarc)
141 */
142
143 void GuiMenubar::macxMenuBarInit()
144 {
145         mac_menubar_ = new QMenuBar;
146
147         /* Since Qt 4.2, the qt/mac menu code has special code for
148            specifying the role of a menu entry. However, it does not
149            work very well with our scheme of creating menus on demand,
150            and therefore we need to put these entries in a special
151            invisible menu. (JMarc)
152         */
153
154         /* The entries of our special mac menu. If we add support for
155          * special entries in MenuBackend, we could imagine something
156          * like
157          *    SpecialItem About " "About LyX" "dialog-show aboutlyx"
158          * and therefore avoid hardcoding. I am not sure it is worth
159          * the hassle, though. (JMarc)
160          */
161         struct MacMenuEntry {
162                 kb_action action;
163                 char const * arg;
164                 char const * label;
165                 QAction::MenuRole role;
166         };
167
168         MacMenuEntry entries[] = {
169                 {LFUN_DIALOG_SHOW, "aboutlyx", "About LyX",
170                  QAction::AboutRole},
171                 {LFUN_DIALOG_SHOW, "prefs", "Preferences",
172                  QAction::PreferencesRole},
173                 {LFUN_RECONFIGURE, "", "Reconfigure",
174                  QAction::ApplicationSpecificRole},
175                 {LFUN_LYX_QUIT, "", "Quit LyX", QAction::QuitRole}
176         };
177         const size_t num_entries = sizeof(entries) / sizeof(MacMenuEntry);
178
179         // the special menu for MenuBackend.
180         Menu special;
181         for (size_t i = 0 ; i < num_entries ; ++i) {
182                 FuncRequest const func(entries[i].action,
183                                        from_utf8(entries[i].arg));
184                 special.add(MenuItem(MenuItem::Command,
185                                      from_utf8(entries[i].label),
186                                      func));
187         }
188         menubackend_.specialMenu(special);
189
190         // add the entries to a QMenu that will eventually be empty
191         // and therefore invisible.
192         QMenu * qMenu = owner_->menuBar()->addMenu("special");
193
194         // we do not use 'special' because it is a temporary variable,
195         // whereas MenuBackend::specialMenu points to a persistent
196         // copy.
197         Menu::const_iterator cit = menubackend_.specialMenu().begin();
198         Menu::const_iterator end = menubackend_.specialMenu().end();
199         for (size_t i = 0 ; cit != end ; ++cit, ++i) {
200                 Action * action = new Action(*owner_, QIcon(), 
201                                              toqstr(cit->label()),
202                                              cit->func(), QString());
203                 action->setMenuRole(entries[i].role);
204                 qMenu->addAction(action);
205
206         }
207 }
208
209 } // namespace frontend
210 } // namespace lyx
211
212 #include "GuiMenubar_moc.cpp"