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()));
59 connect(this, SIGNAL(activated(int)),
60 owner_->view(), SLOT(activated(int)));
64 bool QLPopupMenu::disabled(Menu * menu)
68 Menu::const_iterator m = menu->begin();
69 Menu::const_iterator end = menu->end();
70 for (; m != end; ++m) {
71 if (m->kind() == MenuItem::Submenu && !disabled(m->submenu())) {
74 FuncStatus const status =
75 owner_->view()->getLyXFunc().getStatus(m->action());
76 if (!status.disabled())
84 void QLPopupMenu::populate(Menu * menu)
86 Menu::const_iterator m = menu->begin();
87 Menu::const_iterator end = menu->end();
88 for (; m != end; ++m) {
89 if (m->kind() == MenuItem::Separator) {
91 } else if (m->kind() == MenuItem::Submenu) {
92 pair<int, QLPopupMenu *> res = createMenu(this, &(*m), owner_);
93 setItemEnabled(res.first, !disabled(m->submenu()));
94 res.second->populate(m->submenu());
96 FuncStatus const status =
97 owner_->view()->getLyXFunc().getStatus(m->action());
98 if (status.disabled() && m->optional())
100 insertItem(getLabel(*m).c_str(), m->action());
101 setItemEnabled(m->action(), !status.disabled());
102 setItemChecked(m->action(), status.onoff(true));
108 void QLPopupMenu::showing()
112 Menu const frommenu = owner_->backend().getMenu(name_);
113 owner_->backend().expand(frommenu, tomenu, owner_->view()->buffer());