2 * \file GuiPopupMenu.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Abdelrazak Younes
9 * Full author contact details are available in file CREDITS.
14 #include <boost/current_function.hpp>
16 // Qt defines a macro 'signals' that clashes with a boost namespace.
17 // All is well if the namespace is visible first.
21 #include "GuiPopupMenu.h"
22 #include "GuiMenubar.h"
23 #include "qt_helpers.h"
24 #include "MenuBackend.h"
26 #include "support/lstrings.h"
39 GuiPopupMenu::GuiPopupMenu(GuiMenubar * owner,
40 MenuItem const & mi, bool topLevelMenu)
43 name_ = mi.submenuname();
45 setTitle(toqstr(getLabel(mi)));
48 connect(this, SIGNAL(aboutToShow()), this, SLOT(updateView()));
52 void GuiPopupMenu::updateView()
54 LYXERR(Debug::GUI) << BOOST_CURRENT_FUNCTION << endl;
55 LYXERR(Debug::GUI) << "\tTriggered menu: " << to_utf8(name_) << endl;
62 // Here, We make sure that theLyXFunc points to the correct LyXView.
63 theLyXFunc().setLyXView(owner_->view());
65 Menu const & fromLyxMenu = owner_->backend().getMenu(name_);
66 owner_->backend().expand(fromLyxMenu, topLevelMenu_, owner_->view()->buffer());
68 if (!owner_->backend().hasMenu(topLevelMenu_.name())) {
69 LYXERR(Debug::GUI) << "\tWARNING: menu seems empty"
70 << to_utf8(topLevelMenu_.name()) << endl;
72 populate(this, &topLevelMenu_);
76 void GuiPopupMenu::populate(QMenu * qMenu, Menu * menu)
78 LYXERR(Debug::GUI) << "populating menu " << to_utf8(menu->name()) ;
79 if (menu->size() == 0) {
80 LYXERR(Debug::GUI) << "\tERROR: empty menu "
81 << to_utf8(menu->name()) << endl;
85 LYXERR(Debug::GUI) << " ***** menu entries " << menu->size() << endl;
88 Menu::const_iterator m = menu->begin();
89 Menu::const_iterator end = menu->end();
91 for (; m != end; ++m) {
93 if (m->kind() == MenuItem::Separator) {
95 qMenu->addSeparator();
96 LYXERR(Debug::GUI) << "adding Menubar Separator" << endl;
98 } else if (m->kind() == MenuItem::Submenu) {
100 LYXERR(Debug::GUI) << "** creating New Sub-Menu "
101 << to_utf8(getLabel(*m)) << endl;
102 QMenu * subMenu = qMenu->addMenu(toqstr(getLabel(*m)));
103 populate(subMenu, m->submenu());
105 } else { // we have a MenuItem::Command
107 LYXERR(Debug::GUI) << "creating Menu Item "
108 << to_utf8(m->label()) << endl;
110 docstring label = getLabel(*m);
111 addBinding(label, *m);
113 Action * action = new Action(*(owner_->view()),
115 qMenu->addAction(action);
121 docstring const GuiPopupMenu::getLabel(MenuItem const & mi)
123 docstring const shortcut = mi.shortcut();
124 docstring label = support::subst(mi.label(),
125 from_ascii("&"), from_ascii("&&"));
127 if (!shortcut.empty()) {
128 docstring::size_type pos = label.find(shortcut);
129 if (pos != docstring::npos)
130 label.insert(pos, 1, char_type('&'));
137 void GuiPopupMenu::addBinding(docstring & label, MenuItem const & mi)
140 docstring const binding = mi.binding(false);
142 docstring const binding = mi.binding(true);
144 if (!binding.empty())
145 label += '\t' + binding;
149 } // namespace frontend
152 #include "GuiPopupMenu_moc.cpp"