3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
17 #include "bufferparams.h"
19 #include "funcrequest.h"
20 #include "FuncStatus.h"
30 GView::Position getPosition(ToolbarBackend::Flags const & flags)
32 if (flags & ToolbarBackend::TOP)
34 if (flags & ToolbarBackend::BOTTOM)
36 if (flags & ToolbarBackend::LEFT)
38 if (flags & ToolbarBackend::RIGHT)
44 LyXTextClass const & getTextClass(LyXView const & lv)
46 return lv.buffer()->params().getLyXTextClass();
49 char const * gToolData = "tool_data";
54 GLayoutBox::GLayoutBox(LyXView & owner,
55 Gtk::Toolbar & toolbar,
56 FuncRequest const & func)
60 combo_.signal_changed().connect(
61 sigc::mem_fun(*this,&GLayoutBox::selected));
63 model_ = Gtk::ListStore::create(cols_);
64 combo_.set_model(model_);
65 Gtk::CellRendererText * cell = Gtk::manage(new Gtk::CellRendererText);
66 combo_.pack_start(*cell, true);
67 combo_.add_attribute(*cell,"text",0);
68 combo_.set_wrap_width(2);
69 //Initially there's nothing in the liststore, so set the size
70 //to avoid it jumping too much when the user does something that
71 //causes the first update()
72 combo_.set_size_request(130,-1);
77 reinterpret_cast<void*>(&const_cast<FuncRequest &>(func)));
81 Gtk::ToolItem * toolitem = Gtk::manage(new Gtk::ToolItem);
82 toolitem->add(combo_);
83 toolbar.append(*toolitem);
86 void GLayoutBox::set(string const & layout)
88 LyXTextClass const & tc = getTextClass(owner_);
89 string const target = tc[layout]->name();
92 Gtk::TreeModel::iterator it = model_->children().begin();
93 Gtk::TreeModel::iterator end = model_->children().end();
94 for (; it != end; ++it) {
95 if ((*it)[cols_.name] == target){
96 combo_.set_active(it);
103 lyxerr << "ERROR (GLayoutBox::set): layout not found! name: "
104 << target << std::endl;
108 void GLayoutBox::update()
112 LyXTextClass const & tc = getTextClass(owner_);
114 LyXTextClass::const_iterator it = tc.begin();
115 LyXTextClass::const_iterator const end = tc.end();
118 for (; it != end; ++it)
119 if ((*it)->obsoleted_by().empty()) {
120 Gtk::TreeModel::iterator iter = model_->append();
121 Gtk::TreeModel::Row row = *iter;
122 row[cols_.name] = Glib::locale_to_utf8((*it)->name());
126 //now that we've loaded something into the combobox, forget
127 //the initial fixed size and let GTK decide.
128 combo_.set_size_request(-1,-1);
132 void GLayoutBox::clear()
140 void GLayoutBox::open()
146 void GLayoutBox::setEnabled(bool sensitive)
148 combo_.set_sensitive(sensitive);
152 void GLayoutBox::selected()
157 Glib::ustring layoutGuiName = (*(combo_.get_active()))[cols_.name];
159 // we get two signal, one of it is empty and useless
160 if (layoutGuiName.empty())
162 LyXTextClass const & tc = getTextClass(owner_);
164 LyXTextClass::const_iterator it = tc.begin();
165 LyXTextClass::const_iterator const end = tc.end();
166 for (; it != end; ++it) {
167 string const & name = (*it)->name();
168 if (name == layoutGuiName) {
169 owner_.getLyXFunc().dispatch(
170 FuncRequest(LFUN_LAYOUT, name),
175 lyxerr << "ERROR (GLayoutBox::selected): layout not found! name: "
176 << layoutGuiName << std::endl;
179 } // namespace frontend
183 Toolbars::ToolbarPtr make_toolbar(ToolbarBackend::Toolbar const & tbb,
186 using lyx::frontend::GToolbar;
187 return Toolbars::ToolbarPtr(new GToolbar(tbb, owner));
193 GToolbar::GToolbar(ToolbarBackend::Toolbar const & tbb, LyXView & owner)
194 : owner_(dynamic_cast<GView &>(owner))
196 ToolbarBackend::item_iterator it = tbb.items.begin();
197 ToolbarBackend::item_iterator end = tbb.items.end();
198 for (; it != end; ++it)
199 add(it->first, it->second);
201 toolbar_.set_toolbar_style(Gtk::TOOLBAR_ICONS);
204 GView::Position const position = getPosition(tbb.flags);
206 if (position == GView::Left || position == GView::Right)
207 toolbar_.set_orientation(Gtk::ORIENTATION_VERTICAL);
209 owner_.getBox(position).children().push_back(
210 Gtk::Box_Helpers::Element(toolbar_, Gtk::PACK_SHRINK));
215 void GToolbar::add(FuncRequest const & func, string const & tooltip)
217 switch (func.action) {
218 case ToolbarBackend::SEPARATOR: {
219 Gtk::SeparatorToolItem * space =
220 Gtk::manage(new Gtk::SeparatorToolItem);
221 toolbar_.append(*space);
225 case ToolbarBackend::MINIBUFFER:
226 // Not supported yet.
229 case ToolbarBackend::LAYOUTS: {
230 layout_.reset(new GLayoutBox(owner_, toolbar_, func));
235 Glib::ustring xpmName =
236 Glib::locale_to_utf8(toolbarbackend.getIcon(func));
237 Glib::ustring tip = Glib::locale_to_utf8(tooltip);
238 Gtk::ToolButton * toolbutton;
239 if (xpmName.size() == 0) {
240 toolbutton = Gtk::manage(new Gtk::ToolButton);
242 Gtk::Image * image = Gtk::manage(new Gtk::Image(xpmName));
244 toolbutton = Gtk::manage(new Gtk::ToolButton(*image));
246 // This code is putting a function reference into the GObject data field
247 // named gToolData. That's how we know how to update the status of the
249 toolbutton->set_data(gToolData,
250 reinterpret_cast<void*>(&const_cast<FuncRequest &>(func)));
252 toolbutton->set_tooltip(tooltips_,tip);
253 toolbutton->signal_clicked().connect(sigc::bind(sigc::mem_fun(*this,
254 >oolbar::clicked), FuncRequest(func)));
255 toolbar_.append(*toolbutton);
263 void GToolbar::clicked(FuncRequest func)
265 owner_.getLyXFunc().dispatch(func, true);
269 void GToolbar::hide(bool)
275 void GToolbar::show(bool)
281 void GToolbar::update()
283 int const items = toolbar_.get_n_items();
285 for (int i = 0; i < items; ++i) {
286 Gtk::ToolItem * item = toolbar_.get_nth_item(i);
288 FuncRequest const * func = reinterpret_cast<FuncRequest *>(
289 item->get_data(gToolData));
291 FuncStatus const status = owner_.getLyXFunc().getStatus(*func);
292 item->set_sensitive(status.enabled());
297 } // namespace frontend