]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QLPopupMenu.C
Some string(widget->text()) fixes. Weirdness
[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 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "support/lstrings.h"
18 #include "MenuBackend.h"
19 #include "lyxfunc.h"
20 #include "kbmap.h"
21 #include "debug.h"
22
23 #include "QtView.h"
24
25 #include "QLPopupMenu.h"
26
27 #include <boost/scoped_ptr.hpp>
28
29 using std::pair;
30 using std::make_pair;
31
32 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
33
34 namespace {
35
36 string const getLabel(MenuItem const & mi)
37 {
38         string const shortcut = mi.shortcut();
39         string label = subst(mi.label(), "&", "&&");
40
41         if (shortcut.empty())
42                 return label;
43
44         string::size_type pos = label.find(shortcut);
45         if (pos == string::npos)
46                 return label;
47         label.insert(pos, 1, '&');
48
49         if (mi.kind() == MenuItem::Command) {
50                 // FIXME: backend should do this
51                 string const accel(toplevel_keymap->findbinding(mi.action()));
52
53                 if (!accel.empty()) {
54                         label += '\t' + accel.substr(1, accel.find(']') - 1);
55                 }
56
57                 lyxerr[Debug::GUI] << "Label: " << mi.label()
58                                    << " Shortcut: " << mi.shortcut()
59                                    << " Accel: " << accel << endl;
60         } else
61                 lyxerr[Debug::GUI] << "Label: " << mi.label()
62                                    << " Shortcut: " << mi.shortcut() << endl;
63
64         return label;
65 }
66
67 } // namespace anon
68
69
70 pair<int, QLPopupMenu *>
71 createMenu(QMenuData * parent, MenuItem const * item, Menubar::Pimpl * owner, bool is_toplevel)
72 {
73         // FIXME: leaks ??
74         QLPopupMenu * pm = new QLPopupMenu(owner, item->submenuname(), is_toplevel);
75         int id = parent->insertItem(getLabel(*item).c_str(), pm);
76         return make_pair(id, pm);
77 }
78
79
80 QLPopupMenu::QLPopupMenu(Menubar::Pimpl * owner,
81                          string const & name, bool toplevel)
82         : owner_(owner), name_(name)
83 {
84         if (toplevel)
85                 connect(this, SIGNAL(aboutToShow()), this, SLOT(showing()));
86         connect(this, SIGNAL(activated(int)),
87                 owner_->view(), SLOT(activated(int)));
88 }
89
90
91 // FIXME: should all be in backend
92 bool QLPopupMenu::disabled(Menu * menu)
93 {
94         bool disable = true;
95
96         Menu::const_iterator m = menu->begin();
97         Menu::const_iterator end = menu->end();
98         for (; m != end; ++m) {
99                 if (m->kind() == MenuItem::Submenu) {
100                         if (!disabled(m->submenu()))
101                                 disable = false;
102                 } else if (m->kind() != MenuItem::Separator) {
103                         FuncStatus const status =
104                                 owner_->view()->getLyXFunc()
105                                 .getStatus(m->action());
106                         if (!status.disabled())
107                                 disable = false;
108                 }
109         }
110         return disable;
111 }
112
113
114 void QLPopupMenu::populate(Menu * menu)
115 {
116         Menu::const_iterator m = menu->begin();
117         Menu::const_iterator end = menu->end();
118         for (; m != end; ++m) {
119                 if (m->kind() == MenuItem::Separator) {
120                         insertSeparator();
121                 } else if (m->kind() == MenuItem::Submenu) {
122                         pair<int, QLPopupMenu *> res = createMenu(this, &(*m), owner_);
123                         setItemEnabled(res.first, !disabled(m->submenu()));
124                         res.second->populate(m->submenu());
125                 } else {
126                         FuncStatus const status =
127                                 owner_->view()->getLyXFunc().getStatus(m->action());
128                         if (status.disabled() && m->optional())
129                                 continue;
130                         insertItem(getLabel(*m).c_str(), m->action());
131                         setItemEnabled(m->action(), !status.disabled());
132                         setItemChecked(m->action(), status.onoff(true));
133                 }
134         }
135 }
136
137
138 void QLPopupMenu::showing()
139 {
140         clear();
141         Menu tomenu;
142         Menu const frommenu = owner_->backend().getMenu(name_);
143         owner_->backend().expand(frommenu, tomenu, owner_->view()->buffer());
144         populate(&tomenu);
145 }