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