]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QLPopupMenu.C
Lots and lots of little trivial bits.
[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 = mi.label();
36
37         label = subst(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, "&");
46  
47         return label;
48 }
49
50
51
52
53 pair<int, QLPopupMenu *> createMenu(QMenuData * parent, MenuItem const * item, Menubar::Pimpl * owner, bool is_toplevel)
54 {
55         // FIXME: leaks ??
56         QLPopupMenu * pm = new QLPopupMenu(owner, item->submenuname(), is_toplevel);
57         int id = parent->insertItem(getLabel(*item).c_str(), pm);
58         return make_pair(id, pm);
59 }
60  
61  
62 QLPopupMenu::QLPopupMenu(Menubar::Pimpl * owner, 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 && !disabled(m->submenu())) {
80                         disable = false;
81                 } else {
82                         FuncStatus const status =
83                                 owner_->view()->getLyXFunc().getStatus(m->action());
84                         if (!status.disabled())
85                                 disable = false;
86                 }
87         }
88         return disable;
89 }
90  
91
92 void QLPopupMenu::populate(Menu * menu)
93 {
94         Menu::const_iterator m = menu->begin();
95         Menu::const_iterator end = menu->end();
96         for (; m != end; ++m) {
97                 if (m->kind() == MenuItem::Separator) {
98                         insertSeparator();
99                 } else if (m->kind() == MenuItem::Submenu) {
100                         pair<int, QLPopupMenu *> res = createMenu(this, &(*m), owner_);
101                         setItemEnabled(res.first, !disabled(m->submenu()));
102                         res.second->populate(m->submenu());
103                 } else {
104                         FuncStatus const status =
105                                 owner_->view()->getLyXFunc().getStatus(m->action());
106                         if (status.disabled() && m->optional())
107                                 continue;
108                         insertItem(getLabel(*m).c_str(), m->action());
109                         setItemEnabled(m->action(), !status.disabled());
110                         setItemChecked(m->action(), status.onoff(true));
111                 }
112         }
113 }
114
115  
116 void QLPopupMenu::showing()
117 {
118         clear();
119         Menu tomenu;
120         Menu const frommenu = owner_->backend().getMenu(name_);
121         owner_->backend().expand(frommenu, tomenu, owner_->view()->buffer());
122         populate(&tomenu); 
123 }