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 #pragma implementation
17 #include "MenuBackend.h"
23 #include "QLPopupMenu.h"
25 #include "support/lstrings.h"
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, "&");
51 pair<int, QLPopupMenu *>
52 createMenu(QMenuData * parent, MenuItem const * item, Menubar::Pimpl * owner, bool is_toplevel)
55 QLPopupMenu * pm = new QLPopupMenu(owner, item->submenuname(), is_toplevel);
56 int id = parent->insertItem(getLabel(*item).c_str(), pm);
57 return make_pair(id, pm);
61 QLPopupMenu::QLPopupMenu(Menubar::Pimpl * owner,
62 string const & name, bool toplevel)
63 : owner_(owner), name_(name)
66 connect(this, SIGNAL(aboutToShow()), this, SLOT(showing()));
67 connect(this, SIGNAL(activated(int)),
68 owner_->view(), SLOT(activated(int)));
72 bool QLPopupMenu::disabled(Menu * menu)
76 Menu::const_iterator m = menu->begin();
77 Menu::const_iterator end = menu->end();
78 for (; m != end; ++m) {
79 if (m->kind() == MenuItem::Submenu
80 && !disabled(m->submenu())) {
83 FuncStatus const status =
84 owner_->view()->getLyXFunc()
85 .getStatus(m->action());
86 if (!status.disabled())
94 void QLPopupMenu::populate(Menu * menu)
96 Menu::const_iterator m = menu->begin();
97 Menu::const_iterator end = menu->end();
98 for (; m != end; ++m) {
99 if (m->kind() == MenuItem::Separator) {
101 } else if (m->kind() == MenuItem::Submenu) {
102 pair<int, QLPopupMenu *> res = createMenu(this, &(*m), owner_);
103 setItemEnabled(res.first, !disabled(m->submenu()));
104 res.second->populate(m->submenu());
106 FuncStatus const status =
107 owner_->view()->getLyXFunc().getStatus(m->action());
108 if (status.disabled() && m->optional())
110 insertItem(getLabel(*m).c_str(), m->action());
111 setItemEnabled(m->action(), !status.disabled());
112 setItemChecked(m->action(), status.onoff(true));
118 void QLPopupMenu::showing()
122 Menu const frommenu = owner_->backend().getMenu(name_);
123 owner_->backend().expand(frommenu, tomenu, owner_->view()->buffer());