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 "QLPopupMenu.h"
22 #include "qt_helpers.h"
24 #include <boost/scoped_ptr.hpp>
26 using namespace lyx::support;
34 string const getLabel(MenuItem const & mi)
36 string const shortcut = mi.shortcut();
37 string label = subst(mi.label(), "&", "&&");
42 string::size_type pos = label.find(shortcut);
43 if (pos == string::npos)
45 label.insert(pos, 1, '&');
47 if (mi.kind() == MenuItem::Command) {
48 string const binding(mi.binding());
49 if (!binding.empty()) {
50 label += '\t' + binding;
60 pair<int, QLPopupMenu *>
61 createMenu(QMenuData * parent, MenuItem const * item, Menubar::Pimpl * owner,
65 QLPopupMenu * pm = new QLPopupMenu(owner, item->submenuname(), is_toplevel);
66 int id = parent->insertItem(toqstr(getLabel(*item)), pm);
67 return make_pair(id, pm);
71 QLPopupMenu::QLPopupMenu(Menubar::Pimpl * owner,
72 string const & name, bool toplevel)
73 : owner_(owner), name_(name)
76 connect(this, SIGNAL(aboutToShow()), this, SLOT(showing()));
77 connect(this, SIGNAL(activated(int)),
78 owner_->view(), SLOT(activated(int)));
82 void QLPopupMenu::populate(Menu * menu)
84 Menu::const_iterator m = menu->begin();
85 Menu::const_iterator end = menu->end();
86 for (; m != end; ++m) {
87 if (m->kind() == MenuItem::Separator) {
89 } else if (m->kind() == MenuItem::Submenu) {
90 pair<int, QLPopupMenu *> res = createMenu(this, &(*m), owner_);
91 setItemEnabled(res.first,
92 !m->status().disabled());
93 res.second->populate(m->submenu());
95 FuncStatus const status = m->status();
97 insertItem(toqstr(getLabel(*m)), m->action());
98 setItemEnabled(m->action(), !status.disabled());
99 setItemChecked(m->action(), status.onoff(true));
105 void QLPopupMenu::showing()
109 Menu const frommenu = owner_->backend().getMenu(name_);
110 owner_->backend().expand(frommenu, tomenu, owner_->view());