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