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