]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QLPopupMenu.cpp
Transfer Text::drawSelection() from InsetText::drawSelection() to InsetText::draw...
[lyx.git] / src / frontends / qt4 / QLPopupMenu.cpp
1 /**
2  * \file QLPopupMenu.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  * \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
36 namespace lyx {
37 namespace frontend {
38
39 QLPopupMenu::QLPopupMenu(QLMenubar * owner,
40                                                  MenuItem const & mi, bool topLevelMenu)
41         : owner_(owner)
42 {
43         name_ = mi.submenuname();
44
45         setTitle(toqstr(getLabel(mi)));
46
47         if (topLevelMenu)
48                 connect(this, SIGNAL(aboutToShow()), this, SLOT(update()));
49 }
50
51
52 void QLPopupMenu::update()
53 {
54         LYXERR(Debug::GUI) << BOOST_CURRENT_FUNCTION << endl;
55         LYXERR(Debug::GUI) << "\tTriggered menu: " << to_utf8(name_) << endl;
56
57         clear();
58
59         if (name_.empty())
60                 return;
61
62         // Here, We make sure that theLyXFunc points to the correct LyXView.
63         theLyXFunc().setLyXView(owner_->view());
64
65         Menu const & fromLyxMenu = owner_->backend().getMenu(name_);
66         owner_->backend().expand(fromLyxMenu, topLevelMenu_, owner_->view()->buffer());
67
68         if (!owner_->backend().hasMenu(topLevelMenu_.name())) {
69                 LYXERR(Debug::GUI) << "\tWARNING: menu seems empty"
70                         << to_utf8(topLevelMenu_.name()) << endl;
71         }
72         populate(this, &topLevelMenu_);
73 }
74
75 void QLPopupMenu::populate(QMenu* qMenu, Menu * menu)
76 {
77         LYXERR(Debug::GUI) << "populating menu " << to_utf8(menu->name()) ;
78         if (menu->size() == 0) {
79                 LYXERR(Debug::GUI) << "\tERROR: empty menu "
80                         << to_utf8(menu->name()) << endl;
81                 return;
82         }
83         else {
84                 LYXERR(Debug::GUI) << " *****  menu entries " << menu->size() << endl;
85         }
86
87         Menu::const_iterator m = menu->begin();
88         Menu::const_iterator end = menu->end();
89
90         for (; m != end; ++m) {
91
92                 if (m->kind() == MenuItem::Separator) {
93
94                         qMenu->addSeparator();
95                         LYXERR(Debug::GUI) << "adding Menubar Separator" << endl;
96
97                 } else if (m->kind() == MenuItem::Submenu) {
98
99                         LYXERR(Debug::GUI) << "** creating New Sub-Menu "
100                                 << to_utf8(getLabel(*m)) << endl;
101                         QMenu * subMenu = qMenu->addMenu(toqstr(getLabel(*m)));
102                         populate(subMenu, m->submenu());
103
104                 } else { // we have a MenuItem::Command
105
106                         LYXERR(Debug::GUI) << "creating Menu Item "
107                                 << to_utf8(m->label()) << endl;
108
109                         docstring label = getLabel(*m);
110                         addBinding(label, *m);
111
112                         Action * action = new Action(*(owner_->view()),
113                                                      label, m->func());
114                         qMenu->addAction(action);
115                 }
116         }
117 }
118
119
120 docstring const QLPopupMenu::getLabel(MenuItem const & mi)
121 {
122         docstring const shortcut = mi.shortcut();
123         docstring label = support::subst(mi.label(),
124         from_ascii("&"), from_ascii("&&"));
125
126         if (!shortcut.empty()) {
127                 docstring::size_type pos = label.find(shortcut);
128                 if (pos != docstring::npos)
129                         label.insert(pos, 1, char_type('&'));
130         }
131
132         return label;
133 }
134
135 void QLPopupMenu::addBinding(docstring & label, MenuItem const & mi)
136 {
137 #ifdef Q_WS_MACX
138         docstring const binding(mi.binding(false));
139 #else
140         docstring const binding(mi.binding(true));
141 #endif
142         if (!binding.empty()) {
143                 label += '\t' + binding;
144         }
145 }
146
147 } // namespace frontend
148 } // namespace lyx
149
150 #include "QLPopupMenu_moc.cpp"