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"
21 string const getLabel(MenuItem const & mi)
23 string const shortcut = mi.shortcut();
24 string label = mi.label();
26 label = subst(label, "&", "&&");
31 string::size_type pos = label.find(shortcut);
32 if (pos == string::npos)
34 label.insert(pos, "&");
42 int createMenu(QMenuData * parent, MenuItem const * item, Menubar::Pimpl * owner)
45 QLPopupMenu * pm = new QLPopupMenu(owner, item->submenuname());
46 return parent->insertItem(getLabel(*item).c_str(), pm);
50 QLPopupMenu::QLPopupMenu(Menubar::Pimpl * owner, string const & name)
51 : owner_(owner), name_(name)
53 connect(this, SIGNAL(aboutToShow()), this, SLOT(showing()));
57 bool QLPopupMenu::disabled(string const & name)
62 Menu const frommenu = owner_->backend().getMenu(name);
63 owner_->backend().expand(frommenu, tomenu, owner_->view()->buffer());
64 Menu::const_iterator m = tomenu.begin();
65 Menu::const_iterator end = tomenu.end();
66 for (; m != end; ++m) {
67 if (m->kind() == MenuItem::Submenu && !disabled(m->submenuname())) {
70 FuncStatus const status =
71 owner_->view()->getLyXFunc().getStatus(m->action());
72 if (!status.disabled())
80 void QLPopupMenu::showing()
84 Menu const frommenu = owner_->backend().getMenu(name_);
85 owner_->backend().expand(frommenu, tomenu, owner_->view()->buffer());
86 Menu::const_iterator m = tomenu.begin();
87 Menu::const_iterator end = tomenu.end();
88 for (; m != end; ++m) {
89 if (m->kind() == MenuItem::Separator) {
91 } else if (m->kind() == MenuItem::Submenu) {
92 int id(createMenu(this, m, owner_));
93 setItemEnabled(id, !disabled(m->submenuname()));
95 insertItem(getLabel(*m).c_str(), m->action());
96 FuncStatus const status =
97 owner_->view()->getLyXFunc().getStatus(m->action());
98 setItemEnabled(m->action(), !status.disabled());
99 setItemChecked(m->action(), status.onoff(true));