1 /*
2 * A simple panel for layers
3 *
4 * Authors:
5 * Jon A. Cruz
6 *
7 * Copyright (C) 2006 Jon A. Cruz
8 *
9 * Released under GNU GPL, read the file 'COPYING' for more information
10 */
11 #ifdef HAVE_CONFIG_H
12 # include <config.h>
13 #endif
15 #include <glibmm/i18n.h>
17 #include <gtk/gtkstock.h>
18 #include <gtk/gtkmain.h>
20 #include "inkscape.h"
22 #include "layers-panel.h"
24 #include "layer-manager.h"
25 #include "layer-fns.h"
27 #include "verbs.h"
28 #include "helper/action.h"
30 #include "document.h"
31 #include "desktop.h"
32 #include "sp-object.h"
33 #include "sp-item.h"
34 #include "widgets/icon.h"
35 #include <gtkmm/widget.h>
36 #include "prefs-utils.h"
37 #include "xml/repr.h"
38 #include "svg/css-ostringstream.h"
39 #include "desktop-style.h"
41 //#define DUMP_LAYERS 1
43 namespace Inkscape {
44 namespace UI {
45 namespace Dialogs {
47 LayersPanel* LayersPanel::instance = 0;
49 LayersPanel& LayersPanel::getInstance()
50 {
51 if ( !instance ) {
52 instance = new LayersPanel();
53 }
55 return *instance;
56 }
58 enum {
59 COL_VISIBLE = 1,
60 COL_LOCKED
61 };
63 enum {
64 BUTTON_NEW = 0,
65 BUTTON_RENAME,
66 BUTTON_TOP,
67 BUTTON_BOTTOM,
68 BUTTON_UP,
69 BUTTON_DOWN,
70 // BUTTON_DUPLICATE,
71 BUTTON_DELETE
72 };
74 class ImageToggler : public Gtk::CellRendererPixbuf {
75 public:
76 ImageToggler( char const* on, char const* off) :
77 Glib::ObjectBase(typeid(ImageToggler)),
78 Gtk::CellRendererPixbuf(),
79 _pixOnName(on),
80 _pixOffName(off),
81 _property_active(*this, "active", false),
82 _property_activatable(*this, "activatable", true),
83 _property_pixbuf_on(*this, "pixbuf_on", Glib::RefPtr<Gdk::Pixbuf>(0)),
84 _property_pixbuf_off(*this, "pixbuf_off", Glib::RefPtr<Gdk::Pixbuf>(0))
85 {
86 property_mode() = Gtk::CELL_RENDERER_MODE_ACTIVATABLE;
88 Gtk::Widget* thingie = sp_icon_get_icon(_pixOnName.c_str(), Inkscape::ICON_SIZE_DECORATION);
89 if ( thingie ) {
90 if ( SP_IS_ICON(thingie->gobj()) ) {
91 SPIcon* icon = SP_ICON(thingie->gobj());
92 sp_icon_fetch_pixbuf( icon );
93 _property_pixbuf_on = Glib::wrap( icon->pb, true );
94 }
95 delete thingie;
96 }
97 thingie = sp_icon_get_icon(_pixOffName.c_str(), Inkscape::ICON_SIZE_DECORATION);
98 if ( thingie ) {
99 if ( SP_IS_ICON(thingie->gobj()) ) {
100 SPIcon* icon = SP_ICON(thingie->gobj());
101 sp_icon_fetch_pixbuf( icon );
102 _property_pixbuf_off = Glib::wrap( icon->pb, true );
103 }
104 delete thingie;
105 }
106 property_pixbuf() = _property_pixbuf_off.get_value();
107 }
109 sigc::signal<void, const Glib::ustring&> signal_toggled()
110 {
111 return _signal_toggled;
112 }
114 sigc::signal<void, GdkEvent const *> signal_pre_toggle()
115 {
116 return _signal_pre_toggle;
117 }
119 Glib::PropertyProxy<bool> property_active() { return _property_active.get_proxy(); }
120 Glib::PropertyProxy<bool> property_activatable() { return _property_activatable.get_proxy(); }
121 Glib::PropertyProxy< Glib::RefPtr<Gdk::Pixbuf> > property_pixbuf_on();
122 Glib::PropertyProxy< Glib::RefPtr<Gdk::Pixbuf> > property_pixbuf_off();
123 // virtual Glib::PropertyProxy_Base _property_renderable(); //override
125 protected:
127 virtual void get_size_vfunc( Gtk::Widget& widget,
128 const Gdk::Rectangle* cell_area,
129 int* x_offset,
130 int* y_offset,
131 int* width,
132 int* height ) const
133 {
134 Gtk::CellRendererPixbuf::get_size_vfunc( widget, cell_area, x_offset, y_offset, width, height );
136 if ( width ) {
137 *width += (*width) >> 1;
138 }
139 if ( height ) {
140 *height += (*height) >> 1;
141 }
142 }
145 virtual void render_vfunc( const Glib::RefPtr<Gdk::Drawable>& window,
146 Gtk::Widget& widget,
147 const Gdk::Rectangle& background_area,
148 const Gdk::Rectangle& cell_area,
149 const Gdk::Rectangle& expose_area,
150 Gtk::CellRendererState flags )
151 {
152 property_pixbuf() = _property_active.get_value() ? _property_pixbuf_on : _property_pixbuf_off;
153 Gtk::CellRendererPixbuf::render_vfunc( window, widget, background_area, cell_area, expose_area, flags );
154 }
156 virtual bool activate_vfunc(GdkEvent* event,
157 Gtk::Widget& widget,
158 const Glib::ustring& path,
159 const Gdk::Rectangle& background_area,
160 const Gdk::Rectangle& cell_area,
161 Gtk::CellRendererState flags)
162 {
163 _signal_pre_toggle.emit(event);
164 _signal_toggled.emit(path);
166 return false;
167 }
170 private:
171 Glib::ustring _pixOnName;
172 Glib::ustring _pixOffName;
174 Glib::Property<bool> _property_active;
175 Glib::Property<bool> _property_activatable;
176 Glib::Property< Glib::RefPtr<Gdk::Pixbuf> > _property_pixbuf_on;
177 Glib::Property< Glib::RefPtr<Gdk::Pixbuf> > _property_pixbuf_off;
179 sigc::signal<void, const Glib::ustring&> _signal_toggled;
180 sigc::signal<void, GdkEvent const *> _signal_pre_toggle;
181 };
183 class LayersPanel::InternalUIBounce
184 {
185 public:
186 int _actionCode;
187 SPObject* _target;
188 };
190 static gboolean layers_panel_activated( GtkObject *object, GdkEvent * /*event*/, gpointer data )
191 {
192 if ( data )
193 {
194 LayersPanel* panel = reinterpret_cast<LayersPanel*>(data);
195 panel->setDesktop( SP_ACTIVE_DESKTOP );
196 }
198 return FALSE;
199 }
202 void LayersPanel::_styleButton( Gtk::Button& btn, SPDesktop *desktop, unsigned int code, char const* iconName, char const* fallback )
203 {
204 bool set = false;
206 if ( iconName ) {
207 GtkWidget *child = sp_icon_new( Inkscape::ICON_SIZE_SMALL_TOOLBAR, iconName );
208 gtk_widget_show( child );
209 btn.add( *manage(Glib::wrap(child)) );
210 set = true;
211 }
213 if ( desktop ) {
214 Verb *verb = Verb::get( code );
215 if ( verb ) {
216 SPAction *action = verb->get_action(desktop);
217 if ( !set && action && action->image ) {
218 GtkWidget *child = sp_icon_new( Inkscape::ICON_SIZE_SMALL_TOOLBAR, action->image );
219 gtk_widget_show( child );
220 btn.add( *manage(Glib::wrap(child)) );
221 set = true;
222 }
224 if ( action && action->tip ) {
225 _tips.set_tip( btn, action->tip );
226 }
227 }
228 }
230 if ( !set && fallback ) {
231 btn.set_label( fallback );
232 }
233 }
236 Gtk::MenuItem& LayersPanel::_addPopupItem( SPDesktop *desktop, unsigned int code, char const* iconName, char const* fallback, int id )
237 {
238 GtkWidget* iconWidget = 0;
239 const char* label = 0;
241 if ( iconName ) {
242 iconWidget = sp_icon_new( Inkscape::ICON_SIZE_MENU, iconName );
243 }
245 if ( desktop ) {
246 Verb *verb = Verb::get( code );
247 if ( verb ) {
248 SPAction *action = verb->get_action(desktop);
249 if ( !iconWidget && action && action->image ) {
250 iconWidget = sp_icon_new( Inkscape::ICON_SIZE_MENU, action->image );
251 }
253 if ( action ) {
254 label = action->name;
255 }
256 }
257 }
259 if ( !label && fallback ) {
260 label = fallback;
261 }
263 Gtk::Widget* wrapped = 0;
264 if ( iconWidget ) {
265 wrapped = manage(Glib::wrap(iconWidget));
266 wrapped->show();
267 }
271 Gtk::Menu::MenuList& menulist = _popupMenu.items();
273 if ( wrapped ) {
274 menulist.push_back( Gtk::Menu_Helpers::ImageMenuElem( label, *wrapped, sigc::bind( sigc::mem_fun(*this, &LayersPanel::_takeAction), id)) );
275 } else {
276 menulist.push_back( Gtk::Menu_Helpers::MenuElem( label, sigc::bind( sigc::mem_fun(*this, &LayersPanel::_takeAction), id)) );
277 }
278 return menulist.back();
279 }
281 void LayersPanel::_fireAction( unsigned int code )
282 {
283 if ( _desktop ) {
284 Verb *verb = Verb::get( code );
285 if ( verb ) {
286 SPAction *action = verb->get_action(_desktop);
287 if ( action ) {
288 sp_action_perform( action, NULL );
289 // } else {
290 // g_message("no action");
291 }
292 // } else {
293 // g_message("no verb for %u", code);
294 }
295 // } else {
296 // g_message("no active desktop");
297 }
298 }
300 // SP_VERB_LAYER_NEXT,
301 // SP_VERB_LAYER_PREV,
302 void LayersPanel::_takeAction( int val )
303 {
304 if ( !_pending ) {
305 _pending = new InternalUIBounce();
306 _pending->_actionCode = val;
307 _pending->_target = _selectedLayer();
308 Glib::signal_timeout().connect( sigc::mem_fun(*this, &LayersPanel::_executeAction), 0 );
309 }
310 }
312 bool LayersPanel::_executeAction()
313 {
314 // Make sure selected layer hasn't changed since the action was triggered
315 if ( _pending
316 && (
317 (_pending->_actionCode == BUTTON_NEW)
318 || !( (_desktop && _desktop->currentLayer())
319 && (_desktop->currentLayer() != _pending->_target)
320 )
321 )
322 ) {
323 int val = _pending->_actionCode;
324 // SPObject* target = _pending->_target;
326 switch ( val ) {
327 case BUTTON_NEW:
328 {
329 _fireAction( SP_VERB_LAYER_NEW );
330 }
331 break;
332 case BUTTON_RENAME:
333 {
334 _fireAction( SP_VERB_LAYER_RENAME );
335 }
336 break;
337 case BUTTON_TOP:
338 {
339 _fireAction( SP_VERB_LAYER_TO_TOP );
340 }
341 break;
342 case BUTTON_BOTTOM:
343 {
344 _fireAction( SP_VERB_LAYER_TO_BOTTOM );
345 }
346 break;
347 case BUTTON_UP:
348 {
349 _fireAction( SP_VERB_LAYER_RAISE );
350 }
351 break;
352 case BUTTON_DOWN:
353 {
354 _fireAction( SP_VERB_LAYER_LOWER );
355 }
356 break;
357 case BUTTON_DELETE:
358 {
359 _fireAction( SP_VERB_LAYER_DELETE );
360 }
361 break;
362 }
364 delete _pending;
365 _pending = 0;
366 }
368 return false;
369 }
371 class LayersPanel::ModelColumns : public Gtk::TreeModel::ColumnRecord
372 {
373 public:
375 ModelColumns()
376 {
377 add(_colObject);
378 add(_colVisible);
379 add(_colLocked);
380 add(_colLabel);
381 }
382 virtual ~ModelColumns() {}
384 Gtk::TreeModelColumn<SPObject*> _colObject;
385 Gtk::TreeModelColumn<Glib::ustring> _colLabel;
386 Gtk::TreeModelColumn<bool> _colVisible;
387 Gtk::TreeModelColumn<bool> _colLocked;
388 };
390 void LayersPanel::_updateLayer( SPObject *layer ) {
391 _store->foreach( sigc::bind<SPObject*>(sigc::mem_fun(*this, &LayersPanel::_checkForUpdated), layer) );
392 }
394 bool LayersPanel::_checkForUpdated(const Gtk::TreePath &path, const Gtk::TreeIter& iter, SPObject* layer)
395 {
396 bool stopGoing = false;
397 Gtk::TreeModel::Row row = *iter;
398 Glib::ustring tmp = row[_model->_colLabel];
399 if ( layer == row[_model->_colObject] )
400 {
401 row[_model->_colLabel] = layer->label() ? layer->label() : SP_OBJECT_ID(layer);
402 row[_model->_colVisible] = SP_IS_ITEM(layer) ? !SP_ITEM(layer)->isHidden() : false;
403 row[_model->_colLocked] = SP_IS_ITEM(layer) ? SP_ITEM(layer)->isLocked() : false;
405 stopGoing = true;
406 }
408 return stopGoing;
409 }
411 void LayersPanel::_selectLayer( SPObject *layer ) {
412 if ( !layer || (_desktop && _desktop->doc() && (layer == _desktop->doc()->root)) ) {
413 if ( _tree.get_selection()->count_selected_rows() != 0 ) {
414 _tree.get_selection()->unselect_all();
415 }
416 } else {
417 _store->foreach( sigc::bind<SPObject*>(sigc::mem_fun(*this, &LayersPanel::_checkForSelected), layer) );
418 }
420 _checkTreeSelection();
421 }
423 bool LayersPanel::_checkForSelected(const Gtk::TreePath &path, const Gtk::TreeIter& iter, SPObject* layer)
424 {
425 bool stopGoing = false;
427 Gtk::TreeModel::Row row = *iter;
428 if ( layer == row[_model->_colObject] )
429 {
430 _tree.expand_to_path( path );
432 Glib::RefPtr<Gtk::TreeSelection> select = _tree.get_selection();
434 select->select(iter);
436 stopGoing = true;
437 }
439 return stopGoing;
440 }
442 void LayersPanel::_layersChanged()
443 {
444 // g_message("_layersChanged()");
445 SPDocument* document = _desktop->doc();
446 SPObject* root = document->root;
447 if ( root ) {
448 _selectedConnection.block();
449 if ( _mgr && _mgr->includes( root ) ) {
450 SPObject* target = _desktop->currentLayer();
451 _store->clear();
453 #if DUMP_LAYERS
454 g_message("root:%p {%s} [%s]", root, root->id, root->label() );
455 #endif // DUMP_LAYERS
456 _addLayer( document, root, 0, target, 0 );
457 }
458 _selectedConnection.unblock();
459 }
460 }
462 void LayersPanel::_addLayer( SPDocument* doc, SPObject* layer, Gtk::TreeModel::Row* parentRow, SPObject* target, int level )
463 {
464 if ( layer && (level < _maxNestDepth) ) {
465 unsigned int counter = _mgr->childCount(layer);
466 for ( unsigned int i = 0; i < counter; i++ ) {
467 SPObject *child = _mgr->nthChildOf(layer, i);
468 if ( child ) {
469 #if DUMP_LAYERS
470 g_message(" %3d layer:%p {%s} [%s]", level, child, child->id, child->label() );
471 #endif // DUMP_LAYERS
473 Gtk::TreeModel::iterator iter = parentRow ? _store->prepend(parentRow->children()) : _store->prepend();
474 Gtk::TreeModel::Row row = *iter;
475 row[_model->_colObject] = child;
476 row[_model->_colLabel] = child->label() ? child->label() : SP_OBJECT_ID(child);
477 row[_model->_colVisible] = SP_IS_ITEM(child) ? !SP_ITEM(child)->isHidden() : false;
478 row[_model->_colLocked] = SP_IS_ITEM(child) ? SP_ITEM(child)->isLocked() : false;
480 if ( target && child == target ) {
481 _tree.expand_to_path( _store->get_path(iter) );
483 Glib::RefPtr<Gtk::TreeSelection> select = _tree.get_selection();
484 select->select(iter);
486 _checkTreeSelection();
487 }
489 _addLayer( doc, child, &row, target, level + 1 );
490 }
491 }
492 }
493 }
495 SPObject* LayersPanel::_selectedLayer()
496 {
497 SPObject* obj = 0;
499 Gtk::TreeModel::iterator iter = _tree.get_selection()->get_selected();
500 if ( iter ) {
501 Gtk::TreeModel::Row row = *iter;
502 obj = row[_model->_colObject];
503 }
505 return obj;
506 }
508 void LayersPanel::_pushTreeSelectionToCurrent()
509 {
510 SPObject* inTree = _selectedLayer();
511 // TODO hunt down the possible API abuse in getting NULL
512 if ( _desktop->currentRoot() ) {
513 if ( inTree ) {
514 SPObject* curr = _desktop->currentLayer();
515 if ( curr != inTree ) {
516 _mgr->setCurrentLayer( inTree );
517 }
518 } else {
519 _mgr->setCurrentLayer( _desktop->doc()->root );
520 }
521 }
522 }
524 void LayersPanel::_checkTreeSelection()
525 {
526 bool sensitive = false;
527 bool sensitiveNonTop = false;
528 bool sensitiveNonBottom = false;
529 if ( _tree.get_selection()->count_selected_rows() > 0 ) {
530 sensitive = true;
532 SPObject* inTree = _selectedLayer();
533 if ( inTree ) {
535 sensitiveNonTop = (Inkscape::next_layer(inTree->parent, inTree) != 0);
536 sensitiveNonBottom = (Inkscape::previous_layer(inTree->parent, inTree) != 0);
538 if ( inTree->repr ) {
539 SPCSSAttr *css = sp_repr_css_attr(inTree->repr, "style");
540 if ( css ) {
541 _opacity.set_value( sp_repr_css_double_property( css, "opacity", 1.0 ) * 100 );
542 }
543 }
544 }
545 }
548 for ( std::vector<Gtk::Widget*>::iterator it = _watching.begin(); it != _watching.end(); ++it ) {
549 (*it)->set_sensitive( sensitive );
550 }
551 for ( std::vector<Gtk::Widget*>::iterator it = _watchingNonTop.begin(); it != _watchingNonTop.end(); ++it ) {
552 (*it)->set_sensitive( sensitiveNonTop );
553 }
554 for ( std::vector<Gtk::Widget*>::iterator it = _watchingNonBottom.begin(); it != _watchingNonBottom.end(); ++it ) {
555 (*it)->set_sensitive( sensitiveNonBottom );
556 }
557 }
559 void LayersPanel::_preToggle( GdkEvent const *event )
560 {
561 if ( _toggleEvent ) {
562 gdk_event_free(_toggleEvent);
563 _toggleEvent = 0;
564 }
566 if ( event && (event->type == GDK_BUTTON_PRESS) ) {
567 // Make a copy so we can keep it around.
568 _toggleEvent = gdk_event_copy(const_cast<GdkEvent*>(event));
569 }
570 }
572 void LayersPanel::_toggled( Glib::ustring const& str, int targetCol )
573 {
574 Gtk::TreeModel::Children::iterator iter = _tree.get_model()->get_iter(str);
575 Gtk::TreeModel::Row row = *iter;
577 Glib::ustring tmp = row[_model->_colLabel];
579 SPObject* obj = row[_model->_colObject];
580 SPItem* item = ( obj && SP_IS_ITEM(obj) ) ? SP_ITEM(obj) : 0;
581 if ( item ) {
582 switch ( targetCol ) {
583 case COL_VISIBLE:
584 {
585 bool newValue = !row[_model->_colVisible];
586 row[_model->_colVisible] = newValue;
587 item->setHidden( !newValue );
588 item->updateRepr();
589 sp_document_done( _desktop->doc() , SP_VERB_DIALOG_LAYERS,
590 newValue? _("Unhide layer") : _("Hide layer"));
591 }
592 break;
594 case COL_LOCKED:
595 {
596 bool newValue = !row[_model->_colLocked];
597 row[_model->_colLocked] = newValue;
598 item->setLocked( newValue );
599 item->updateRepr();
600 sp_document_done( _desktop->doc() , SP_VERB_DIALOG_LAYERS,
601 newValue? _("Lock layer") : _("Unlock layer"));
602 }
603 break;
604 }
605 }
606 }
608 void LayersPanel::_handleButtonEvent(GdkEventButton* evt)
609 {
610 // TODO - fix to a better is-popup function
611 if ( (evt->type == GDK_BUTTON_PRESS) && (evt->button == 3) ) {
614 {
615 Gtk::TreeModel::Path path;
616 Gtk::TreeViewColumn* col = 0;
617 int x = static_cast<int>(evt->x);
618 int y = static_cast<int>(evt->y);
619 int x2 = 0;
620 int y2 = 0;
621 if ( _tree.get_path_at_pos( x, y,
622 path, col,
623 x2, y2 ) ) {
624 _checkTreeSelection();
625 _popupMenu.popup(evt->button, evt->time);
626 }
627 }
629 }
630 }
632 void LayersPanel::_handleRowChange( Gtk::TreeModel::Path const& path, Gtk::TreeModel::iterator const& iter )
633 {
634 Gtk::TreeModel::Row row = *iter;
635 if ( row ) {
636 SPObject* obj = row[_model->_colObject];
637 if ( obj ) {
638 gchar const* oldLabel = obj->label();
639 Glib::ustring tmp = row[_model->_colLabel];
640 if ( oldLabel && oldLabel[0] && !tmp.empty() && (tmp != oldLabel) ) {
641 _mgr->renameLayer( obj, tmp.c_str() );
642 row[_model->_colLabel] = obj->label();
643 }
644 }
645 }
646 }
648 bool LayersPanel::_rowSelectFunction( Glib::RefPtr<Gtk::TreeModel> const & model, Gtk::TreeModel::Path const & path, bool currentlySelected )
649 {
650 bool val = true;
651 if ( !currentlySelected && _toggleEvent )
652 {
653 GdkEvent* event = gtk_get_current_event();
654 if ( event ) {
655 // (keep these checks separate, so we know when to call gdk_event_free()
656 if ( event->type == GDK_BUTTON_PRESS ) {
657 GdkEventButton const* target = reinterpret_cast<GdkEventButton const*>(_toggleEvent);
658 GdkEventButton const* evtb = reinterpret_cast<GdkEventButton const*>(event);
660 if ( (evtb->window == target->window)
661 && (evtb->send_event == target->send_event)
662 && (evtb->time == target->time)
663 && (evtb->state == target->state)
664 )
665 {
666 // Ooooh! It's a magic one
667 val = false;
668 }
669 }
670 gdk_event_free(event);
671 }
672 }
673 return val;
674 }
677 void LayersPanel::_opacityChanged()
678 {
679 SPObject* layer = _selectedLayer();
681 if ( _desktop && layer && !_opacityConnection.blocked() ) {
682 _opacityConnection.block();
685 Gtk::Adjustment* adj = _opacity.get_adjustment();
686 SPCSSAttr *css = sp_repr_css_attr_new();
688 Inkscape::CSSOStringStream os;
689 os << CLAMP( adj->get_value() / 100, 0.0, 1.0 );
690 sp_repr_css_set_property( css, "opacity", os.str().c_str() );
692 sp_desktop_apply_css_recursive( layer, css, true );
693 layer->updateRepr();
695 sp_repr_css_attr_unref( css );
697 sp_document_maybe_done( _desktop->doc(), "layers:opacity", SP_VERB_DIALOG_LAYERS,
698 _("Change layer opacity"));
700 _opacityConnection.unblock();
701 }
702 }
706 /**
707 * Constructor
708 */
709 LayersPanel::LayersPanel() :
710 Inkscape::UI::Widget::Panel( Glib::ustring(), "dialogs.layers" ),
711 _maxNestDepth(20),
712 _mgr(0),
713 _desktop(0),
714 _model(0),
715 _pending(0),
716 _toggleEvent(0)
717 {
718 _maxNestDepth = prefs_get_int_attribute_limited("dialogs.layers", "maxDepth", 20, 1, 1000);
720 ModelColumns *zoop = new ModelColumns();
721 _model = zoop;
723 _store = Gtk::TreeStore::create( *zoop );
725 _tree.set_model( _store );
726 _tree.set_headers_visible(false);
728 ImageToggler* eyeRenderer = manage( new ImageToggler("visible", "hidden") );
729 int visibleColNum = _tree.append_column("vis", *eyeRenderer) - 1;
730 eyeRenderer->signal_pre_toggle().connect( sigc::mem_fun(*this, &LayersPanel::_preToggle) );
731 eyeRenderer->signal_toggled().connect( sigc::bind( sigc::mem_fun(*this, &LayersPanel::_toggled), (int)COL_VISIBLE) );
732 eyeRenderer->property_activatable() = true;
733 Gtk::TreeViewColumn* col = _tree.get_column(visibleColNum);
734 if ( col ) {
735 col->add_attribute( eyeRenderer->property_active(), _model->_colVisible );
736 }
738 ImageToggler * renderer = manage( new ImageToggler("width_height_lock", "lock_unlocked") );
739 int lockedColNum = _tree.append_column("lock", *renderer) - 1;
740 renderer->signal_pre_toggle().connect( sigc::mem_fun(*this, &LayersPanel::_preToggle) );
741 renderer->signal_toggled().connect( sigc::bind( sigc::mem_fun(*this, &LayersPanel::_toggled), (int)COL_LOCKED) );
742 renderer->property_activatable() = true;
743 col = _tree.get_column(lockedColNum);
744 if ( col ) {
745 col->add_attribute( renderer->property_active(), _model->_colLocked );
746 }
748 int nameColNum = _tree.append_column_editable("Name", _model->_colLabel) - 1;
750 _tree.set_expander_column( *_tree.get_column(nameColNum) );
753 _selectedConnection = _tree.get_selection()->signal_changed().connect( sigc::mem_fun(*this, &LayersPanel::_pushTreeSelectionToCurrent) );
754 _tree.get_selection()->set_select_function( sigc::mem_fun(*this, &LayersPanel::_rowSelectFunction) );
756 _tree.get_model()->signal_row_changed().connect( sigc::mem_fun(*this, &LayersPanel::_handleRowChange) );
757 _tree.signal_button_press_event().connect_notify( sigc::mem_fun(*this, &LayersPanel::_handleButtonEvent) );
759 _scroller.add( _tree );
760 _scroller.set_policy( Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC );
763 _opacityBox.pack_start( *manage( new Gtk::Label(_("Opacity, %:"))), Gtk::PACK_SHRINK );
765 _opacity.set_draw_value(false);
766 _opacity.set_value(100.0);
767 _opacity.set_range(0.0, 100.0);
768 _opacity.set_increments(1, 10);
769 _opacityBox.pack_start( _opacity, Gtk::PACK_EXPAND_WIDGET );
771 _spinBtn.configure(*_opacity.get_adjustment(), 0, 1);
773 _spinBtn.set_width_chars(5);
774 _opacityBox.pack_end( _spinBtn, Gtk::PACK_SHRINK );
775 _watching.push_back( &_opacityBox );
777 _getContents()->pack_start( _scroller, Gtk::PACK_EXPAND_WIDGET );
779 _getContents()->pack_end(_opacityBox, Gtk::PACK_SHRINK);
780 _getContents()->pack_end(_buttonsRow, Gtk::PACK_SHRINK);
782 _opacityConnection = _opacity.get_adjustment()->signal_value_changed().connect( sigc::mem_fun(*this, &LayersPanel::_opacityChanged) );
784 SPDesktop* targetDesktop = SP_ACTIVE_DESKTOP;
786 _buttonsRow.set_child_min_width( 16 );
788 Gtk::Button* btn = manage( new Gtk::Button() );
789 _styleButton( *btn, targetDesktop, SP_VERB_LAYER_NEW, GTK_STOCK_ADD, _("New") );
790 btn->signal_clicked().connect( sigc::bind( sigc::mem_fun(*this, &LayersPanel::_takeAction), (int)BUTTON_NEW) );
791 _buttonsRow.add( *btn );
793 btn = manage( new Gtk::Button() );
794 _styleButton( *btn, targetDesktop, SP_VERB_LAYER_TO_TOP, GTK_STOCK_GOTO_TOP, _("Top") );
795 btn->signal_clicked().connect( sigc::bind( sigc::mem_fun(*this, &LayersPanel::_takeAction), (int)BUTTON_TOP) );
796 _watchingNonTop.push_back( btn );
797 _buttonsRow.add( *btn );
799 btn = manage( new Gtk::Button() );
800 _styleButton( *btn, targetDesktop, SP_VERB_LAYER_RAISE, GTK_STOCK_GO_UP, _("Up") );
801 btn->signal_clicked().connect( sigc::bind( sigc::mem_fun(*this, &LayersPanel::_takeAction), (int)BUTTON_UP) );
802 _watchingNonTop.push_back( btn );
803 _buttonsRow.add( *btn );
805 btn = manage( new Gtk::Button() );
806 _styleButton( *btn, targetDesktop, SP_VERB_LAYER_LOWER, GTK_STOCK_GO_DOWN, _("Dn") );
807 btn->signal_clicked().connect( sigc::bind( sigc::mem_fun(*this, &LayersPanel::_takeAction), (int)BUTTON_DOWN) );
808 _watchingNonBottom.push_back( btn );
809 _buttonsRow.add( *btn );
811 btn = manage( new Gtk::Button() );
812 _styleButton( *btn, targetDesktop, SP_VERB_LAYER_TO_BOTTOM, GTK_STOCK_GOTO_BOTTOM, _("Bot") );
813 btn->signal_clicked().connect( sigc::bind( sigc::mem_fun(*this, &LayersPanel::_takeAction), (int)BUTTON_BOTTOM) );
814 _watchingNonBottom.push_back( btn );
815 _buttonsRow.add( *btn );
817 // btn = manage( new Gtk::Button("Dup") );
818 // btn->signal_clicked().connect( sigc::bind( sigc::mem_fun(*this, &LayersPanel::_takeAction), (int)BUTTON_DUPLICATE) );
819 // _buttonsRow.add( *btn );
821 btn = manage( new Gtk::Button() );
822 _styleButton( *btn, targetDesktop, SP_VERB_LAYER_DELETE, GTK_STOCK_REMOVE, _("X") );
823 btn->signal_clicked().connect( sigc::bind( sigc::mem_fun(*this, &LayersPanel::_takeAction), (int)BUTTON_DELETE) );
824 _watching.push_back( btn );
825 _buttonsRow.add( *btn );
830 // -------------------------------------------------------
831 {
832 _watching.push_back( &_addPopupItem( targetDesktop, SP_VERB_LAYER_RENAME, 0, "Rename", (int)BUTTON_RENAME ) );
833 _watching.push_back( &_addPopupItem( targetDesktop, SP_VERB_LAYER_NEW, 0, "New", (int)BUTTON_NEW ) );
835 _popupMenu.items().push_back( Gtk::Menu_Helpers::SeparatorElem() );
837 _watchingNonTop.push_back( &_addPopupItem( targetDesktop, SP_VERB_LAYER_RAISE, GTK_STOCK_GO_UP, "Up", (int)BUTTON_UP ) );
838 _watchingNonBottom.push_back( &_addPopupItem( targetDesktop, SP_VERB_LAYER_LOWER, GTK_STOCK_GO_DOWN, "Down", (int)BUTTON_DOWN ) );
840 _popupMenu.show_all_children();
841 }
842 // -------------------------------------------------------
846 for ( std::vector<Gtk::Widget*>::iterator it = _watching.begin(); it != _watching.end(); ++it ) {
847 (*it)->set_sensitive( false );
848 }
849 for ( std::vector<Gtk::Widget*>::iterator it = _watchingNonTop.begin(); it != _watchingNonTop.end(); ++it ) {
850 (*it)->set_sensitive( false );
851 }
852 for ( std::vector<Gtk::Widget*>::iterator it = _watchingNonBottom.begin(); it != _watchingNonBottom.end(); ++it ) {
853 (*it)->set_sensitive( false );
854 }
856 g_signal_connect( G_OBJECT(INKSCAPE), "activate_desktop", G_CALLBACK( layers_panel_activated ), this );
859 setDesktop( targetDesktop );
863 show_all_children();
865 restorePanelPrefs();
866 }
868 LayersPanel::~LayersPanel()
869 {
870 if ( _model )
871 {
872 delete _model;
873 }
875 if ( _toggleEvent )
876 {
877 gdk_event_free( _toggleEvent );
878 _toggleEvent = 0;
879 }
880 }
883 void LayersPanel::setDesktop( SPDesktop* desktop )
884 {
885 if ( desktop != _desktop ) {
886 _layerChangedConnection.disconnect();
887 _layerUpdatedConnection.disconnect();
888 _changedConnection.disconnect();
889 if ( _mgr ) {
890 _mgr = 0;
891 }
892 if ( _desktop ) {
893 _desktop = 0;
894 }
896 _desktop = SP_ACTIVE_DESKTOP;
897 if ( _desktop ) {
898 setLabel( _desktop->doc()->name );
900 _mgr = _desktop->layer_manager;
901 if ( _mgr ) {
902 _layerChangedConnection = _mgr->connectCurrentLayerChanged( sigc::mem_fun(*this, &LayersPanel::_selectLayer) );
903 _layerUpdatedConnection = _mgr->connectLayerDetailsChanged( sigc::mem_fun(*this, &LayersPanel::_updateLayer) );
904 _changedConnection = _mgr->connectChanged( sigc::mem_fun(*this, &LayersPanel::_layersChanged) );
905 }
907 _layersChanged();
908 }
909 }
910 /*
911 GSList const *layers=sp_document_get_resource_list( _desktop->doc(), "layer" );
912 g_message( "layers list starts at %p", layers );
913 for ( GSList const *iter=layers ; iter ; iter = iter->next ) {
914 SPObject *layer=static_cast<SPObject *>(iter->data);
915 g_message(" {%s} [%s]", layer->id, layer->label() );
916 }
917 */
918 }
922 } //namespace Dialogs
923 } //namespace UI
924 } //namespace Inkscape
927 /*
928 Local Variables:
929 mode:c++
930 c-file-style:"stroustrup"
931 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
932 indent-tabs-mode:nil
933 fill-column:99
934 End:
935 */
936 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :