Code

Option to keep selection when changing layers
[inkscape.git] / src / dialogs / layers-panel.cpp
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"
38 //#define DUMP_LAYERS 1
40 namespace Inkscape {
41 namespace UI {
42 namespace Dialogs {
44 LayersPanel* LayersPanel::instance = 0;
46 LayersPanel& LayersPanel::getInstance()
47 {
48     if ( !instance ) {
49         instance = new LayersPanel();
50     }
52     return *instance;
53 }
55 enum {
56     COL_VISIBLE = 1,
57     COL_LOCKED
58 };
60 enum {
61     BUTTON_NEW = 0,
62     BUTTON_RENAME,
63     BUTTON_TOP,
64     BUTTON_BOTTOM,
65     BUTTON_UP,
66     BUTTON_DOWN,
67 //    BUTTON_DUPLICATE,
68     BUTTON_DELETE
69 };
71 class ImageToggler : public Gtk::CellRendererPixbuf {
72 public:
73     ImageToggler( char const* on, char const* off) :
74         Glib::ObjectBase(typeid(ImageToggler)),
75         Gtk::CellRendererPixbuf(),
76         _pixOnName(on),
77         _pixOffName(off),
78         _property_active(*this, "active", false),
79         _property_activatable(*this, "activatable", true),
80         _property_pixbuf_on(*this, "pixbuf_on", Glib::RefPtr<Gdk::Pixbuf>(0)),
81         _property_pixbuf_off(*this, "pixbuf_off", Glib::RefPtr<Gdk::Pixbuf>(0))
82     {
83         property_mode() = Gtk::CELL_RENDERER_MODE_ACTIVATABLE;
85         Gtk::Widget* thingie = sp_icon_get_icon(_pixOnName.c_str(), Inkscape::ICON_SIZE_DECORATION);
86         if ( thingie ) {
87             if ( SP_IS_ICON(thingie->gobj()) ) {
88                 SPIcon* icon = SP_ICON(thingie->gobj());
89                 sp_icon_fetch_pixbuf( icon );
90                 _property_pixbuf_on = Glib::wrap( icon->pb, true );
91             }
92             delete thingie;
93         }
94         thingie = sp_icon_get_icon(_pixOffName.c_str(), Inkscape::ICON_SIZE_DECORATION);
95         if ( thingie ) {
96             if ( SP_IS_ICON(thingie->gobj()) ) {
97                 SPIcon* icon = SP_ICON(thingie->gobj());
98                 sp_icon_fetch_pixbuf( icon );
99                 _property_pixbuf_off = Glib::wrap( icon->pb, true );
100             }
101             delete thingie;
102         }
103         property_pixbuf() = _property_pixbuf_off.get_value();
104     }
106     sigc::signal<void, const Glib::ustring&> signal_toggled()
107     {
108         return _signal_toggled;
109     }
111     sigc::signal<void, GdkEvent const *> signal_pre_toggle()
112     {
113         return _signal_pre_toggle;
114     }
116     Glib::PropertyProxy<bool> property_active() { return _property_active.get_proxy(); }
117     Glib::PropertyProxy<bool> property_activatable() { return _property_activatable.get_proxy(); }
118     Glib::PropertyProxy< Glib::RefPtr<Gdk::Pixbuf> > property_pixbuf_on();
119     Glib::PropertyProxy< Glib::RefPtr<Gdk::Pixbuf> > property_pixbuf_off();
120 //  virtual Glib::PropertyProxy_Base _property_renderable(); //override
122 protected:
124     virtual void get_size_vfunc( Gtk::Widget& widget,
125                                  const Gdk::Rectangle* cell_area,
126                                  int* x_offset,
127                                  int* y_offset,
128                                  int* width,
129                                  int* height ) const
130     {
131         Gtk::CellRendererPixbuf::get_size_vfunc( widget, cell_area, x_offset, y_offset, width, height );
133         if ( width ) {
134             *width += (*width) >> 1;
135         }
136         if ( height ) {
137             *height += (*height) >> 1;
138         }
139     }
142     virtual void render_vfunc( const Glib::RefPtr<Gdk::Drawable>& window,
143                                Gtk::Widget& widget,
144                                const Gdk::Rectangle& background_area,
145                                const Gdk::Rectangle& cell_area,
146                                const Gdk::Rectangle& expose_area,
147                                Gtk::CellRendererState flags )
148     {
149         property_pixbuf() = _property_active.get_value() ? _property_pixbuf_on : _property_pixbuf_off;
150         Gtk::CellRendererPixbuf::render_vfunc( window, widget, background_area, cell_area, expose_area, flags );
151     }
153     virtual bool activate_vfunc(GdkEvent* event,
154                                 Gtk::Widget& widget,
155                                 const Glib::ustring& path,
156                                 const Gdk::Rectangle& background_area,
157                                 const Gdk::Rectangle& cell_area,
158                                 Gtk::CellRendererState flags)
159     {
160         _signal_pre_toggle.emit(event);
161         _signal_toggled.emit(path);
163         return false;
164     }
167 private:
168     Glib::ustring _pixOnName;
169     Glib::ustring _pixOffName;
171     Glib::Property<bool> _property_active;
172     Glib::Property<bool> _property_activatable;
173     Glib::Property< Glib::RefPtr<Gdk::Pixbuf> > _property_pixbuf_on;
174     Glib::Property< Glib::RefPtr<Gdk::Pixbuf> > _property_pixbuf_off;
176     sigc::signal<void, const Glib::ustring&> _signal_toggled;
177     sigc::signal<void, GdkEvent const *> _signal_pre_toggle;
178 };
180 class LayersPanel::InternalUIBounce
182 public:
183     int _actionCode;
184     SPObject* _target;
185 };
187 static gboolean layers_panel_activated( GtkObject *object, GdkEvent * /*event*/, gpointer data )
189     if ( data )
190     {
191         LayersPanel* panel = reinterpret_cast<LayersPanel*>(data);
192         panel->setDesktop( SP_ACTIVE_DESKTOP );
193     }
195     return FALSE;
199 void LayersPanel::_styleButton( Gtk::Button& btn, SPDesktop *desktop, unsigned int code, char const* iconName, char const* fallback )
201     bool set = false;
203     if ( iconName ) {
204         GtkWidget *child = sp_icon_new( Inkscape::ICON_SIZE_SMALL_TOOLBAR, iconName );
205         gtk_widget_show( child );
206         btn.add( *manage(Glib::wrap(child)) );
207         set = true;
208     }
210     if ( desktop ) {
211         Verb *verb = Verb::get( code );
212         if ( verb ) {
213             SPAction *action = verb->get_action(desktop);
214             if ( !set && action && action->image ) {
215                 GtkWidget *child = sp_icon_new( Inkscape::ICON_SIZE_SMALL_TOOLBAR, action->image );
216                 gtk_widget_show( child );
217                 btn.add( *manage(Glib::wrap(child)) );
218                 set = true;
219             }
221             if ( action && action->tip ) {
222                 _tips.set_tip( btn, action->tip );
223             }
224         }
225     }
227     if ( !set && fallback ) {
228         btn.set_label( fallback );
229     }
233 Gtk::MenuItem& LayersPanel::_addPopupItem( SPDesktop *desktop, unsigned int code, char const* iconName, char const* fallback, int id )
235     GtkWidget* iconWidget = 0;
236     const char* label = 0;
238     if ( iconName ) {
239         iconWidget = sp_icon_new( Inkscape::ICON_SIZE_MENU, iconName );
240     }
242     if ( desktop ) {
243         Verb *verb = Verb::get( code );
244         if ( verb ) {
245             SPAction *action = verb->get_action(desktop);
246             if ( !iconWidget && action && action->image ) {
247                 iconWidget = sp_icon_new( Inkscape::ICON_SIZE_MENU, action->image );
248             }
250             if ( action ) {
251                 label = action->name;
252             }
253         }
254     }
256     if ( !label && fallback ) {
257         label = fallback;
258     }
260     Gtk::Widget* wrapped = 0;
261     if ( iconWidget ) {
262         wrapped = manage(Glib::wrap(iconWidget));
263         wrapped->show();
264     }
268     Gtk::Menu::MenuList& menulist = _popupMenu.items();
270     if ( wrapped ) {
271         menulist.push_back( Gtk::Menu_Helpers::ImageMenuElem( label, *wrapped, sigc::bind( sigc::mem_fun(*this, &LayersPanel::_takeAction), id)) );
272     } else {
273         menulist.push_back( Gtk::Menu_Helpers::MenuElem( label, sigc::bind( sigc::mem_fun(*this, &LayersPanel::_takeAction), id)) );
274     }
275     return menulist.back();
278 void LayersPanel::_fireAction( unsigned int code )
280     if ( _desktop ) {
281         Verb *verb = Verb::get( code );
282         if ( verb ) {
283             SPAction *action = verb->get_action(_desktop);
284             if ( action ) {
285                 sp_action_perform( action, NULL );
286 //             } else {
287 //                 g_message("no action");
288             }
289 //         } else {
290 //             g_message("no verb for %u", code);
291         }
292 //     } else {
293 //         g_message("no active desktop");
294     }
297 //     SP_VERB_LAYER_NEXT,
298 //     SP_VERB_LAYER_PREV,
299 void LayersPanel::_takeAction( int val )
301     if ( !_pending ) {
302         _pending = new InternalUIBounce();
303         _pending->_actionCode = val;
304         _pending->_target = _selectedLayer();
305         Glib::signal_timeout().connect( sigc::mem_fun(*this, &LayersPanel::_executeAction), 0 );
306     }
309 bool LayersPanel::_executeAction()
311     // Make sure selected layer hasn't changed since the action was triggered
312     if ( _pending
313          && !( (_desktop && _desktop->currentLayer())
314                && (_desktop->currentLayer() != _pending->_target)
315              ) ) {
316         int val = _pending->_actionCode;
317 //        SPObject* target = _pending->_target;
319         switch ( val ) {
320             case BUTTON_NEW:
321             {
322                 _fireAction( SP_VERB_LAYER_NEW );
323             }
324             break;
325             case BUTTON_RENAME:
326             {
327                 _fireAction( SP_VERB_LAYER_RENAME );
328             }
329             break;
330             case BUTTON_TOP:
331             {
332                 _fireAction( SP_VERB_LAYER_TO_TOP );
333             }
334             break;
335             case BUTTON_BOTTOM:
336             {
337                 _fireAction( SP_VERB_LAYER_TO_BOTTOM );
338             }
339             break;
340             case BUTTON_UP:
341             {
342                 _fireAction( SP_VERB_LAYER_RAISE );
343             }
344             break;
345             case BUTTON_DOWN:
346             {
347                 _fireAction( SP_VERB_LAYER_LOWER );
348             }
349             break;
350             case BUTTON_DELETE:
351             {
352                 _fireAction( SP_VERB_LAYER_DELETE );
353             }
354             break;
355         }
357         delete _pending;
358         _pending = 0;
359     }
361     return false;
364 class LayersPanel::ModelColumns : public Gtk::TreeModel::ColumnRecord
366 public:
368     ModelColumns()
369     {
370         add(_colObject);
371         add(_colVisible);
372         add(_colLocked);
373         add(_colLabel);
374     }
375     virtual ~ModelColumns() {}
377     Gtk::TreeModelColumn<SPObject*> _colObject;
378     Gtk::TreeModelColumn<Glib::ustring> _colLabel;
379     Gtk::TreeModelColumn<bool> _colVisible;
380     Gtk::TreeModelColumn<bool> _colLocked;
381 };
383 void LayersPanel::_updateLayer( SPObject *layer ) {
384     _store->foreach( sigc::bind<SPObject*>(sigc::mem_fun(*this, &LayersPanel::_checkForUpdated), layer) );
387 bool LayersPanel::_checkForUpdated(const Gtk::TreePath &path, const Gtk::TreeIter& iter, SPObject* layer)
389     bool stopGoing = false;
390     Gtk::TreeModel::Row row = *iter;
391     Glib::ustring tmp = row[_model->_colLabel];
392     if ( layer == row[_model->_colObject] )
393     {
394         row[_model->_colLabel] = layer->label() ? layer->label() : SP_OBJECT_ID(layer);
395         row[_model->_colVisible] = SP_IS_ITEM(layer) ? !SP_ITEM(layer)->isHidden() : false;
396         row[_model->_colLocked] = SP_IS_ITEM(layer) ? SP_ITEM(layer)->isLocked() : false;
398         stopGoing = true;
399     }
401     return stopGoing;
404 void LayersPanel::_selectLayer( SPObject *layer ) {
405     if ( !layer || (_desktop && _desktop->doc() && (layer == _desktop->doc()->root)) ) {
406         if ( _tree.get_selection()->count_selected_rows() != 0 ) {
407             _tree.get_selection()->unselect_all();
408         }
409     } else {
410         _store->foreach( sigc::bind<SPObject*>(sigc::mem_fun(*this, &LayersPanel::_checkForSelected), layer) );
411     }
413     _checkTreeSelection();
416 bool LayersPanel::_checkForSelected(const Gtk::TreePath &path, const Gtk::TreeIter& iter, SPObject* layer)
418     bool stopGoing = false;
420     Gtk::TreeModel::Row row = *iter;
421     if ( layer == row[_model->_colObject] )
422     {
423         _tree.expand_to_path( path );
425         Glib::RefPtr<Gtk::TreeSelection> select = _tree.get_selection();
427         select->select(iter);
429         stopGoing = true;
430     }
432     return stopGoing;
435 void LayersPanel::_layersChanged()
437 //    g_message("_layersChanged()");
438     SPDocument* document = _desktop->doc();
439     SPObject* root = document->root;
440     if ( root ) {
441         if ( _mgr && _mgr->includes( root ) ) {
442             SPObject* target = _desktop->currentLayer();
443             _store->clear();
445 #if DUMP_LAYERS
446             g_message("root:%p  {%s}   [%s]", root, root->id, root->label() );
447 #endif // DUMP_LAYERS
448             _addLayer( document, root, 0, target, 0 );
449         }
450     }
453 void LayersPanel::_addLayer( SPDocument* doc, SPObject* layer, Gtk::TreeModel::Row* parentRow, SPObject* target, int level )
455     if ( layer && (level < _maxNestDepth) ) {
456         unsigned int counter = _mgr->childCount(layer);
457         for ( unsigned int i = 0; i < counter; i++ ) {
458             SPObject *child = _mgr->nthChildOf(layer, i);
459             if ( child ) {
460 #if DUMP_LAYERS
461                 g_message(" %3d    layer:%p  {%s}   [%s]", level, child, child->id, child->label() );
462 #endif // DUMP_LAYERS
464                 Gtk::TreeModel::iterator iter = parentRow ? _store->prepend(parentRow->children()) : _store->prepend();
465                 Gtk::TreeModel::Row row = *iter;
466                 row[_model->_colObject] = child;
467                 row[_model->_colLabel] = child->label() ? child->label() : SP_OBJECT_ID(child);
468                 row[_model->_colVisible] = SP_IS_ITEM(child) ? !SP_ITEM(child)->isHidden() : false;
469                 row[_model->_colLocked] = SP_IS_ITEM(child) ? SP_ITEM(child)->isLocked() : false;
471                 if ( target && child == target ) {
472                     _tree.expand_to_path( _store->get_path(iter) );
474                     Glib::RefPtr<Gtk::TreeSelection> select = _tree.get_selection();
475                     select->select(iter);
477                     _checkTreeSelection();
478                 }
480                 _addLayer( doc, child, &row, target, level + 1 );
481             }
482         }
483     }
486 SPObject* LayersPanel::_selectedLayer()
488     SPObject* obj = 0;
490     Gtk::TreeModel::iterator iter = _tree.get_selection()->get_selected();
491     if ( iter ) {
492         Gtk::TreeModel::Row row = *iter;
493         obj = row[_model->_colObject];
494     }
496     return obj;
499 void LayersPanel::_pushTreeSelectionToCurrent()
501     SPObject* inTree = _selectedLayer();
502     if ( inTree ) {
503         SPObject* curr = _desktop->currentLayer();
504         if ( curr != inTree ) {
505             _mgr->setCurrentLayer( inTree );
506         }
507     } else {
508         _mgr->setCurrentLayer( _desktop->doc()->root );
509     }
512 void LayersPanel::_checkTreeSelection()
514     bool sensitive = false;
515     bool sensitiveNonTop = false;
516     bool sensitiveNonBottom = false;
517     if ( _tree.get_selection()->count_selected_rows() > 0 ) {
518         sensitive = true;
520         SPObject* inTree = _selectedLayer();
521         if ( inTree ) {
523             sensitiveNonTop = (Inkscape::next_layer(inTree->parent, inTree) != 0);
524             sensitiveNonBottom = (Inkscape::previous_layer(inTree->parent, inTree) != 0);
525         }
526     }
528     for ( std::vector<Gtk::Widget*>::iterator it = _watching.begin(); it != _watching.end(); ++it ) {
529         (*it)->set_sensitive( sensitive );
530     }
531     for ( std::vector<Gtk::Widget*>::iterator it = _watchingNonTop.begin(); it != _watchingNonTop.end(); ++it ) {
532         (*it)->set_sensitive( sensitiveNonTop );
533     }
534     for ( std::vector<Gtk::Widget*>::iterator it = _watchingNonBottom.begin(); it != _watchingNonBottom.end(); ++it ) {
535         (*it)->set_sensitive( sensitiveNonBottom );
536     }
539 void LayersPanel::_preToggle( GdkEvent const *event )
541     if ( _toggleEvent ) {
542         gdk_event_free(_toggleEvent);
543         _toggleEvent = 0;
544     }
546     if ( event && (event->type == GDK_BUTTON_PRESS) ) {
547         // Make a copy so we can keep it around.
548         _toggleEvent = gdk_event_copy(const_cast<GdkEvent*>(event));
549     }
552 void LayersPanel::_toggled( Glib::ustring const& str, int targetCol )
554     Gtk::TreeModel::Children::iterator iter = _tree.get_model()->get_iter(str);
555     Gtk::TreeModel::Row row = *iter;
557     Glib::ustring tmp = row[_model->_colLabel];
559     SPObject* obj = row[_model->_colObject];
560     SPItem* item = ( obj && SP_IS_ITEM(obj) ) ? SP_ITEM(obj) : 0;
561     if ( item ) {
562         switch ( targetCol ) {
563             case COL_VISIBLE:
564             {
565                 bool newValue = !row[_model->_colVisible];
566                 row[_model->_colVisible] = newValue;
567                 item->setHidden( !newValue  );
568                 item->updateRepr();
569                 sp_document_done( _desktop->doc() );
570             }
571             break;
573             case COL_LOCKED:
574             {
575                 bool newValue = !row[_model->_colLocked];
576                 row[_model->_colLocked] = newValue;
577                 item->setLocked( newValue );
578                 item->updateRepr();
579                 sp_document_done( _desktop->doc() );
580             }
581             break;
582         }
583     }
586 void LayersPanel::_handleButtonEvent(GdkEventButton* evt)
588     // TODO - fix to a better is-popup function
589     if ( (evt->type == GDK_BUTTON_PRESS) && (evt->button == 3) ) {
592         {
593             Gtk::TreeModel::Path path;
594             Gtk::TreeViewColumn* col = 0;
595             int x = static_cast<int>(evt->x);
596             int y = static_cast<int>(evt->y);
597             int x2 = 0;
598             int y2 = 0;
599             if ( _tree.get_path_at_pos( x, y,
600                                         path, col,
601                                         x2, y2 ) ) {
602                 _checkTreeSelection();
603                 _popupMenu.popup(evt->button, evt->time);
604             }
605         }
607     }
610 void LayersPanel::_handleRowChange( Gtk::TreeModel::Path const& path, Gtk::TreeModel::iterator const& iter )
612     Gtk::TreeModel::Row row = *iter;
613     if ( row ) {
614         SPObject* obj = row[_model->_colObject];
615         if ( obj ) {
616             gchar const* oldLabel = obj->label();
617             Glib::ustring tmp = row[_model->_colLabel];
618             if ( oldLabel && oldLabel[0] && !tmp.empty() && (tmp != oldLabel) ) {
619                 _mgr->renameLayer( obj, tmp.c_str() );
620                 row[_model->_colLabel] = obj->label();
621             }
622         }
623     }
626 bool LayersPanel::_rowSelectFunction( Glib::RefPtr<Gtk::TreeModel> const & model, Gtk::TreeModel::Path const & path, bool currentlySelected )
628     bool val = true;
629     if ( !currentlySelected && _toggleEvent )
630     {
631         GdkEvent* event = gtk_get_current_event();
632         if ( event ) {
633             // (keep these checks separate, so we know when to call gdk_event_free()
634             if ( event->type == GDK_BUTTON_PRESS ) {
635                 GdkEventButton const* target = reinterpret_cast<GdkEventButton const*>(_toggleEvent);
636                 GdkEventButton const* evtb = reinterpret_cast<GdkEventButton const*>(event);
638                 if ( (evtb->window == target->window)
639                      && (evtb->send_event == target->send_event)
640                      && (evtb->time == target->time)
641                      && (evtb->state == target->state)
642                     )
643                 {
644                     // Ooooh! It's a magic one
645                     val = false;
646                 }
647             }
648             gdk_event_free(event);
649         }
650     }
651     return val;
654 /**
655  * Constructor
656  */
657 LayersPanel::LayersPanel() :
658     Inkscape::UI::Widget::Panel( Glib::ustring(), "dialogs.layers" ),
659     _maxNestDepth(20),
660     _mgr(0),
661     _desktop(0),
662     _model(0),
663     _pending(0),
664     _toggleEvent(0)
666     _maxNestDepth = prefs_get_int_attribute_limited("dialogs.layers", "maxDepth", 20, 1, 1000);
668     ModelColumns *zoop = new ModelColumns();
669     _model = zoop;
671     _store = Gtk::TreeStore::create( *zoop );
673     _tree.set_model( _store );
674     _tree.set_headers_visible(false);
676     ImageToggler* eyeRenderer = manage( new ImageToggler("visible", "hidden") );
677     int visibleColNum = _tree.append_column("vis", *eyeRenderer) - 1;
678     eyeRenderer->signal_pre_toggle().connect( sigc::mem_fun(*this, &LayersPanel::_preToggle) );
679     eyeRenderer->signal_toggled().connect( sigc::bind( sigc::mem_fun(*this, &LayersPanel::_toggled), (int)COL_VISIBLE) );
680     eyeRenderer->property_activatable() = true;
681     Gtk::TreeViewColumn* col = _tree.get_column(visibleColNum);
682     if ( col ) {
683         col->add_attribute( eyeRenderer->property_active(), _model->_colVisible );
684     }
686     ImageToggler * renderer = manage( new ImageToggler("width_height_lock", "lock_unlocked") );
687     int lockedColNum = _tree.append_column("lock", *renderer) - 1;
688     renderer->signal_pre_toggle().connect( sigc::mem_fun(*this, &LayersPanel::_preToggle) );
689     renderer->signal_toggled().connect( sigc::bind( sigc::mem_fun(*this, &LayersPanel::_toggled), (int)COL_LOCKED) );
690     renderer->property_activatable() = true;
691     col = _tree.get_column(lockedColNum);
692     if ( col ) {
693         col->add_attribute( renderer->property_active(), _model->_colLocked );
694     }
696     int nameColNum = _tree.append_column_editable("Name", _model->_colLabel) - 1;
698     _tree.set_expander_column( *_tree.get_column(nameColNum) );
701     _tree.get_selection()->signal_changed().connect( sigc::mem_fun(*this, &LayersPanel::_pushTreeSelectionToCurrent) );
702     _tree.get_selection()->set_select_function( sigc::mem_fun(*this, &LayersPanel::_rowSelectFunction) );
704     _tree.get_model()->signal_row_changed().connect( sigc::mem_fun(*this, &LayersPanel::_handleRowChange) );
705     _tree.signal_button_press_event().connect_notify( sigc::mem_fun(*this, &LayersPanel::_handleButtonEvent) );
707     _scroller.add( _tree );
708     _scroller.set_policy( Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC );
709     _getContents()->pack_start( _scroller, Gtk::PACK_EXPAND_WIDGET );
710     _getContents()->pack_end(_buttonsRow, Gtk::PACK_SHRINK);
712     SPDesktop* targetDesktop = SP_ACTIVE_DESKTOP;
714     _buttonsRow.set_child_min_width( 16 );
716     Gtk::Button* btn = manage( new Gtk::Button() );
717     _styleButton( *btn, targetDesktop, SP_VERB_LAYER_NEW, GTK_STOCK_ADD, _("New") );
718     btn->signal_clicked().connect( sigc::bind( sigc::mem_fun(*this, &LayersPanel::_takeAction), (int)BUTTON_NEW) );
719     _buttonsRow.add( *btn );
721     btn = manage( new Gtk::Button() );
722     _styleButton( *btn, targetDesktop, SP_VERB_LAYER_TO_TOP, GTK_STOCK_GOTO_TOP, _("Top") );
723     btn->signal_clicked().connect( sigc::bind( sigc::mem_fun(*this, &LayersPanel::_takeAction), (int)BUTTON_TOP) );
724     _watchingNonTop.push_back( btn );
725     _buttonsRow.add( *btn );
727     btn = manage( new Gtk::Button() );
728     _styleButton( *btn, targetDesktop, SP_VERB_LAYER_RAISE, GTK_STOCK_GO_UP, _("Up") );
729     btn->signal_clicked().connect( sigc::bind( sigc::mem_fun(*this, &LayersPanel::_takeAction), (int)BUTTON_UP) );
730     _watchingNonTop.push_back( btn );
731     _buttonsRow.add( *btn );
733     btn = manage( new Gtk::Button() );
734     _styleButton( *btn, targetDesktop, SP_VERB_LAYER_LOWER, GTK_STOCK_GO_DOWN, _("Dn") );
735     btn->signal_clicked().connect( sigc::bind( sigc::mem_fun(*this, &LayersPanel::_takeAction), (int)BUTTON_DOWN) );
736     _watchingNonBottom.push_back( btn );
737     _buttonsRow.add( *btn );
739     btn = manage( new Gtk::Button() );
740     _styleButton( *btn, targetDesktop, SP_VERB_LAYER_TO_BOTTOM, GTK_STOCK_GOTO_BOTTOM, _("Bot") );
741     btn->signal_clicked().connect( sigc::bind( sigc::mem_fun(*this, &LayersPanel::_takeAction), (int)BUTTON_BOTTOM) );
742     _watchingNonBottom.push_back( btn );
743     _buttonsRow.add( *btn );
745 //     btn = manage( new Gtk::Button("Dup") );
746 //     btn->signal_clicked().connect( sigc::bind( sigc::mem_fun(*this, &LayersPanel::_takeAction), (int)BUTTON_DUPLICATE) );
747 //     _buttonsRow.add( *btn );
749     btn = manage( new Gtk::Button() );
750     _styleButton( *btn, targetDesktop, SP_VERB_LAYER_DELETE, GTK_STOCK_REMOVE, _("X") );
751     btn->signal_clicked().connect( sigc::bind( sigc::mem_fun(*this, &LayersPanel::_takeAction), (int)BUTTON_DELETE) );
752     _watching.push_back( btn );
753     _buttonsRow.add( *btn );
758     // -------------------------------------------------------
759     {
760         _watching.push_back( &_addPopupItem( targetDesktop, SP_VERB_LAYER_RENAME, 0, "Rename", (int)BUTTON_RENAME ) );
761         _watching.push_back( &_addPopupItem( targetDesktop, SP_VERB_LAYER_NEW, 0, "New", (int)BUTTON_NEW ) );
763          _popupMenu.items().push_back( Gtk::Menu_Helpers::SeparatorElem() );
765         _watchingNonTop.push_back( &_addPopupItem( targetDesktop, SP_VERB_LAYER_RAISE, GTK_STOCK_GO_UP, "Up", (int)BUTTON_UP ) );
766         _watchingNonBottom.push_back( &_addPopupItem( targetDesktop, SP_VERB_LAYER_LOWER, GTK_STOCK_GO_DOWN, "Down", (int)BUTTON_DOWN ) );
768         _popupMenu.show_all_children();
769     }
770     // -------------------------------------------------------
774     for ( std::vector<Gtk::Widget*>::iterator it = _watching.begin(); it != _watching.end(); ++it ) {
775         (*it)->set_sensitive( false );
776     }
777     for ( std::vector<Gtk::Widget*>::iterator it = _watchingNonTop.begin(); it != _watchingNonTop.end(); ++it ) {
778         (*it)->set_sensitive( false );
779     }
780     for ( std::vector<Gtk::Widget*>::iterator it = _watchingNonBottom.begin(); it != _watchingNonBottom.end(); ++it ) {
781         (*it)->set_sensitive( false );
782     }
784     g_signal_connect( G_OBJECT(INKSCAPE), "activate_desktop", G_CALLBACK( layers_panel_activated ), this );
787     setDesktop( targetDesktop );
791     show_all_children();
793     restorePanelPrefs();
796 LayersPanel::~LayersPanel()
798     if ( _model )
799     {
800         delete _model;
801     }
803     if ( _toggleEvent )
804     {
805         gdk_event_free( _toggleEvent );
806         _toggleEvent = 0;
807     }
811 void LayersPanel::setDesktop( SPDesktop* desktop )
813     if ( desktop != _desktop ) {
814         _layerChangedConnection.disconnect();
815         _layerUpdatedConnection.disconnect();
816         _changedConnection.disconnect();
817         if ( _mgr ) {
818             _mgr = 0;
819         }
820         if ( _desktop ) {
821             _desktop = 0;
822         }
824         _desktop = SP_ACTIVE_DESKTOP;
825         if ( _desktop ) {
826             setLabel( _desktop->doc()->name );
828             _mgr = _desktop->layer_manager;
829             if ( _mgr ) {
830                 _layerChangedConnection = _mgr->connectCurrentLayerChanged( sigc::mem_fun(*this, &LayersPanel::_selectLayer) );
831                 _layerUpdatedConnection = _mgr->connectLayerDetailsChanged( sigc::mem_fun(*this, &LayersPanel::_updateLayer) );
832                 _changedConnection = _mgr->connectChanged( sigc::mem_fun(*this, &LayersPanel::_layersChanged) );
833             }
835             _layersChanged();
836         }
837     }
838 /*
839     GSList const *layers=sp_document_get_resource_list( _desktop->doc(), "layer" );
840     g_message( "layers list starts at %p", layers );
841     for ( GSList const *iter=layers ; iter ; iter = iter->next ) {
842         SPObject *layer=static_cast<SPObject *>(iter->data);
843         g_message("  {%s}   [%s]", layer->id, layer->label() );
844     }
845 */
850 } //namespace Dialogs
851 } //namespace UI
852 } //namespace Inkscape
855 /*
856   Local Variables:
857   mode:c++
858   c-file-style:"stroustrup"
859   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
860   indent-tabs-mode:nil
861   fill-column:99
862   End:
863 */
864 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :