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 = mi.label();
37 label = subst(label, "&", "&&");
42 string::size_type pos = label.find(shortcut);
43 if (pos == string::npos)
45 label.insert(pos, "&");
53 pair<int, QLPopupMenu *> createMenu(QMenuData * parent, MenuItem const * item, Menubar::Pimpl * owner, bool is_toplevel)
56 QLPopupMenu * pm = new QLPopupMenu(owner, item->submenuname(), is_toplevel);
57 int id = parent->insertItem(getLabel(*item).c_str(), pm);
58 return make_pair(id, pm);
62 QLPopupMenu::QLPopupMenu(Menubar::Pimpl * owner, 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 && !disabled(m->submenu())) {
82 FuncStatus const status =
83 owner_->view()->getLyXFunc().getStatus(m->action());
84 if (!status.disabled())
92 void QLPopupMenu::populate(Menu * menu)
94 Menu::const_iterator m = menu->begin();
95 Menu::const_iterator end = menu->end();
96 for (; m != end; ++m) {
97 if (m->kind() == MenuItem::Separator) {
99 } else if (m->kind() == MenuItem::Submenu) {
100 pair<int, QLPopupMenu *> res = createMenu(this, &(*m), owner_);
101 setItemEnabled(res.first, !disabled(m->submenu()));
102 res.second->populate(m->submenu());
104 FuncStatus const status =
105 owner_->view()->getLyXFunc().getStatus(m->action());
106 if (status.disabled() && m->optional())
108 insertItem(getLabel(*m).c_str(), m->action());
109 setItemEnabled(m->action(), !status.disabled());
110 setItemChecked(m->action(), status.onoff(true));
116 void QLPopupMenu::showing()
120 Menu const frommenu = owner_->backend().getMenu(name_);
121 owner_->backend().expand(frommenu, tomenu, owner_->view()->buffer());