]> git.lyx.org Git - features.git/blob - src/frontends/qt2/Menubar_pimpl.C
6d9cfa47c7feed9969767a26813da03709bc825c
[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 extern LyXAction lyxaction;
67
68
69 Menubar::Pimpl::Pimpl(LyXView * view, MenuBackend const & mbe) 
70         : owner_(static_cast<QtView*>(view)), menubackend_(mbe)
71 {
72         Menu::const_iterator m = mbe.getMenubar().begin();
73         Menu::const_iterator end = mbe.getMenubar().end();
74         for (; m != end; ++m) {
75                 Menu tomenu;
76                 Menu const frommenu = menubackend_.getMenu(m->submenuname());
77                 menubackend_.expand(frommenu, tomenu, owner_->buffer());
78                 fillMenu(createMenu(owner_->menuBar(), &(*m)), tomenu);
79         }
80 }
81
82
83 QPopupMenu * Menubar::Pimpl::createMenu(QMenuData * parent, MenuItem const * item)
84 {
85         // FIXME: does this leak or not ?
86         QPopupMenu * pm = new QPopupMenu();
87         int const parentid = parent->insertItem(getLabel(*item).c_str(), pm);
88
89         MenuItemInfo const info(parent, parentid, item);
90         items_[item->label()] = info;
91         return pm;
92 }
93
94  
95 void Menubar::Pimpl::fillMenu(QMenuData * qmenu, Menu const & menu)
96 {
97         Menu::const_iterator m = menu.begin();
98         Menu::const_iterator end = menu.end();
99         for (; m != end; ++m) {
100                 if (m->kind() == MenuItem::Separator) {
101                         qmenu->insertSeparator();
102                 } else if (m->kind() == MenuItem::Submenu) {
103                         fillMenu(createMenu(qmenu, &(*m)), *m->submenu());
104                 } else {
105                         qmenu->insertItem(getLabel(*m).c_str(), m->action());
106                         MenuItemInfo const info(qmenu, m->action(), &(*m));
107                         items_[m->label()] = info;
108                         updateItem(info);
109                 }
110         }
111 }
112
113  
114 // FIXME: this is probably buggy with respect to enabling
115 // two-level submenus
116 void Menubar::Pimpl::updateSubmenu(MenuItemInfo const & i)
117 {
118 #if 0 // SEGFAULTS
119 // 7  0x0809d372 in Menu::begin (this=0x0) at MenuBackend.h:138
120 // 8  0x081dcf60 in Menubar::Pimpl::updateSubmenu (this=0x839eaa0, i=@0xbffff010) at Menubar_pimpl.C:116 
121         bool enable = false;
122  
123         Menu::const_iterator m = i.item_->submenu().begin();
124         Menu::const_iterator end = i.item_->submenu().end();
125         for (; m != end; ++m) {
126                 if (m->action() > 0) {
127                         FuncStatus const status =
128                                 owner_->getLyXFunc()->getStatus(m->action());
129                         if (!status.disabled())
130                                 enable = true;
131                 }
132         }
133 #else
134         bool enable = true;
135 #endif
136         i.parent_->setItemEnabled(i.id_, enable);
137 }
138  
139  
140 void Menubar::Pimpl::updateItem(MenuItemInfo const & i)
141 {
142         if (i.item_->kind() == MenuItem::Submenu) {
143                 updateSubmenu(i);
144                 return;
145         }
146  
147         // FIXME
148         if (i.id_ < 0)
149                 return;
150  
151         FuncStatus const status = owner_->getLyXFunc()->getStatus(i.id_);
152         i.parent_->setItemEnabled(i.id_, !status.disabled());
153         i.parent_->setItemChecked(i.id_, status.onoff(true));
154 }
155
156  
157 void Menubar::Pimpl::update()
158 {
159         // FIXME: handle special stuff to be updated.
160  
161         ItemMap::const_iterator cit = items_.begin();
162         ItemMap::const_iterator end = items_.end();
163
164         for (; cit != end; ++cit)
165                 updateItem(cit->second);
166 }
167
168
169 void Menubar::Pimpl::openByName(string const & name)
170 {
171         lyxerr << "Menubar::Pimpl::openByName: menu " << name << endl;
172
173         ItemMap::iterator it = items_.find(name);
174
175         if (it == items_.end())
176                 lyxerr << "NOT FOUND " << name << endl;
177
178         // FIXME 
179 }