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