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 "GuiPopupMenu.h"
18 #include "GuiMenubar.h"
19 #include "qt_helpers.h"
21 #include "MenuBackend.h"
23 #include "support/lstrings.h"
24 #include "support/debug.h"
31 GuiPopupMenu::GuiPopupMenu(GuiMenubar * owner, MenuItem const & mi,
33 : QMenu(owner->menuBar()), owner_(owner)
35 name_ = mi.submenuname();
37 setTitle(toqstr(getLabel(mi)));
40 connect(this, SIGNAL(aboutToShow()), this, SLOT(updateView()));
44 void GuiPopupMenu::updateView()
46 LYXERR(Debug::GUI, "GuiPopupMenu::updateView()"
47 << "\tTriggered menu: " << to_utf8(name_));
54 // Here, We make sure that theLyXFunc points to the correct LyXView.
55 theLyXFunc().setLyXView(owner_->view());
57 Menu const & fromLyxMenu = owner_->backend().getMenu(name_);
58 owner_->backend().expand(fromLyxMenu, topLevelMenu_, owner_->view()->buffer());
60 if (!owner_->backend().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 << to_utf8(getLabel(*m)));
91 QMenu * subMenu = qMenu->addMenu(toqstr(getLabel(*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 docstring label = getLabel(*m);
100 addBinding(label, *m);
102 Action * action = new Action(*(owner_->view()),
103 QIcon(), toqstr(label), m->func(), QString());
104 qMenu->addAction(action);
110 docstring const GuiPopupMenu::getLabel(MenuItem const & mi)
112 docstring const shortcut = mi.shortcut();
113 docstring label = support::subst(mi.label(),
114 from_ascii("&"), from_ascii("&&"));
116 if (!shortcut.empty()) {
117 size_t pos = label.find(shortcut);
118 if (pos != docstring::npos)
119 label.insert(pos, 1, char_type('&'));
126 void GuiPopupMenu::addBinding(docstring & label, MenuItem const & mi)
129 docstring const binding = mi.binding(false);
131 docstring const binding = mi.binding(true);
133 if (!binding.empty())
134 label += '\t' + binding;
138 } // namespace frontend
141 #include "GuiPopupMenu_moc.cpp"