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