]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QLMenubar.C
Added initial qt4 work by Abdelrazak Younes
[lyx.git] / src / frontends / qt4 / QLMenubar.C
1 /**
2  * \file qt2/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 "QtView.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 #include "support/lstrings.h"
25
26 #include "debug.h"
27
28 #ifdef Q_WS_MACX
29 #include "kbmap.h"
30 #include "QLyXKeySym.h"
31 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
32 #endif
33
34 #include <QMenuBar>
35 #include <QCursor>
36
37 using std::pair;
38 using std::string;
39 using std::endl;
40
41 namespace {
42
43 } // namespace anon
44
45 namespace lyx {
46
47
48 namespace frontend {
49
50 // MacOSX specific stuff is at the end.
51
52 QLMenubar::QLMenubar(LyXView * view, MenuBackend & mbe)
53         : owner_(static_cast<QtView*>(view)), menubackend_(mbe)
54 {
55         macxMenuBarInit();
56
57         lyxerr[Debug::GUI] << "populating menu bar" << menubackend_.getMenubar().name() << endl;
58
59         if (menubackend_.getMenubar().size() == 0) {
60                 lyxerr[Debug::GUI] << "\tERROR: empty menu bar" << menubackend_.getMenubar().name() << endl;
61                 return;
62                 //                      continue;
63         }
64         else {
65                 lyxerr[Debug::GUI] << "menu bar entries " << menubackend_.getMenubar().size();
66         }
67         //      for (; m != end; ++m) {
68
69         Menu menu;
70         menubackend_.expand(menubackend_.getMenubar(), menu, owner_);
71
72         Menu::const_iterator m = menu.begin();
73         Menu::const_iterator end = menu.end();
74
75         for (; m != end; ++m) {
76
77                 if (m->kind() != MenuItem::Submenu) {
78                         lyxerr[Debug::GUI] << "\tERROR: not a submenu " << m->label() << endl;
79                         continue;
80                 }
81
82                 lyxerr[Debug::GUI] << "menu bar item " << m->label() << " is a submenu named " << m->submenuname() << endl;
83
84                 string name = m->submenuname();
85                 if (!menubackend_.hasMenu(name)) {
86                         lyxerr[Debug::GUI] << "\tERROR: " << name << " submenu has no menu!" << endl;
87                         continue;
88                 }
89
90                 Menu menu;
91                 menubackend_.expand(menubackend_.getMenubar(), menu, owner_);
92
93                 QLPopupMenu * qMenu = new QLPopupMenu(this, *m, true);
94                 owner_->menuBar()->addMenu(qMenu);
95
96                 pair<NameMap::iterator, bool> I = name_map_.insert(make_pair(name, qMenu));
97                 if (!I.second) {
98                         lyxerr[Debug::GUI] << "\tERROR: " << name << " submenu is already there!" << endl;
99                 }
100 /*
101                 QObject::connect(qMenu, SIGNAL(aboutToShow()), this, SLOT(update()));
102                 QObject::connect(qMenu, SIGNAL(triggered(QAction *)), this, SLOT(update()));
103                 QObject::connect(qMenu->menuAction(), SIGNAL(triggered()), this, SLOT(update()));
104 */
105         }
106         //QObject::connect(owner_->menuBar(), SIGNAL(triggered()), this, SLOT(update()));
107 }
108
109 void QLMenubar::openByName(string const & name)
110 {
111         NameMap::const_iterator const cit = name_map_.find(name);
112         if (cit == name_map_.end())
113                 return;
114
115         // I (Abdel) don't understand this comment:
116         // this will have to do I'm afraid.
117         cit->second->exec(QCursor::pos());
118 }
119
120
121 void QLMenubar::update()
122 { }
123
124 QtView * QLMenubar::view()
125 {
126         return owner_;
127 }
128
129
130 MenuBackend const & QLMenubar::backend()
131 {
132         return menubackend_;
133 }
134
135
136 /*
137   Here is what the Qt documentation says about how a menubar is chosen:
138
139      1) If the window has a QMenuBar then it is used. 2) If the window
140      is a modal then its menubar is used. If no menubar is specified
141      then a default menubar is used (as documented below) 3) If the
142      window has no parent then the default menubar is used (as
143      documented below).
144
145      The above 3 steps are applied all the way up the parent window
146      chain until one of the above are satisifed. If all else fails a
147      default menubar will be created, the default menubar on Qt/Mac is
148      an empty menubar, however you can create a different default
149      menubar by creating a parentless QMenuBar, the first one created
150      will thus be designated the default menubar, and will be used
151      whenever a default menubar is needed.
152
153   Thus, for Qt/Mac, we add the menus to a free standing menubar, so
154   that this menubar will be used also when one of LyX' dialogs has
155   focus. (JMarc)
156 */
157 QMenuBar * QLMenubar::menuBar() const
158 {
159 #ifdef Q_WS_MAC
160         return menubar_.get();
161 #else
162         return owner_->menuBar();
163 #endif
164 }
165
166 void QLMenubar::macxMenuBarInit()
167 {
168 #ifdef Q_WS_MACX
169         menubar_ = new QMenuBar;
170
171         // this is the name of the menu that contains our special entries
172         menubackend_.specialMenu("LyX");
173         // make sure that the special entries are added to the first
174         // menu even before this menu has been opened.
175         name_map_[menubackend_.getMenubar().begin()->submenuname()]->showing();
176 #endif
177 }
178
179 } // namespace frontend
180 } // namespace lyx