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