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()" << endl;
52 LYXERR(Debug::GUI) << "\tTriggered menu: " << to_utf8(name_) << endl;
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()) << endl;
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 "
78 << to_utf8(menu->name()) << endl;
81 LYXERR(Debug::GUI) << " ***** menu entries " << menu->size() << endl;
83 Menu::const_iterator m = menu->begin();
84 Menu::const_iterator end = menu->end();
86 for (; m != end; ++m) {
88 if (m->kind() == MenuItem::Separator) {
90 qMenu->addSeparator();
91 LYXERR(Debug::GUI) << "adding Menubar Separator" << endl;
93 } else if (m->kind() == MenuItem::Submenu) {
95 LYXERR(Debug::GUI) << "** creating New Sub-Menu "
96 << to_utf8(getLabel(*m)) << endl;
97 QMenu * subMenu = qMenu->addMenu(toqstr(getLabel(*m)));
98 populate(subMenu, m->submenu());
100 } else { // we have a MenuItem::Command
102 LYXERR(Debug::GUI) << "creating Menu Item "
103 << to_utf8(m->label()) << endl;
105 docstring label = getLabel(*m);
106 addBinding(label, *m);
108 Action * action = new Action(*(owner_->view()),
109 QIcon(), toqstr(label), m->func(), QString());
110 qMenu->addAction(action);
116 docstring const GuiPopupMenu::getLabel(MenuItem const & mi)
118 docstring const shortcut = mi.shortcut();
119 docstring label = support::subst(mi.label(),
120 from_ascii("&"), from_ascii("&&"));
122 if (!shortcut.empty()) {
123 docstring::size_type pos = label.find(shortcut);
124 if (pos != docstring::npos)
125 label.insert(pos, 1, char_type('&'));
132 void GuiPopupMenu::addBinding(docstring & label, MenuItem const & mi)
135 docstring const binding = mi.binding(false);
137 docstring const binding = mi.binding(true);
139 if (!binding.empty())
140 label += '\t' + binding;
144 } // namespace frontend
147 #include "GuiPopupMenu_moc.cpp"