]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiPopupMenu.cpp
fix memory leaks
[lyx.git] / src / frontends / qt4 / GuiPopupMenu.cpp
1 /**
2  * \file GuiPopupMenu.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  * \author Abdelrazak Younes
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "GuiView.h"
15
16 #include "Action.h"
17 #include "GuiPopupMenu.h"
18 #include "GuiMenubar.h"
19 #include "qt_helpers.h"
20 #include "LyXFunc.h"
21 #include "MenuBackend.h"
22
23 #include "support/lstrings.h"
24 #include "support/debug.h"
25
26 #include <QMenuBar>
27
28 namespace lyx {
29 namespace frontend {
30
31 GuiPopupMenu::GuiPopupMenu(GuiMenubar * owner, MenuItem const & mi,
32                 bool topLevelMenu)
33         : QMenu(owner->menuBar()), owner_(owner)
34 {
35         name_ = mi.submenuname();
36
37         setTitle(toqstr(getLabel(mi)));
38
39         if (topLevelMenu)
40                 connect(this, SIGNAL(aboutToShow()), this, SLOT(updateView()));
41 }
42
43
44 void GuiPopupMenu::updateView()
45 {
46         LYXERR(Debug::GUI, "GuiPopupMenu::updateView()"
47                 << "\tTriggered menu: " << to_utf8(name_));
48
49         clear();
50
51         if (name_.empty())
52                 return;
53
54         // Here, We make sure that theLyXFunc points to the correct LyXView.
55         theLyXFunc().setLyXView(owner_->view());
56
57         Menu const & fromLyxMenu = owner_->backend().getMenu(name_);
58         owner_->backend().expand(fromLyxMenu, topLevelMenu_, owner_->view()->buffer());
59
60         if (!owner_->backend().hasMenu(topLevelMenu_.name())) {
61                 LYXERR(Debug::GUI, "\tWARNING: menu seems empty"
62                         << to_utf8(topLevelMenu_.name()));
63         }
64         populate(this, &topLevelMenu_);
65 }
66
67
68 void GuiPopupMenu::populate(QMenu * qMenu, Menu * menu)
69 {
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()));
73                 return;
74         }
75         LYXERR(Debug::GUI, " *****  menu entries " << menu->size());
76
77         Menu::const_iterator m = menu->begin();
78         Menu::const_iterator end = menu->end();
79
80         for (; m != end; ++m) {
81
82                 if (m->kind() == MenuItem::Separator) {
83
84                         qMenu->addSeparator();
85                         LYXERR(Debug::GUI, "adding Menubar Separator");
86
87                 } else if (m->kind() == MenuItem::Submenu) {
88
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());
93
94                 } else { // we have a MenuItem::Command
95
96                         LYXERR(Debug::GUI, "creating Menu Item "
97                                 << to_utf8(m->label()));
98
99                         docstring label = getLabel(*m);
100                         addBinding(label, *m);
101
102                         Action * action = new Action(*(owner_->view()),
103                                 QIcon(), toqstr(label), m->func(), QString());
104                         qMenu->addAction(action);
105                 }
106         }
107 }
108
109
110 docstring const GuiPopupMenu::getLabel(MenuItem const & mi)
111 {
112         docstring const shortcut = mi.shortcut();
113         docstring label = support::subst(mi.label(),
114         from_ascii("&"), from_ascii("&&"));
115
116         if (!shortcut.empty()) {
117                 size_t pos = label.find(shortcut);
118                 if (pos != docstring::npos)
119                         label.insert(pos, 1, char_type('&'));
120         }
121
122         return label;
123 }
124
125
126 void GuiPopupMenu::addBinding(docstring & label, MenuItem const & mi)
127 {
128 #ifdef Q_WS_MACX
129         docstring const binding = mi.binding(false);
130 #else
131         docstring const binding = mi.binding(true);
132 #endif
133         if (!binding.empty())
134                 label += '\t' + binding;
135 }
136
137
138 } // namespace frontend
139 } // namespace lyx
140
141 #include "GuiPopupMenu_moc.cpp"