]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QLPopupMenu.C
Populate menus on popup - good news JMarc, it seems to work (almost) perfectly
[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 namespace {
20  
21 string const getLabel(MenuItem const & mi)
22 {
23         string const shortcut = mi.shortcut();
24         string label = mi.label();
25
26         label = subst(label, "&", "&&");
27  
28         if (shortcut.empty())
29                 return label;
30  
31         string::size_type pos = label.find(shortcut);
32         if (pos == string::npos)
33                 return label;
34         label.insert(pos, "&");
35  
36         return label;
37 }
38
39
40
41
42 int createMenu(QMenuData * parent, MenuItem const * item, Menubar::Pimpl * owner)
43 {
44         // FIXME: leaks ??
45         QLPopupMenu * pm = new QLPopupMenu(owner, item->submenuname());
46         return parent->insertItem(getLabel(*item).c_str(), pm);
47 }
48  
49  
50 QLPopupMenu::QLPopupMenu(Menubar::Pimpl * owner, string const & name)
51         : owner_(owner), name_(name)
52 {
53         connect(this, SIGNAL(aboutToShow()), this, SLOT(showing()));
54 }
55  
56
57 bool QLPopupMenu::disabled(string const & name)
58 {
59         bool disable = true;
60  
61         Menu tomenu;
62         Menu const frommenu = owner_->backend().getMenu(name);
63         owner_->backend().expand(frommenu, tomenu, owner_->view()->buffer());
64         Menu::const_iterator m = tomenu.begin();
65         Menu::const_iterator end = tomenu.end();
66         for (; m != end; ++m) {
67                 if (m->kind() == MenuItem::Submenu && !disabled(m->submenuname())) {
68                         disable = false;
69                 } else {
70                         FuncStatus const status =
71                                 owner_->view()->getLyXFunc().getStatus(m->action());
72                         if (!status.disabled())
73                                 disable = false;
74                 }
75         }
76         return disable;
77 }
78  
79
80 void QLPopupMenu::showing()
81 {
82         clear();
83         Menu tomenu;
84         Menu const frommenu = owner_->backend().getMenu(name_);
85         owner_->backend().expand(frommenu, tomenu, owner_->view()->buffer());
86         Menu::const_iterator m = tomenu.begin();
87         Menu::const_iterator end = tomenu.end();
88         for (; m != end; ++m) {
89                 if (m->kind() == MenuItem::Separator) {
90                         insertSeparator();
91                 } else if (m->kind() == MenuItem::Submenu) {
92                         int id(createMenu(this, m, owner_));
93                         setItemEnabled(id, !disabled(m->submenuname()));
94                 } else {
95                         insertItem(getLabel(*m).c_str(), m->action());
96                         FuncStatus const status =
97                                 owner_->view()->getLyXFunc().getStatus(m->action());
98                         setItemEnabled(m->action(), !status.disabled());
99                         setItemChecked(m->action(), status.onoff(true));
100                 }
101         }
102 }