2 * \file Menubar_pimpl.C
3 * Copyright 1999-2001 The LyX Team.
4 * See the file COPYING.
6 * \author Lars Gullik Bjønnes, larsbj@lyx.org
14 #pragma implementation
17 #include "Menubar_pimpl.h"
18 #include "MenuBackend.h"
19 #include "LyXAction.h"
24 #include "FloatList.h"
25 #include "support/lstrings.h"
26 #include "support/LAssert.h"
33 #include <qpopupmenu.h>
43 string const getLabel(MenuItem const & mi)
45 string const shortcut = mi.shortcut();
46 string label = mi.label();
48 label = subst(label, "&", "&&");
53 string::size_type pos = label.find(shortcut);
54 if (pos == string::npos)
56 label.insert(pos, "&");
63 typedef vector<int>::size_type size_type;
65 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
66 extern LyXAction lyxaction;
69 Menubar::Pimpl::Pimpl(LyXView * view, MenuBackend const & mbe)
70 : owner_(static_cast<QtView*>(view)), menubackend_(mbe)
72 Menu::const_iterator m = mbe.getMenubar().begin();
73 Menu::const_iterator end = mbe.getMenubar().end();
74 for (; m != end; ++m) {
76 Menu const frommenu = menubackend_.getMenu(m->submenuname());
77 menubackend_.expand(frommenu, tomenu, owner_->buffer());
78 fillMenu(createMenu(owner_->menuBar(), &(*m)), tomenu);
83 QPopupMenu * Menubar::Pimpl::createMenu(QMenuData * parent, MenuItem const * item)
85 // FIXME: does this leak or not ?
86 QPopupMenu * pm = new QPopupMenu();
87 int const parentid = parent->insertItem(getLabel(*item).c_str(), pm);
89 MenuItemInfo const info(parent, parentid, item);
90 items_[item->label()] = info;
95 void Menubar::Pimpl::fillMenu(QMenuData * qmenu, Menu const & menu)
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());
105 qmenu->insertItem(getLabel(*m).c_str(), m->action());
106 MenuItemInfo const info(qmenu, m->action(), &(*m));
107 items_[m->label()] = info;
114 // FIXME: this is probably buggy with respect to enabling
115 // two-level submenus
116 void Menubar::Pimpl::updateSubmenu(MenuItemInfo const & i)
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
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())
136 i.parent_->setItemEnabled(i.id_, enable);
140 void Menubar::Pimpl::updateItem(MenuItemInfo const & i)
142 if (i.item_->kind() == MenuItem::Submenu) {
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));
157 void Menubar::Pimpl::update()
159 // FIXME: handle special stuff to be updated.
161 ItemMap::const_iterator cit = items_.begin();
162 ItemMap::const_iterator end = items_.end();
164 for (; cit != end; ++cit)
165 updateItem(cit->second);
169 void Menubar::Pimpl::openByName(string const & name)
171 lyxerr << "Menubar::Pimpl::openByName: menu " << name << endl;
173 ItemMap::iterator it = items_.find(name);
175 if (it == items_.end())
176 lyxerr << "NOT FOUND " << name << endl;