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