]> git.lyx.org Git - lyx.git/blob - src/frontends/gtk/GToolbar.C
Get rid of Pseudo Actions
[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);
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                    ToolbarBackend::Item const & item)
96 {
97         FuncRequest const & func = item.first;
98         string const & tooltip = item.second;
99         switch (func.action) {
100         case ToolbarBackend::SEPARATOR:
101                 toolbar->tools().push_back(Gtk::Toolbar_Helpers::Space());
102                 break;
103         case ToolbarBackend::MINIBUFFER:
104                 // Not supported yet.
105                 break;
106         case ToolbarBackend::LAYOUTS:
107         {
108                 combo_.show();
109                 toolbar->tools().push_back(
110                         Gtk::Toolbar_Helpers::Element(combo_));
111                 toolbar->tools().back().get_widget()->set_data(
112                         gToolData,
113                         reinterpret_cast<void*>(&const_cast<ToolbarBackend::Item&>(item)));
114                 break;
115         }
116         default:
117         {
118                 Glib::ustring xpmName =
119                         Glib::locale_to_utf8(toolbarbackend.getIcon(func));
120                 Glib::ustring tip = Glib::locale_to_utf8(tooltip);
121                 if (xpmName.size() == 0) {
122                         toolbar->tools().push_back(
123                                 Gtk::Toolbar_Helpers::ButtonElem(
124                                         "",
125                                         SigC::bind(SigC::slot(*this, &GToolbar::onButtonClicked),
126                                                    FuncRequest(func)),
127                                         tip));
128                 } else {
129                         Gtk::Image * image =
130                                 Gtk::manage(new Gtk::Image(xpmName));
131                         image->show();
132                         toolbar->tools().push_back(
133                                 Gtk::Toolbar_Helpers::ButtonElem(
134                                         "",
135                                         *image,
136                                         SigC::bind(SigC::slot(*this, &GToolbar::onButtonClicked),
137                                                    FuncRequest(func)),
138                                         tip));
139                 }
140                 toolbar->tools().back().get_content()->set_data(
141                         gToolData,
142                         reinterpret_cast<void*>(&const_cast<ToolbarBackend::Item&>(item)));
143                 break;
144         }
145         }
146 }
147
148
149 void GToolbar::onButtonClicked(FuncRequest func)
150 {
151         view_->getLyXFunc().dispatch(func, true);
152 }
153
154
155 void GToolbar::onLayoutSelected()
156 {
157         if (internal_)
158                 return;
159         string layoutGuiName = combo_.get_entry()->get_text();
160         // we get two signal, one of it is empty and useless
161         if (layoutGuiName.empty())
162                 return;
163         LyXTextClass const & tc =
164                 view_->buffer()->params().getLyXTextClass();
165
166         LyXTextClass::const_iterator end = tc.end();
167         for (LyXTextClass::const_iterator cit = tc.begin();
168              cit != end; ++cit) {
169                 if ((*cit)->name() == layoutGuiName) {
170                         view_->getLyXFunc().dispatch(
171                                 FuncRequest(LFUN_LAYOUT, (*cit)->name()),
172                                 true);
173                         return;
174                 }
175         }
176         lyxerr << "ERROR (GToolbar::layoutSelected): layout not found! name : "
177                << layoutGuiName
178                << std::endl;
179 }
180
181
182 void GToolbar::displayToolbar(ToolbarBackend::Toolbar const & /*tb*/, bool /*show*/)
183 {
184 }
185
186
187 void GToolbar::update()
188 {
189         std::vector<Gtk::Toolbar*>::iterator itToolbar;
190         for (itToolbar = toolbars_.begin();
191              itToolbar != toolbars_.end(); ++itToolbar) {
192                 Gtk::Toolbar * toolbar = *itToolbar;
193                 Gtk::Toolbar_Helpers::ToolList::iterator it;
194                 for (it = toolbar->tools().begin();
195                      it != toolbar->tools().end(); ++it) {
196                         Gtk::Widget * widget;
197                         switch (it->get_type()) {
198                         case Gtk::TOOLBAR_CHILD_WIDGET:
199                                 widget = it->get_widget();
200                                 break;
201                         case Gtk::TOOLBAR_CHILD_SPACE:
202                                 continue;
203                         default:
204                                 widget = it->get_content();
205                         }
206                         ToolbarBackend::Item * item =
207                         reinterpret_cast<ToolbarBackend::Item*>(
208                                 widget->get_data(gToolData));
209                         FuncStatus const status = view_->
210                                 getLyXFunc().getStatus(item->first);
211                         bool sensitive = !status.disabled();
212                         widget->set_sensitive(sensitive);
213                         if (it->get_type() != Gtk::TOOLBAR_CHILD_BUTTON)
214                                 return;
215                         if (status.onoff(true))
216                                 static_cast<Gtk::Button*>(widget)->
217                                         set_relief(Gtk::RELIEF_NORMAL);
218                         if (status.onoff(false))
219                                 static_cast<Gtk::Button*>(widget)->
220                                         set_relief(Gtk::RELIEF_NONE);
221                 }
222         }
223 }
224
225
226 void GToolbar::setLayout(string const & layout)
227 {
228         LyXTextClass const & tc =
229                 view_->buffer()->params().getLyXTextClass();
230         internal_ = true;
231         combo_.get_entry()->set_text(tc[layout]->name());
232         internal_ = false;
233 }
234
235
236 void GToolbar::updateLayoutList()
237 {
238         LyXTextClass const & tc =
239                 view_->buffer()->params().getLyXTextClass();
240         LyXTextClass::const_iterator end = tc.end();
241         std::vector<Glib::ustring> strings;
242         for (LyXTextClass::const_iterator cit = tc.begin();
243              cit != end; ++cit)
244                 if ((*cit)->obsoleted_by().empty())
245                         strings.push_back(
246                                 Glib::locale_to_utf8((*cit)->name()));
247         internal_ = true;
248         combo_.set_popdown_strings(strings);
249         internal_ = false;
250 }
251
252
253 void GToolbar::openLayoutList()
254 {
255         combo_.get_list()->activate();
256 }
257
258
259 void GToolbar::clearLayoutList()
260 {
261         internal_ = true;
262         comboClear(combo_);
263         internal_ = false;
264 }