]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QLMenubar.C
67a8e60798cd495e3b13f49c2ae47812206df693
[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 "QLMenubar.h"
18 #include "QLPopupMenu.h"
19
20 #include "qt_helpers.h"
21 #include "support/lstrings.h"
22
23 #include "MenuBackend.h"
24
25 #include "debug.h"
26
27 #include <QMenuBar>
28 #include <QCursor>
29
30 using std::pair;
31 using std::string;
32 using std::endl;
33
34 namespace {
35
36 } // namespace anon
37
38 namespace lyx {
39
40
41 namespace frontend {
42
43 // MacOSX specific stuff is at the end.
44
45 QLMenubar::QLMenubar(LyXView * view, MenuBackend & mbe)
46         : owner_(static_cast<GuiView*>(view)), menubackend_(mbe)
47 {
48         macxMenuBarInit();
49
50         lyxerr[Debug::GUI] << "populating menu bar" << lyx::to_utf8(menubackend_.getMenubar().name()) << endl;
51
52         if (menubackend_.getMenubar().size() == 0) {
53                 lyxerr[Debug::GUI] << "\tERROR: empty menu bar" << lyx::to_utf8(menubackend_.getMenubar().name()) << endl;
54                 return;
55                 //                      continue;
56         }
57         else {
58                 lyxerr[Debug::GUI] << "menu bar entries " << menubackend_.getMenubar().size();
59         }
60         //      for (; m != end; ++m) {
61
62         Menu menu;
63         menubackend_.expand(menubackend_.getMenubar(), menu, owner_->buffer());
64
65         Menu::const_iterator m = menu.begin();
66         Menu::const_iterator end = menu.end();
67
68         for (; m != end; ++m) {
69
70                 if (m->kind() != MenuItem::Submenu) {
71                         lyxerr[Debug::GUI] << "\tERROR: not a submenu " << lyx::to_utf8(m->label()) << endl;
72                         continue;
73                 }
74
75                 lyxerr[Debug::GUI] << "menu bar item " << lyx::to_utf8(m->label()) << " is a submenu named " << lyx::to_utf8(m->submenuname()) << endl;
76
77                 docstring name = m->submenuname();
78                 if (!menubackend_.hasMenu(name)) {
79                         lyxerr[Debug::GUI] << "\tERROR: " << lyx::to_utf8(name) << " submenu has no menu!" << endl;
80                         continue;
81                 }
82
83                 Menu menu;
84                 menubackend_.expand(menubackend_.getMenubar(), menu, owner_->buffer());
85
86                 QLPopupMenu * qMenu = new QLPopupMenu(this, *m, true);
87                 owner_->menuBar()->addMenu(qMenu);
88
89                 pair<NameMap::iterator, bool> I = name_map_.insert(make_pair(name, qMenu));
90                 if (!I.second) {
91                         lyxerr[Debug::GUI] << "\tERROR: " << lyx::to_utf8(name) << " 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 void QLMenubar::update()
115 { }
116
117 GuiView * QLMenubar::view()
118 {
119         return owner_;
120 }
121
122
123 MenuBackend const & QLMenubar::backend()
124 {
125         return menubackend_;
126 }
127
128
129 /*
130   Here is what the Qt documentation says about how a menubar is chosen:
131
132      1) If the window has a QMenuBar then it is used. 2) If the window
133      is a modal then its menubar is used. If no menubar is specified
134      then a default menubar is used (as documented below) 3) If the
135      window has no parent then the default menubar is used (as
136      documented below).
137
138      The above 3 steps are applied all the way up the parent window
139      chain until one of the above are satisifed. If all else fails a
140      default menubar will be created, the default menubar on Qt/Mac is
141      an empty menubar, however you can create a different default
142      menubar by creating a parentless QMenuBar, the first one created
143      will thus be designated the default menubar, and will be used
144      whenever a default menubar is needed.
145
146   Thus, for Qt/Mac, we add the menus to a free standing menubar, so
147   that this menubar will be used also when one of LyX' dialogs has
148   focus. (JMarc)
149 */
150 QMenuBar * QLMenubar::menuBar() const
151 {
152 #ifdef Q_WS_MACX
153         return mac_menubar_.get();
154 #else
155         return owner_->menuBar();
156 #endif
157 }
158
159 void QLMenubar::macxMenuBarInit()
160 {
161 #ifdef Q_WS_MACX
162         mac_menubar_.reset(new QMenuBar);
163
164         // this is the name of the menu that contains our special entries
165         menubackend_.specialMenu(lyx::from_ascii("LyX"));
166         // make sure that the special entries are added to the first
167         // menu even before this menu has been opened.
168         //name_map_[menubackend_.getMenubar().begin()->submenuname()]->update();
169 #endif
170 }
171
172 } // namespace frontend
173 } // namespace lyx
174
175 #include "QLMenubar_moc.cpp"