3 * Copyright 2002 the LyX Team
4 * Read the file COPYING
6 * \author John Levon <levon@movementarian.org>
9 #include "MenuBackend.h"
15 #include "QLPopupMenu.h"
17 #include "support/lstrings.h"
24 string const getLabel(MenuItem const & mi)
26 string const shortcut = mi.shortcut();
27 string label = mi.label();
29 label = subst(label, "&", "&&");
34 string::size_type pos = label.find(shortcut);
35 if (pos == string::npos)
37 label.insert(pos, "&");
45 pair<int, QLPopupMenu *> createMenu(QMenuData * parent, MenuItem const * item, Menubar::Pimpl * owner, bool is_toplevel)
48 QLPopupMenu * pm = new QLPopupMenu(owner, item->submenuname(), is_toplevel);
49 int id = parent->insertItem(getLabel(*item).c_str(), pm);
50 return make_pair(id, pm);
54 QLPopupMenu::QLPopupMenu(Menubar::Pimpl * owner, string const & name, bool toplevel)
55 : owner_(owner), name_(name)
58 connect(this, SIGNAL(aboutToShow()), this, SLOT(showing()));
62 bool QLPopupMenu::disabled(Menu * menu)
66 Menu::const_iterator m = menu->begin();
67 Menu::const_iterator end = menu->end();
68 for (; m != end; ++m) {
69 if (m->kind() == MenuItem::Submenu && !disabled(m->submenu())) {
72 FuncStatus const status =
73 owner_->view()->getLyXFunc().getStatus(m->action());
74 if (!status.disabled())
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, !disabled(m->submenu()));
92 res.second->populate(m->submenu());
94 FuncStatus const status =
95 owner_->view()->getLyXFunc().getStatus(m->action());
96 if (status.disabled() && m->optional())
98 insertItem(getLabel(*m).c_str(), 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()->buffer());