1 /* === S Y N F I G ========================================================= */
2 /*! \file canvastreestore.cpp
3 ** \brief Template File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2008 Chris Moore
11 ** This package is free software; you can redistribute it and/or
12 ** modify it under the terms of the GNU General Public License as
13 ** published by the Free Software Foundation; either version 2 of
14 ** the License, or (at your option) any later version.
16 ** This package is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ** General Public License for more details.
22 /* ========================================================================= */
24 /* === H E A D E R S ======================================================= */
33 #include "trees/canvastreestore.h"
34 #include <synfig/valuenode.h>
35 #include "iconcontroller.h"
36 #include <synfig/valuenode_timedswap.h>
37 #include <synfig/valuenode_animated.h>
38 #include <gtkmm/button.h>
39 #include <synfigapp/instance.h>
40 #include "cellrenderer/cellrenderer_value.h"
41 #include "cellrenderer/cellrenderer_timetrack.h"
48 /* === U S I N G =========================================================== */
52 using namespace synfig;
53 using namespace studio;
55 /* === M A C R O S ========================================================= */
57 /* === G L O B A L S ======================================================= */
59 /* === P R O C E D U R E S ================================================= */
61 /* === M E T H O D S ======================================================= */
63 static CanvasTreeStore::Model& ModelHack()
65 static CanvasTreeStore::Model* model(0);
66 if(!model)model=new CanvasTreeStore::Model;
70 CanvasTreeStore::CanvasTreeStore(etl::loose_handle<synfigapp::CanvasInterface> canvas_interface_):
71 Gtk::TreeStore(ModelHack()),
72 canvas_interface_ (canvas_interface_)
76 CanvasTreeStore::~CanvasTreeStore()
81 CanvasTreeStore::get_value_vfunc(const Gtk::TreeModel::iterator& iter, int column, Glib::ValueBase& value)const
83 if(column==model.value.index())
85 synfigapp::ValueDesc value_desc((*iter)[model.value_desc]);
87 Glib::Value<synfig::ValueBase> x;
88 g_value_init(x.gobj(),x.value_type());
95 if(value_desc.is_const())
96 x.set(value_desc.get_value());
98 if(value_desc.is_value_node())
99 x.set((*value_desc.get_value_node())(canvas_interface()->get_time()));
102 synfig::error(__FILE__":%d: Unable to figure out value",__LINE__);
106 g_value_init(value.gobj(),x.value_type());
107 g_value_copy(x.gobj(),value.gobj());
110 if(column==model.is_value_node.index())
112 synfigapp::ValueDesc value_desc((*iter)[model.value_desc]);
115 g_value_init(x.gobj(),x.value_type());
117 x.set(value_desc && value_desc.is_value_node());
119 g_value_init(value.gobj(),x.value_type());
120 g_value_copy(x.gobj(),value.gobj());
123 if(column==model.is_shared.index())
125 synfigapp::ValueDesc value_desc((*iter)[model.value_desc]);
128 g_value_init(x.gobj(),x.value_type());
130 x.set(value_desc.is_value_node() && value_desc.get_value_node()->rcount()>1);
132 g_value_init(value.gobj(),x.value_type());
133 g_value_copy(x.gobj(),value.gobj());
136 if(column==model.is_exported.index())
138 synfigapp::ValueDesc value_desc((*iter)[model.value_desc]);
141 g_value_init(x.gobj(),x.value_type());
143 x.set(value_desc.is_value_node() && value_desc.get_value_node()->is_exported());
145 g_value_init(value.gobj(),x.value_type());
146 g_value_copy(x.gobj(),value.gobj());
149 if(column==model.is_canvas.index())
151 synfigapp::ValueDesc value_desc((*iter)[model.value_desc]);
154 g_value_init(x.gobj(),x.value_type());
156 x.set(!value_desc && (Canvas::Handle)(*iter)[model.canvas]);
158 g_value_init(value.gobj(),x.value_type());
159 g_value_copy(x.gobj(),value.gobj());
162 if(column==model.id.index())
164 synfigapp::ValueDesc value_desc((*iter)[model.value_desc]);
166 Glib::Value<Glib::ustring> x;
167 g_value_init(x.gobj(),x.value_type());
169 if(value_desc && value_desc.is_value_node())
170 x.set(value_desc.get_value_node()->get_id());
171 else if(!value_desc && Canvas::Handle((*iter)[model.canvas]))
172 x.set(Canvas::Handle((*iter)[model.canvas])->get_id());
174 return Gtk::TreeStore::get_value_vfunc(iter,column,value);
176 g_value_init(value.gobj(),x.value_type());
177 g_value_copy(x.gobj(),value.gobj());
180 if(column==model.is_editable.index())
182 synfigapp::ValueDesc value_desc((*iter)[model.value_desc]);
185 g_value_init(x.gobj(),x.value_type());
187 x.set(!value_desc.is_value_node() || synfigapp::is_editable(value_desc.get_value_node()));
189 g_value_init(value.gobj(),x.value_type());
190 g_value_copy(x.gobj(),value.gobj());
193 if(column==model.type.index())
195 synfigapp::ValueDesc value_desc((*iter)[model.value_desc]);
197 Glib::Value<Glib::ustring> x;
198 g_value_init(x.gobj(),x.value_type());
203 if((*iter)[model.is_canvas])
208 if(!value_desc.is_value_node() || value_desc.get_value_node()->get_name()=="constant")
210 x.set(ValueBase::type_local_name(value_desc.get_value_type()));
214 x.set(value_desc.get_value_node()->get_local_name());
218 g_value_init(value.gobj(),x.value_type());
219 g_value_copy(x.gobj(),value.gobj());
222 if(column==model.label.index())
224 synfigapp::ValueDesc value_desc((*iter)[model.value_desc]);
226 Glib::Value<Glib::ustring> x;
227 g_value_init(x.gobj(),x.value_type());
232 Canvas::Handle canvas((*iter)[model.canvas]);
235 if(!canvas->get_id().empty())
236 x.set(canvas->get_id());
238 if(!canvas->get_name().empty())
239 x.set(canvas->get_name());
241 x.set(_("[Unnamed]"));
244 return Gtk::TreeStore::get_value_vfunc(iter,column,value);
248 ValueNode::Handle value_node=value_desc.get_value_node();
250 // Setup the row's label
251 if(value_node->get_id().empty())
252 x.set(Glib::ustring((*iter)[model.name]));
253 else if(Glib::ustring((*iter)[model.name]).empty())
254 x.set(value_node->get_id());
256 x.set(Glib::ustring((*iter)[model.name])+" ("+value_node->get_id()+')');
259 g_value_init(value.gobj(),x.value_type());
260 g_value_copy(x.gobj(),value.gobj());
263 if(column==model.icon.index())
265 synfigapp::ValueDesc value_desc((*iter)[model.value_desc]);
267 return Gtk::TreeStore::get_value_vfunc(iter,column,value);
269 Glib::Value<Glib::RefPtr<Gdk::Pixbuf> > x;
270 g_value_init(x.gobj(),x.value_type());
272 x.set(get_tree_pixbuf(value_desc.get_value_type()));
274 g_value_init(value.gobj(),x.value_type());
275 g_value_copy(x.gobj(),value.gobj());
278 Gtk::TreeStore::get_value_vfunc(iter,column,value);
282 CanvasTreeStore::find_first_value_desc(const synfigapp::ValueDesc& value_desc, Gtk::TreeIter& iter)
284 iter=children().begin();
285 while(iter && value_desc!=(*iter)[model.value_desc])
287 if(!iter->children().empty())
289 Gtk::TreeIter iter2(iter->children().begin());
290 //! \todo confirm that the && should be done before the ||
291 if((iter2 && value_desc==(*iter2)[model.value_desc]) || find_next_value_desc(value_desc, iter2))
297 Gtk::TreeIter iter2(++iter);
299 iter==iter->parent();
303 return (bool)iter && value_desc==(*iter)[model.value_desc];
307 CanvasTreeStore::find_next_value_desc(const synfigapp::ValueDesc& value_desc, Gtk::TreeIter& iter)
309 if(!iter) return find_first_value_desc(value_desc,iter);
312 if(!iter->children().empty())
314 Gtk::TreeIter iter2(iter->children().begin());
315 //! \todo confirm that the && should be done before the ||
316 if((iter2 && value_desc==(*iter2)[model.value_desc]) || find_next_value_desc(value_desc, iter2))
322 Gtk::TreeIter iter2(++iter);
325 iter==iter->parent();
330 } while(iter && value_desc!=(*iter)[model.value_desc]);
331 return (bool)iter && value_desc==(*iter)[model.value_desc];
335 CanvasTreeStore::find_first_value_node(const synfig::ValueNode::Handle& value_node, Gtk::TreeIter& iter)
337 iter=children().begin();
338 while(iter && value_node!=(ValueNode::Handle)(*iter)[model.value_node])
340 if(!iter->children().empty())
342 Gtk::TreeIter iter2(iter->children().begin());
343 //! \todo confirm that the && should be done before the ||
344 if((iter2 && value_node==(ValueNode::Handle)(*iter2)[model.value_node]) || find_next_value_node(value_node, iter2))
350 Gtk::TreeIter iter2(++iter);
352 iter==iter->parent();
356 return (bool)iter && value_node==(ValueNode::Handle)(*iter)[model.value_node];
360 CanvasTreeStore::find_next_value_node(const synfig::ValueNode::Handle& value_node, Gtk::TreeIter& iter)
362 if(!iter) return find_first_value_node(value_node,iter);
365 if(!iter->children().empty())
367 Gtk::TreeIter iter2(iter->children().begin());
368 //! \todo confirm that the && should be done before the ||
369 if((iter2 && value_node==(ValueNode::Handle)(*iter2)[model.value_node]) || find_next_value_node(value_node, iter2))
375 Gtk::TreeIter iter2(++iter);
378 iter==iter->parent();
383 } while(iter && value_node!=(ValueNode::Handle)(*iter)[model.value_node]);
384 return (bool)iter && value_node==(ValueNode::Handle)(*iter)[model.value_node];
388 CanvasTreeStore::set_row(Gtk::TreeRow row,synfigapp::ValueDesc value_desc, bool do_children)
390 Gtk::TreeModel::Children children = row.children();
391 while(!children.empty() && erase(children.begin()))
394 row[model.value_desc]=value_desc;
397 //row[model.icon] = get_tree_pixbuf(value_desc.get_value_type());
399 if(value_desc.is_value_node())
401 ValueNode::Handle value_node=value_desc.get_value_node();
405 row[model.value_node] = value_node;
406 //row[model.is_canvas] = false;
407 //row[model.is_value_node] = true;
408 //row[model.is_editable] = synfigapp::is_editable(value_node);
409 //row[model.id]=value_node->get_id();
412 if(value_desc.parent_is_canvas())
413 row[model.canvas]=value_desc.get_canvas();
415 row[model.canvas]=canvas_interface()->get_canvas();
417 LinkableValueNode::Handle linkable;
418 linkable=LinkableValueNode::Handle::cast_dynamic(value_node);
420 if(linkable && do_children)
422 row[model.link_count] = linkable->link_count();
423 for(int i=0;i<linkable->link_count();i++)
425 Gtk::TreeRow child_row=*(append(row.children()));
426 child_row[model.link_id] = i;
427 child_row[model.canvas] = static_cast<Canvas::Handle>(row[model.canvas]);
428 child_row[model.name] = linkable->link_local_name(i);
429 set_row(child_row,synfigapp::ValueDesc(linkable,i));
436 //row[model.is_value_node] = false;
437 //row[model.is_editable] = true;
438 //row[model.label] = Glib::ustring(row[model.name]);
442 catch(synfig::Exception::IDNotFound x)
444 synfig::error(__FILE__":%d: IDNotFound thrown",__LINE__);
449 // We should never get to this point
454 CanvasTreeStore::refresh_row(Gtk::TreeModel::Row &row, bool do_children)
456 synfigapp::ValueDesc value_desc=row[model.value_desc];
460 if((bool)row[model.is_value_node] != value_desc.is_value_node() ||
461 (!bool(row[model.is_value_node]) && row[model.link_count]!=0))
463 set_row(row,value_desc,do_children);
467 if(row[model.is_value_node])
469 ValueNode::Handle value_node(value_desc.get_value_node());
471 if(ValueNode::Handle(row[model.value_node])!=value_node)
473 rebuild_row(row,do_children);
477 //row[model.id]=value_node->get_id();
479 // Setup the row's label
481 if(value_node->get_id().empty())
482 row[model.label] = Glib::ustring(row[model.name]);
483 else if(Glib::ustring(row[model.name]).empty())
484 row[model.label] = value_node->get_id();
486 row[model.label] = Glib::ustring(row[model.name])+" ("+value_node->get_id()+')';
489 LinkableValueNode::Handle linkable;
490 linkable=LinkableValueNode::Handle::cast_dynamic(value_node);
491 if(do_children && linkable && ((int)row[model.link_count] != linkable->link_count()))
493 // Gtk::TreeModel::Children children = row.children();
494 // while(!children.empty() && erase(children.begin()));
496 set_row(row,value_desc);
502 //row[model.label] = Glib::ustring(row[model.name]);
503 //row[model.is_value_node] = false;
504 //row[model.is_editable] = true;
510 Gtk::TreeModel::Children children = row.children();
511 Gtk::TreeModel::Children::iterator iter;
513 if(!children.empty())
514 for(iter = children.begin(); iter != children.end(); ++iter)
516 Gtk::TreeRow row=*iter;
522 CanvasTreeStore::rebuild_row(Gtk::TreeModel::Row &row, bool do_children)
524 synfigapp::ValueDesc value_desc=(synfigapp::ValueDesc)row[model.value_desc];
526 if(value_desc && value_desc.get_value_node())
528 ValueNode::Handle value_node;
529 value_node=value_desc.get_value_node();
531 assert(value_node);if(!value_node)return;
533 if(value_node && value_node!=(ValueNode::Handle)row[model.value_node])
535 // Gtk::TreeModel::Children children = row.children();
536 // while(!children.empty() && erase(children.begin()));
538 set_row(row,value_desc,do_children);
542 LinkableValueNode::Handle linkable;
543 linkable=LinkableValueNode::Handle::cast_dynamic(value_node);
545 if( do_children && linkable && (int)row[model.link_count] != linkable->link_count())
547 // Gtk::TreeModel::Children children = row.children();
548 // while(!children.empty() && erase(children.begin()));
550 set_row(row,value_desc);
555 // value_node=row[model.value_node];
557 row[model.id]=value_node->get_id();
559 // Setup the row's label
560 if(value_node->get_id().empty())
561 row[model.label] = Glib::ustring(row[model.name]);
562 else if(Glib::ustring(row[model.name]).empty())
563 row[model.label] = value_node->get_id();
565 row[model.label] = Glib::ustring(row[model.name])+" ("+value_node->get_id()+')';
569 row[model.label] = Glib::ustring(row[model.name]);
570 row[model.is_value_node] = false;
571 row[model.is_editable] = true;
572 Gtk::TreeModel::Children children = row.children();
573 while(!children.empty() && erase(children.begin()))
579 Gtk::TreeModel::Children children = row.children();
580 Gtk::TreeModel::Children::iterator iter;
581 if(!children.empty())
582 for(iter = children.begin(); iter != children.end(); ++iter)
584 Gtk::TreeRow row=*iter;
589 CellRenderer_ValueBase*
590 CanvasTreeStore::add_cell_renderer_value(Gtk::TreeView::Column* column)
592 const CanvasTreeStore::Model model;
594 CellRenderer_ValueBase* ret;
596 ret=Gtk::manage( new CellRenderer_ValueBase() );
598 column->pack_start(*ret,true);
599 column->add_attribute(ret->property_value(), model.value);
600 column->add_attribute(ret->property_editable(), model.is_editable);
601 column->add_attribute(ret->property_canvas(), model.canvas);
606 CellRenderer_TimeTrack*
607 CanvasTreeStore::add_cell_renderer_value_node(Gtk::TreeView::Column* column)
609 const CanvasTreeStore::Model model;
611 CellRenderer_TimeTrack* ret;
613 ret = Gtk::manage( new CellRenderer_TimeTrack() );
615 column->pack_start(*ret,true);
616 //column->add_attribute(ret->property_visible(), model.is_value_node);
617 column->add_attribute(ret->property_value_desc(), model.value_desc);
618 column->add_attribute(ret->property_canvas(), model.canvas);