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;
32 string const getLabel(MenuItem const & mi)
34 string const shortcut = mi.shortcut();
35 string label = subst(mi.label(), "&", "&&");
37 if (!shortcut.empty()) {
38 string::size_type pos = label.find(shortcut);
39 if (pos != string::npos)
40 label.insert(pos, 1, '&');
43 if (mi.kind() == MenuItem::Command) {
44 string const binding(mi.binding());
45 if (!binding.empty()) {
46 label += '\t' + binding;
56 pair<int, QLPopupMenu *>
57 createMenu(QMenuData * parent, MenuItem const * item, QLMenubar * owner,
61 QLPopupMenu * pm = new QLPopupMenu(owner, item->submenuname(), is_toplevel);
62 int const id = parent->insertItem(toqstr(getLabel(*item)), pm);
63 return make_pair(id, pm);
67 QLPopupMenu::QLPopupMenu(QLMenubar * owner,
68 string const & name, bool toplevel)
69 : owner_(owner), name_(name)
72 connect(this, SIGNAL(aboutToShow()), this, SLOT(showing()));
73 connect(this, SIGNAL(activated(int)),
74 this, SLOT(fire(int)));
78 void QLPopupMenu::fire(int index)
80 owner_->view()->activated(funcs_[index]);
84 void QLPopupMenu::populate(Menu * menu)
88 Menu::const_iterator m = menu->begin();
89 Menu::const_iterator end = menu->end();
90 for (; m != end; ++m) {
91 if (m->kind() == MenuItem::Separator) {
93 } else if (m->kind() == MenuItem::Submenu) {
94 pair<int, QLPopupMenu *> res = createMenu(this, &(*m), owner_);
95 setItemEnabled(res.first,
96 !m->status().disabled());
97 res.second->populate(m->submenu());
99 FuncStatus const status = m->status();
101 Funcs::iterator fit =
102 funcs_.insert(funcs_.end(), m->func());
103 int const index = std::distance(funcs_.begin(), fit);
105 insertItem(toqstr(getLabel(*m)), index);
106 setItemEnabled(index, !status.disabled());
107 setItemChecked(index, status.onoff(true));
113 void QLPopupMenu::showing()
117 Menu const frommenu = owner_->backend().getMenu(name_);
118 owner_->backend().expand(frommenu, tomenu, owner_->view());