]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QLPopupMenu.C
another compile fix from herbert
[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 "MenuBackend.h"
18 #include "lyxfunc.h"
19 #include "debug.h"
20
21 #include "QtView.h"
22
23 #include "QLPopupMenu.h"
24
25 #include "support/lstrings.h"
26
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, "&");
44
45         return label;
46 }
47
48 } // namespace anon
49
50
51 pair<int, QLPopupMenu *>
52 createMenu(QMenuData * parent, MenuItem const * item, Menubar::Pimpl * owner, bool is_toplevel)
53 {
54         // FIXME: leaks ??
55         QLPopupMenu * pm = new QLPopupMenu(owner, item->submenuname(), is_toplevel);
56         int id = parent->insertItem(getLabel(*item).c_str(), pm);
57         return make_pair(id, pm);
58 }
59
60
61 QLPopupMenu::QLPopupMenu(Menubar::Pimpl * owner,
62                          string const & name, bool toplevel)
63         : owner_(owner), name_(name)
64 {
65         if (toplevel)
66                 connect(this, SIGNAL(aboutToShow()), this, SLOT(showing()));
67         connect(this, SIGNAL(activated(int)),
68                 owner_->view(), SLOT(activated(int)));
69 }
70
71
72 bool QLPopupMenu::disabled(Menu * menu)
73 {
74         bool disable = true;
75
76         Menu::const_iterator m = menu->begin();
77         Menu::const_iterator end = menu->end();
78         for (; m != end; ++m) {
79                 if (m->kind() == MenuItem::Submenu
80                     && !disabled(m->submenu())) {
81                         disable = false;
82                 } else {
83                         FuncStatus const status =
84                                 owner_->view()->getLyXFunc()
85                                 .getStatus(m->action());
86                         if (!status.disabled())
87                                 disable = false;
88                 }
89         }
90         return disable;
91 }
92
93
94 void QLPopupMenu::populate(Menu * menu)
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::Separator) {
100                         insertSeparator();
101                 } else if (m->kind() == MenuItem::Submenu) {
102                         pair<int, QLPopupMenu *> res = createMenu(this, &(*m), owner_);
103                         setItemEnabled(res.first, !disabled(m->submenu()));
104                         res.second->populate(m->submenu());
105                 } else {
106                         FuncStatus const status =
107                                 owner_->view()->getLyXFunc().getStatus(m->action());
108                         if (status.disabled() && m->optional())
109                                 continue;
110                         insertItem(getLabel(*m).c_str(), m->action());
111                         setItemEnabled(m->action(), !status.disabled());
112                         setItemChecked(m->action(), status.onoff(true));
113                 }
114         }
115 }
116
117
118 void QLPopupMenu::showing()
119 {
120         clear();
121         Menu tomenu;
122         Menu const frommenu = owner_->backend().getMenu(name_);
123         owner_->backend().expand(frommenu, tomenu, owner_->view()->buffer());
124         populate(&tomenu);
125 }