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 {
29 LyxMenu() { menu_.reset(new ::Menu); }
31 ::Menu& getBackMenu() { return *menu_.get(); }
33 void clearBackMenu() { menu_.reset(new ::Menu); }
35 std::auto_ptr< ::Menu > menu_;
39 Glib::ustring labelTrans(string const & label, string const & shortcut)
41 string labelN = label;
42 string::size_type i = label.find(shortcut);
43 if (i == string::npos)
44 return Glib::locale_to_utf8(label);
45 labelN.insert(i, "_");
46 return Glib::locale_to_utf8(labelN);
50 void ClearMenu(Gtk::MenuShell * menu)
52 Gtk::Menu_Helpers::MenuList::iterator m = menu->items().begin();
53 Gtk::Menu_Helpers::MenuList::iterator end = menu->items().end();
55 for (; m != end; ++m) {
56 if ((subMenu = m->get_submenu()) != 0) {
61 menu->items().clear();
68 GMenubar::GMenubar(LyXView * lyxView, MenuBackend const & /*menuBackend*/) :
71 GView * gview = static_cast<GView*>(lyxView);
72 Menu const & menu = menubackend.getMenubar();
73 Menu::const_iterator i = menu.begin();
74 Menu::const_iterator end = menu.end();
75 for (; i != end; ++i) {
76 if (i->kind() != MenuItem::Submenu) {
77 lyxerr << "ERROR: GMenubar::createMenubar:"
78 " only submenus can appear in a menubar"
82 Gtk::Menu * gmenu = new LyxMenu;
83 menubar_.items().push_back(
84 Gtk::Menu_Helpers::MenuElem(
85 labelTrans(i->label(), i->shortcut()),
87 menubar_.items().back().signal_activate().connect(
88 sigc::bind(sigc::mem_fun(*this, &GMenubar::onSubMenuActivate), &(*i),
89 &menubar_.items().back()));
90 mainMenuNames_.push_back(i->submenuname());
93 gview->getBox(GView::Top).children().push_back(
94 Gtk::Box_Helpers::Element(menubar_, Gtk::PACK_SHRINK));
100 ClearMenu(&menubar_);
104 void GMenubar::update()
109 void GMenubar::openByName(string const & name)
111 Glib::ustring uname = Glib::locale_to_utf8(name);
112 std::vector<Glib::ustring>::iterator it =
113 std::find(mainMenuNames_.begin(), mainMenuNames_.end(),
115 if (it != mainMenuNames_.end()) {
116 Gtk::MenuItem& mitem = menubar_.items()[it - mainMenuNames_.begin()];
121 lyxerr << "GMenubar::openByName: menu "
122 << name << " not found" << std::endl;
126 bool GMenubar::submenuDisabled(MenuItem const * item)
128 Menu & from = menubackend.getMenu(item->submenuname());
130 menubackend.expand(from, to, view_);
131 Menu::const_iterator i = to.begin();
132 Menu::const_iterator end = to.end();
133 for (; i != end; ++i) {
135 case MenuItem::Submenu:
136 if (!submenuDisabled(&(*i)))
139 case MenuItem::Command:
141 FuncStatus const flag =
142 view_->getLyXFunc().getStatus(i->func());
155 void GMenubar::onSubMenuActivate(MenuItem const * item,
156 Gtk::MenuItem * gitem)
158 Gtk::Menu * gmenu = gitem->get_submenu();
160 LyxMenu * lyxmenu = static_cast<LyxMenu*>(gmenu);
161 lyxmenu->clearBackMenu();
162 Menu * fmenu = &menubackend.getMenu(item->submenuname());
163 menubackend.expand(*fmenu, lyxmenu->getBackMenu(), view_);
164 Menu::const_iterator i = lyxmenu->getBackMenu().begin();
165 Menu::const_iterator end = lyxmenu->getBackMenu().end();
166 Gtk::Menu * gmenu_new;
167 for (; i != end; ++i) {
169 case MenuItem::Submenu:
170 gmenu_new = new LyxMenu;
171 gmenu->items().push_back(
172 Gtk::Menu_Helpers::MenuElem(
173 labelTrans(i->label(), i->shortcut()),
175 gmenu->items().back().signal_activate().connect(
176 sigc::bind(sigc::mem_fun(*this, &GMenubar::onSubMenuActivate),
178 &gmenu->items().back()));
179 if (submenuDisabled(&(*i)))
180 gmenu->items().back().set_sensitive(false);
182 case MenuItem::Command:
185 #warning Bindings are not inserted into the menu labels here. (Lgb)
187 FuncStatus const flag =
188 view_->getLyXFunc().getStatus(i->func());
189 bool on = flag.onoff(true);
190 bool off = flag.onoff(false);
193 gmenu->items().push_back(
194 Gtk::Menu_Helpers::CheckMenuElem(
195 labelTrans(i->label(),
197 Gtk::CheckMenuItem& citem =
198 static_cast<Gtk::CheckMenuItem&>(
199 gmenu->items().back());
200 citem.set_active(on);
202 gmenu->items().push_back(
203 Gtk::Menu_Helpers::MenuElem(
204 labelTrans(i->label(),
207 Gtk::MenuItem & item = gmenu->items().back();
208 item.signal_activate().connect(
209 sigc::bind(sigc::mem_fun(*this, &GMenubar::onCommandActivate),
212 item.set_sensitive(false);
215 case MenuItem::Separator:
216 gmenu->items().push_back(
217 Gtk::Menu_Helpers::SeparatorElem());
220 lyxerr << "GMenubar::create_submenu: "
221 "this should not happen" << std::endl;
228 void GMenubar::onCommandActivate(MenuItem const * item,
229 Gtk::MenuItem * /*gitem*/)
231 view_->getLyXFunc().dispatch(item->func(), true);
234 } // namespace frontend