]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QLPopupMenu.C
compile fix
[lyx.git] / src / frontends / qt2 / QLPopupMenu.C
1 /**
2  * \file QLPopupMenu.h
3  * Copyright 2002 the LyX Team
4  * Read the file COPYING
5  *
6  * \author John Levon <levon@movementarian.org>
7  */
8
9 #include "MenuBackend.h"
10 #include "lyxfunc.h"
11 #include "debug.h"
12  
13 #include "QtView.h"
14  
15 #include "QLPopupMenu.h"
16
17 #include "support/lstrings.h"
18
19 using std::pair;
20 using std::make_pair;
21  
22 namespace {
23  
24 string const getLabel(MenuItem const & mi)
25 {
26         string const shortcut = mi.shortcut();
27         string label = mi.label();
28
29         label = subst(label, "&", "&&");
30  
31         if (shortcut.empty())
32                 return label;
33  
34         string::size_type pos = label.find(shortcut);
35         if (pos == string::npos)
36                 return label;
37         label.insert(pos, "&");
38  
39         return label;
40 }
41
42
43
44
45 pair<int, QLPopupMenu *> createMenu(QMenuData * parent, MenuItem const * item, Menubar::Pimpl * owner, bool is_toplevel)
46 {
47         // FIXME: leaks ??
48         QLPopupMenu * pm = new QLPopupMenu(owner, item->submenuname(), is_toplevel);
49         int id = parent->insertItem(getLabel(*item).c_str(), pm);
50         return make_pair(id, pm);
51 }
52  
53  
54 QLPopupMenu::QLPopupMenu(Menubar::Pimpl * owner, string const & name, bool toplevel)
55         : owner_(owner), name_(name)
56 {
57         if (toplevel)
58                 connect(this, SIGNAL(aboutToShow()), this, SLOT(showing()));
59         connect(this, SIGNAL(activated(int)),
60                 owner_->view(), SLOT(activated(int)));
61 }
62  
63
64 bool QLPopupMenu::disabled(Menu * menu)
65 {
66         bool disable = true;
67  
68         Menu::const_iterator m = menu->begin();
69         Menu::const_iterator end = menu->end();
70         for (; m != end; ++m) {
71                 if (m->kind() == MenuItem::Submenu && !disabled(m->submenu())) {
72                         disable = false;
73                 } else {
74                         FuncStatus const status =
75                                 owner_->view()->getLyXFunc().getStatus(m->action());
76                         if (!status.disabled())
77                                 disable = false;
78                 }
79         }
80         return disable;
81 }
82  
83
84 void QLPopupMenu::populate(Menu * menu)
85 {
86         Menu::const_iterator m = menu->begin();
87         Menu::const_iterator end = menu->end();
88         for (; m != end; ++m) {
89                 if (m->kind() == MenuItem::Separator) {
90                         insertSeparator();
91                 } else if (m->kind() == MenuItem::Submenu) {
92                         pair<int, QLPopupMenu *> res = createMenu(this, &(*m), owner_);
93                         setItemEnabled(res.first, !disabled(m->submenu()));
94                         res.second->populate(m->submenu());
95                 } else {
96                         FuncStatus const status =
97                                 owner_->view()->getLyXFunc().getStatus(m->action());
98                         if (status.disabled() && m->optional())
99                                 continue;
100                         insertItem(getLabel(*m).c_str(), m->action());
101                         setItemEnabled(m->action(), !status.disabled());
102                         setItemChecked(m->action(), status.onoff(true));
103                 }
104         }
105 }
106
107  
108 void QLPopupMenu::showing()
109 {
110         clear();
111         Menu tomenu;
112         Menu const frommenu = owner_->backend().getMenu(name_);
113         owner_->backend().expand(frommenu, tomenu, owner_->view()->buffer());
114         populate(&tomenu); 
115 }