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 "support/lstrings.h"
18 #include "MenuBackend.h"
25 #include "QLPopupMenu.h"
27 #include <boost/scoped_ptr.hpp>
32 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
36 string const getLabel(MenuItem const & mi)
38 string const shortcut = mi.shortcut();
39 string label = subst(mi.label(), "&", "&&");
44 string::size_type pos = label.find(shortcut);
45 if (pos == string::npos)
47 label.insert(pos, 1, '&');
49 if (mi.kind() == MenuItem::Command) {
50 // FIXME: backend should do this
51 string const accel(toplevel_keymap->findbinding(mi.action()));
54 label += '\t' + accel.substr(1, accel.find(']') - 1);
57 lyxerr[Debug::GUI] << "Label: " << mi.label()
58 << " Shortcut: " << mi.shortcut()
59 << " Accel: " << accel << endl;
61 lyxerr[Debug::GUI] << "Label: " << mi.label()
62 << " Shortcut: " << mi.shortcut() << endl;
70 pair<int, QLPopupMenu *>
71 createMenu(QMenuData * parent, MenuItem const * item, Menubar::Pimpl * owner, bool is_toplevel)
74 QLPopupMenu * pm = new QLPopupMenu(owner, item->submenuname(), is_toplevel);
75 int id = parent->insertItem(getLabel(*item).c_str(), pm);
76 return make_pair(id, pm);
80 QLPopupMenu::QLPopupMenu(Menubar::Pimpl * owner,
81 string const & name, bool toplevel)
82 : owner_(owner), name_(name)
85 connect(this, SIGNAL(aboutToShow()), this, SLOT(showing()));
86 connect(this, SIGNAL(activated(int)),
87 owner_->view(), SLOT(activated(int)));
91 // FIXME: should all be in backend
92 bool QLPopupMenu::disabled(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::Submenu) {
100 if (!disabled(m->submenu()))
102 } else if (m->kind() != MenuItem::Separator) {
103 FuncStatus const status =
104 owner_->view()->getLyXFunc()
105 .getStatus(m->action());
106 if (!status.disabled())
114 void QLPopupMenu::populate(Menu * menu)
116 Menu::const_iterator m = menu->begin();
117 Menu::const_iterator end = menu->end();
118 for (; m != end; ++m) {
119 if (m->kind() == MenuItem::Separator) {
121 } else if (m->kind() == MenuItem::Submenu) {
122 pair<int, QLPopupMenu *> res = createMenu(this, &(*m), owner_);
123 setItemEnabled(res.first, !disabled(m->submenu()));
124 res.second->populate(m->submenu());
126 FuncStatus const status =
127 owner_->view()->getLyXFunc().getStatus(m->action());
128 if (status.disabled() && m->optional())
130 insertItem(getLabel(*m).c_str(), m->action());
131 setItemEnabled(m->action(), !status.disabled());
132 setItemChecked(m->action(), status.onoff(true));
138 void QLPopupMenu::showing()
142 Menu const frommenu = owner_->backend().getMenu(name_);
143 owner_->backend().expand(frommenu, tomenu, owner_->view()->buffer());