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