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