1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
|
#include <gtkmm.h>
#include "toolbar_manager.h"
#include "workbench/wb_command_ui.h"
#include "image_cache.h"
#include "linux_utilities/gtk_helpers.h"
class ColorComboColumns : public Gtk::TreeModel::ColumnRecord
{
public:
Gtk::TreeModelColumn<std::string> color;
Gtk::TreeModelColumn<Glib::RefPtr<Gdk::Pixbuf> > image;
ColorComboColumns()
{
add(color);
add(image);
}
};
static ColorComboColumns *color_combo_columns= 0;
static Gtk::ComboBox *create_color_combo(const std::vector<std::string> &colors, const std::string &selected)
{
int i= 0;
if (!color_combo_columns)
{
static ColorComboColumns color_combo_columns_instance;
color_combo_columns= &color_combo_columns_instance;
}
Glib::RefPtr<Gtk::ListStore> model= Gtk::ListStore::create(*color_combo_columns);
Gtk::ComboBox *combo= new Gtk::ComboBox(Glib::RefPtr<Gtk::TreeModel>(model));
combo->pack_start(color_combo_columns->image);
for (std::vector<std::string>::const_iterator iter= colors.begin();
iter != colors.end(); ++iter, ++i)
{
Gtk::TreeRow row= *model->append();
Glib::RefPtr<Gdk::Pixbuf> pixbuf;
Gdk::Color color(*iter);
combo->get_colormap()->alloc_color(color);
pixbuf= Gdk::Pixbuf::create(Gdk::COLORSPACE_RGB, false, 8, 16, 14);
pixbuf->fill(color.get_pixel()<<8);
row[color_combo_columns->color]= *iter;
row[color_combo_columns->image]= pixbuf;
if (selected == *iter)
combo->set_active(i);
}
return combo;
}
//------------------------------------------------------------------------------
ToolbarManager::ToolbarManager(wb::CommandUI *be)
: _be(be)
{}
//------------------------------------------------------------------------------
static Glib::RefPtr<Gdk::Pixbuf> crop_pixbuf(Glib::RefPtr<Gdk::Pixbuf> pixbuf, int x, int y)
{
return Gdk::Pixbuf::create_subpixbuf(pixbuf, x, y, pixbuf->get_width() - 2*x, pixbuf->get_height() - 2*y);
}
//------------------------------------------------------------------------------
void ToolbarManager::add_toolbar_item(Gtk::Box* toolbar, const bec::ToolbarItem& item, bool right)
{
static ImageCache *images = ImageCache::get_instance();
Gtk::Widget *w= 0;
switch ( item.type )
{
case bec::ToolbarAction:
{
Gtk::Image *img = new Gtk::Image(images->image(item.icon));
Gtk::Button *btn = new Gtk::Button();
btn->set_focus_on_click(false);
btn->set_border_width(0);
btn->add(*Gtk::manage(img));
btn->set_name(item.command);
#if GTK_VERSION_GT(2,10)
btn->set_tooltip_text(item.tooltip);
#endif
btn->set_relief(Gtk::RELIEF_NONE);
btn->show_all();
btn->signal_clicked().connect(sigc::bind(sigc::mem_fun(*this, &ToolbarManager::clicked), btn));
w= btn;
break;
}
case bec::ToolbarToggle:
{
Gtk::ToggleButton *btn;
if (item.icon != 0)
{
btn= new Gtk::ToggleButton();
btn->add(*Gtk::manage(new Gtk::Image(images->image(item.checked && item.alt_icon ? item.alt_icon : item.icon))));
}
else
btn= new Gtk::CheckButton(item.caption);
btn->set_name(item.command);
#if GTK_VERSION_GT(2,10)
btn->set_tooltip_text(item.tooltip);
#endif
btn->set_relief(Gtk::RELIEF_NONE);
btn->set_active(item.checked);
btn->show_all();
btn->signal_clicked().connect(sigc::bind(sigc::mem_fun(*this, &ToolbarManager::clicked), btn));
w= btn;
break;
}
case bec::ToolbarRadio:
{
Gtk::Image *img = new Gtk::Image(crop_pixbuf(images->image(item.icon), 1, 1));
Gtk::ToggleButton *btn = new Gtk::ToggleButton();
btn->add(*Gtk::manage(img));
btn->set_name(item.command);
btn->set_focus_on_click(false);
btn->set_border_width(0);
if (!item.caption.empty())
btn->set_label(item.caption);
#if GTK_VERSION_GT(2,10)
btn->set_tooltip_text(item.tooltip);
#endif
btn->set_relief(Gtk::RELIEF_NONE);
btn->set_active(item.checked);
btn->show_all();
btn->signal_clicked().connect(sigc::bind(sigc::mem_fun(*this, &ToolbarManager::clicked), btn));
w= btn;
break;
}
case bec::ToolbarCheck:
{
Gtk::CheckButton *btn = new Gtk::CheckButton(item.caption);
btn->set_name(item.command);
#if GTK_VERSION_GT(2,10)
btn->set_tooltip_text(item.tooltip);
#endif
btn->set_active(item.checked);
btn->show_all();
btn->signal_clicked().connect(sigc::bind(sigc::mem_fun(*this, &ToolbarManager::toggled), btn, item.name, item.command));
w= btn;
break;
}
case bec::ToolbarSeparator:
{
Gtk::Alignment *align= new Gtk::Alignment(0.5, 0.5);
Gtk::Separator *sep;
if (dynamic_cast<Gtk::HBox*>(toolbar))
sep= new Gtk::VSeparator();
else
sep= new Gtk::HSeparator();
align->set_padding(2, 1, 2, 1);
sep->show();
align->add(*Gtk::manage(sep));
w= align;
break;
}
case bec::ToolbarLabel:
{
Gtk::Label *label= new Gtk::Label(item.command, 0.0, 0.5);
label->set_markup("<small>"+item.command+"</small>");
w= label;
}
break;
case bec::ToolbarDropDown:
{
Gtk::Alignment *align= new Gtk::Alignment(0.5, 0.5, 0.0, 0.0);
std::vector<std::string> items;
std::string selected;
Gtk::ComboBox *combo;
//!!! items= _be->get_dropdown_items(item.name, item.command, selected);
if (items.size() > 0 && items[0][0] == '#')
{
combo= create_color_combo(items, selected);
combo->signal_changed().connect(sigc::bind(sigc::mem_fun(this, &ToolbarManager::color_combo_changed),
item.name, combo));
}
else
{
int i= 0, index= 0;
combo= new Gtk::ComboBoxText();
for (std::vector<std::string>::const_iterator iter= items.begin();
iter != items.end(); ++iter, ++i)
{
((Gtk::ComboBoxText*)combo)->append_text(*iter);
if (*iter == selected)
index= i;
combo->signal_changed().connect(sigc::bind(sigc::mem_fun(this, &ToolbarManager::combo_changed),
item.name, (Gtk::ComboBoxText*)combo));
}
combo->set_active(index);
}
combo->set_name(item.command);
align->add(*Gtk::manage(combo));
combo->show();
w= align;
}
break;
}
if (w)
{
w->set_sensitive(item.enabled);
w->show();
if (right)
toolbar->pack_end(*Gtk::manage(w), false, false);
else
toolbar->pack_start(*Gtk::manage(w), false, false);
}
}
//------------------------------------------------------------------------------
void ToolbarManager::rebuild_toolbar(Gtk::Box *toolbar, const std::vector<bec::ToolbarItem> &items)
{
toolbar->foreach(sigc::mem_fun(*toolbar, &Gtk::Container::remove));
std::vector<bec::ToolbarItem>::const_iterator cur_toolbar_item = items.begin();
for (; cur_toolbar_item != items.end(); ++cur_toolbar_item)
{
add_toolbar_item(toolbar, *cur_toolbar_item, g_str_has_suffix(cur_toolbar_item->name.c_str(), "right"));
}
toolbar->show_all_children();
}
//------------------------------------------------------------------------------
// This function is used to free toolbar action slot, when toolbar is deleted.
// The function is passed as DestoryNotify to toolbar->set_data, see below in
// ToolbarManager::rebuild_toolbar
static void deleteAllocatedCallback(gpointer data)
{
if (data)
{
ToolbarManager::CallbackSlot* cb = (ToolbarManager::CallbackSlot*)data;
delete cb;
}
}
//------------------------------------------------------------------------------
//static
void ToolbarManager::rebuild_toolbar(Gtk::Box *toolbar, const std::vector<bec::ToolbarItem> items,
const CustomItemSlot &create_item,
const CallbackSlot &cb)
{
static ImageCache *images = ImageCache::get_instance();
Gtk::Widget *w = 0;
// Allocate callback on heap, to store ptr in toolbar for later use
ToolbarManager::CallbackSlot* cb_wrapped = new ToolbarManager::CallbackSlot(cb);
toolbar->set_data("slot", cb_wrapped, &deleteAllocatedCallback);
toolbar->foreach(sigc::mem_fun(*toolbar, &Gtk::Container::remove));
bool right = false;
std::vector<bec::ToolbarItem>::const_iterator cur_toolbar_item = items.begin();
for (; cur_toolbar_item != items.end(); ++cur_toolbar_item)
{
const bec::ToolbarItem &item = *cur_toolbar_item;
if (create_item)
w = create_item(item);
else
w = 0;
if (!w)
{
//add_toolbar_item(toolbar, *cur_toolbar_item, g_str_has_suffix(cur_toolbar_item->name.c_str(), "right"), cb_wrapped);
switch (item.type)
{
case bec::ToolbarAction:
case bec::ToolbarCheck:
{
Gtk::Image *img = new Gtk::Image(images->image(item.checked ? item.alt_icon : item.icon));
Gtk::Button *btn = new Gtk::Button();
btn->set_focus_on_click(false);
btn->set_border_width(0);
btn->add(*Gtk::manage(img));
btn->set_name(item.command);
#if GTK_VERSION_GT(2,10)
btn->set_tooltip_text(item.tooltip);
#endif
btn->set_relief(Gtk::RELIEF_NONE);
btn->set_data("slot", cb_wrapped); // No need to add a DestroyNotiy callback here. It is handled in toolbar
btn->show_all();
btn->signal_clicked().connect(sigc::bind(sigc::ptr_fun(&ToolbarManager::clicked_to_slot), btn));
w= btn;
break;
}
case bec::ToolbarLabel:
{
Gtk::Label *label= new Gtk::Label(item.caption, 0.0, 0.5);
label->set_markup("<small>"+item.caption+"</small>");
w= label;
break;
}
case bec::ToolbarSeparator:
{
if (item.command == "expander")
right = true;
else
{
Gtk::Alignment *align= new Gtk::Alignment(0.5, 0.5);
Gtk::Separator *sep;
if (dynamic_cast<Gtk::HBox*>(toolbar))
sep= new Gtk::VSeparator();
else
sep= new Gtk::HSeparator();
align->set_padding(2, 1, 2, 1);
sep->show();
align->add(*Gtk::manage(sep));
w= align;
}
break;
}
default:
g_message("skipping toolbar item %s", item.command.c_str());
break;
}
}
if (w)
{
w->set_sensitive(item.enabled);
w->show();
if (right)
toolbar->pack_end(*Gtk::manage(w), false, false);
else
toolbar->pack_start(*Gtk::manage(w), false, false);
}
}
toolbar->show_all_children();
}
//------------------------------------------------------------------------------
void ToolbarManager::clicked(Gtk::Button *btn)
{
_be->activate_command(btn->get_name());
}
//------------------------------------------------------------------------------
void ToolbarManager::clicked_to_slot(Gtk::Button *btn)
{
ToolbarManager::CallbackSlot* cb = (ToolbarManager::CallbackSlot*)btn->get_data("slot");
if (cb)
(*cb)(btn->get_name());
}
void ToolbarManager::toggled(const Gtk::ToggleButton *btn, const std::string &name, const std::string &option)
{
//!!! _be->toggle_checkbox_item(name, option, btn->get_active());
}
void ToolbarManager::combo_changed(const std::string &toolbar, Gtk::ComboBoxText *combo)
{
if (combo->get_active_row_number() >= 0)
{
//!!! _be->select_dropdown_item(toolbar, combo->get_name(), combo->get_active_text());
}
}
void ToolbarManager::color_combo_changed(const std::string &toolbar, Gtk::ComboBox *combo)
{
if (combo->get_active_row_number() >= 0)
{
Gtk::TreeRow row= *combo->get_active();
//!!! _be->select_dropdown_item(toolbar, combo->get_name(), row[color_combo_columns->color]);
}
}
bool ToolbarManager::on_searchitem_key_release_event(GdkEventKey* event, Gtk::Entry* e)
{
if (event->keyval == GDK_Return || event->keyval == GDK_KP_Enter)
{
_search_text = e->get_text();
_be->activate_command("builtin:searchbox");
}
return false;
}
void ToolbarManager::focus_searchbox(Gtk::Box *toolbar)
{
Gtk::Entry *entry = reinterpret_cast<Gtk::Entry*>(toolbar->get_data("search_entry"));
if (entry)
entry->grab_focus();
}
|