2 * \file QLPopupMenu.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 "QLPopupMenu.h"
22 #include "QLMenubar.h"
23 #include "qt_helpers.h"
24 #include "MenuBackend.h"
26 #include "support/lstrings.h"
39 QLPopupMenu::QLPopupMenu(QLMenubar * owner,
40 MenuItem const & mi, bool topLevelMenu)
43 name_ = mi.submenuname();
45 setTitle(toqstr(getLabel(mi)));
48 connect(this, SIGNAL(aboutToShow()), this, SLOT(update()));
52 void QLPopupMenu::update()
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_);
75 void QLPopupMenu::populate(QMenu* qMenu, Menu * menu)
77 LYXERR(Debug::GUI) << "populating menu " << to_utf8(menu->name()) ;
78 if (menu->size() == 0) {
79 LYXERR(Debug::GUI) << "\tERROR: empty menu "
80 << to_utf8(menu->name()) << endl;
84 LYXERR(Debug::GUI) << " ***** menu entries " << menu->size() << endl;
87 Menu::const_iterator m = menu->begin();
88 Menu::const_iterator end = menu->end();
90 for (; m != end; ++m) {
92 if (m->kind() == MenuItem::Separator) {
94 qMenu->addSeparator();
95 LYXERR(Debug::GUI) << "adding Menubar Separator" << endl;
97 } else if (m->kind() == MenuItem::Submenu) {
99 LYXERR(Debug::GUI) << "** creating New Sub-Menu "
100 << to_utf8(getLabel(*m)) << endl;
101 QMenu * subMenu = qMenu->addMenu(toqstr(getLabel(*m)));
102 populate(subMenu, m->submenu());
104 } else { // we have a MenuItem::Command
106 LYXERR(Debug::GUI) << "creating Menu Item "
107 << to_utf8(m->label()) << endl;
109 docstring label = getLabel(*m);
110 addBinding(label, *m);
112 Action * action = new Action(*(owner_->view()),
114 qMenu->addAction(action);
120 docstring const QLPopupMenu::getLabel(MenuItem const & mi)
122 docstring const shortcut = mi.shortcut();
123 docstring label = support::subst(mi.label(),
124 from_ascii("&"), from_ascii("&&"));
126 if (!shortcut.empty()) {
127 docstring::size_type pos = label.find(shortcut);
128 if (pos != docstring::npos)
129 label.insert(pos, 1, char_type('&'));
135 void QLPopupMenu::addBinding(docstring & label, MenuItem const & mi)
138 docstring const binding(mi.binding(false));
140 docstring const binding(mi.binding(true));
142 if (!binding.empty()) {
143 label += '\t' + binding;
147 } // namespace frontend
150 #include "QLPopupMenu_moc.cpp"