]> git.lyx.org Git - features.git/blob - src/frontends/qt2/Menubar_pimpl.C
some minor lyxaction cleanup
[features.git] / src / frontends / qt2 / Menubar_pimpl.C
1 /**
2  * \file Menubar_pimpl.C
3  * Copyright 1999-2001 The LyX Team.
4  * See the file COPYING.
5  *
6  * \author  Lars Gullik Bjønnes, larsbj@lyx.org
7  */
8
9 #include <config.h>
10
11 #include <algorithm>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "Menubar_pimpl.h"
18 #include "MenuBackend.h"
19 #include "LyXAction.h"
20 #include "kbmap.h"
21 #include "buffer.h"
22 #include "Dialogs.h"
23 #include "lyxfunc.h"
24 #include "FloatList.h"
25 #include "support/lstrings.h"
26 #include "support/LAssert.h"
27 #include "gettext.h"
28 #include "debug.h"
29
30 #include "QtView.h"
31  
32 #include <qmenubar.h>
33 #include <qpopupmenu.h>
34  
35 using std::endl;
36 using std::vector;
37 using std::max;
38 using std::min;
39 using std::for_each;
40
41 namespace {
42  
43 string const getLabel(MenuItem const & mi)
44 {
45         string const shortcut = mi.shortcut();
46         string label = mi.label();
47
48         label = subst(label, "&", "&&");
49  
50         if (shortcut.empty())
51                 return label;
52  
53         string::size_type pos = label.find(shortcut);
54         if (pos == string::npos)
55                 return label;
56         label.insert(pos, "&");
57  
58         return label;
59 }
60
61 };
62  
63 typedef vector<int>::size_type size_type;
64
65 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
66
67
68 Menubar::Pimpl::Pimpl(LyXView * view, MenuBackend const & mbe) 
69         : owner_(static_cast<QtView*>(view)), menubackend_(mbe)
70 {
71         Menu::const_iterator m = mbe.getMenubar().begin();
72         Menu::const_iterator end = mbe.getMenubar().end();
73         for (; m != end; ++m) {
74                 Menu tomenu;
75                 Menu const frommenu = menubackend_.getMenu(m->submenuname());
76                 menubackend_.expand(frommenu, tomenu, owner_->buffer());
77                 fillMenu(createMenu(owner_->menuBar(), &(*m)), tomenu);
78         }
79 }
80
81
82 QPopupMenu * Menubar::Pimpl::createMenu(QMenuData * parent, MenuItem const * item)
83 {
84         // FIXME: does this leak or not ?
85         QPopupMenu * pm = new QPopupMenu();
86         int const parentid = parent->insertItem(getLabel(*item).c_str(), pm);
87
88         MenuItemInfo const info(parent, parentid, item);
89         items_[item->label()] = info;
90         return pm;
91 }
92
93  
94 void Menubar::Pimpl::fillMenu(QMenuData * qmenu, Menu const & 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                         qmenu->insertSeparator();
101                 } else if (m->kind() == MenuItem::Submenu) {
102                         fillMenu(createMenu(qmenu, &(*m)), *m->submenu());
103                 } else {
104                         qmenu->insertItem(getLabel(*m).c_str(), m->action());
105                         MenuItemInfo const info(qmenu, m->action(), &(*m));
106                         items_[m->label()] = info;
107                         updateItem(info);
108                 }
109         }
110 }
111
112  
113 // FIXME: this is probably buggy with respect to enabling
114 // two-level submenus
115 void Menubar::Pimpl::updateSubmenu(MenuItemInfo const & i)
116 {
117 #if 0 // SEGFAULTS
118 // 7  0x0809d372 in Menu::begin (this=0x0) at MenuBackend.h:138
119 // 8  0x081dcf60 in Menubar::Pimpl::updateSubmenu (this=0x839eaa0, i=@0xbffff010) at Menubar_pimpl.C:116 
120         bool enable = false;
121  
122         Menu::const_iterator m = i.item_->submenu().begin();
123         Menu::const_iterator end = i.item_->submenu().end();
124         for (; m != end; ++m) {
125                 if (m->action() > 0) {
126                         FuncStatus const status =
127                                 owner_->getLyXFunc()->getStatus(m->action());
128                         if (!status.disabled())
129                                 enable = true;
130                 }
131         }
132 #else
133         bool enable = true;
134 #endif
135         i.parent_->setItemEnabled(i.id_, enable);
136 }
137  
138  
139 void Menubar::Pimpl::updateItem(MenuItemInfo const & i)
140 {
141         if (i.item_->kind() == MenuItem::Submenu) {
142                 updateSubmenu(i);
143                 return;
144         }
145  
146         // FIXME
147         if (i.id_ < 0)
148                 return;
149  
150         FuncStatus const status = owner_->getLyXFunc()->getStatus(i.id_);
151         i.parent_->setItemEnabled(i.id_, !status.disabled());
152         i.parent_->setItemChecked(i.id_, status.onoff(true));
153 }
154
155  
156 void Menubar::Pimpl::update()
157 {
158         // FIXME: handle special stuff to be updated.
159  
160         ItemMap::const_iterator cit = items_.begin();
161         ItemMap::const_iterator end = items_.end();
162
163         for (; cit != end; ++cit)
164                 updateItem(cit->second);
165 }
166
167
168 void Menubar::Pimpl::openByName(string const & name)
169 {
170         lyxerr << "Menubar::Pimpl::openByName: menu " << name << endl;
171
172         ItemMap::iterator it = items_.find(name);
173
174         if (it == items_.end())
175                 lyxerr << "NOT FOUND " << name << endl;
176
177         // FIXME 
178 }