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.
17 #include "GuiApplication.h"
18 #include "GuiPopupMenu.h"
19 #include "qt_helpers.h"
22 #include "MenuBackend.h"
24 #include "support/debug.h"
25 #include "support/lstrings.h"
30 GuiPopupMenu::GuiPopupMenu(GuiView * owner, MenuItem const & mi,
32 : QMenu(owner), owner_(owner)
34 name_ = toqstr(mi.submenuname());
39 connect(this, SIGNAL(aboutToShow()), this, SLOT(updateView()));
43 void GuiPopupMenu::updateView()
45 LYXERR(Debug::GUI, "GuiPopupMenu::updateView()"
46 << "\tTriggered menu: " << fromqstr(name_));
53 // Here, We make sure that theLyXFunc points to the correct LyXView.
54 theLyXFunc().setLyXView(owner_);
56 MenuBackend const & menubackend = guiApp->menuBackend();
57 Menu const & fromLyxMenu = menubackend.getMenu(qstring_to_ucs4(name_));
58 menubackend.expand(fromLyxMenu, topLevelMenu_, owner_->buffer());
60 if (!menubackend.hasMenu(topLevelMenu_.name())) {
61 LYXERR(Debug::GUI, "\tWARNING: menu seems empty"
62 << to_utf8(topLevelMenu_.name()));
64 populate(this, &topLevelMenu_);
68 void GuiPopupMenu::populate(QMenu * qMenu, Menu * menu)
70 LYXERR(Debug::GUI, "populating menu " << to_utf8(menu->name()));
71 if (menu->size() == 0) {
72 LYXERR(Debug::GUI, "\tERROR: empty menu " << to_utf8(menu->name()));
75 LYXERR(Debug::GUI, " ***** menu entries " << menu->size());
77 Menu::const_iterator m = menu->begin();
78 Menu::const_iterator end = menu->end();
80 for (; m != end; ++m) {
82 if (m->kind() == MenuItem::Separator) {
84 qMenu->addSeparator();
85 LYXERR(Debug::GUI, "adding Menubar Separator");
87 } else if (m->kind() == MenuItem::Submenu) {
89 LYXERR(Debug::GUI, "** creating New Sub-Menu "
90 << fromqstr(label(*m)));
91 QMenu * subMenu = qMenu->addMenu(label(*m));
92 populate(subMenu, m->submenu());
94 } else { // we have a MenuItem::Command
96 LYXERR(Debug::GUI, "creating Menu Item "
97 << to_utf8(m->label()));
99 Action * action = new Action(*(owner_),
100 QIcon(), label(*m), m->func(), QString());
101 qMenu->addAction(action);
107 QString GuiPopupMenu::label(MenuItem const & mi) const
109 docstring label = support::subst(mi.label(),
110 from_ascii("&"), from_ascii("&&"));
112 docstring const shortcut = mi.shortcut();
113 if (!shortcut.empty()) {
114 size_t pos = label.find(shortcut);
115 if (pos != docstring::npos)
116 label.insert(pos, 1, char_type('&'));
119 docstring const binding = mi.binding();
120 if (!binding.empty())
121 label += '\t' + binding;
123 return toqstr(label);
127 } // namespace frontend
130 #include "GuiPopupMenu_moc.cpp"