]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QLPopupMenu.C
fix navigate
[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 }
60  
61
62 bool QLPopupMenu::disabled(Menu * menu)
63 {
64         bool disable = true;
65  
66         Menu::const_iterator m = menu->begin();
67         Menu::const_iterator end = menu->end();
68         for (; m != end; ++m) {
69                 if (m->kind() == MenuItem::Submenu && !disabled(m->submenu())) {
70                         disable = false;
71                 } else {
72                         FuncStatus const status =
73                                 owner_->view()->getLyXFunc().getStatus(m->action());
74                         if (!status.disabled())
75                                 disable = false;
76                 }
77         }
78         return disable;
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, !disabled(m->submenu()));
92                         res.second->populate(m->submenu());
93                 } else {
94                         FuncStatus const status =
95                                 owner_->view()->getLyXFunc().getStatus(m->action());
96                         if (status.disabled() && m->optional())
97                                 continue;
98                         insertItem(getLabel(*m).c_str(), m->action());
99                         setItemEnabled(m->action(), !status.disabled());
100                         setItemChecked(m->action(), status.onoff(true));
101                 }
102         }
103 }
104
105  
106 void QLPopupMenu::showing()
107 {
108         clear();
109         Menu tomenu;
110         Menu const frommenu = owner_->backend().getMenu(name_);
111         owner_->backend().expand(frommenu, tomenu, owner_->view()->buffer());
112         populate(&tomenu); 
113 }