Fix some FTBFS issues when compiling with GCC 4.3
[synfig.git] / synfig-studio / trunk / src / gtkmm / layerparamtreestore.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file layerparamtreestore.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 "layerparamtreestore.h"
33 #include "iconcontroller.h"
34 #include <gtkmm/button.h>
35 #include <synfig/paramdesc.h>
36 #include "layertree.h"
37 #include <synfigapp/action_system.h>
38 #include <synfigapp/instance.h>
39 #include "app.h"
40 #include <ETL/clock>
41
42 #include "general.h"
43
44 #endif
45
46 /* === U S I N G =========================================================== */
47
48 using namespace std;
49 using namespace etl;
50 using namespace synfig;
51 using namespace studio;
52
53 /* === M A C R O S ========================================================= */
54
55 class Profiler : private etl::clock
56 {
57         const std::string name;
58 public:
59         Profiler(const std::string& name):name(name) { reset(); }
60         ~Profiler() { float time(operator()()); synfig::info("%s: took %f msec",name.c_str(),time*1000); }
61 };
62
63 /* === G L O B A L S ======================================================= */
64
65 /* === P R O C E D U R E S ================================================= */
66
67 /* === M E T H O D S ======================================================= */
68
69 static LayerParamTreeStore::Model& ModelHack()
70 {
71         static LayerParamTreeStore::Model* model(0);
72         if(!model)model=new LayerParamTreeStore::Model;
73         return *model;
74 }
75
76 LayerParamTreeStore::LayerParamTreeStore(etl::loose_handle<synfigapp::CanvasInterface> canvas_interface_,LayerTree* layer_tree):
77         Gtk::TreeStore                  (ModelHack()),
78         CanvasTreeStore                 (canvas_interface_),
79         layer_tree                              (layer_tree)
80 {
81         queued=false;
82         // Connect all the signals
83         canvas_interface()->signal_value_node_changed().connect(sigc::mem_fun(*this,&studio::LayerParamTreeStore::on_value_node_changed));
84         canvas_interface()->signal_value_node_renamed().connect(sigc::mem_fun(*this,&studio::LayerParamTreeStore::on_value_node_renamed));
85         canvas_interface()->signal_value_node_added().connect(sigc::mem_fun(*this,&studio::LayerParamTreeStore::on_value_node_added));
86         canvas_interface()->signal_value_node_deleted().connect(sigc::mem_fun(*this,&studio::LayerParamTreeStore::on_value_node_deleted));
87         canvas_interface()->signal_value_node_replaced().connect(sigc::mem_fun(*this,&studio::LayerParamTreeStore::on_value_node_replaced));
88         canvas_interface()->signal_layer_param_changed().connect(sigc::mem_fun(*this,&studio::LayerParamTreeStore::on_layer_param_changed));
89
90         canvas_interface()->signal_value_node_child_added().connect(sigc::mem_fun(*this,&studio::LayerParamTreeStore::on_value_node_child_added));
91         canvas_interface()->signal_value_node_child_removed().connect(sigc::mem_fun(*this,&studio::LayerParamTreeStore::on_value_node_child_removed));
92
93
94         layer_tree->get_selection()->signal_changed().connect(sigc::mem_fun(*this,&LayerParamTreeStore::queue_rebuild));
95
96         signal_changed().connect(sigc::mem_fun(*this,&LayerParamTreeStore::queue_refresh));
97         rebuild();
98 }
99
100 LayerParamTreeStore::~LayerParamTreeStore()
101 {
102         while(!changed_connection_list.empty())
103         {
104                 changed_connection_list.back().disconnect();
105                 changed_connection_list.pop_back();
106         }
107         synfig::info("LayerParamTreeStore::~LayerParamTreeStore(): Deleted");
108 }
109
110 Glib::RefPtr<LayerParamTreeStore>
111 LayerParamTreeStore::create(etl::loose_handle<synfigapp::CanvasInterface> canvas_interface_, LayerTree*layer_tree)
112 {
113         return Glib::RefPtr<LayerParamTreeStore>(new LayerParamTreeStore(canvas_interface_,layer_tree));
114 }
115
116
117
118 void
119 LayerParamTreeStore::get_value_vfunc (const Gtk::TreeModel::iterator& iter, int column, Glib::ValueBase& value)const
120 {
121         if(column<0)
122         {
123                 synfig::error("LayerParamTreeStore::get_value_vfunc(): Bad column!");
124                 return;
125         }
126
127 /*      if(column==model.label.index())
128         {
129                 synfig::Layer::Handle layer((*iter)[model.layer]);
130
131                 if(!layer)return;
132
133                 Glib::Value<Glib::ustring> x;
134                 g_value_init(x.gobj(),x.value_type());
135
136                 x.set(layer->get_non_empty_description());
137
138                 g_value_init(value.gobj(),x.value_type());
139                 g_value_copy(x.gobj(),value.gobj());
140         }
141         else
142 */
143         if(column==model.label.index())
144         {
145                 synfigapp::ValueDesc value_desc((*iter)[model.value_desc]);
146                 Glib::ustring label;
147
148                 if(!(*iter)[model.is_toplevel])
149                         return CanvasTreeStore::get_value_vfunc(iter,column,value);
150                 synfig::ParamDesc param_desc((*iter)[model.param_desc]);
151                 label=param_desc.get_local_name();
152
153                 if(!(*iter)[model.is_inconsistent])
154                 if(value_desc.is_value_node() && value_desc.get_value_node()->is_exported())
155                 {
156                         label+=strprintf(" (%s)",value_desc.get_value_node()->get_id().c_str());
157                 }
158
159                 Glib::Value<Glib::ustring> x;
160                 g_value_init(x.gobj(),x.value_type());
161
162                 x.set(label);
163
164                 g_value_init(value.gobj(),x.value_type());
165                 g_value_copy(x.gobj(),value.gobj());
166         }
167         else
168         if(column==model.is_toplevel.index())
169         {
170                 Glib::Value<bool> x;
171                 g_value_init(x.gobj(),x.value_type());
172
173                 TreeModel::Path path(get_path(iter));
174
175                 x.set(path.get_depth()<=1);
176
177                 g_value_init(value.gobj(),x.value_type());
178                 g_value_copy(x.gobj(),value.gobj());
179         }
180         else
181         if(column==model.is_inconsistent.index())
182         {
183                 if((*iter)[model.is_toplevel])
184                 {
185                         CanvasTreeStore::get_value_vfunc(iter,column,value);
186                         return;
187                 }
188
189                 Glib::Value<bool> x;
190                 g_value_init(x.gobj(),x.value_type());
191
192                 x.set(false);
193
194                 g_value_init(value.gobj(),x.value_type());
195                 g_value_copy(x.gobj(),value.gobj());
196         }
197         else
198         CanvasTreeStore::get_value_vfunc(iter,column,value);
199 }
200
201
202
203 void
204 LayerParamTreeStore::set_value_impl(const Gtk::TreeModel::iterator& iter, int column, const Glib::ValueBase& value)
205 {
206         //if(!iterator_sane(row))
207         //      return;
208
209         if(column>=get_n_columns_vfunc())
210         {
211                 g_warning("LayerTreeStore::set_value_impl: Bad column (%d)",column);
212                 return;
213         }
214
215         if(!g_value_type_compatible(G_VALUE_TYPE(value.gobj()),get_column_type_vfunc(column)))
216         {
217                 g_warning("LayerTreeStore::set_value_impl: Bad value type");
218                 return;
219         }
220
221         try
222         {
223                 if(column==model.value.index())
224                 {
225                         Glib::Value<synfig::ValueBase> x;
226                         g_value_init(x.gobj(),model.value.type());
227                         g_value_copy(value.gobj(),x.gobj());
228
229                         if((bool)(*iter)[model.is_toplevel])
230                         {
231                                 synfigapp::Action::PassiveGrouper group(canvas_interface()->get_instance().get(),_("Set Layer Params"));
232
233                                 synfig::ParamDesc param_desc((*iter)[model.param_desc]);
234
235                                 LayerList::iterator iter2(layer_list.begin());
236
237                                 for(;iter2!=layer_list.end();++iter2)
238                                 {
239                                         if(!canvas_interface()->change_value(synfigapp::ValueDesc(*iter2,param_desc.get_name()),x.get()))
240                                         {
241                                                 // ERROR!
242                                                 group.cancel();
243                                                 App::dialog_error_blocking(_("Error"),_("Unable to set all layer parameters."));
244
245                                                 return;
246                                         }
247                                 }
248                         }
249                         else
250                         {
251                                 canvas_interface()->change_value((*iter)[model.value_desc],x.get());
252                         }
253                         return;
254                 }
255                 else
256 /*
257                 if(column==model.active.index())
258                 {
259                         synfig::Layer::Handle layer((*iter)[model.layer]);
260
261                         if(!layer)return;
262
263                         Glib::Value<bool> x;
264                         g_value_init(x.gobj(),model.active.type());
265                         g_value_copy(value.gobj(),x.gobj());
266
267                         synfigapp::Action::Handle action(synfigapp::Action::create("layer_activate"));
268
269                         if(!action)
270                                 return;
271
272                         action->set_param("canvas",canvas_interface()->get_canvas());
273                         action->set_param("canvas_interface",canvas_interface());
274                         action->set_param("layer",layer);
275                         action->set_param("new_status",bool(x.get()));
276
277                         canvas_interface()->get_instance()->perform_action(action);
278                         return;
279                 }
280                 else
281 */
282                 CanvasTreeStore::set_value_impl(iter,column, value);
283         }
284         catch(std::exception x)
285         {
286                 g_warning(x.what());
287         }
288 }
289
290
291
292
293
294
295
296
297
298
299 void
300 LayerParamTreeStore::rebuild()
301 {
302         // Profiler profiler("LayerParamTreeStore::rebuild()");
303         if(queued)queued=false;
304         clear();
305         layer_list=layer_tree->get_selected_layers();
306
307         if(layer_list.size()<=0)
308                 return;
309
310         // Get rid of all the connections,
311         // and clear the connection map.
312         //while(!connection_map.empty())connection_map.begin()->second.disconnect(),connection_map.erase(connection_map.begin());
313         while(!changed_connection_list.empty())
314         {
315                 changed_connection_list.back().disconnect();
316                 changed_connection_list.pop_back();
317         }
318
319         struct REBUILD_HELPER
320         {
321                 ParamVocab vocab;
322
323                 static ParamVocab::iterator find_param_desc(ParamVocab& vocab, const synfig::String& x)
324                 {
325                         ParamVocab::iterator iter;
326
327                         for(iter=vocab.begin();iter!=vocab.end();++iter)
328                                 if(iter->get_name()==x)
329                                         break;
330                         return iter;
331                 }
332
333                 void process_vocab(ParamVocab x)
334                 {
335                         ParamVocab::iterator iter;
336
337                         for(iter=vocab.begin();iter!=vocab.end();++iter)
338                         {
339                                 ParamVocab::iterator iter2(find_param_desc(x,iter->get_name()));
340                                 if(iter2==x.end())
341                                 {
342                                         // remove it and start over
343                                         vocab.erase(iter);
344                                         iter=vocab.begin();
345                                         iter--;
346                                         continue;
347                                 }
348                         }
349                 }
350
351         } rebuild_helper;
352
353
354         {
355                 LayerList::iterator iter(layer_list.begin());
356                 rebuild_helper.vocab=(*iter)->get_param_vocab();
357
358                 for(++iter;iter!=layer_list.end();++iter)
359                 {
360                         rebuild_helper.process_vocab((*iter)->get_param_vocab());
361                         changed_connection_list.push_back(
362                                 (*iter)->signal_changed().connect(
363                                         sigc::mem_fun(
364                                                 *this,
365                                                 &LayerParamTreeStore::changed
366                                         )
367                                 )
368                         );
369                 }
370         }
371
372         ParamVocab::iterator iter;
373         for(iter=rebuild_helper.vocab.begin();iter!=rebuild_helper.vocab.end();++iter)
374         {
375                 if(iter->get_hidden())
376                         continue;
377
378                 /*
379                 if(iter->get_animation_only())
380                 {
381                         int length(layer_list.front()->get_canvas()->rend_desc().get_frame_end()-layer_list.front()->get_canvas()->rend_desc().get_frame_start());
382                         if(!length)
383                                 continue;
384                 }
385                 */
386                 Gtk::TreeRow row(*(append()));
387                 synfigapp::ValueDesc value_desc(layer_list.front(),iter->get_name());
388                 CanvasTreeStore::set_row(row,value_desc);
389                 if(value_desc.is_value_node())
390                 {
391                         changed_connection_list.push_back(
392                                 value_desc.get_value_node()->signal_changed().connect(
393                                         sigc::mem_fun(
394                                                 this,
395                                                 &LayerParamTreeStore::changed
396                                         )
397                                 )
398                         );
399                 }
400                 if(value_desc.get_value_type()==ValueBase::TYPE_CANVAS)
401                 {
402                         Canvas::Handle canvas_handle = value_desc.get_value().get(Canvas::Handle());
403                         if(canvas_handle) changed_connection_list.push_back(
404                                 canvas_handle->signal_changed().connect(
405                                         sigc::mem_fun(
406                                                 this,
407                                                 &LayerParamTreeStore::changed
408                                         )
409                                 )
410                         );
411                 }
412                 //row[model.label] = iter->get_local_name();
413                 row[model.param_desc] = *iter;
414                 row[model.canvas] = layer_list.front()->get_canvas();
415                 row[model.is_inconsistent] = false;
416                 //row[model.is_toplevel] = true;
417
418
419                 LayerList::iterator iter2(layer_list.begin());
420                 ValueBase value((*iter2)->get_param(iter->get_name()));
421                 for(++iter2;iter2!=layer_list.end();++iter2)
422                 {
423                         if(value!=((*iter2)->get_param(iter->get_name())))
424                         {
425                                 row[model.is_inconsistent] = true;
426                                 while(!row.children().empty() && erase(row.children().begin()));
427                                 break;
428                         }
429                 }
430         }
431 }
432
433 void
434 LayerParamTreeStore::queue_refresh()
435 {
436         if(queued)
437                 return;
438         queued=1;
439         queue_connection.disconnect();
440         queue_connection=Glib::signal_timeout().connect(
441                 sigc::bind_return(
442                         sigc::mem_fun(*this,&LayerParamTreeStore::refresh),
443                         false
444                 )
445         ,150);
446
447 }
448
449 void
450 LayerParamTreeStore::queue_rebuild()
451 {
452         if(queued==2)
453                 return;
454         queued=2;
455         queue_connection.disconnect();
456         queue_connection=Glib::signal_timeout().connect(
457                 sigc::bind_return(
458                         sigc::mem_fun(*this,&LayerParamTreeStore::rebuild),
459                         false
460                 )
461         ,150);
462
463 }
464
465 void
466 LayerParamTreeStore::refresh()
467 {
468         if(queued)queued=false;
469
470         Gtk::TreeModel::Children children_(children());
471
472         Gtk::TreeModel::Children::iterator iter;
473
474         if(!children_.empty())
475                 for(iter = children_.begin(); iter && iter != children_.end(); ++iter)
476                 {
477                         Gtk::TreeRow row=*iter;
478                         refresh_row(row);
479                 }
480 }
481
482 void
483 LayerParamTreeStore::refresh_row(Gtk::TreeModel::Row &row)
484 {
485         if(row[model.is_toplevel])
486         {
487                 row[model.is_inconsistent] = false;
488                 ParamDesc param_desc(row[model.param_desc]);
489
490                 LayerList::iterator iter2(layer_list.begin());
491                 ValueBase value((*iter2)->get_param(param_desc.get_name()));
492                 for(++iter2;iter2!=layer_list.end();++iter2)
493                 {
494                         if(value!=((*iter2)->get_param(param_desc.get_name())))
495                         {
496                                 row[model.is_inconsistent] = true;
497                                 while(!row.children().empty() && erase(row.children().begin()));
498                                 return;
499                         }
500                 }
501         }
502
503         //handle<ValueNode> value_node=row[model.value_node];
504         //if(value_node)
505         {
506                 CanvasTreeStore::refresh_row(row);
507                 return;
508         }
509 }
510
511 void
512 LayerParamTreeStore::set_row(Gtk::TreeRow row,synfigapp::ValueDesc value_desc)
513 {
514         Gtk::TreeModel::Children children = row.children();
515         while(!children.empty() && erase(children.begin()));
516
517         CanvasTreeStore::set_row(row,value_desc);
518 }
519
520 void
521 LayerParamTreeStore::on_value_node_added(synfig::ValueNode::Handle /*value_node*/)
522 {
523 //      queue_refresh();
524 }
525
526 void
527 LayerParamTreeStore::on_value_node_deleted(synfig::ValueNode::Handle /*value_node*/)
528 {
529 //      queue_refresh();
530 }
531
532 void
533 LayerParamTreeStore::on_value_node_child_added(synfig::ValueNode::Handle /*value_node*/,synfig::ValueNode::Handle /*child*/)
534 {
535         queue_rebuild();
536 }
537
538 void
539 LayerParamTreeStore::on_value_node_child_removed(synfig::ValueNode::Handle /*value_node*/,synfig::ValueNode::Handle /*child*/)
540 {
541         rebuild();
542 }
543
544 void
545 LayerParamTreeStore::on_value_node_changed(synfig::ValueNode::Handle /*value_node*/)
546 {
547         queue_refresh();
548 }
549
550 void
551 LayerParamTreeStore::on_value_node_renamed(synfig::ValueNode::Handle /*value_node*/)
552 {
553         rebuild();
554 }
555
556 void
557 LayerParamTreeStore::on_value_node_replaced(synfig::ValueNode::Handle /*replaced_value_node*/,synfig::ValueNode::Handle /*new_value_node*/)
558 {
559         queue_rebuild();
560 }
561
562 void
563 LayerParamTreeStore::on_layer_param_changed(synfig::Layer::Handle /*handle*/,synfig::String /*param_name*/)
564 {
565         queue_refresh();
566 }