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