]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QLPopupMenu.C
renaming in frontends/qt4/ui: s/Q//g
[lyx.git] / src / frontends / qt4 / QLPopupMenu.C
1 /**
2  * \file QLPopupMenu.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  * \author Abdelrazak Younes
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include <boost/current_function.hpp>
15
16 // Qt defines a macro 'signals' that clashes with a boost namespace.
17 // All is well if the namespace is visible first.
18 #include "GuiView.h"
19
20 #include "Action.h"
21 #include "QLPopupMenu.h"
22 #include "QLMenubar.h"
23 #include "qt_helpers.h"
24 #include "MenuBackend.h"
25
26 #include "support/lstrings.h"
27 #include "debug.h"
28
29
30 using std::make_pair;
31 using std::string;
32 using std::pair;
33 using std::endl;
34
35 namespace {
36
37 } // namespace anon
38
39 namespace lyx {
40
41 namespace frontend {
42
43
44 QLPopupMenu::QLPopupMenu(QLMenubar * owner,
45                                                  MenuItem const & mi, bool topLevelMenu)
46         : owner_(owner)
47 {
48         name_ = mi.submenuname();
49
50         setTitle(toqstr(getLabel(mi)));
51
52         if (topLevelMenu)
53                 connect(this, SIGNAL(aboutToShow()), this, SLOT(update()));
54 }
55
56
57
58 void QLPopupMenu::update()
59 {
60         LYXERR(Debug::GUI) << BOOST_CURRENT_FUNCTION << endl;
61         LYXERR(Debug::GUI) << "\tTriggered menu: " << lyx::to_utf8(name_) << endl;
62
63         clear();
64
65         if (name_.empty())
66                 return;
67
68         // Here, We make sure that theLyXFunc points to the correct LyXView.
69         theLyXFunc().setLyXView(owner_->view());
70
71         Menu const & fromLyxMenu = owner_->backend().getMenu(name_);
72         owner_->backend().expand(fromLyxMenu, topLevelMenu_, owner_->view()->buffer());
73
74         if (!owner_->backend().hasMenu(topLevelMenu_.name())) {
75                 LYXERR(Debug::GUI) << "\tWARNING: menu seems empty" << lyx::to_utf8(topLevelMenu_.name()) << endl;
76         }
77         populate(this, &topLevelMenu_);
78 }
79
80 void QLPopupMenu::populate(QMenu* qMenu, Menu * menu)
81 {
82         LYXERR(Debug::GUI) << "populating menu " << lyx::to_utf8(menu->name()) ;
83         if (menu->size() == 0) {
84                 LYXERR(Debug::GUI) << "\tERROR: empty menu " << lyx::to_utf8(menu->name()) << endl;
85                 return;
86         }
87         else {
88                 LYXERR(Debug::GUI) << " *****  menu entries " << menu->size() << endl;
89         }
90
91         Menu::const_iterator m = menu->begin();
92         Menu::const_iterator end = menu->end();
93
94         for (; m != end; ++m) {
95
96                 if (m->kind() == MenuItem::Separator) {
97
98                         qMenu->addSeparator();
99                         LYXERR(Debug::GUI) << "adding Menubar Separator" << endl;
100
101                 } else if (m->kind() == MenuItem::Submenu) {
102
103                         LYXERR(Debug::GUI) << "** creating New Sub-Menu " << lyx::to_utf8(getLabel(*m)) << endl;
104                         QMenu * subMenu = qMenu->addMenu(toqstr(getLabel(*m)));
105                         populate(subMenu, m->submenu());
106
107                 } else { // we have a MenuItem::Command
108
109                         LYXERR(Debug::GUI) << "creating Menu Item " << lyx::to_utf8(m->label()) << endl;
110
111                         docstring label = getLabel(*m);
112                         addBinding(label, *m);
113
114                         Action * action = new Action(*(owner_->view()),
115                                                      label, m->func());
116                         qMenu->addAction(action);
117                 }
118         }
119 }
120
121 docstring const QLPopupMenu::getLabel(MenuItem const & mi)
122 {
123         docstring const shortcut = mi.shortcut();
124         docstring label = support::subst(mi.label(),
125                                       lyx::from_ascii("&"),
126                                       lyx::from_ascii("&&"));
127
128         if (!shortcut.empty()) {
129                 docstring::size_type pos = label.find(shortcut);
130                 if (pos != docstring::npos)
131                         label.insert(pos, 1, char_type('&'));
132         }
133
134         return label;
135 }
136
137 void QLPopupMenu::addBinding(docstring & label, MenuItem const & mi)
138 {
139 #ifdef Q_WS_MACX
140         docstring const binding(mi.binding(false));
141 #else
142         docstring const binding(mi.binding(true));
143 #endif
144         if (!binding.empty()) {
145                 label += '\t' + binding;
146         }
147 }
148
149 } // namespace frontend
150 } // namespace lyx
151
152 #include "QLPopupMenu_moc.cpp"