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.
27 class LyxMenu : public Gtk::Menu
30 LyxMenu() { menu_.reset(new ::Menu); }
32 ::Menu& getBackMenu() { return *menu_.get(); }
34 void clearBackMenu() { menu_.reset(new ::Menu); }
36 std::auto_ptr< ::Menu > menu_;
40 Glib::ustring labelTrans(string const & label, string const & shortcut)
42 string labelN = label;
43 string::size_type i = label.find(shortcut);
44 if (i == string::npos)
45 return Glib::locale_to_utf8(label);
46 labelN.insert(i, "_");
47 return Glib::locale_to_utf8(labelN);
51 void ClearMenu(Gtk::MenuShell * menu)
53 Gtk::Menu_Helpers::MenuList::iterator m, end;
54 m = menu->items().begin();
55 end = menu->items().end();
57 for (; m != end; ++m) {
58 if ((subMenu = m->get_submenu()) != 0) {
63 menu->items().clear();
70 GMenubar::GMenubar(LyXView *lyxView, MenuBackend const & /*menuBackend*/) :
73 GView * gview = static_cast<GView*>(lyxView);
74 Menu const & menu = menubackend.getMenubar();
75 Menu::const_iterator i = menu.begin();
76 Menu::const_iterator end = menu.end();
77 for (; i != end; ++i) {
78 if (i->kind() != MenuItem::Submenu) {
79 lyxerr << "ERROR: GMenubar::createMenubar:"
80 " only submenus can appear in a menubar"
84 Gtk::Menu * gmenu = new LyxMenu;
85 menubar_.items().push_back(
86 Gtk::Menu_Helpers::MenuElem(
87 labelTrans(i->label(), i->shortcut()),
89 menubar_.items().back().signal_activate().connect(
90 SigC::bind(SigC::slot(*this, &GMenubar::onSubMenuActivate), &(*i),
91 &menubar_.items().back()));
92 mainMenuNames_.push_back(i->submenuname());
95 gview->getBox(GView::Top).children().push_back(
96 Gtk::Box_Helpers::Element(menubar_, Gtk::PACK_SHRINK));
100 GMenubar::~GMenubar()
102 ClearMenu(&menubar_);
106 void GMenubar::update()
111 void GMenubar::openByName(string const & name)
113 Glib::ustring uname = Glib::locale_to_utf8(name);
114 std::vector<Glib::ustring>::iterator it =
115 std::find(mainMenuNames_.begin(), mainMenuNames_.end(),
117 if (it != mainMenuNames_.end()) {
118 Gtk::MenuItem& mitem = menubar_.items()[it - mainMenuNames_.begin()];
123 lyxerr << "GMenubar::openByName: menu "
124 << name << " not found" << std::endl;
128 bool GMenubar::submenuDisabled(MenuItem const * item)
130 Menu & from = menubackend.getMenu(item->submenuname());
132 menubackend.expand(from, to, view_);
133 Menu::const_iterator i = to.begin();
134 Menu::const_iterator end = to.end();
135 for (; i != end; ++i) {
137 case MenuItem::Submenu:
138 if (!submenuDisabled(&(*i)))
141 case MenuItem::Command:
143 FuncStatus const flag =
144 view_->getLyXFunc().getStatus(i->func());
157 void GMenubar::onSubMenuActivate(MenuItem const * item,
158 Gtk::MenuItem * gitem)
160 Gtk::Menu * gmenu = gitem->get_submenu();
162 LyxMenu * lyxmenu = static_cast<LyxMenu*>(gmenu);
163 lyxmenu->clearBackMenu();
164 Menu * fmenu = &menubackend.getMenu(item->submenuname());
165 menubackend.expand(*fmenu, lyxmenu->getBackMenu(), view_);
166 Menu::const_iterator i = lyxmenu->getBackMenu().begin();
167 Menu::const_iterator end = lyxmenu->getBackMenu().end();
168 Gtk::Menu * gmenu_new;
169 for (; i != end; ++i) {
171 case MenuItem::Submenu:
172 gmenu_new = new LyxMenu;
173 gmenu->items().push_back(
174 Gtk::Menu_Helpers::MenuElem(
175 labelTrans(i->label(), i->shortcut()),
177 gmenu->items().back().signal_activate().connect(
178 SigC::bind(SigC::slot(*this, &GMenubar::onSubMenuActivate),
180 &gmenu->items().back()));
181 if (submenuDisabled(&(*i)))
182 gmenu->items().back().set_sensitive(false);
184 case MenuItem::Command:
187 #warning Bindings are not inserted into the menu labels here. (Lgb)
189 FuncStatus const flag =
190 view_->getLyXFunc().getStatus(i->func());
191 bool on = flag.onoff(true);
192 bool off = flag.onoff(false);
195 gmenu->items().push_back(
196 Gtk::Menu_Helpers::CheckMenuElem(
197 labelTrans(i->label(),
199 Gtk::CheckMenuItem& citem =
200 static_cast<Gtk::CheckMenuItem&>(
201 gmenu->items().back());
202 citem.set_active(on);
204 gmenu->items().push_back(
205 Gtk::Menu_Helpers::MenuElem(
206 labelTrans(i->label(),
209 Gtk::MenuItem & item = gmenu->items().back();
210 item.signal_activate().connect(
211 SigC::bind(SigC::slot(*this, &GMenubar::onCommandActivate),
214 item.set_sensitive(false);
217 case MenuItem::Separator:
218 gmenu->items().push_back(
219 Gtk::Menu_Helpers::SeparatorElem());
222 lyxerr << "GMenubar::create_submenu: "
223 "this should not happen" << std::endl;
230 void GMenubar::onCommandActivate(MenuItem const * item,
231 Gtk::MenuItem * /*gitem*/)
233 view_->getLyXFunc().dispatch(item->func(), true);
236 } // namespace frontend