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