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