3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
22 class LyxMenu : public Gtk::Menu
25 LyxMenu() { menu_.reset(new ::Menu); }
27 ::Menu& getBackMenu() { return *menu_.get(); }
29 void clearBackMenu() { menu_.reset(new ::Menu); }
31 std::auto_ptr< ::Menu > menu_;
35 Glib::ustring labelTrans(string const & label, string const & shortcut)
37 string labelN = label;
38 string::size_type i = label.find(shortcut);
39 if (i == string::npos)
40 return Glib::locale_to_utf8(label);
41 labelN.insert(i, "_");
42 return Glib::locale_to_utf8(labelN);
46 void ClearMenu(Gtk::MenuShell * menu)
48 Gtk::Menu_Helpers::MenuList::iterator m, end;
49 m = menu->items().begin();
50 end = menu->items().end();
52 for (; m != end; ++m) {
53 if ((subMenu = m->get_submenu()) != 0) {
58 menu->items().clear();
65 GMenubar::GMenubar(LyXView *lyxView, MenuBackend const & /*menuBackend*/) :
68 GView * gview = static_cast<GView*>(lyxView);
69 Gtk::VBox& vbox = gview->getVBox();
70 Menu const & menu = menubackend.getMenubar();
71 Menu::const_iterator i = menu.begin();
72 Menu::const_iterator end = menu.end();
73 for (; i != end; ++i) {
74 if (i->kind() != MenuItem::Submenu) {
75 lyxerr << "ERROR: GMenubar::createMenubar:"
76 " only submenus can appear in a menubar"
80 Gtk::Menu * gmenu = new LyxMenu;
81 menubar_.items().push_back(
82 Gtk::Menu_Helpers::MenuElem(
83 labelTrans(i->label(), i->shortcut()),
85 menubar_.items().back().signal_activate().connect(
86 SigC::bind(SigC::slot(*this, &GMenubar::onSubMenuActivate), &(*i),
87 &menubar_.items().back()));
88 mainMenuNames_.push_back(i->submenuname());
91 vbox.children().push_back(Gtk::Box_Helpers::Element(menubar_,
102 void GMenubar::update()
107 void GMenubar::openByName(string const & name)
109 Glib::ustring uname = Glib::locale_to_utf8(name);
110 std::vector<Glib::ustring>::iterator it =
111 std::find(mainMenuNames_.begin(), mainMenuNames_.end(),
113 if (it != mainMenuNames_.end()) {
114 Gtk::MenuItem& mitem = menubar_.items()[it - mainMenuNames_.begin()];
119 lyxerr << "GMenubar::openByName: menu "
120 << name << " not found" << std::endl;
124 bool GMenubar::submenuDisabled(MenuItem const * item)
126 Menu & from = menubackend.getMenu(item->submenuname());
128 menubackend.expand(from, to, view_);
129 Menu::const_iterator i = to.begin();
130 Menu::const_iterator end = to.end();
131 for (; i != end; ++i) {
133 case MenuItem::Submenu:
134 if (!submenuDisabled(&(*i)))
137 case MenuItem::Command:
139 FuncStatus const flag =
140 view_->getLyXFunc().getStatus(i->func());
141 if (!flag.disabled())
153 void GMenubar::onSubMenuActivate(MenuItem const * item,
154 Gtk::MenuItem * gitem)
156 Gtk::Menu * gmenu = gitem->get_submenu();
158 LyxMenu * lyxmenu = static_cast<LyxMenu*>(gmenu);
159 lyxmenu->clearBackMenu();
160 Menu * fmenu = &menubackend.getMenu(item->submenuname());
161 menubackend.expand(*fmenu, lyxmenu->getBackMenu(), view_);
162 Menu::const_iterator i = lyxmenu->getBackMenu().begin();
163 Menu::const_iterator end = lyxmenu->getBackMenu().end();
164 Gtk::Menu * gmenu_new;
165 for (; i != end; ++i) {
167 case MenuItem::Submenu:
168 gmenu_new = new LyxMenu;
169 gmenu->items().push_back(
170 Gtk::Menu_Helpers::MenuElem(
171 labelTrans(i->label(), i->shortcut()),
173 gmenu->items().back().signal_activate().connect(
174 SigC::bind(SigC::slot(*this, &GMenubar::onSubMenuActivate),
176 &gmenu->items().back()));
177 if (submenuDisabled(&(*i)))
178 gmenu->items().back().set_sensitive(false);
180 case MenuItem::Command:
182 #warning Bindings are not inserted into the menu labels here. (Lgb)
183 FuncStatus const flag =
184 view_->getLyXFunc().getStatus(i->func());
185 bool on = flag.onoff(true);
186 bool off = flag.onoff(false);
189 gmenu->items().push_back(
190 Gtk::Menu_Helpers::CheckMenuElem(
191 labelTrans(i->label(),
193 Gtk::CheckMenuItem& citem =
194 static_cast<Gtk::CheckMenuItem&>(
195 gmenu->items().back());
196 citem.set_active(on);
198 gmenu->items().push_back(
199 Gtk::Menu_Helpers::MenuElem(
200 labelTrans(i->label(),
203 Gtk::MenuItem & item = gmenu->items().back();
204 item.signal_activate().connect(
205 SigC::bind(SigC::slot(*this, &GMenubar::onCommandActivate),
208 item.set_sensitive(false);
211 case MenuItem::Separator:
212 gmenu->items().push_back(
213 Gtk::Menu_Helpers::SeparatorElem());
216 lyxerr << "GMenubar::create_submenu: "
217 "this should not happen" << std::endl;
224 void GMenubar::onCommandActivate(MenuItem const * item,
225 Gtk::MenuItem * /*gitem*/)
227 view_->getLyXFunc().dispatch(item->func(), true);