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 // Qt defines a macro 'signals' that clashes with a boost namespace.
15 // All is well if the namespace is visible first.
19 #include "GuiPopupMenu.h"
20 #include "GuiMenubar.h"
21 #include "qt_helpers.h"
23 #include "MenuBackend.h"
25 #include "support/lstrings.h"
38 GuiPopupMenu::GuiPopupMenu(GuiMenubar * owner,
39 MenuItem const & mi, bool topLevelMenu)
42 name_ = mi.submenuname();
44 setTitle(toqstr(getLabel(mi)));
47 connect(this, SIGNAL(aboutToShow()), this, SLOT(updateView()));
51 void GuiPopupMenu::updateView()
53 LYXERR(Debug::GUI) << "GuiPopupMenu::updateView()" << endl;
54 LYXERR(Debug::GUI) << "\tTriggered menu: " << to_utf8(name_) << endl;
61 // Here, We make sure that theLyXFunc points to the correct LyXView.
62 theLyXFunc().setLyXView(owner_->view());
64 Menu const & fromLyxMenu = owner_->backend().getMenu(name_);
65 owner_->backend().expand(fromLyxMenu, topLevelMenu_, owner_->view()->buffer());
67 if (!owner_->backend().hasMenu(topLevelMenu_.name())) {
68 LYXERR(Debug::GUI) << "\tWARNING: menu seems empty"
69 << to_utf8(topLevelMenu_.name()) << endl;
71 populate(this, &topLevelMenu_);
75 void GuiPopupMenu::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 GuiPopupMenu::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('&'));
136 void GuiPopupMenu::addBinding(docstring & label, MenuItem const & mi)
139 docstring const binding = mi.binding(false);
141 docstring const binding = mi.binding(true);
143 if (!binding.empty())
144 label += '\t' + binding;
148 } // namespace frontend
151 #include "GuiPopupMenu_moc.cpp"