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