]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QLPopupMenu.C
db023eb4a3bf9dddc0fb4a2a7312943279b01af3
[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                 lyxerr[Debug::GUI] << "Label: " << mi.label()
52                                    << " Shortcut: " << mi.shortcut()
53                                    << " Accel: " << binding << endl;
54         } else
55                 lyxerr[Debug::GUI] << "Label: " << mi.label()
56                                    << " Shortcut: " << mi.shortcut() << endl;
57
58         return label;
59 }
60
61 } // namespace anon
62
63
64 pair<int, QLPopupMenu *>
65 createMenu(QMenuData * parent, MenuItem const * item, Menubar::Pimpl * owner,
66            bool is_toplevel)
67 {
68         // FIXME: leaks ??
69         QLPopupMenu * pm = new QLPopupMenu(owner, item->submenuname(), is_toplevel);
70         int id = parent->insertItem(toqstr(getLabel(*item)), pm);
71         return make_pair(id, pm);
72 }
73
74
75 QLPopupMenu::QLPopupMenu(Menubar::Pimpl * owner,
76                          string const & name, bool toplevel)
77         : owner_(owner), name_(name)
78 {
79         if (toplevel)
80                 connect(this, SIGNAL(aboutToShow()), this, SLOT(showing()));
81         connect(this, SIGNAL(activated(int)),
82                 owner_->view(), SLOT(activated(int)));
83 }
84
85
86 void QLPopupMenu::populate(Menu * menu)
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                         insertItem(toqstr(getLabel(*m)), m->action());
102                         setItemEnabled(m->action(), !status.disabled());
103                         setItemChecked(m->action(), status.onoff(true));
104                 }
105         }
106 }
107
108
109 void QLPopupMenu::showing()
110 {
111         clear();
112         Menu tomenu;
113         Menu const frommenu = owner_->backend().getMenu(name_);
114         owner_->backend().expand(frommenu, tomenu, owner_->view());
115         populate(&tomenu);
116 }