3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
9 * Full author contact details are available in file CREDITS.
18 #include "ToolbarBackend.h" // for getIcon
31 class LyxMenu : public Gtk::Menu {
33 LyxMenu() { menu_.reset(new ::Menu); }
35 ::Menu& getBackMenu() { return *menu_.get(); }
37 void clearBackMenu() { menu_.reset(new ::Menu); }
39 std::auto_ptr< ::Menu > menu_;
43 Glib::ustring labelTrans(string const & label, string const & shortcut)
45 string labelN = label;
46 string::size_type i = label.find(shortcut);
47 if (i == string::npos)
48 return Glib::locale_to_utf8(label);
49 labelN.insert(i, "_");
50 return Glib::locale_to_utf8(labelN);
54 void ClearMenu(Gtk::MenuShell * menu)
56 Gtk::Menu_Helpers::MenuList::iterator m = menu->items().begin();
57 Gtk::Menu_Helpers::MenuList::iterator end = menu->items().end();
59 for (; m != end; ++m) {
60 if ((subMenu = m->get_submenu()) != 0) {
65 menu->items().clear();
72 GMenubar::GMenubar(LyXView * lyxView, MenuBackend const & /*menuBackend*/) :
75 GView * gview = static_cast<GView*>(lyxView);
76 Menu const & menu = menubackend.getMenubar();
77 Menu::const_iterator i = menu.begin();
78 Menu::const_iterator end = menu.end();
79 for (; i != end; ++i) {
80 if (i->kind() != MenuItem::Submenu) {
81 lyxerr << "ERROR: GMenubar::createMenubar:"
82 " only submenus can appear in a menubar"
86 Gtk::Menu * gmenu = new LyxMenu;
87 menubar_.items().push_back(
88 Gtk::Menu_Helpers::MenuElem(
89 labelTrans(i->label(), i->shortcut()),
91 menubar_.items().back().signal_activate().connect(
92 sigc::bind(sigc::mem_fun(*this, &GMenubar::onSubMenuActivate), &(*i),
93 &menubar_.items().back()));
94 mainMenuNames_.push_back(i->submenuname());
97 gview->getBox(GView::Top).children().push_back(
98 Gtk::Box_Helpers::Element(menubar_, Gtk::PACK_SHRINK));
102 GMenubar::~GMenubar()
104 ClearMenu(&menubar_);
108 void GMenubar::update()
113 void GMenubar::openByName(string const & name)
115 Glib::ustring uname = Glib::locale_to_utf8(name);
116 std::vector<Glib::ustring>::iterator it =
117 std::find(mainMenuNames_.begin(), mainMenuNames_.end(),
119 if (it != mainMenuNames_.end()) {
120 Gtk::MenuItem& mitem = menubar_.items()[it - mainMenuNames_.begin()];
125 lyxerr << "GMenubar::openByName: menu "
126 << name << " not found" << std::endl;
130 void GMenubar::onSubMenuActivate(MenuItem const * item,
131 Gtk::MenuItem * gitem)
133 Gtk::Menu * gmenu = gitem->get_submenu();
135 LyxMenu * lyxmenu = static_cast<LyxMenu*>(gmenu);
136 lyxmenu->clearBackMenu();
137 Menu * fmenu = item->submenuname().empty() ?
139 &menubackend.getMenu(item->submenuname());
141 // Choose size for icons on command items
144 Gtk::IconSize::lookup(Gtk::ICON_SIZE_MENU, iconwidth, iconheight);
146 menubackend.expand(*fmenu, lyxmenu->getBackMenu(), view_);
147 Menu::const_iterator i = lyxmenu->getBackMenu().begin();
148 Menu::const_iterator end = lyxmenu->getBackMenu().end();
149 Gtk::Menu * gmenu_new;
150 for (; i != end; ++i) {
152 case MenuItem::Submenu:
153 gmenu_new = new LyxMenu;
154 gmenu->items().push_back(
155 Gtk::Menu_Helpers::MenuElem(
156 labelTrans(i->label(), i->shortcut()),
158 gmenu->items().back().signal_activate().connect(
159 sigc::bind(sigc::mem_fun(*this, &GMenubar::onSubMenuActivate),
161 &gmenu->items().back()));
162 if (!i->status().enabled())
163 gmenu->items().back().set_sensitive(false);
165 case MenuItem::Command:
167 FuncStatus const flag = i->status();
168 bool on = flag.onoff(true);
169 bool off = flag.onoff(false);
172 gmenu->items().push_back(
173 Gtk::Menu_Helpers::CheckMenuElem(
174 labelTrans(i->label(),
176 Gtk::CheckMenuItem& citem =
177 static_cast<Gtk::CheckMenuItem&>(
178 gmenu->items().back());
179 citem.set_active(on);
181 // Choose an icon from the funcrequest
182 Gtk::BuiltinStockID stockID = getGTKStockIcon(i->func());
183 Gtk::Image * image = NULL;
184 // Prefer stock graphics
185 if (stockID != Gtk::Stock::MISSING_IMAGE) {
186 image = Gtk::manage(new Gtk::Image(stockID, Gtk::ICON_SIZE_MENU));
188 Glib::ustring xpmName =
189 Glib::locale_to_utf8(toolbarbackend.getIcon(i->func()));
190 if (xpmName.find("unknown.xpm") == -1) {
191 // Load icon and shrink it for menu size
192 Glib::RefPtr<Gdk::Pixbuf> bigicon =
193 Gdk::Pixbuf::create_from_file(xpmName);
194 Glib::RefPtr<Gdk::Pixbuf> smallicon =
195 bigicon->scale_simple(iconwidth,iconheight,Gdk::INTERP_TILES);
196 image = Gtk::manage(new Gtk::Image(smallicon));
200 Gtk::ImageMenuItem * item = Gtk::manage(new Gtk::ImageMenuItem);
202 item->set_image(*image);
204 // This hbox is necessary because add_accel_label is protected,
205 // and even if you subclass Gtk::MenuItem then add_accel_label
206 // doesn't do what you'd expect.
207 Gtk::HBox * hbox = Gtk::manage(new Gtk::HBox);
208 Gtk::Label * label1 = Gtk::manage(new Gtk::Label(
209 labelTrans(i->label(), i->shortcut()), true));
210 Gtk::Label * label2 = Gtk::manage(new Gtk::Label(
211 " " + i->binding(), false));
212 hbox->pack_start(*label1, false, false, 0);
213 hbox->pack_end(*label2, false, false, 0);
216 gmenu->append(*item);
219 Gtk::MenuItem & item = gmenu->items().back();
220 item.signal_activate().connect(
221 sigc::bind(sigc::mem_fun(*this, &GMenubar::onCommandActivate),
224 item.set_sensitive(false);
227 case MenuItem::Separator:
228 gmenu->items().push_back(
229 Gtk::Menu_Helpers::SeparatorElem());
232 lyxerr << "GMenubar::create_submenu: "
233 "this should not happen" << std::endl;
240 void GMenubar::onCommandActivate(MenuItem const * item,
241 Gtk::MenuItem * /*gitem*/)
243 view_->getLyXFunc().dispatch(item->func());
246 } // namespace frontend