]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QLPopupMenu.C
Minipage is no more (long live the box inset)
[lyx.git] / src / frontends / qt2 / QLPopupMenu.C
1 /**
2  * \file QLPopupMenu.C
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
17 #include "QtView.h"
18
19 #include "QLMenubar.h"
20 #include "QLPopupMenu.h"
21 #include "qt_helpers.h"
22
23 using lyx::support::subst;
24
25 using std::make_pair;
26 using std::string;
27 using std::pair;
28
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                 string::size_type pos = label.find(shortcut);
39                 if (pos != string::npos)
40                         label.insert(pos, 1, '&');
41         }
42
43         if (mi.kind() == MenuItem::Command) {
44                 string const binding(mi.binding());
45                 if (!binding.empty()) {
46                         label += '\t' + binding;
47                 }
48         }
49
50         return label;
51 }
52
53 } // namespace anon
54
55
56 pair<int, QLPopupMenu *>
57 createMenu(QMenuData * parent, MenuItem const * item, QLMenubar * owner,
58            bool is_toplevel)
59 {
60         // FIXME: leaks ??
61         QLPopupMenu * pm = new QLPopupMenu(owner, item->submenuname(), is_toplevel);
62         int const id = parent->insertItem(toqstr(getLabel(*item)), pm);
63         return make_pair(id, pm);
64 }
65
66
67 QLPopupMenu::QLPopupMenu(QLMenubar * owner,
68                          string const & name, bool toplevel)
69         : owner_(owner), name_(name)
70 {
71         if (toplevel)
72                 connect(this, SIGNAL(aboutToShow()), this, SLOT(showing()));
73         connect(this, SIGNAL(activated(int)),
74                 this, SLOT(fire(int)));
75 }
76
77
78 void QLPopupMenu::fire(int index)
79 {
80         owner_->view()->activated(funcs_[index]);
81 }
82
83
84 void QLPopupMenu::populate(Menu * menu)
85 {
86         funcs_.clear();
87
88         Menu::const_iterator m = menu->begin();
89         Menu::const_iterator end = menu->end();
90         for (; m != end; ++m) {
91                 if (m->kind() == MenuItem::Separator) {
92                         insertSeparator();
93                 } else if (m->kind() == MenuItem::Submenu) {
94                         pair<int, QLPopupMenu *> res = createMenu(this, &(*m), owner_);
95                         setItemEnabled(res.first,
96                                        !m->status().disabled());
97                         res.second->populate(m->submenu());
98                 } else {
99                         FuncStatus const status = m->status();
100
101                         Funcs::iterator fit =
102                                 funcs_.insert(funcs_.end(), m->func());
103                         int const index = std::distance(funcs_.begin(), fit);
104
105                         insertItem(toqstr(getLabel(*m)), index);
106                         setItemEnabled(index, !status.disabled());
107                         setItemChecked(index, status.onoff(true));
108                 }
109         }
110 }
111
112
113 void QLPopupMenu::showing()
114 {
115         clear();
116         Menu tomenu;
117         Menu const frommenu = owner_->backend().getMenu(name_);
118         owner_->backend().expand(frommenu, tomenu, owner_->view());
119         populate(&tomenu);
120 }