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"
23 #include "FloatList.h"
24 #include "support/lstrings.h"
25 #include "support/LAssert.h"
32 #include <qpopupmenu.h>
42 string const getLabel(MenuItem const & mi)
44 string const shortcut = mi.shortcut();
45 string label = mi.label();
47 label = subst(label, "&", "&&");
52 string::size_type pos = label.find(shortcut);
53 if (pos == string::npos)
55 label.insert(pos, "&");
62 typedef vector<int>::size_type size_type;
64 extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
67 Menubar::Pimpl::Pimpl(LyXView * view, MenuBackend const & mbe)
68 : owner_(static_cast<QtView*>(view)), menubackend_(mbe)
70 Menu::const_iterator m = mbe.getMenubar().begin();
71 Menu::const_iterator end = mbe.getMenubar().end();
72 for (; m != end; ++m) {
74 Menu const frommenu = menubackend_.getMenu(m->submenuname());
75 menubackend_.expand(frommenu, tomenu, owner_->buffer());
76 fillMenu(createMenu(owner_->menuBar(), &(*m)), tomenu);
81 QPopupMenu * Menubar::Pimpl::createMenu(QMenuData * parent, MenuItem const * item)
83 // FIXME: does this leak or not ?
84 QPopupMenu * pm = new QPopupMenu();
85 int const parentid = parent->insertItem(getLabel(*item).c_str(), pm);
87 MenuItemInfo const info(parent, parentid, item);
88 items_[item->label()] = info;
93 void Menubar::Pimpl::fillMenu(QMenuData * qmenu, Menu const & menu)
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());
103 qmenu->insertItem(getLabel(*m).c_str(), m->action());
104 MenuItemInfo const info(qmenu, m->action(), &(*m));
105 items_[m->label()] = info;
112 // FIXME: this is probably buggy with respect to enabling
113 // two-level submenus
114 void Menubar::Pimpl::updateSubmenu(MenuItemInfo const & i)
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
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())
134 i.parent_->setItemEnabled(i.id_, enable);
138 void Menubar::Pimpl::updateItem(MenuItemInfo const & i)
140 if (i.item_->kind() == MenuItem::Submenu) {
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));
155 void Menubar::Pimpl::update()
157 // FIXME: handle special stuff to be updated.
159 ItemMap::const_iterator cit = items_.begin();
160 ItemMap::const_iterator end = items_.end();
162 for (; cit != end; ++cit)
163 updateItem(cit->second);
167 void Menubar::Pimpl::openByName(string const & name)
169 lyxerr << "Menubar::Pimpl::openByName: menu " << name << endl;
171 ItemMap::iterator it = items_.find(name);
173 if (it == items_.end())
174 lyxerr << "NOT FOUND " << name << endl;