]> git.lyx.org Git - lyx.git/blob - src/frontends/gtk/GMenubar.C
Use MenuItem::status() instead of own submenudisabled function
[lyx.git] / src / frontends / gtk / GMenubar.C
1 /**
2  * \file GMenubar.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Huang Ying
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "GMenubar.h"
14 #include "GView.h"
15 #include "debug.h"
16 #include "lyxfunc.h"
17
18 using std::string;
19
20 namespace lyx {
21 namespace frontend {
22
23 namespace
24 {
25
26 class LyxMenu : public Gtk::Menu {
27 public:
28         LyxMenu() { menu_.reset(new ::Menu); }
29
30         ::Menu& getBackMenu() { return *menu_.get(); }
31
32         void clearBackMenu() { menu_.reset(new ::Menu); }
33 private:
34         std::auto_ptr< ::Menu > menu_;
35 };
36
37
38 Glib::ustring labelTrans(string const & label, string const & shortcut)
39 {
40         string labelN = label;
41         string::size_type i = label.find(shortcut);
42         if (i == string::npos)
43                 return Glib::locale_to_utf8(label);
44         labelN.insert(i, "_");
45         return Glib::locale_to_utf8(labelN);
46 }
47
48
49 void ClearMenu(Gtk::MenuShell * menu)
50 {
51         Gtk::Menu_Helpers::MenuList::iterator m = menu->items().begin();
52         Gtk::Menu_Helpers::MenuList::iterator end = menu->items().end();
53         Gtk::Menu * subMenu;
54         for (; m != end; ++m) {
55                 if ((subMenu = m->get_submenu()) != 0) {
56                         ClearMenu(subMenu);
57                         delete subMenu;
58                 }
59         }
60         menu->items().clear();
61 }
62
63
64 }
65
66
67 GMenubar::GMenubar(LyXView * lyxView, MenuBackend const & /*menuBackend*/) :
68         view_(lyxView)
69 {
70         GView * gview = static_cast<GView*>(lyxView);
71         Menu const & menu = menubackend.getMenubar();
72         Menu::const_iterator i = menu.begin();
73         Menu::const_iterator end = menu.end();
74         for (; i != end; ++i) {
75                 if (i->kind() != MenuItem::Submenu) {
76                         lyxerr << "ERROR: GMenubar::createMenubar:"
77                                 " only submenus can appear in a menubar"
78                                << std::endl;
79                         continue;
80                 }
81                 Gtk::Menu * gmenu = new LyxMenu;
82                 menubar_.items().push_back(
83                         Gtk::Menu_Helpers::MenuElem(
84                                 labelTrans(i->label(), i->shortcut()),
85                                 *gmenu));
86                 menubar_.items().back().signal_activate().connect(
87                         sigc::bind(sigc::mem_fun(*this, &GMenubar::onSubMenuActivate), &(*i),
88                                    &menubar_.items().back()));
89                 mainMenuNames_.push_back(i->submenuname());
90         }
91         menubar_.show();
92         gview->getBox(GView::Top).children().push_back(
93                 Gtk::Box_Helpers::Element(menubar_, Gtk::PACK_SHRINK));
94 }
95
96
97 GMenubar::~GMenubar()
98 {
99         ClearMenu(&menubar_);
100 }
101
102
103 void GMenubar::update()
104 {
105 }
106
107
108 void GMenubar::openByName(string const & name)
109 {
110         Glib::ustring uname = Glib::locale_to_utf8(name);
111         std::vector<Glib::ustring>::iterator it =
112                 std::find(mainMenuNames_.begin(), mainMenuNames_.end(),
113                           uname);
114         if (it != mainMenuNames_.end()) {
115                 Gtk::MenuItem& mitem = menubar_.items()[it - mainMenuNames_.begin()];
116                 mitem.select();
117                 mitem.activate();
118                 return;
119         }
120         lyxerr << "GMenubar::openByName: menu "
121                << name << " not found" << std::endl;
122 }
123
124
125 void GMenubar::onSubMenuActivate(MenuItem const * item,
126                                  Gtk::MenuItem * gitem)
127 {
128         Gtk::Menu * gmenu = gitem->get_submenu();
129         ClearMenu(gmenu);
130         LyxMenu * lyxmenu = static_cast<LyxMenu*>(gmenu);
131         lyxmenu->clearBackMenu();
132         Menu * fmenu = item->submenuname().empty() ?
133                 item->submenu() :
134                 &menubackend.getMenu(item->submenuname());
135
136         menubackend.expand(*fmenu, lyxmenu->getBackMenu(), view_);
137         Menu::const_iterator i = lyxmenu->getBackMenu().begin();
138         Menu::const_iterator end = lyxmenu->getBackMenu().end();
139         Gtk::Menu * gmenu_new;
140         for (; i != end; ++i) {
141                 switch (i->kind()) {
142                 case MenuItem::Submenu:
143                         gmenu_new = new LyxMenu;
144                         gmenu->items().push_back(
145                                 Gtk::Menu_Helpers::MenuElem(
146                                         labelTrans(i->label(), i->shortcut()),
147                                         *gmenu_new));
148                         gmenu->items().back().signal_activate().connect(
149                                 sigc::bind(sigc::mem_fun(*this, &GMenubar::onSubMenuActivate),
150                                            &(*i),
151                                            &gmenu->items().back()));
152                         if (!i->status().enabled())
153                                 gmenu->items().back().set_sensitive(false);
154                         break;
155                 case MenuItem::Command:
156                 {
157                         FuncStatus const flag = i->status();
158                         bool on = flag.onoff(true);
159                         bool off = flag.onoff(false);
160
161                         if (on || off) {
162                                 gmenu->items().push_back(
163                                         Gtk::Menu_Helpers::CheckMenuElem(
164                                                 labelTrans(i->label(),
165                                                            i->shortcut())));
166                                 Gtk::CheckMenuItem& citem =
167                                         static_cast<Gtk::CheckMenuItem&>(
168                                                 gmenu->items().back());
169                                 citem.set_active(on);
170                         } else {
171                                 // This is necessary because add_accel_label is protected,
172                                 // and even if you subclass Gtk::MenuItem then add_accel_label
173                                 // doesn't do what you'd expect.
174                                 Gtk::MenuItem * item = Gtk::manage(new Gtk::MenuItem);
175                                 Gtk::HBox * hbox = Gtk::manage(new Gtk::HBox);
176                                 Gtk::Label * label1 = Gtk::manage(new Gtk::Label(
177                                         labelTrans(i->label(), i->shortcut()), true));
178                                 Gtk::Label * label2 = Gtk::manage(new Gtk::Label(
179                                         "   " + i->binding(), false));
180                                 hbox->pack_start(*label1, false, false, 0);
181                                 hbox->pack_end(*label2, false, false, 0);
182                                 item->add(*hbox);
183
184                                 gmenu->append(*item);
185                                 item->show_all();
186                         }
187                         Gtk::MenuItem & item = gmenu->items().back();
188                         item.signal_activate().connect(
189                                 sigc::bind(sigc::mem_fun(*this, &GMenubar::onCommandActivate),
190                                            &(*i), &item));
191                         if (!flag.enabled())
192                                 item.set_sensitive(false);
193                         break;
194                 }
195                 case MenuItem::Separator:
196                         gmenu->items().push_back(
197                                 Gtk::Menu_Helpers::SeparatorElem());
198                         break;
199                 default:
200                         lyxerr << "GMenubar::create_submenu: "
201                                 "this should not happen" << std::endl;
202                         break;
203                 }
204         }
205 }
206
207
208 void GMenubar::onCommandActivate(MenuItem const * item,
209                                        Gtk::MenuItem * /*gitem*/)
210 {
211         view_->getLyXFunc().dispatch(item->func(), true);
212 }
213
214 } // namespace frontend
215 } // namespace lyx