]> git.lyx.org Git - features.git/blob - src/frontends/gtk/GToolbar.C
The Gtk patch.
[features.git] / src / frontends / gtk / GToolbar.C
1 /**
2  * \file GToolbar.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 "GToolbar.h"
15 #include "GView.h"
16 #include "LyXAction.h"
17 #include "lyxfunc.h"
18 #include "FuncStatus.h"
19 #include "buffer.h"
20 #include "funcrequest.h"
21 #include "gettext.h"
22 #include "Tooltips.h"
23 #include "support/filetools.h"
24 #include "support/lstrings.h"
25 #include "debug.h"
26
27
28 namespace
29 {
30
31
32 char const * gToolData = "tool_data";
33
34
35 inline void comboClear(Gtk::Combo & combo)
36 {
37         std::vector<Glib::ustring> strings;
38         strings.push_back("");
39         combo.set_popdown_strings(strings);
40 }
41
42
43 inline bool comboIsEmpty(Gtk::Combo & combo)
44 {
45         std::vector<Glib::ustring> strings = combo.get_popdown_strings();
46         return (strings.empty() || (strings.size() == 1 && strings[0] == ""));
47 }
48
49
50 }
51
52
53 GToolbar::GToolbar(LyXView * lyxView, int /*x*/, int /*y*/)
54         : view_(lyxView), internal_(false)
55 {
56         combo_.set_value_in_list();
57         combo_.get_entry()->set_editable(false);
58         combo_.unset_flags(Gtk::CAN_FOCUS | Gtk::CAN_DEFAULT);
59         combo_.get_entry()->unset_flags(Gtk::CAN_FOCUS | Gtk::CAN_DEFAULT);
60         comboClear(combo_);
61         combo_.get_entry()->signal_changed().connect(
62                 SigC::slot(*this,
63                            &GToolbar::onLayoutSelected));
64         GView * gview = static_cast<GView*>(lyxView);
65         vbox_.show();
66         Gtk::VBox & vbox = gview->getVBox();
67         vbox.children().push_back(Gtk::Box_Helpers::Element(vbox_,
68                                                             Gtk::PACK_SHRINK));
69 }
70
71
72 GToolbar::~GToolbar()
73 {
74 }
75
76
77 void GToolbar::add(ToolbarBackend::Toolbar const & tb)
78 {
79         Gtk::Toolbar * toolbar = manage(new Gtk::Toolbar());
80         ToolbarBackend::item_iterator it = tb.items.begin();
81         ToolbarBackend::item_iterator end = tb.items.end();
82         for (; it != end; ++it)
83                 add(toolbar, it->first, it->second);
84         toolbar->set_toolbar_style(Gtk::TOOLBAR_ICONS);
85         toolbar->show();
86         vbox_.children().push_back(
87                 Gtk::Box_Helpers::Element(*toolbar,
88                                           Gtk::PACK_SHRINK));
89         toolbars_.push_back(toolbar);
90 }
91
92
93 void GToolbar::add(Gtk::Toolbar * toolbar, 
94                          int action,
95                          string const & tooltip)
96 {
97         switch (action) {
98         case ToolbarBackend::SEPARATOR:
99                 toolbar->tools().push_back(Gtk::Toolbar_Helpers::Space());
100                 break;
101         case ToolbarBackend::MINIBUFFER:
102                 // Not supported yet.
103                 break;
104         case ToolbarBackend::LAYOUTS:
105         {
106                 combo_.show();
107                 toolbar->tools().push_back(
108                         Gtk::Toolbar_Helpers::Element(combo_));
109                 toolbar->tools().back().get_widget()->set_data(
110                         gToolData,
111                         reinterpret_cast<void*>(LFUN_LAYOUT));
112                 break;
113         }
114         default:
115         {
116                 Glib::ustring xpmName = 
117                         Glib::locale_to_utf8(toolbarbackend.getIcon(action));
118                 Glib::ustring tip = Glib::locale_to_utf8(tooltip);
119                 if (xpmName.size() == 0) {
120                         toolbar->tools().push_back(
121                                 Gtk::Toolbar_Helpers::ButtonElem(
122                                         "",
123                                         SigC::bind(SigC::slot(*this, &GToolbar::onButtonClicked),
124                                                    action),
125                                         tip));
126                 } else {
127                         Gtk::Image * image = 
128                                 Gtk::manage(new Gtk::Image(xpmName));
129                         image->show();
130                         toolbar->tools().push_back(
131                                 Gtk::Toolbar_Helpers::ButtonElem(
132                                         "",
133                                         *image,
134                                         SigC::bind(SigC::slot(*this, &GToolbar::onButtonClicked),
135                                                    action),
136                                         tip));
137                 }
138                 toolbar->tools().back().get_content()->set_data(
139                         gToolData,
140                         reinterpret_cast<void*>(action));
141                 break;
142         }
143         }
144 }
145
146
147 void GToolbar::onButtonClicked(int action)
148 {
149         view_->getLyXFunc().dispatch(action, true);
150 }
151
152
153 void GToolbar::onLayoutSelected()
154 {
155         if (internal_)
156                 return;
157         string layoutGuiName = combo_.get_entry()->get_text();
158         // we get two signal, one of it is empty and useless
159         if (layoutGuiName.empty())
160                 return;
161         LyXTextClass const & tc =
162                 view_->buffer()->params.getLyXTextClass();
163
164         LyXTextClass::const_iterator end = tc.end();
165         for (LyXTextClass::const_iterator cit = tc.begin();
166              cit != end; ++cit) {
167                 if ((*cit)->name() == layoutGuiName) {
168                         view_->getLyXFunc().dispatch(
169                                 FuncRequest(LFUN_LAYOUT, (*cit)->name()),
170                                 true);
171                         return;
172                 }
173         }
174         lyxerr << "ERROR (GToolbar::layoutSelected): layout not found! name : "
175                << layoutGuiName
176                << std::endl;
177 }
178
179
180 void GToolbar::displayToolbar(ToolbarBackend::Toolbar const & /*tb*/, bool /*show*/)
181 {
182 }
183
184
185 void GToolbar::update()
186 {
187         std::vector<Gtk::Toolbar*>::iterator itToolbar;
188         for (itToolbar = toolbars_.begin();
189              itToolbar != toolbars_.end(); ++itToolbar) {
190                 Gtk::Toolbar * toolbar = *itToolbar;
191                 Gtk::Toolbar_Helpers::ToolList::iterator it;
192                 for (it = toolbar->tools().begin();
193                      it != toolbar->tools().end(); ++it) {
194                         Gtk::Widget * widget;
195                         switch (it->get_type()) {
196                         case Gtk::TOOLBAR_CHILD_WIDGET:
197                                 widget = it->get_widget();
198                                 break;
199                         case Gtk::TOOLBAR_CHILD_SPACE:
200                                 continue;
201                         default:
202                                 widget = it->get_content();
203                         }
204                         int action = reinterpret_cast<int>(
205                                 widget->get_data(gToolData));
206                         FuncStatus const status = view_->
207                                 getLyXFunc().getStatus(action);
208                         bool sensitive = !status.disabled();
209                         widget->set_sensitive(sensitive);
210                         if (it->get_type() != Gtk::TOOLBAR_CHILD_BUTTON)
211                                 return;
212                         if (status.onoff(true))
213                                 static_cast<Gtk::Button*>(widget)->
214                                         set_relief(Gtk::RELIEF_NORMAL);
215                         if (status.onoff(false))
216                                 static_cast<Gtk::Button*>(widget)->
217                                         set_relief(Gtk::RELIEF_NONE);
218                 }
219         }
220 }
221
222
223 void GToolbar::setLayout(string const & layout)
224 {
225         LyXTextClass const & tc =
226                 view_->buffer()->params.getLyXTextClass();
227         internal_ = true;
228         combo_.get_entry()->set_text(tc[layout]->name());
229         internal_ = false;
230 }
231
232
233 void GToolbar::updateLayoutList()
234 {
235         LyXTextClass const & tc =
236                 view_->buffer()->params.getLyXTextClass();
237         LyXTextClass::const_iterator end = tc.end();
238         std::vector<Glib::ustring> strings;
239         for (LyXTextClass::const_iterator cit = tc.begin();
240              cit != end; ++cit)
241                 if ((*cit)->obsoleted_by().empty())
242                         strings.push_back(
243                                 Glib::locale_to_utf8((*cit)->name()));
244         internal_ = true;
245         combo_.set_popdown_strings(strings);
246         internal_ = false;
247 }
248
249
250 void GToolbar::openLayoutList()
251 {
252         combo_.get_list()->activate();
253 }
254
255
256 void GToolbar::clearLayoutList()
257 {
258         internal_ = true;
259         comboClear(combo_);
260         internal_ = false;
261 }