16b7fc8cb9f8687ffdcc5ca942f1c59309c410b7
[synfig.git] / layertreestore.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file layertreestore.cpp
3 **      \brief Template File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2008 Chris Moore
10 **
11 **      This package is free software; you can redistribute it and/or
12 **      modify it under the terms of the GNU General Public License as
13 **      published by the Free Software Foundation; either version 2 of
14 **      the License, or (at your option) any later version.
15 **
16 **      This package is distributed in the hope that it will be useful,
17 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
18 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 **      General Public License for more details.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === H E A D E R S ======================================================= */
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include "layertreestore.h"
34 #include "iconcontroller.h"
35 #include <gtkmm/button.h>
36 #include <synfig/paramdesc.h>
37 #include <synfigapp/action.h>
38 #include <synfigapp/instance.h>
39 #include "app.h"
40 #include "instance.h"
41 #include <synfig/layer_pastecanvas.h>
42 #include <synfigapp/action_system.h>
43
44 #include <gtk/gtkversion.h>
45 #include <ETL/clock>
46 #include "general.h"
47
48 #endif
49
50 /* === U S I N G =========================================================== */
51
52 using namespace std;
53 using namespace etl;
54 using namespace synfig;
55 using namespace studio;
56
57 /* === M A C R O S ========================================================= */
58
59 /* === G L O B A L S ======================================================= */
60
61 /* === P R O C E D U R E S ================================================= */
62
63 /* === M E T H O D S ======================================================= */
64
65 static LayerTreeStore::Model& ModelHack()
66 {
67         static LayerTreeStore::Model* model(0);
68         if(!model)model=new LayerTreeStore::Model;
69         return *model;
70 }
71
72 LayerTreeStore::LayerTreeStore(etl::loose_handle<synfigapp::CanvasInterface> canvas_interface_):
73         Gtk::TreeStore                  (ModelHack()),
74         queued                                  (false),
75         canvas_interface_               (canvas_interface_)
76 {
77         layer_icon=Gtk::Button().render_icon(Gtk::StockID("synfig-layer"),Gtk::ICON_SIZE_SMALL_TOOLBAR);
78
79         // Connect Signals to Terminals
80         canvas_interface()->signal_layer_status_changed().connect(sigc::mem_fun(*this,&studio::LayerTreeStore::on_layer_status_changed));
81         canvas_interface()->signal_layer_lowered().connect(sigc::mem_fun(*this,&studio::LayerTreeStore::on_layer_lowered));
82         canvas_interface()->signal_layer_raised().connect(sigc::mem_fun(*this,&studio::LayerTreeStore::on_layer_raised));
83         canvas_interface()->signal_layer_removed().connect(sigc::mem_fun(*this,&studio::LayerTreeStore::on_layer_removed));
84         canvas_interface()->signal_layer_inserted().connect(sigc::mem_fun(*this,&studio::LayerTreeStore::on_layer_inserted));
85         canvas_interface()->signal_layer_moved().connect(sigc::mem_fun(*this,&studio::LayerTreeStore::on_layer_moved));
86         //canvas_interface()->signal_layer_param_changed().connect(sigc::mem_fun(*this,&studio::LayerTreeStore::on_layer_param_changed));
87         canvas_interface()->signal_layer_new_description().connect(sigc::mem_fun(*this,&studio::LayerTreeStore::on_layer_new_description));
88
89         canvas_interface()->signal_time_changed().connect(sigc::mem_fun(*this,&studio::LayerTreeStore::refresh));
90
91         //canvas_interface()->signal_value_node_changed().connect(sigc::mem_fun(*this,&studio::LayerTreeStore::on_value_node_changed));
92         //canvas_interface()->signal_value_node_added().connect(sigc::mem_fun(*this,&studio::LayerTreeStore::on_value_node_added));
93         //canvas_interface()->signal_value_node_deleted().connect(sigc::mem_fun(*this,&studio::LayerTreeStore::on_value_node_deleted));
94         //canvas_interface()->signal_value_node_replaced().connect(sigc::mem_fun(*this,&studio::LayerTreeStore::on_value_node_replaced));
95
96         set_default_sort_func(sigc::ptr_fun(index_sorter));
97
98 //      rebuild();
99 }
100
101 LayerTreeStore::~LayerTreeStore()
102 {
103         if (getenv("SYNFIG_DEBUG_DESTRUCTORS"))
104                 synfig::info("LayerTreeStore::~LayerTreeStore(): Deleted");
105 }
106
107 int
108 LayerTreeStore::z_sorter(const Gtk::TreeModel::iterator &rhs,const Gtk::TreeModel::iterator &lhs)
109 {
110         const Model model;
111
112         float diff((float)(*rhs)[model.z_depth]-(float)(*lhs)[model.z_depth]);
113
114         if(diff<0)
115                 return -1;
116         if(diff>0)
117                 return 1;
118         return 0;
119 }
120
121 int
122 LayerTreeStore::index_sorter(const Gtk::TreeModel::iterator &rhs,const Gtk::TreeModel::iterator &lhs)
123 {
124         const Model model;
125
126         return ((int)(*rhs)[model.index]-(int)(*lhs)[model.index]);
127 }
128
129 bool
130 LayerTreeStore::search_func(const Glib::RefPtr<TreeModel>&,int,const Glib::ustring& x,const TreeModel::iterator& iter)
131 {
132         const Model model;
133
134         Glib::ustring substr(x.uppercase());
135         Glib::ustring label((*iter)[model.label]);
136         label=label.uppercase();
137
138         return label.find(substr)==Glib::ustring::npos;
139 }
140
141
142 Glib::RefPtr<LayerTreeStore>
143 LayerTreeStore::create(etl::loose_handle<synfigapp::CanvasInterface> canvas_interface_)
144 {
145         return Glib::RefPtr<LayerTreeStore>(new LayerTreeStore(canvas_interface_));
146 }
147
148 void
149 LayerTreeStore::get_value_vfunc (const Gtk::TreeModel::iterator& iter, int column, Glib::ValueBase& value)const
150 {
151         if(column==model.index.index())
152         {
153                 synfig::Layer::Handle layer((*iter)[model.layer]);
154
155                 if(!layer)return;
156
157                 Glib::Value<int> x;
158                 g_value_init(x.gobj(),x.value_type());
159
160                 x.set(layer->get_depth());
161
162                 g_value_init(value.gobj(),x.value_type());
163                 g_value_copy(x.gobj(),value.gobj());
164         }
165         else if(column==model.z_depth.index())
166         {
167                 synfig::Layer::Handle layer((*iter)[model.layer]);
168
169                 if(!layer)return;
170
171                 Glib::Value<float> x;
172                 g_value_init(x.gobj(),x.value_type());
173
174                 x.set(layer->get_z_depth(canvas_interface()->get_time())*1.0001+layer->get_depth());
175
176                 g_value_init(value.gobj(),x.value_type());
177                 g_value_copy(x.gobj(),value.gobj());
178         }
179         else if(column==model.children_lock.index())
180         {
181                 synfig::Layer::Handle layer((*iter)[model.layer]);
182
183                 if(!layer)return;
184
185                 Glib::Value<bool> x;
186                 g_value_init(x.gobj(),x.value_type());
187                 x.set(false);
188
189                 ValueBase v(layer->get_param("children_lock"));
190                 if(v.same_type_as(bool()))
191                         x.set(v.get(bool()));
192
193                 g_value_init(value.gobj(),x.value_type());
194                 g_value_copy(x.gobj(),value.gobj());
195         }
196         else if(column==model.label.index())
197         {
198                 synfig::Layer::Handle layer((*iter)[model.layer]);
199
200                 if(!layer)return;
201
202                 Glib::Value<Glib::ustring> x;
203                 g_value_init(x.gobj(),x.value_type());
204
205                 x.set(layer->get_non_empty_description());
206
207                 g_value_init(value.gobj(),x.value_type());
208                 //g_value_copy(x.gobj(),value.gobj());
209                 value=x;
210         }
211         else if(column==model.tooltip.index())
212         {
213                 synfig::Layer::Handle layer((*iter)[model.layer]);
214
215                 if(!layer)return;
216
217                 Glib::Value<Glib::ustring> x;
218                 g_value_init(x.gobj(),x.value_type());
219
220
221                 x.set(layer->get_local_name());
222
223                 g_value_init(value.gobj(),x.value_type());
224                 //g_value_copy(x.gobj(),value.gobj());
225                 value=x;
226         }
227         else if(column==model.canvas.index())
228         {
229                 synfig::Layer::Handle layer((*iter)[model.layer]);
230
231                 if(!layer)return;
232
233                 Glib::Value<Canvas::Handle> x;
234                 g_value_init(x.gobj(),x.value_type());
235
236
237                 x.set(layer->get_canvas());
238
239                 g_value_init(value.gobj(),x.value_type());
240                 //g_value_copy(x.gobj(),value.gobj());
241                 value=x;
242         }
243         else if(column==model.active.index())
244         {
245                 synfig::Layer::Handle layer((*iter)[model.layer]);
246
247                 if(!layer)return;
248
249                 Glib::Value<bool> x;
250                 g_value_init(x.gobj(),x.value_type());
251
252                 x.set(layer->active());
253
254                 g_value_init(value.gobj(),x.value_type());
255                 g_value_copy(x.gobj(),value.gobj());
256         }
257         else if(column==model.icon.index())
258         {
259                 synfig::Layer::Handle layer((*iter)[model.layer]);
260                 if(!layer)return;
261
262                 Glib::Value<Glib::RefPtr<Gdk::Pixbuf> > x;
263                 g_value_init(x.gobj(),x.value_type());
264
265                 //x.set(layer_icon);
266                 x.set(get_tree_pixbuf_layer(layer->get_name()));
267
268                 g_value_init(value.gobj(),x.value_type());
269                 g_value_copy(x.gobj(),value.gobj());
270         }
271         else
272                 Gtk::TreeStore::get_value_vfunc(iter,column,value);
273 }
274
275 void
276 LayerTreeStore::set_value_impl(const Gtk::TreeModel::iterator& iter, int column, const Glib::ValueBase& value)
277 {
278         //if(!iterator_sane(row))
279         //      return;
280
281         if(column>=get_n_columns_vfunc())
282         {
283                 g_warning("LayerTreeStore::set_value_impl: Bad column (%d)",column);
284                 return;
285         }
286
287         if(!g_value_type_compatible(G_VALUE_TYPE(value.gobj()),get_column_type_vfunc(column)))
288         {
289                 g_warning("LayerTreeStore::set_value_impl: Bad value type");
290                 return;
291         }
292
293         try
294         {
295                 if(column==model.label.index())
296                 {
297                         Glib::Value<Glib::ustring> x;
298                         g_value_init(x.gobj(),model.label.type());
299                         g_value_copy(value.gobj(),x.gobj());
300
301                         synfig::Layer::Handle layer((*iter)[model.layer]);
302                         if(!layer)
303                                 return;
304                         synfig::String new_desc(x.get());
305
306                         if(new_desc==layer->get_local_name())
307                                 new_desc=synfig::String();
308
309                         if(new_desc==layer->get_description())
310                                 return;
311
312                         synfigapp::Action::Handle action(synfigapp::Action::create("LayerSetDesc"));
313
314                         if(!action)
315                                 return;
316
317                         action->set_param("canvas",canvas_interface()->get_canvas());
318                         action->set_param("canvas_interface",canvas_interface());
319                         action->set_param("layer",layer);
320                         action->set_param("new_description",synfig::String(x.get()));
321
322                         canvas_interface()->get_instance()->perform_action(action);
323                         return;
324                 }
325                 else if(column==model.active.index())
326                 {
327                         synfig::Layer::Handle layer((*iter)[model.layer]);
328
329                         if(!layer)return;
330
331                         Glib::Value<bool> x;
332                         g_value_init(x.gobj(),model.active.type());
333                         g_value_copy(value.gobj(),x.gobj());
334
335                         synfigapp::Action::Handle action(synfigapp::Action::create("LayerActivate"));
336
337                         if(!action)
338                                 return;
339
340                         action->set_param("canvas",canvas_interface()->get_canvas());
341                         action->set_param("canvas_interface",canvas_interface());
342                         action->set_param("layer",layer);
343                         action->set_param("new_status",bool(x.get()));
344
345                         canvas_interface()->get_instance()->perform_action(action);
346                         return;
347                 }
348                 else
349                         Gtk::TreeStore::set_value_impl(iter,column, value);
350
351         }
352         catch(std::exception x)
353         {
354                 g_warning("%s", x.what());
355         }
356 }
357
358
359
360
361 bool
362 LayerTreeStore::row_draggable_vfunc (const TreeModel::Path& /*path*/)const
363 {
364         //if(!get_iter(path)) return false;
365 //      Gtk::TreeModel::Row row(*get_iter(path));
366
367         return true;
368 //      return (bool)true;
369 }
370
371 bool
372 LayerTreeStore::drag_data_get_vfunc (const TreeModel::Path& path, Gtk::SelectionData& selection_data)const
373 {
374         if(!const_cast<LayerTreeStore*>(this)->get_iter(path)) return false;
375         //synfig::info("Dragged data of type \"%s\"",selection_data.get_data_type());
376         //synfig::info("Dragged data of target \"%s\"",gdk_atom_name(selection_data->target));
377         //synfig::info("Dragged selection=\"%s\"",gdk_atom_name(selection_data->selection));
378
379         Gtk::TreeModel::Row row(*const_cast<LayerTreeStore*>(this)->get_iter(path));
380
381         if((bool)true)
382         {
383                 Layer* layer(((Layer::Handle)row[model.layer]).get());
384                 assert(layer);
385                 bool included(false);
386
387                 //gtk_selection_data_set (selection_data, gdk_atom_intern("LAYER",false), 8, reinterpret_cast<const guchar*>(&layer), sizeof(layer));
388
389                 std::vector<Layer*> layers;
390                 // The following is a hack for multiple row DND
391                 {
392                         synfigapp::SelectionManager::LayerList bleh(get_canvas_interface()->get_selection_manager()->get_selected_layers());
393                         if(bleh.empty())
394                         {
395                                 selection_data.set("LAYER", 8, reinterpret_cast<const guchar*>(&layer), sizeof(layer));
396                                 return true;
397                         }
398                         while(!bleh.empty())
399                         {
400                                 if(bleh.back().get()==layer)
401                                         included=true;
402                                 layers.push_back(bleh.back().get());
403                                 bleh.pop_back();
404                         }
405                 }
406                 if(!included)
407                         layers.push_back(layer);
408                 selection_data.set("LAYER", 8, reinterpret_cast<const guchar*>(&layers.front()), sizeof(void*)*layers.size());
409
410                 return true;
411         }
412         return false;
413 }
414
415 bool
416 LayerTreeStore::drag_data_delete_vfunc (const TreeModel::Path& /*path*/)
417 {
418         return true;
419 }
420
421 bool
422 LayerTreeStore::row_drop_possible_vfunc (const TreeModel::Path& dest, const Gtk::SelectionData& selection_data)const
423 {
424         //if(!const_cast<LayerTreeStore*>(this)->get_iter(dest)) return false;
425
426         //synfig::info("possible_drop -- data of type \"%s\"",selection_data.get_data_type());
427         //synfig::info("possible_drop -- data of target \"%s\"",gdk_atom_name(selection_data->target));
428         //synfig::info("possible_drop -- selection=\"%s\"",gdk_atom_name(selection_data->selection));
429
430         //Gtk::TreeModel::Row row(*get_iter(dest));
431
432         if(synfig::String(selection_data.get_data_type())=="LAYER" && (bool)true)
433         {
434                 //Layer::Handle src(reinterpret_cast<Layer**>(const_cast<guint8*>(selection_data.get_data()))[i]);
435                 //assert(src);
436
437                 //return true;
438                 TreeModel::Path dest_parent(dest);
439                 if(!dest_parent.up() || dest.get_depth()==1)
440                 {
441                         //row=(*get_iter(dest));
442                         //dest_canvas=(Canvas::Handle)(row[model.canvas]);
443                         return true;
444                 }
445                 else if((bool)const_cast<LayerTreeStore*>(this)->get_iter(dest_parent))
446                         return (bool)(Canvas::Handle)(*const_cast<LayerTreeStore*>(this)->get_iter(dest_parent))[model.contained_canvas];
447         }
448         return false;
449 }
450
451 bool
452 LayerTreeStore::drag_data_received_vfunc (const TreeModel::Path& dest, const Gtk::SelectionData& selection_data)
453 {
454
455         //if(!dest_parent.up() || !get_iter(dest)) return false;
456
457         bool ret=false;
458         int i(0);
459
460
461         //synfig::info("Dropped data of type \"%s\"",selection_data.get_data_type());
462         //synfig::info("Dropped data of target \"%s\"",gdk_atom_name(selection_data->target));
463         //synfig::info("Dropped selection=\"%s\"",gdk_atom_name(selection_data->selection));
464         synfigapp::Action::PassiveGrouper passive_grouper(canvas_interface()->get_instance().get(),_("Move Layers"));
465
466         // Save the selection data
467         synfigapp::SelectionManager::LayerList selected_layer_list=canvas_interface()->get_selection_manager()->get_selected_layers();
468
469         if ((selection_data.get_length() >= 0) && (selection_data.get_format() == 8))
470         {
471                 Gtk::TreeModel::Row row;
472                 Canvas::Handle dest_canvas;
473
474                 int dest_layer_depth=dest.back();
475
476                 TreeModel::Path dest_parent(dest);
477                 if(!dest_parent.up() || !get_iter(dest_parent))
478                 {
479                         TreeModel::Path dest_(dest);
480                         if(!get_iter(dest_))
481                                 dest_.prev();
482
483                         if(!get_iter(dest_))
484                                 return false;
485
486                         {
487                                 row=(*get_iter(dest_));
488                                 dest_canvas=(Canvas::Handle)(row[model.canvas]);
489                         }
490                 }
491                 else
492                 {
493                         row=(*get_iter(dest_parent));
494                         dest_canvas=row[model.contained_canvas];
495                 }
496
497                 assert(dest_canvas);
498
499                 Layer::Handle dest_layer(row[model.layer]);
500
501                 if(synfig::String(selection_data.get_data_type())=="LAYER")for(unsigned int i=0;i<selection_data.get_length()/sizeof(void*);i++)
502                 {
503                         //synfig::info("dest_layer_depth=%d",dest_layer_depth);
504
505                         Layer::Handle src(reinterpret_cast<Layer**>(const_cast<guint8*>(selection_data.get_data()))[i]);
506                         assert(src);
507                         if(dest_layer==src)
508                                 continue;
509
510                         if(dest_canvas==src->get_canvas() && src->get_depth()<dest_layer_depth)
511                                 dest_layer_depth--;
512
513                         // In this case, we are just moving.
514 //                      if(dest_canvas==src->get_canvas())
515                         {
516                                 //if(dest_canvas==src->get_canvas() && dest_layer_depth && dest_layer_depth>src->get_depth())
517                                 //      dest_layer_depth--;
518                                 if(dest_canvas==src->get_canvas() && dest_layer_depth==src->get_depth())
519                                         continue;
520
521                                 synfigapp::Action::Handle action(synfigapp::Action::create("LayerMove"));
522                                 action->set_param("canvas",dest_canvas);
523                                 action->set_param("canvas_interface",canvas_interface());
524                                 action->set_param("layer",src);
525                                 action->set_param("new_index",dest_layer_depth);
526                                 action->set_param("dest_canvas",dest_canvas);
527                                 if(canvas_interface()->get_instance()->perform_action(action))
528                                         ret=true;
529                                 else
530                                 {
531                                         passive_grouper.cancel();
532                                         return false;
533                                 }
534                                 continue;
535                         }
536                         /*else // In this case we need to remove and then add
537                         {
538
539                                 synfigapp::Action::Handle action;
540                                 action=synfigapp::Action::create("LayerRemove");
541                                 action->set_param("canvas",Canvas::Handle(src->get_canvas()));
542                                 if(!action->set_param("canvas_interface",App::get_instance(src->get_canvas())->find_canvas_interface(src->get_canvas())))
543                                         action->set_param("canvas_interface",canvas_interface());
544                                 action->set_param("layer",src);
545                                 if(!canvas_interface()->get_instance()->perform_action(action))
546                                 {
547                                         passive_grouper.cancel();
548                                         ret=false;
549                                         return false;
550                                 }
551
552                                 action=synfigapp::Action::create("LayerAdd");
553                                 action->set_param("canvas",dest_canvas);
554                                 action->set_param("canvas_interface",canvas_interface());
555                                 action->set_param("new",src);
556                                 if(!canvas_interface()->get_instance()->perform_action(action))
557                                 {
558                                         passive_grouper.cancel();
559                                         ret=false;
560                                         return false;
561                                 }
562
563                                 if(dest_layer_depth!=0)
564                                 {
565                                         action=synfigapp::Action::create("LayerMove");
566                                         action->set_param("canvas",dest_canvas);
567                                         action->set_param("canvas_interface",canvas_interface());
568                                         action->set_param("layer",src);
569                                         action->set_param("new_index",dest_layer_depth);
570                                         if(!canvas_interface()->get_instance()->perform_action(action))
571                                         {
572                                                 passive_grouper.cancel();
573                                                 ret=false;
574                                                 return false;
575                                         }
576                                 }
577                                 ret=true;
578                         }
579                         */
580                 }
581         }
582         synfig::info("I supposedly moved %d layers",i);
583
584         // Reselect the previously selected layers
585         canvas_interface()->get_selection_manager()->set_selected_layers(selected_layer_list);
586
587         return ret;
588 }
589
590 void
591 LayerTreeStore::queue_rebuild()
592 {
593         if (queued) return;
594         queued = false;
595         queue_connection.disconnect();
596         queue_connection=Glib::signal_timeout().connect(
597                 sigc::bind_return(
598                         sigc::mem_fun(*this,&LayerTreeStore::rebuild),
599                         false
600                 )
601         ,150);
602 }
603
604 void
605 LayerTreeStore::rebuild()
606 {
607         if (queued) queued = false;
608
609         // disconnect any subcanvas_changed connections
610         std::map<synfig::Layer::Handle, sigc::connection>::iterator iter;
611         for (iter = subcanvas_changed_connections.begin(); iter != subcanvas_changed_connections.end(); iter++)
612                 iter->second.disconnect();
613         subcanvas_changed_connections.clear();
614
615         //etl::clock timer;timer.reset();
616
617         //synfig::warning("---------rebuilding layer table---------");
618         // Save the selection data
619         synfigapp::SelectionManager::LayerList layer_list=canvas_interface()->get_selection_manager()->get_selected_layers();
620
621         // Clear out the current list
622         clear();
623
624         // Go ahead and add all the layers
625         std::for_each(
626                 canvas_interface()->get_canvas()->rbegin(), canvas_interface()->get_canvas()->rend(),
627                 sigc::mem_fun(*this, &studio::LayerTreeStore::on_layer_added)
628         );
629
630         // Reselect the previously selected layers
631         if(!layer_list.empty())
632                 canvas_interface()->get_selection_manager()->set_selected_layers(layer_list);
633
634         //synfig::info("LayerTreeStore::rebuild() took %f seconds",float(timer()));
635 }
636
637 void
638 LayerTreeStore::refresh()
639 {
640         etl::clock timer;timer.reset();
641
642         Gtk::TreeModel::Children children_(children());
643
644         Gtk::TreeModel::Children::iterator iter;
645
646         if(!children_.empty())
647                 for(iter = children_.begin(); iter && iter != children_.end(); ++iter)
648                 {
649                         Gtk::TreeRow row=*iter;
650                         refresh_row(row);
651                 }
652         //synfig::info("LayerTreeStore::refresh() took %f seconds",float(timer()));
653 }
654
655 void
656 LayerTreeStore::refresh_row(Gtk::TreeModel::Row &row)
657 {
658         Layer::Handle layer=row[model.layer];
659         /*
660         {
661                 row[model.name] = layer->get_local_name();
662                 if(layer->get_description().empty())
663                 {
664                         row[model.label] = layer->get_local_name();
665                         row[model.tooltip] = Glib::ustring("Layer");
666                 }
667                 else
668                 {
669                         row[model.label] = layer->get_description();
670                         row[model.tooltip] = layer->get_local_name();
671                 }
672         }
673         */
674
675         if(layer->dynamic_param_list().count("z_depth"))
676                 row[model.z_depth]=Time::begin();
677         //      row_changed(get_path(row),row);
678
679         Gtk::TreeModel::Children children = row.children();
680         Gtk::TreeModel::Children::iterator iter;
681
682         if(!children.empty())
683                 for(iter = children.begin(); iter && iter != children.end(); ++iter)
684                 {
685                         Gtk::TreeRow row=*iter;
686                         refresh_row(row);
687                 }
688 }
689
690
691 void
692 LayerTreeStore::set_row_layer(Gtk::TreeRow &row,synfig::Layer::Handle &handle)
693 {
694         //row[model.id] = handle->get_name();
695         //row[model.name] = handle->get_local_name();
696         /*if(handle->get_description().empty())
697         {
698                 //row[model.label] = handle->get_local_name();
699                 row[model.tooltip] = Glib::ustring("Layer");
700         }
701         else
702         {
703                 //row[model.label] = handle->get_description();
704                 row[model.tooltip] = handle->get_local_name();
705         }*/
706
707         //row[model.active] = handle->active();
708         row[model.layer] = handle;
709         //row[model.canvas] = handle->get_canvas();
710         //row[model.icon] = layer_icon;
711
712         synfig::Layer::ParamList paramlist=handle->get_param_list();
713
714         synfig::Layer::Vocab vocab=handle->get_param_vocab();
715         synfig::Layer::Vocab::iterator iter;
716
717         for(iter=vocab.begin();iter!=vocab.end();++iter)
718         {
719                 if(iter->get_hidden())
720                         continue;
721                 if(handle->get_param(iter->get_name()).get_type()!=ValueBase::TYPE_CANVAS)
722                         continue;
723
724                 {
725                         Canvas::Handle canvas;
726                         canvas=handle->get_param(iter->get_name()).get(canvas);
727                         if(!canvas)
728                                 continue;
729
730                         Canvas::reverse_iterator iter;
731                         row[model.contained_canvas]=canvas;
732
733                         for(iter=canvas->rbegin();iter!=canvas->rend();++iter)
734                         {
735                                 Gtk::TreeRow row_(*(prepend(row.children())));
736                                 set_row_layer(row_,*iter);
737                         }
738                         continue;
739                 }
740
741                 /*
742                 etl::handle<ValueNode> value_node;
743                 if(handle.constant()->dynamic_param_list().count(iter->get_name()))
744                         value_node=handle->dynamic_param_list()[iter->get_name()];
745
746                 Gtk::TreeRow child_row = *(append(row.children()));
747                 set_row_param(
748                         child_row,
749                         handle,
750                         iter->get_name(),
751                         iter->get_local_name(),
752                         paramlist[iter->get_name()],
753                         value_node,
754                         &*iter
755                 );
756                 */
757         }
758 }
759
760 void
761 LayerTreeStore::on_layer_added(synfig::Layer::Handle layer)
762 {
763         if (etl::handle<Layer_PasteCanvas>::cast_dynamic(layer))
764                 subcanvas_changed_connections[layer] =
765                         (etl::handle<Layer_PasteCanvas>::cast_dynamic(layer))->signal_subcanvas_changed().connect(
766                                 sigc::mem_fun(*this,&studio::LayerTreeStore::queue_rebuild)
767                         );
768
769         assert(layer);
770         Gtk::TreeRow row;
771         if(canvas_interface()->get_canvas()==layer->get_canvas())
772         {
773                 row=*(prepend());
774         }
775         else
776         {
777                 Gtk::TreeModel::Children::iterator iter;
778                 if(!find_canvas_row(layer->get_canvas(),iter))
779                 {
780                         rebuild();
781                         return;
782                 }
783                 row=*(prepend(iter->children()));
784         }
785         set_row_layer(row,layer);
786 }
787
788 void
789 LayerTreeStore::on_layer_removed(synfig::Layer::Handle handle)
790 {
791         if (etl::handle<Layer_PasteCanvas>::cast_dynamic(handle))
792         {
793                 subcanvas_changed_connections[handle].disconnect();
794                 subcanvas_changed_connections.erase(handle);
795         }
796         Gtk::TreeModel::Children::iterator iter;
797         if(find_layer_row(handle,iter))
798                 erase(iter);
799         else
800         {
801                 synfig::error("LayerTreeStore::on_layer_removed():Unable to find layer to be removed, forced to rebuild...");
802                 rebuild();
803         }
804 }
805
806 void
807 LayerTreeStore::on_layer_inserted(synfig::Layer::Handle handle,int depth)
808 {
809         if(depth==0)
810         {
811                 on_layer_added(handle);
812                 return;
813         }
814
815         Gtk::TreeModel::Children children_(children());
816         if(canvas_interface()->get_canvas()!=handle->get_canvas())
817         {
818                 Gtk::TreeModel::Children::iterator iter;
819                 if(!find_canvas_row(handle->get_canvas(),iter))
820                 {
821                         synfig::error("LayerTreeStore::on_layer_inserted():Unable to find canvas row, forced to rebuild...");
822                         rebuild();
823                         return;
824                 }
825                 children_=iter->children();
826         }
827
828         Gtk::TreeModel::Children::iterator iter(children_.begin());
829         while(depth-- && iter)
830         {
831                 ++iter;
832                 if(!iter || iter==children_.end())
833                 {
834                         synfig::error("LayerTreeStore::on_layer_inserted():Unable to achieve desired depth, forced to rebuild...");
835                         rebuild();
836                         return;
837                 }
838         }
839
840         Gtk::TreeModel::Row row(*insert(iter));
841         set_row_layer(row,handle);
842 }
843
844 void
845 LayerTreeStore::on_layer_status_changed(synfig::Layer::Handle handle,bool /*x*/)
846 {
847         Gtk::TreeModel::Children::iterator iter;
848         if(find_layer_row(handle,iter))
849                 (*iter)[model.layer]=handle;
850         else
851         {
852                 synfig::warning("Couldn't find layer to be activated in layer list. Rebuilding index...");
853                 rebuild();
854         }
855 }
856
857 void
858 LayerTreeStore::on_layer_lowered(synfig::Layer::Handle layer)
859 {
860         Gtk::TreeModel::Children::iterator iter, iter2;
861         if(find_layer_row(layer,iter))
862         {
863                 // Save the selection data
864                 //synfigapp::SelectionManager::LayerList layer_list=canvas_interface()->get_selection_manager()->get_selected_layers();
865                 iter2=iter;
866                 iter2++;
867                 if(!iter2)
868                 {
869                         rebuild();
870                         return;
871                 }
872
873                 //Gtk::TreeModel::Row row(*iter);
874                 Gtk::TreeModel::Row row2 = *iter2;
875                 synfig::Layer::Handle layer2=row2[model.layer];
876
877                 erase(iter2);
878                 row2=*insert(iter);
879                 set_row_layer(row2,layer2);
880
881         }
882         else
883                 rebuild();
884 }
885
886 void
887 LayerTreeStore::on_layer_raised(synfig::Layer::Handle layer)
888 {
889         Gtk::TreeModel::Children::iterator iter, iter2;
890
891         Gtk::TreeModel::Children children_(children());
892
893         if(find_layer_row_(layer, canvas_interface()->get_canvas(), children_, iter,iter2))
894         {
895                 if(iter!=iter2)
896                 {
897                         //Gtk::TreeModel::Row row = *iter;
898                         Gtk::TreeModel::Row row2 = *iter2;
899                         synfig::Layer::Handle layer2=row2[model.layer];
900
901                         erase(iter2);
902                         iter++;
903                         row2=*insert(iter);
904                         set_row_layer(row2,layer2);
905
906                         return;
907                 }
908         }
909
910         rebuild();
911 }
912
913 void
914 LayerTreeStore::on_layer_moved(synfig::Layer::Handle layer,int depth, synfig::Canvas::Handle /*canvas*/)
915 {
916         on_layer_removed(layer);
917         on_layer_inserted(layer,depth);
918 }
919
920 void
921 LayerTreeStore::on_layer_param_changed(synfig::Layer::Handle handle,synfig::String param_name)
922 {
923         if(param_name=="z_depth")
924         {
925                 Gtk::TreeModel::Children::iterator iter;
926                 if(find_layer_row(handle,iter))
927                 {
928                         (*iter)[model.z_depth]=Time::begin();
929                 }
930         }
931
932         /*
933         Gtk::TreeModel::Children::iterator iter;
934         if(find_layer_row(handle,iter))
935         {
936                 Gtk::TreeModel::Children children(iter->children());
937
938                 for(iter = children.begin(); iter && iter != children.end(); ++iter)
939                 {
940                         if((Glib::ustring)(*iter)[model.param_name]==param_name)
941                         {
942                                 Gtk::TreeRow row=*iter;
943                                 refresh_row(row);
944                                 return;
945                         }
946                 }
947         }
948         rebuild();
949         */
950 }
951
952 void
953 LayerTreeStore::on_layer_new_description(synfig::Layer::Handle handle,synfig::String desc)
954 {
955         Gtk::TreeModel::Children::iterator iter;
956         if(find_layer_row(handle,iter))
957         {
958                 Gtk::TreeRow row(*iter);
959
960                 Layer::Handle layer(row[model.layer]);
961
962                 if(desc.empty())
963                 {
964                         //row[model.label]=layer->get_local_name();
965                         row[model.tooltip]=Glib::ustring(_("Layer"));
966                 }
967                 else
968                         //row[model.label]=layer->get_description();
969                         row[model.tooltip]=layer->get_local_name();
970         }
971         else
972         {
973                 rebuild();
974         }
975 }
976
977 bool
978 LayerTreeStore::find_canvas_row_(synfig::Canvas::Handle canvas, synfig::Canvas::Handle parent, Gtk::TreeModel::Children layers, Gtk::TreeModel::Children::iterator &iter)
979 {
980         if(canvas==parent)
981                 return false;
982
983         {
984                 for(iter=layers.begin(); iter && iter != layers.end(); ++iter)
985                 {
986                         Gtk::TreeModel::Row row = *iter;
987                         if(canvas==(synfig::Canvas::Handle)row[model.contained_canvas])
988                                 return true;
989                 }
990
991                 iter=children().end();
992                 //return false;
993         }
994
995         Gtk::TreeModel::Children::iterator iter2;
996         //Gtk::TreeModel::Children::iterator iter3;
997
998         for(iter2 = layers.begin(); iter2 && iter2 != layers.end(); ++iter2)
999         {
1000                 Gtk::TreeModel::Row row = *iter2;
1001                 assert((bool)true);
1002
1003                 if(row.children().empty())
1004                         continue;
1005
1006                 Canvas::Handle sub_canvas((*row.children().begin())[model.canvas]);
1007                 if(!sub_canvas)
1008                         continue;
1009
1010                 if(find_canvas_row_(canvas,sub_canvas,iter2->children(),iter))
1011                         return true;
1012         }
1013
1014         iter=children().end();
1015         return false;
1016 }
1017
1018 bool
1019 LayerTreeStore::find_canvas_row(synfig::Canvas::Handle canvas, Gtk::TreeModel::Children::iterator &iter)
1020 {
1021         return find_canvas_row_(canvas,canvas_interface()->get_canvas(),children(),iter);
1022 }
1023
1024
1025 bool
1026 LayerTreeStore::find_layer_row_(const synfig::Layer::Handle &layer, synfig::Canvas::Handle /*canvas*/, Gtk::TreeModel::Children layers, Gtk::TreeModel::Children::iterator &iter, Gtk::TreeModel::Children::iterator &prev)
1027 {
1028         assert(layer);
1029
1030         //if(layer->get_canvas()==canvas)
1031         {
1032                 for(iter=prev=layers.begin(); iter && iter != layers.end(); prev=iter++)
1033                 {
1034                         Gtk::TreeModel::Row row = *iter;
1035                         if(layer==(synfig::Layer::Handle)row[model.layer])
1036                                 return true;
1037                 }
1038
1039                 iter=children().end();
1040                 //return false;
1041         }
1042
1043         Gtk::TreeModel::Children::iterator iter2;
1044
1045         for(iter2 = layers.begin(); iter2 && iter2 != layers.end(); ++iter2)
1046         {
1047                 Gtk::TreeModel::Row row = *iter2;
1048                 assert((bool)true);
1049
1050                 if(row.children().empty())
1051                         continue;
1052
1053                 Canvas::Handle canvas((*row.children().begin())[model.canvas]);
1054                 if(!canvas)
1055                         continue;
1056
1057                 if(find_layer_row_(layer,canvas,iter2->children(),iter,prev))
1058                         return true;
1059         }
1060
1061         iter=children().end();
1062         return false;
1063 }
1064
1065 bool
1066 LayerTreeStore::find_layer_row(const synfig::Layer::Handle &layer, Gtk::TreeModel::Children::iterator &iter)
1067 {
1068         Gtk::TreeModel::Children::iterator prev;
1069         return find_layer_row_(layer,canvas_interface()->get_canvas(),children(),iter,prev);
1070 }
1071
1072 bool
1073 LayerTreeStore::find_prev_layer_row(const synfig::Layer::Handle &layer, Gtk::TreeModel::Children::iterator &prev)
1074 {
1075         Gtk::TreeModel::Children::iterator iter;
1076         if(!find_layer_row_(layer,canvas_interface()->get_canvas(),children(),iter,prev))
1077                 return false;
1078         if(iter==children().begin())
1079                 return false;
1080         return true;
1081 }