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