]> git.lyx.org Git - features.git/blob - src/frontends/qt4/QLMenubar.cpp
delete unneeded Menubar virtual interface.
[features.git] / src / frontends / qt4 / QLMenubar.cpp
1 /**
2  * \file qt4/QLMenubar.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 "QLMenubar.h"
19 #include "QLPopupMenu.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 QLMenubar::QLMenubar(LyXView * view, MenuBackend & mbe)
41         : owner_(static_cast<GuiView*>(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                 QLPopupMenu * qMenu = new QLPopupMenu(this, *m, true);
86                 owner_->menuBar()->addMenu(qMenu);
87
88                 pair<NameMap::iterator, bool> I = name_map_.insert(make_pair(name, qMenu));
89                 if (!I.second) {
90                         LYXERR(Debug::GUI) << "\tERROR: " << to_utf8(name)
91                                 << " submenu is already there!" << endl;
92                 }
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 void QLMenubar::openByName(docstring const & name)
103 {
104         NameMap::const_iterator const cit = name_map_.find(name);
105         if (cit == name_map_.end())
106                 return;
107
108         // I (Abdel) don't understand this comment:
109         // this will have to do I'm afraid.
110         cit->second->exec(QCursor::pos());
111 }
112
113
114 GuiView * QLMenubar::view()
115 {
116         return owner_;
117 }
118
119
120 MenuBackend const & QLMenubar::backend()
121 {
122         return menubackend_;
123 }
124
125
126 /// Some special Qt/Mac support hacks
127
128 /*
129   Here is what the Qt documentation says about how a menubar is chosen:
130
131      1) If the window has a QMenuBar then it is used. 2) If the window
132      is a modal then its menubar is used. If no menubar is specified
133      then a default menubar is used (as documented below) 3) If the
134      window has no parent then the default menubar is used (as
135      documented below).
136
137      The above 3 steps are applied all the way up the parent window
138      chain until one of the above are satisifed. If all else fails a
139      default menubar will be created, the default menubar on Qt/Mac is
140      an empty menubar, however you can create a different default
141      menubar by creating a parentless QMenuBar, the first one created
142      will thus be designated the default menubar, and will be used
143      whenever a default menubar is needed.
144
145   Thus, for Qt/Mac, we add the menus to a free standing menubar, so
146   that this menubar will be used also when one of LyX' dialogs has
147   focus. (JMarc)
148 */
149 QMenuBar * QLMenubar::menuBar() const
150 {
151 #ifdef Q_WS_MACX
152         return mac_menubar_.get();
153 #else
154         return owner_->menuBar();
155 #endif
156 }
157
158 #ifdef Q_WS_MACX
159 extern void qt_mac_set_menubar_merge(bool b);
160 #endif
161
162 void QLMenubar::macxMenuBarInit()
163 {
164 #ifdef Q_WS_MACX
165         mac_menubar_.reset(new QMenuBar);
166
167 # if QT_VERSION >= 0x040200
168         /* Since Qt 4.2, the qt/mac menu code has special code for
169            specifying the role of a menu entry. However, it does not
170            work very well with our scheme of creating menus on demand,
171            and therefore we need to put these entries in a special
172            invisible menu. (JMarc)
173         */
174
175         /* The entries of our special mac menu. If we add support for
176          * special entries in MenuBackend, we could imagine something
177          * like
178          *    SpecialItem About " "About LyX" "dialog-show aboutlyx"
179          * and therefore avoid hardcoding. I am not sure it is worth
180          * the hassle, though. (JMarc)
181          */
182         struct MacMenuEntry {
183                 kb_action action;
184                 char const * arg;
185                 char const * label;
186                 QAction::MenuRole role;
187         };
188
189         MacMenuEntry entries[] = {
190                 {LFUN_DIALOG_SHOW, "aboutlyx", "About LyX",
191                  QAction::AboutRole},
192                 {LFUN_DIALOG_SHOW, "prefs", "Preferences",
193                  QAction::PreferencesRole},
194                 {LFUN_RECONFIGURE, "", "Reconfigure",
195                  QAction::ApplicationSpecificRole},
196                 {LFUN_LYX_QUIT, "", "Quit LyX", QAction::QuitRole}
197         };
198         const size_t num_entries = sizeof(entries) / sizeof(MacMenuEntry);
199
200         // the special menu for MenuBackend.
201         Menu special;
202         for (size_t i = 0 ; i < num_entries ; ++i) {
203                 FuncRequest const func(entries[i].action,
204                                        from_utf8(entries[i].arg));
205                 special.add(MenuItem(MenuItem::Command,
206                                      from_utf8(entries[i].label),
207                                      func));
208         }
209         menubackend_.specialMenu(special);
210
211         // add the entries to a QMenu that will eventually be empty
212         // and therefore invisible.
213         QMenu * qMenu = owner_->menuBar()->addMenu("special");
214
215         // we do not use 'special' because it is a temporary variable,
216         // whereas MenuBackend::specialMenu points to a persistent
217         // copy.
218         Menu::const_iterator cit = menubackend_.specialMenu().begin();
219         Menu::const_iterator end = menubackend_.specialMenu().end();
220         for (size_t i = 0 ; cit != end ; ++cit, ++i) {
221                 Action * action = new Action(*owner_, cit->label(),
222                                              cit->func());
223                 action->setMenuRole(entries[i].role);
224                 qMenu->addAction(action);
225
226         }
227 # else
228         qt_mac_set_menubar_merge(false);
229 # endif // QT_VERSION >= 0x040200
230 #endif // Q_WS_MACX
231 }
232
233 } // namespace frontend
234 } // namespace lyx
235
236 #include "QLMenubar_moc.cpp"