]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/gtk/GMenubar.C
Display accelerator (binding) labels in menus.
[lyx.git] / src / frontends / gtk / GMenubar.C
index a5baacaa3d3e5207c4f4e478b6088b5c984d5d01..45a2159d62a9fb364c1ee93ce843ac7691ff6338 100644 (file)
@@ -5,22 +5,25 @@
  *
  * \author Huang Ying
  *
- * Full author contact details are available in file CREDITS
+ * Full author contact details are available in file CREDITS.
  */
 
 #include <config.h>
-#include <gtkmm.h>
 
 #include "GMenubar.h"
 #include "GView.h"
 #include "debug.h"
 #include "lyxfunc.h"
 
+using std::string;
+
+namespace lyx {
+namespace frontend {
+
 namespace
 {
 
-class LyxMenu : public Gtk::Menu
-{
+class LyxMenu : public Gtk::Menu {
 public:
        LyxMenu() { menu_.reset(new ::Menu); }
 
@@ -45,9 +48,8 @@ Glib::ustring labelTrans(string const & label, string const & shortcut)
 
 void ClearMenu(Gtk::MenuShell * menu)
 {
-       Gtk::Menu_Helpers::MenuList::iterator m, end;
-       m = menu->items().begin();
-       end = menu->items().end();
+       Gtk::Menu_Helpers::MenuList::iterator m = menu->items().begin();
+       Gtk::Menu_Helpers::MenuList::iterator end = menu->items().end();
        Gtk::Menu * subMenu;
        for (; m != end; ++m) {
                if ((subMenu = m->get_submenu()) != 0) {
@@ -62,11 +64,10 @@ void ClearMenu(Gtk::MenuShell * menu)
 }
 
 
-GMenubar::GMenubar(LyXView *lyxView, MenuBackend const & /*menuBackend*/) :
+GMenubar::GMenubar(LyXView * lyxView, MenuBackend const & /*menuBackend*/) :
        view_(lyxView)
 {
        GView * gview = static_cast<GView*>(lyxView);
-       Gtk::VBox& vbox = gview->getVBox();
        Menu const & menu = menubackend.getMenubar();
        Menu::const_iterator i = menu.begin();
        Menu::const_iterator end = menu.end();
@@ -83,13 +84,13 @@ GMenubar::GMenubar(LyXView *lyxView, MenuBackend const & /*menuBackend*/) :
                                labelTrans(i->label(), i->shortcut()),
                                *gmenu));
                menubar_.items().back().signal_activate().connect(
-                       SigC::bind(SigC::slot(*this, &GMenubar::onSubMenuActivate), &(*i),
+                       sigc::bind(sigc::mem_fun(*this, &GMenubar::onSubMenuActivate), &(*i),
                                   &menubar_.items().back()));
                mainMenuNames_.push_back(i->submenuname());
        }
        menubar_.show();
-       vbox.children().push_back(Gtk::Box_Helpers::Element(menubar_,
-                                                           Gtk::PACK_SHRINK));
+       gview->getBox(GView::Top).children().push_back(
+               Gtk::Box_Helpers::Element(menubar_, Gtk::PACK_SHRINK));
 }
 
 
@@ -137,8 +138,8 @@ bool GMenubar::submenuDisabled(MenuItem const * item)
                case MenuItem::Command:
                {
                        FuncStatus const flag =
-                               view_->getLyXFunc().getStatus(i->action());
-                       if (!flag.disabled())
+                               view_->getLyXFunc().getStatus(i->func());
+                       if (flag.enabled())
                                return false;
                        break;
                }
@@ -151,7 +152,7 @@ bool GMenubar::submenuDisabled(MenuItem const * item)
 
 
 void GMenubar::onSubMenuActivate(MenuItem const * item,
-                                      Gtk::MenuItem * gitem)
+                                Gtk::MenuItem * gitem)
 {
        Gtk::Menu * gmenu = gitem->get_submenu();
        ClearMenu(gmenu);
@@ -171,7 +172,7 @@ void GMenubar::onSubMenuActivate(MenuItem const * item,
                                        labelTrans(i->label(), i->shortcut()),
                                        *gmenu_new));
                        gmenu->items().back().signal_activate().connect(
-                               SigC::bind(SigC::slot(*this, &GMenubar::onSubMenuActivate),
+                               sigc::bind(sigc::mem_fun(*this, &GMenubar::onSubMenuActivate),
                                           &(*i),
                                           &gmenu->items().back()));
                        if (submenuDisabled(&(*i)))
@@ -180,31 +181,41 @@ void GMenubar::onSubMenuActivate(MenuItem const * item,
                case MenuItem::Command:
                {
                        FuncStatus const flag =
-                               view_->getLyXFunc().getStatus(i->action());
-                       bool on, off;
-                       on = flag.onoff(true);
-                       off = flag.onoff(false);
-                       
+                               view_->getLyXFunc().getStatus(i->func());
+                       bool on = flag.onoff(true);
+                       bool off = flag.onoff(false);
+
                        if (on || off) {
                                gmenu->items().push_back(
                                        Gtk::Menu_Helpers::CheckMenuElem(
                                                labelTrans(i->label(),
                                                           i->shortcut())));
-                               Gtk::CheckMenuItem& citem = 
+                               Gtk::CheckMenuItem& citem =
                                        static_cast<Gtk::CheckMenuItem&>(
                                                gmenu->items().back());
                                citem.set_active(on);
                        } else {
-                               gmenu->items().push_back(
-                                       Gtk::Menu_Helpers::MenuElem(
-                                               labelTrans(i->label(),
-                                                          i->shortcut())));
+                               //This is necessary because add_accel_label is protected,
+                               //and even if you subclass Gtk::MenuItem then add_accel_label
+                               //doesn't do what you'd expect.
+                               Gtk::MenuItem * item = Gtk::manage(new Gtk::MenuItem);
+                               Gtk::HBox * hbox = Gtk::manage(new Gtk::HBox);
+                               Gtk::Label * label1 = Gtk::manage(new Gtk::Label(
+                                       labelTrans(i->label(), i->shortcut()), true));
+                               Gtk::Label * label2 = Gtk::manage(new Gtk::Label(
+                                       "   " + i->binding(), false));
+                               hbox->pack_start(*label1, false, false, 0);
+                               hbox->pack_end(*label2, false, false, 0);
+                               item->add(*hbox);
+
+                               gmenu->append(*item);
+                               item->show_all();
                        }
                        Gtk::MenuItem & item = gmenu->items().back();
                        item.signal_activate().connect(
-                               SigC::bind(SigC::slot(*this, &GMenubar::onCommandActivate),
+                               sigc::bind(sigc::mem_fun(*this, &GMenubar::onCommandActivate),
                                           &(*i), &item));
-                       if (flag.disabled())
+                       if (!flag.enabled())
                                item.set_sensitive(false);
                        break;
                }
@@ -224,5 +235,8 @@ void GMenubar::onSubMenuActivate(MenuItem const * item,
 void GMenubar::onCommandActivate(MenuItem const * item,
                                       Gtk::MenuItem * /*gitem*/)
 {
-       view_->getLyXFunc().dispatch(item->action(), true);
+       view_->getLyXFunc().dispatch(item->func(), true);
 }
+
+} // namespace frontend
+} // namespace lyx