3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
14 #include "support/lstrings.h"
15 #include "MenuBackend.h"
19 #include "QLMenubar.h"
20 #include "QLPopupMenu.h"
21 #include "qt_helpers.h"
23 using lyx::support::subst;
33 string const getLabel(MenuItem const & mi)
35 string const shortcut = mi.shortcut();
36 string label = subst(mi.label(), "&", "&&");
38 if (!shortcut.empty()) {
39 string::size_type pos = label.find(shortcut);
40 if (pos != string::npos)
41 label.insert(pos, 1, '&');
44 if (mi.kind() == MenuItem::Command) {
45 string const binding(mi.binding());
46 if (!binding.empty()) {
47 label += '\t' + binding;
57 pair<int, QLPopupMenu *>
58 createMenu(QMenuData * parent, MenuItem const * item, QLMenubar * owner,
62 QLPopupMenu * pm = new QLPopupMenu(owner, item->submenuname(), is_toplevel);
63 int const id = parent->insertItem(toqstr(getLabel(*item)), pm);
64 return make_pair(id, pm);
68 QLPopupMenu::QLPopupMenu(QLMenubar * owner,
69 string const & name, bool toplevel)
70 : owner_(owner), name_(name)
73 connect(this, SIGNAL(aboutToShow()), this, SLOT(showing()));
74 connect(this, SIGNAL(activated(int)),
75 this, SLOT(fire(int)));
79 void QLPopupMenu::fire(int index)
81 owner_->view()->activated(funcs_[index]);
85 void QLPopupMenu::populate(Menu * menu)
89 Menu::const_iterator m = menu->begin();
90 Menu::const_iterator end = menu->end();
91 for (; m != end; ++m) {
92 if (m->kind() == MenuItem::Separator) {
94 } else if (m->kind() == MenuItem::Submenu) {
95 pair<int, QLPopupMenu *> res = createMenu(this, &(*m), owner_);
96 setItemEnabled(res.first,
97 !m->status().disabled());
98 res.second->populate(m->submenu());
100 FuncStatus const status = m->status();
102 Funcs::iterator fit =
103 funcs_.insert(funcs_.end(), m->func());
104 int const index = distance(funcs_.begin(), fit);
106 insertItem(toqstr(getLabel(*m)), index);
107 setItemEnabled(index, !status.disabled());
108 setItemChecked(index, status.onoff(true));
114 void QLPopupMenu::showing()
118 Menu const frommenu = owner_->backend().getMenu(name_);
119 owner_->backend().expand(frommenu, tomenu, owner_->view());