3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
9 * Full author contact details are available in file CREDITS.
20 #include "bufferparams.h"
22 #include "funcrequest.h"
23 #include "FuncStatus.h"
33 GView::Position getPosition(ToolbarBackend::Flags const & flags)
35 if (flags & ToolbarBackend::TOP)
37 if (flags & ToolbarBackend::BOTTOM)
39 if (flags & ToolbarBackend::LEFT)
41 if (flags & ToolbarBackend::RIGHT)
47 LyXTextClass const & getTextClass(LyXView const & lv)
49 return lv.buffer()->params().getLyXTextClass();
52 char const * gToolData = "tool_data";
57 GLayoutBox::GLayoutBox(LyXView & owner,
58 Gtk::Toolbar & toolbar,
59 FuncRequest const & func)
63 combo_.signal_changed().connect(
64 sigc::mem_fun(*this,&GLayoutBox::selected));
66 model_ = Gtk::ListStore::create(cols_);
67 combo_.set_model(model_);
68 Gtk::CellRendererText * cell = Gtk::manage(new Gtk::CellRendererText);
69 combo_.pack_start(*cell, true);
70 combo_.add_attribute(*cell,"text",0);
71 combo_.set_wrap_width(2);
72 // Initially there's nothing in the liststore, so set the size
73 // to avoid it jumping too much when the user does something that
74 // causes the first update()
75 combo_.set_size_request(130,-1);
80 reinterpret_cast<void*>(&const_cast<FuncRequest &>(func)));
84 Gtk::ToolItem * toolitem = Gtk::manage(new Gtk::ToolItem);
85 toolitem->add(combo_);
86 toolbar.append(*toolitem);
89 void GLayoutBox::set(string const & layout)
91 LyXTextClass const & tc = getTextClass(owner_);
92 string const target = tc[layout]->name();
95 Gtk::TreeModel::iterator it = model_->children().begin();
96 Gtk::TreeModel::iterator end = model_->children().end();
97 for (; it != end; ++it) {
98 if ((*it)[cols_.name] == target){
99 combo_.set_active(it);
106 lyxerr << "ERROR (GLayoutBox::set): layout not found! name: "
107 << target << std::endl;
111 void GLayoutBox::update()
115 LyXTextClass const & tc = getTextClass(owner_);
117 LyXTextClass::const_iterator it = tc.begin();
118 LyXTextClass::const_iterator const end = tc.end();
121 for (; it != end; ++it)
122 if ((*it)->obsoleted_by().empty()) {
123 Gtk::TreeModel::iterator iter = model_->append();
124 Gtk::TreeModel::Row row = *iter;
125 row[cols_.name] = Glib::locale_to_utf8((*it)->name());
129 // now that we've loaded something into the combobox, forget
130 // the initial fixed size and let GTK decide.
131 combo_.set_size_request(-1,-1);
135 void GLayoutBox::clear()
143 void GLayoutBox::open()
149 void GLayoutBox::setEnabled(bool sensitive)
151 combo_.set_sensitive(sensitive);
155 void GLayoutBox::selected()
160 Glib::ustring layoutGuiName = (*(combo_.get_active()))[cols_.name];
162 // we get two signal, one of them is empty and useless
163 if (layoutGuiName.empty())
166 layoutSelected(owner_, layoutGuiName);
169 } // namespace frontend
173 Toolbars::ToolbarPtr make_toolbar(ToolbarBackend::Toolbar const & tbb,
176 using lyx::frontend::GToolbar;
177 return Toolbars::ToolbarPtr(new GToolbar(tbb, owner));
183 GToolbar::GToolbar(ToolbarBackend::Toolbar const & tbb, LyXView & owner)
184 : owner_(dynamic_cast<GView &>(owner))
186 ToolbarBackend::item_iterator it = tbb.items.begin();
187 ToolbarBackend::item_iterator end = tbb.items.end();
188 for (; it != end; ++it)
189 add(it->first, it->second);
191 toolbar_.set_toolbar_style(Gtk::TOOLBAR_ICONS);
194 GView::Position const position = getPosition(tbb.flags);
196 if (position == GView::Left || position == GView::Right)
197 toolbar_.set_orientation(Gtk::ORIENTATION_VERTICAL);
199 owner_.getBox(position).children().push_back(
200 Gtk::Box_Helpers::Element(toolbar_, Gtk::PACK_SHRINK));
202 toolbar_.set_tooltips(true);
205 void GToolbar::add(FuncRequest const & func, string const & tooltip)
207 switch (func.action) {
208 case ToolbarBackend::SEPARATOR: {
209 Gtk::SeparatorToolItem * space =
210 Gtk::manage(new Gtk::SeparatorToolItem);
211 toolbar_.append(*space);
215 case ToolbarBackend::MINIBUFFER:
216 // Not supported yet.
219 case ToolbarBackend::LAYOUTS: {
220 layout_.reset(new GLayoutBox(owner_, toolbar_, func));
225 // choose an icon from the funcrequest
226 Gtk::BuiltinStockID stockID = getGTKStockIcon(func);
228 Glib::ustring tip = Glib::locale_to_utf8(tooltip);
230 Gtk::ToolButton * toolbutton;
231 if (stockID != Gtk::Stock::MISSING_IMAGE) {
232 // Prefer stock gtk graphics
233 Gtk::IconSize size(Gtk::ICON_SIZE_LARGE_TOOLBAR);
234 Gtk::Image * image = Gtk::manage(new Gtk::Image(stockID, size));
236 toolbutton = Gtk::manage(new Gtk::ToolButton(*image));
238 Glib::ustring xpmName =
239 Glib::locale_to_utf8(toolbarbackend.getIcon(func));
240 Gtk::Image * image = Gtk::manage(new Gtk::Image(xpmName));
242 toolbutton = Gtk::manage(new Gtk::ToolButton(*image));
245 // This code is putting a function reference into the GObject data field
246 // named gToolData. That's how we know how to update the status of the
248 toolbutton->set_data(gToolData,
249 reinterpret_cast<void*>(&const_cast<FuncRequest &>(func)));
251 toolbutton->set_tooltip(*toolbar_.get_tooltips_object(),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);
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