]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QLPopupMenu.C
Get rid of the static_casts.
[lyx.git] / src / frontends / qt2 / QLPopupMenu.C
1 /**
2  * \file QLPopupMenu.h
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  *
8  * Full author contact details are available in file CREDITS
9  */
10
11 #include <config.h>
12
13
14 #include "support/lstrings.h"
15 #include "MenuBackend.h"
16 #include "lyxfunc.h"
17 #include "debug.h"
18
19 #include "QtView.h"
20
21 #include "QLPopupMenu.h"
22 #include "qt_helpers.h"
23
24 #include <boost/scoped_ptr.hpp>
25
26 using std::endl;
27 using std::pair;
28 using std::make_pair;
29
30 namespace {
31
32 string const getLabel(MenuItem const & mi)
33 {
34         string const shortcut = mi.shortcut();
35         string label = subst(mi.label(), "&", "&&");
36
37         if (shortcut.empty())
38                 return label;
39
40         string::size_type pos = label.find(shortcut);
41         if (pos == string::npos)
42                 return label;
43         label.insert(pos, 1, '&');
44
45         if (mi.kind() == MenuItem::Command) {
46                 string const binding(mi.binding());
47                 if (!binding.empty()) {
48                         label += '\t' + binding;
49                 }
50         }
51
52         return label;
53 }
54
55 } // namespace anon
56
57
58 pair<int, QLPopupMenu *>
59 createMenu(QMenuData * parent, MenuItem const * item, Menubar::Pimpl * owner,
60            bool is_toplevel)
61 {
62         // FIXME: leaks ??
63         QLPopupMenu * pm = new QLPopupMenu(owner, item->submenuname(), is_toplevel);
64         int id = parent->insertItem(toqstr(getLabel(*item)), pm);
65         return make_pair(id, pm);
66 }
67
68
69 QLPopupMenu::QLPopupMenu(Menubar::Pimpl * owner,
70                          string const & name, bool toplevel)
71         : owner_(owner), name_(name)
72 {
73         if (toplevel)
74                 connect(this, SIGNAL(aboutToShow()), this, SLOT(showing()));
75         connect(this, SIGNAL(activated(int)),
76                 owner_->view(), SLOT(activated(int)));
77 }
78
79
80 void QLPopupMenu::populate(Menu * menu)
81 {
82         Menu::const_iterator m = menu->begin();
83         Menu::const_iterator end = menu->end();
84         for (; m != end; ++m) {
85                 if (m->kind() == MenuItem::Separator) {
86                         insertSeparator();
87                 } else if (m->kind() == MenuItem::Submenu) {
88                         pair<int, QLPopupMenu *> res = createMenu(this, &(*m), owner_);
89                         setItemEnabled(res.first,
90                                        !m->status().disabled());
91                         res.second->populate(m->submenu());
92                 } else {
93                         FuncStatus const status = m->status();
94
95                         insertItem(toqstr(getLabel(*m)), m->action());
96                         setItemEnabled(m->action(), !status.disabled());
97                         setItemChecked(m->action(), status.onoff(true));
98                 }
99         }
100 }
101
102
103 void QLPopupMenu::showing()
104 {
105         clear();
106         Menu tomenu;
107         Menu const frommenu = owner_->backend().getMenu(name_);
108         owner_->backend().expand(frommenu, tomenu, owner_->view());
109         populate(&tomenu);
110 }