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