]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiPopupMenu.cpp
header cleanup
[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 "GuiApplication.h"
18 #include "GuiPopupMenu.h"
19 #include "qt_helpers.h"
20
21 #include "LyXFunc.h"
22 #include "MenuBackend.h"
23
24 #include "support/lstrings.h"
25 #include "support/debug.h"
26
27 namespace lyx {
28 namespace frontend {
29
30 GuiPopupMenu::GuiPopupMenu(GuiView * owner, MenuItem const & mi,
31                 bool topLevelMenu)
32         : QMenu(owner), owner_(owner)
33 {
34         name_ = mi.submenuname();
35
36         setTitle(toqstr(getLabel(mi)));
37
38         if (topLevelMenu)
39                 connect(this, SIGNAL(aboutToShow()), this, SLOT(updateView()));
40 }
41
42
43 void GuiPopupMenu::updateView()
44 {
45         LYXERR(Debug::GUI, "GuiPopupMenu::updateView()"
46                 << "\tTriggered menu: " << to_utf8(name_));
47
48         clear();
49
50         if (name_.empty())
51                 return;
52
53         // Here, We make sure that theLyXFunc points to the correct LyXView.
54         theLyXFunc().setLyXView(owner_);
55
56         MenuBackend const & menubackend = guiApp->menuBackend();
57         Menu const & fromLyxMenu = menubackend.getMenu(name_);
58         menubackend.expand(fromLyxMenu, topLevelMenu_, owner_->buffer());
59
60         if (!menubackend.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 const label = getLabel(*m);
100
101                         Action * action = new Action(*(owner_),
102                                 QIcon(), toqstr(label), m->func(), QString());
103                         qMenu->addAction(action);
104                 }
105         }
106 }
107
108
109 docstring const GuiPopupMenu::getLabel(MenuItem const & mi)
110 {
111         docstring label = support::subst(mi.label(), 
112                                          from_ascii("&"), from_ascii("&&"));
113
114         docstring const shortcut = mi.shortcut();
115         if (!shortcut.empty()) {
116                 size_t pos = label.find(shortcut);
117                 if (pos != docstring::npos)
118                         label.insert(pos, 1, char_type('&'));
119         }
120
121         docstring const binding = mi.binding();
122         if (!binding.empty())
123                 label += '\t' + binding;
124
125         return label;
126 }
127
128
129 } // namespace frontend
130 } // namespace lyx
131
132 #include "GuiPopupMenu_moc.cpp"