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"
36 GuiPopupMenu::GuiPopupMenu(GuiMenubar * owner, MenuItem const & mi,
40 name_ = mi.submenuname();
42 setTitle(toqstr(getLabel(mi)));
45 connect(this, SIGNAL(aboutToShow()), this, SLOT(updateView()));
49 void GuiPopupMenu::updateView()
51 LYXERR(Debug::GUI, "GuiPopupMenu::updateView()"
52 << "\tTriggered menu: " << to_utf8(name_));
59 // Here, We make sure that theLyXFunc points to the correct LyXView.
60 theLyXFunc().setLyXView(owner_->view());
62 Menu const & fromLyxMenu = owner_->backend().getMenu(name_);
63 owner_->backend().expand(fromLyxMenu, topLevelMenu_, owner_->view()->buffer());
65 if (!owner_->backend().hasMenu(topLevelMenu_.name())) {
66 LYXERR(Debug::GUI, "\tWARNING: menu seems empty"
67 << to_utf8(topLevelMenu_.name()));
69 populate(this, &topLevelMenu_);
73 void GuiPopupMenu::populate(QMenu * qMenu, Menu * menu)
75 LYXERR(Debug::GUI, "populating menu " << to_utf8(menu->name()));
76 if (menu->size() == 0) {
77 LYXERR(Debug::GUI, "\tERROR: empty menu " << to_utf8(menu->name()));
80 LYXERR(Debug::GUI, " ***** menu entries " << menu->size());
82 Menu::const_iterator m = menu->begin();
83 Menu::const_iterator end = menu->end();
85 for (; m != end; ++m) {
87 if (m->kind() == MenuItem::Separator) {
89 qMenu->addSeparator();
90 LYXERR(Debug::GUI, "adding Menubar Separator");
92 } else if (m->kind() == MenuItem::Submenu) {
94 LYXERR(Debug::GUI, "** creating New Sub-Menu "
95 << to_utf8(getLabel(*m)));
96 QMenu * subMenu = qMenu->addMenu(toqstr(getLabel(*m)));
97 populate(subMenu, m->submenu());
99 } else { // we have a MenuItem::Command
101 LYXERR(Debug::GUI, "creating Menu Item "
102 << to_utf8(m->label()));
104 docstring label = getLabel(*m);
105 addBinding(label, *m);
107 Action * action = new Action(*(owner_->view()),
108 QIcon(), toqstr(label), m->func(), QString());
109 qMenu->addAction(action);
115 docstring const GuiPopupMenu::getLabel(MenuItem const & mi)
117 docstring const shortcut = mi.shortcut();
118 docstring label = support::subst(mi.label(),
119 from_ascii("&"), from_ascii("&&"));
121 if (!shortcut.empty()) {
122 docstring::size_type pos = label.find(shortcut);
123 if (pos != docstring::npos)
124 label.insert(pos, 1, char_type('&'));
131 void GuiPopupMenu::addBinding(docstring & label, MenuItem const & mi)
134 docstring const binding = mi.binding(false);
136 docstring const binding = mi.binding(true);
138 if (!binding.empty())
139 label += '\t' + binding;
143 } // namespace frontend
146 #include "GuiPopupMenu_moc.cpp"