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"
21 #include "QLMenubar.h"
22 #include "QLPopupMenu.h"
23 #include "qt_helpers.h"
25 #include <boost/scoped_ptr.hpp>
27 using namespace lyx::support;
35 string const getLabel(MenuItem const & mi)
37 string const shortcut = mi.shortcut();
38 string label = subst(mi.label(), "&", "&&");
43 string::size_type pos = label.find(shortcut);
44 if (pos == string::npos)
46 label.insert(pos, 1, '&');
48 if (mi.kind() == MenuItem::Command) {
49 string const binding(mi.binding());
50 if (!binding.empty()) {
51 label += '\t' + binding;
61 pair<int, QLPopupMenu *>
62 createMenu(QMenuData * parent, MenuItem const * item, QLMenubar * owner,
66 QLPopupMenu * pm = new QLPopupMenu(owner, item->submenuname(), is_toplevel);
67 int id = parent->insertItem(toqstr(getLabel(*item)), pm);
68 return make_pair(id, pm);
72 QLPopupMenu::QLPopupMenu(QLMenubar * owner,
73 string const & name, bool toplevel)
74 : owner_(owner), name_(name)
77 connect(this, SIGNAL(aboutToShow()), this, SLOT(showing()));
78 connect(this, SIGNAL(activated(int)),
79 owner_->view(), SLOT(activated(int)));
83 void QLPopupMenu::populate(Menu * menu)
85 Menu::const_iterator m = menu->begin();
86 Menu::const_iterator end = menu->end();
87 for (; m != end; ++m) {
88 if (m->kind() == MenuItem::Separator) {
90 } else if (m->kind() == MenuItem::Submenu) {
91 pair<int, QLPopupMenu *> res = createMenu(this, &(*m), owner_);
92 setItemEnabled(res.first,
93 !m->status().disabled());
94 res.second->populate(m->submenu());
96 FuncStatus const status = m->status();
98 insertItem(toqstr(getLabel(*m)), m->action());
99 setItemEnabled(m->action(), !status.disabled());
100 setItemChecked(m->action(), status.onoff(true));
106 void QLPopupMenu::showing()
110 Menu const frommenu = owner_->backend().getMenu(name_);
111 owner_->backend().expand(frommenu, tomenu, owner_->view());