]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiPopupMenu.cpp
* GuiMenuBar:
[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 "qt_helpers.h"
19
20 #include "LyXFunc.h"
21 #include "MenuBackend.h"
22
23 #include "support/lstrings.h"
24 #include "support/debug.h"
25
26 namespace lyx {
27 namespace frontend {
28
29 GuiPopupMenu::GuiPopupMenu(GuiView * owner, MenuItem const & mi,
30                 bool topLevelMenu)
31         : QMenu(owner), owner_(owner)
32 {
33         name_ = mi.submenuname();
34
35         setTitle(toqstr(getLabel(mi)));
36
37         if (topLevelMenu)
38                 connect(this, SIGNAL(aboutToShow()), this, SLOT(updateView()));
39 }
40
41
42 void GuiPopupMenu::updateView()
43 {
44         LYXERR(Debug::GUI, "GuiPopupMenu::updateView()"
45                 << "\tTriggered menu: " << to_utf8(name_));
46
47         clear();
48
49         if (name_.empty())
50                 return;
51
52         // Here, We make sure that theLyXFunc points to the correct LyXView.
53         theLyXFunc().setLyXView(owner_);
54
55         Menu const & fromLyxMenu = menubackend.getMenu(name_);
56         menubackend.expand(fromLyxMenu, topLevelMenu_, owner_->buffer());
57
58         if (!menubackend.hasMenu(topLevelMenu_.name())) {
59                 LYXERR(Debug::GUI, "\tWARNING: menu seems empty"
60                         << to_utf8(topLevelMenu_.name()));
61         }
62         populate(this, &topLevelMenu_);
63 }
64
65
66 void GuiPopupMenu::populate(QMenu * qMenu, Menu * menu)
67 {
68         LYXERR(Debug::GUI, "populating menu " << to_utf8(menu->name()));
69         if (menu->size() == 0) {
70                 LYXERR(Debug::GUI, "\tERROR: empty menu " << to_utf8(menu->name()));
71                 return;
72         }
73         LYXERR(Debug::GUI, " *****  menu entries " << menu->size());
74
75         Menu::const_iterator m = menu->begin();
76         Menu::const_iterator end = menu->end();
77
78         for (; m != end; ++m) {
79
80                 if (m->kind() == MenuItem::Separator) {
81
82                         qMenu->addSeparator();
83                         LYXERR(Debug::GUI, "adding Menubar Separator");
84
85                 } else if (m->kind() == MenuItem::Submenu) {
86
87                         LYXERR(Debug::GUI, "** creating New Sub-Menu "
88                                 << to_utf8(getLabel(*m)));
89                         QMenu * subMenu = qMenu->addMenu(toqstr(getLabel(*m)));
90                         populate(subMenu, m->submenu());
91
92                 } else { // we have a MenuItem::Command
93
94                         LYXERR(Debug::GUI, "creating Menu Item "
95                                 << to_utf8(m->label()));
96
97                         docstring label = getLabel(*m);
98                         addBinding(label, *m);
99
100                         Action * action = new Action(*(owner_),
101                                 QIcon(), toqstr(label), m->func(), QString());
102                         qMenu->addAction(action);
103                 }
104         }
105 }
106
107
108 docstring const GuiPopupMenu::getLabel(MenuItem const & mi)
109 {
110         docstring const shortcut = mi.shortcut();
111         docstring label = support::subst(mi.label(),
112         from_ascii("&"), from_ascii("&&"));
113
114         if (!shortcut.empty()) {
115                 size_t pos = label.find(shortcut);
116                 if (pos != docstring::npos)
117                         label.insert(pos, 1, char_type('&'));
118         }
119
120         return label;
121 }
122
123
124 void GuiPopupMenu::addBinding(docstring & label, MenuItem const & mi)
125 {
126 #ifdef Q_WS_MACX
127         docstring const binding = mi.binding(false);
128 #else
129         docstring const binding = mi.binding(true);
130 #endif
131         if (!binding.empty())
132                 label += '\t' + binding;
133 }
134
135
136 } // namespace frontend
137 } // namespace lyx
138
139 #include "GuiPopupMenu_moc.cpp"