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