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