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