]> git.lyx.org Git - lyx.git/blob - src/frontends/gtk/GMenubar.C
GTK compile fixes.
[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 #include <gtkmm.h>
13
14 #include "GMenubar.h"
15 #include "GView.h"
16 #include "debug.h"
17 #include "lyxfunc.h"
18
19 using std::string;
20
21 namespace
22 {
23
24 class LyxMenu : public Gtk::Menu
25 {
26 public:
27         LyxMenu() { menu_.reset(new ::Menu); }
28
29         ::Menu& getBackMenu() { return *menu_.get(); }
30
31         void clearBackMenu() { menu_.reset(new ::Menu); }
32 private:
33         std::auto_ptr< ::Menu > menu_;
34 };
35
36
37 Glib::ustring labelTrans(string const & label, string const & shortcut)
38 {
39         string labelN = label;
40         string::size_type i = label.find(shortcut);
41         if (i == string::npos)
42                 return Glib::locale_to_utf8(label);
43         labelN.insert(i, "_");
44         return Glib::locale_to_utf8(labelN);
45 }
46
47
48 void ClearMenu(Gtk::MenuShell * menu)
49 {
50         Gtk::Menu_Helpers::MenuList::iterator m, end;
51         m = menu->items().begin();
52         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         Gtk::VBox& vbox = gview->getVBox();
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"
79                                << std::endl;
80                         continue;
81                 }
82                 Gtk::Menu * gmenu = new LyxMenu;
83                 menubar_.items().push_back(
84                         Gtk::Menu_Helpers::MenuElem(
85                                 labelTrans(i->label(), i->shortcut()),
86                                 *gmenu));
87                 menubar_.items().back().signal_activate().connect(
88                         SigC::bind(SigC::slot(*this, &GMenubar::onSubMenuActivate), &(*i),
89                                    &menubar_.items().back()));
90                 mainMenuNames_.push_back(i->submenuname());
91         }
92         menubar_.show();
93         vbox.children().push_back(Gtk::Box_Helpers::Element(menubar_,
94                                                             Gtk::PACK_SHRINK));
95 }
96
97
98 GMenubar::~GMenubar()
99 {
100         ClearMenu(&menubar_);
101 }
102
103
104 void GMenubar::update()
105 {
106 }
107
108
109 void GMenubar::openByName(string const & name)
110 {
111         Glib::ustring uname = Glib::locale_to_utf8(name);
112         std::vector<Glib::ustring>::iterator it =
113                 std::find(mainMenuNames_.begin(), mainMenuNames_.end(),
114                           uname);
115         if (it != mainMenuNames_.end()) {
116                 Gtk::MenuItem& mitem = menubar_.items()[it - mainMenuNames_.begin()];
117                 mitem.select();
118                 mitem.activate();
119                 return;
120         }
121         lyxerr << "GMenubar::openByName: menu "
122                << name << " not found" << std::endl;
123 }
124
125
126 bool GMenubar::submenuDisabled(MenuItem const * item)
127 {
128         Menu & from = menubackend.getMenu(item->submenuname());
129         Menu to;
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) {
134                 switch (i->kind()) {
135                 case MenuItem::Submenu:
136                         if (!submenuDisabled(&(*i)))
137                                 return false;
138                         break;
139                 case MenuItem::Command:
140                 {
141                         FuncStatus const flag =
142                                 view_->getLyXFunc().getStatus(i->func());
143                         if (flag.enabled())
144                                 return false;
145                         break;
146                 }
147                 default:
148                         break;
149                 }
150         }
151         return true;
152 }
153
154
155 void GMenubar::onSubMenuActivate(MenuItem const * item,
156                                  Gtk::MenuItem * gitem)
157 {
158         Gtk::Menu * gmenu = gitem->get_submenu();
159         ClearMenu(gmenu);
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) {
168                 switch (i->kind()) {
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()),
174                                         *gmenu_new));
175                         gmenu->items().back().signal_activate().connect(
176                                 SigC::bind(SigC::slot(*this, &GMenubar::onSubMenuActivate),
177                                            &(*i),
178                                            &gmenu->items().back()));
179                         if (submenuDisabled(&(*i)))
180                                 gmenu->items().back().set_sensitive(false);
181                         break;
182                 case MenuItem::Command:
183                 {
184                         #warning Bindings are not inserted into the menu labels here. (Lgb)
185                         FuncStatus const flag =
186                                 view_->getLyXFunc().getStatus(i->func());
187                         bool on = flag.onoff(true);
188                         bool off = flag.onoff(false);
189
190                         if (on || off) {
191                                 gmenu->items().push_back(
192                                         Gtk::Menu_Helpers::CheckMenuElem(
193                                                 labelTrans(i->label(),
194                                                            i->shortcut())));
195                                 Gtk::CheckMenuItem& citem =
196                                         static_cast<Gtk::CheckMenuItem&>(
197                                                 gmenu->items().back());
198                                 citem.set_active(on);
199                         } else {
200                                 gmenu->items().push_back(
201                                         Gtk::Menu_Helpers::MenuElem(
202                                                 labelTrans(i->label(),
203                                                            i->shortcut())));
204                         }
205                         Gtk::MenuItem & item = gmenu->items().back();
206                         item.signal_activate().connect(
207                                 SigC::bind(SigC::slot(*this, &GMenubar::onCommandActivate),
208                                            &(*i), &item));
209                         if (!flag.enabled())
210                                 item.set_sensitive(false);
211                         break;
212                 }
213                 case MenuItem::Separator:
214                         gmenu->items().push_back(
215                                 Gtk::Menu_Helpers::SeparatorElem());
216                         break;
217                 default:
218                         lyxerr << "GMenubar::create_submenu: "
219                                 "this should not happen" << std::endl;
220                         break;
221                 }
222         }
223 }
224
225
226 void GMenubar::onCommandActivate(MenuItem const * item,
227                                        Gtk::MenuItem * /*gitem*/)
228 {
229         view_->getLyXFunc().dispatch(item->func(), true);
230 }