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>
32 string const getLabel(MenuItem const & mi)
34 string const shortcut = mi.shortcut();
35 string label = subst(mi.label(), "&", "&&");
40 string::size_type pos = label.find(shortcut);
41 if (pos == string::npos)
43 label.insert(pos, 1, '&');
45 if (mi.kind() == MenuItem::Command) {
46 string const binding(mi.binding());
47 if (!binding.empty()) {
48 label += '\t' + binding;
58 pair<int, QLPopupMenu *>
59 createMenu(QMenuData * parent, MenuItem const * item, Menubar::Pimpl * owner,
63 QLPopupMenu * pm = new QLPopupMenu(owner, item->submenuname(), is_toplevel);
64 int id = parent->insertItem(toqstr(getLabel(*item)), pm);
65 return make_pair(id, pm);
69 QLPopupMenu::QLPopupMenu(Menubar::Pimpl * owner,
70 string const & name, bool toplevel)
71 : owner_(owner), name_(name)
74 connect(this, SIGNAL(aboutToShow()), this, SLOT(showing()));
75 connect(this, SIGNAL(activated(int)),
76 owner_->view(), SLOT(activated(int)));
80 void QLPopupMenu::populate(Menu * menu)
82 Menu::const_iterator m = menu->begin();
83 Menu::const_iterator end = menu->end();
84 for (; m != end; ++m) {
85 if (m->kind() == MenuItem::Separator) {
87 } else if (m->kind() == MenuItem::Submenu) {
88 pair<int, QLPopupMenu *> res = createMenu(this, &(*m), owner_);
89 setItemEnabled(res.first,
90 !m->status().disabled());
91 res.second->populate(m->submenu());
93 FuncStatus const status = m->status();
95 insertItem(toqstr(getLabel(*m)), m->action());
96 setItemEnabled(m->action(), !status.disabled());
97 setItemChecked(m->action(), status.onoff(true));
103 void QLPopupMenu::showing()
107 Menu const frommenu = owner_->backend().getMenu(name_);
108 owner_->backend().expand(frommenu, tomenu, owner_->view());