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
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.
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.
21 /* ========================================================================= */
23 /* === H E A D E R S ======================================================= */
32 #include "canvastreestore.h"
33 #include <synfig/valuenode.h>
34 #include "iconcontroler.h"
35 #include <synfig/valuenode_timedswap.h>
36 #include <synfig/valuenode_animated.h>
37 #include <gtkmm/button.h>
38 #include <synfigapp/instance.h>
39 #include "cellrenderer_value.h"
40 #include "cellrenderer_timetrack.h"
45 /* === U S I N G =========================================================== */
49 using namespace synfig;
50 using namespace studio;
52 /* === M A C R O S ========================================================= */
54 /* === G L O B A L S ======================================================= */
56 /* === P R O C E D U R E S ================================================= */
58 /* === M E T H O D S ======================================================= */
60 static CanvasTreeStore::Model& ModelHack()
62 static CanvasTreeStore::Model* model(0);
63 if(!model)model=new CanvasTreeStore::Model;
67 CanvasTreeStore::CanvasTreeStore(etl::loose_handle<synfigapp::CanvasInterface> canvas_interface_):
68 Gtk::TreeStore(ModelHack()),
69 canvas_interface_ (canvas_interface_)
73 CanvasTreeStore::~CanvasTreeStore()
78 CanvasTreeStore::get_value_vfunc(const Gtk::TreeModel::iterator& iter, int column, Glib::ValueBase& value)const
80 if(column==model.value.index())
82 synfigapp::ValueDesc value_desc((*iter)[model.value_desc]);
84 Glib::Value<synfig::ValueBase> x;
85 g_value_init(x.gobj(),x.value_type());
92 if(value_desc.is_const())
93 x.set(value_desc.get_value());
95 if(value_desc.is_value_node())
96 x.set((*value_desc.get_value_node())(canvas_interface()->get_time()));
99 synfig::error(__FILE__":%d: Unable to figure out value",__LINE__);
103 g_value_init(value.gobj(),x.value_type());
104 g_value_copy(x.gobj(),value.gobj());
107 if(column==model.is_value_node.index())
109 synfigapp::ValueDesc value_desc((*iter)[model.value_desc]);
112 g_value_init(x.gobj(),x.value_type());
114 x.set(value_desc && value_desc.is_value_node());
116 g_value_init(value.gobj(),x.value_type());
117 g_value_copy(x.gobj(),value.gobj());
120 if(column==model.is_shared.index())
122 synfigapp::ValueDesc value_desc((*iter)[model.value_desc]);
125 g_value_init(x.gobj(),x.value_type());
127 x.set(value_desc.is_value_node() && value_desc.get_value_node()->rcount()>1);
129 g_value_init(value.gobj(),x.value_type());
130 g_value_copy(x.gobj(),value.gobj());
133 if(column==model.is_exported.index())
135 synfigapp::ValueDesc value_desc((*iter)[model.value_desc]);
138 g_value_init(x.gobj(),x.value_type());
140 x.set(value_desc.is_value_node() && value_desc.get_value_node()->is_exported());
142 g_value_init(value.gobj(),x.value_type());
143 g_value_copy(x.gobj(),value.gobj());
146 if(column==model.is_canvas.index())
148 synfigapp::ValueDesc value_desc((*iter)[model.value_desc]);
151 g_value_init(x.gobj(),x.value_type());
153 x.set(!value_desc && (Canvas::Handle)(*iter)[model.canvas]);
155 g_value_init(value.gobj(),x.value_type());
156 g_value_copy(x.gobj(),value.gobj());
159 if(column==model.id.index())
161 synfigapp::ValueDesc value_desc((*iter)[model.value_desc]);
163 Glib::Value<Glib::ustring> x;
164 g_value_init(x.gobj(),x.value_type());
166 if(value_desc && value_desc.is_value_node())
167 x.set(value_desc.get_value_node()->get_id());
168 else if(!value_desc && Canvas::Handle((*iter)[model.canvas]))
169 x.set(Canvas::Handle((*iter)[model.canvas])->get_id());
171 return Gtk::TreeStore::get_value_vfunc(iter,column,value);
173 g_value_init(value.gobj(),x.value_type());
174 g_value_copy(x.gobj(),value.gobj());
177 if(column==model.is_editable.index())
179 synfigapp::ValueDesc value_desc((*iter)[model.value_desc]);
182 g_value_init(x.gobj(),x.value_type());
184 x.set(!value_desc.is_value_node() || synfigapp::is_editable(value_desc.get_value_node()));
186 g_value_init(value.gobj(),x.value_type());
187 g_value_copy(x.gobj(),value.gobj());
190 if(column==model.type.index())
192 synfigapp::ValueDesc value_desc((*iter)[model.value_desc]);
194 Glib::Value<Glib::ustring> x;
195 g_value_init(x.gobj(),x.value_type());
200 if((*iter)[model.is_canvas])
205 if(!value_desc.is_value_node() || value_desc.get_value_node()->get_name()=="constant")
207 x.set(ValueBase::type_name(value_desc.get_value_type()));
211 x.set(value_desc.get_value_node()->get_local_name());
215 g_value_init(value.gobj(),x.value_type());
216 g_value_copy(x.gobj(),value.gobj());
219 if(column==model.label.index())
221 synfigapp::ValueDesc value_desc((*iter)[model.value_desc]);
223 Glib::Value<Glib::ustring> x;
224 g_value_init(x.gobj(),x.value_type());
229 Canvas::Handle canvas((*iter)[model.canvas]);
232 if(!canvas->get_id().empty())
233 x.set(canvas->get_id());
235 if(!canvas->get_name().empty())
236 x.set(canvas->get_name());
238 x.set(_("[Unnamed]"));
241 return Gtk::TreeStore::get_value_vfunc(iter,column,value);
245 ValueNode::Handle value_node=value_desc.get_value_node();
247 // Setup the row's label
248 if(value_node->get_id().empty())
249 x.set(Glib::ustring((*iter)[model.name]));
250 else if(Glib::ustring((*iter)[model.name]).empty())
251 x.set(value_node->get_id());
253 x.set(Glib::ustring((*iter)[model.name])+" ("+value_node->get_id()+')');
256 g_value_init(value.gobj(),x.value_type());
257 g_value_copy(x.gobj(),value.gobj());
260 if(column==model.icon.index())
262 synfigapp::ValueDesc value_desc((*iter)[model.value_desc]);
264 return Gtk::TreeStore::get_value_vfunc(iter,column,value);
266 Glib::Value<Glib::RefPtr<Gdk::Pixbuf> > x;
267 g_value_init(x.gobj(),x.value_type());
269 x.set(get_tree_pixbuf(value_desc.get_value_type()));
271 g_value_init(value.gobj(),x.value_type());
272 g_value_copy(x.gobj(),value.gobj());
275 Gtk::TreeStore::get_value_vfunc(iter,column,value);
279 CanvasTreeStore::find_first_value_desc(const synfigapp::ValueDesc& value_desc, Gtk::TreeIter& iter)
281 iter=children().begin();
282 while(iter && value_desc!=(*iter)[model.value_desc])
284 if(!iter->children().empty())
286 Gtk::TreeIter iter2(iter->children().begin());
287 if(iter2 && value_desc==(*iter2)[model.value_desc] || find_next_value_desc(value_desc, iter2))
293 Gtk::TreeIter iter2(++iter);
295 iter==iter->parent();
299 return (bool)iter && value_desc==(*iter)[model.value_desc];
303 CanvasTreeStore::find_next_value_desc(const synfigapp::ValueDesc& value_desc, Gtk::TreeIter& iter)
305 if(!iter) return find_first_value_desc(value_desc,iter);
308 if(!iter->children().empty())
310 Gtk::TreeIter iter2(iter->children().begin());
311 if(iter2 && value_desc==(*iter2)[model.value_desc] || find_next_value_desc(value_desc, iter2))
317 Gtk::TreeIter iter2(++iter);
320 iter==iter->parent();
325 } while(iter && value_desc!=(*iter)[model.value_desc]);
326 return (bool)iter && value_desc==(*iter)[model.value_desc];
330 CanvasTreeStore::find_first_value_node(const ValueNode::Handle& value_node, Gtk::TreeIter& iter)
332 iter=children().begin();
333 while(iter && value_node!=(ValueNode::Handle)(*iter)[model.value_node])
335 if(!iter->children().empty())
337 Gtk::TreeIter iter2(iter->children().begin());
338 if(iter2 && value_node==(ValueNode::Handle)(*iter2)[model.value_node] || find_next_value_node(value_node, iter2))
344 Gtk::TreeIter iter2(++iter);
346 iter==iter->parent();
350 return (bool)iter && value_node==(ValueNode::Handle)(*iter)[model.value_node];
354 CanvasTreeStore::find_next_value_node(const ValueNode::Handle& value_node, Gtk::TreeIter& iter)
356 if(!iter) return find_first_value_node(value_node,iter);
359 if(!iter->children().empty())
361 Gtk::TreeIter iter2(iter->children().begin());
362 if(iter2 && value_node==(ValueNode::Handle)(*iter2)[model.value_node] || find_next_value_node(value_node, iter2))
368 Gtk::TreeIter iter2(++iter);
371 iter==iter->parent();
376 } while(iter && value_node!=(ValueNode::Handle)(*iter)[model.value_node]);
377 return (bool)iter && value_node==(ValueNode::Handle)(*iter)[model.value_node];
381 CanvasTreeStore::set_row(Gtk::TreeRow row,synfigapp::ValueDesc value_desc, bool do_children)
383 Gtk::TreeModel::Children children = row.children();
384 while(!children.empty() && erase(children.begin()));
386 row[model.value_desc]=value_desc;
389 //row[model.icon] = get_tree_pixbuf(value_desc.get_value_type());
391 if(value_desc.is_value_node())
393 ValueNode::Handle value_node=value_desc.get_value_node();
397 row[model.value_node] = value_node;
398 //row[model.is_canvas] = false;
399 //row[model.is_value_node] = true;
400 //row[model.is_editable] = synfigapp::is_editable(value_node);
401 //row[model.id]=value_node->get_id();
404 if(value_desc.parent_is_canvas())
405 row[model.canvas]=value_desc.get_canvas();
407 row[model.canvas]=canvas_interface()->get_canvas();
409 LinkableValueNode::Handle linkable;
410 linkable=LinkableValueNode::Handle::cast_dynamic(value_node);
412 if(linkable && do_children)
414 row[model.link_count] = linkable->link_count();
415 for(int i=0;i<linkable->link_count();i++)
417 Gtk::TreeRow child_row=*(append(row.children()));
418 child_row[model.link_id] = i;
419 child_row[model.canvas] = static_cast<Canvas::Handle>(row[model.canvas]);
420 child_row[model.name] = linkable->link_local_name(i);
421 set_row(child_row,synfigapp::ValueDesc(linkable,i));
428 //row[model.is_value_node] = false;
429 //row[model.is_editable] = true;
430 //row[model.label] = Glib::ustring(row[model.name]);
434 catch(synfig::Exception::IDNotFound x)
436 synfig::error(__FILE__":%d: IDNotFound thrown",__LINE__);
441 // We should never get to this point
446 CanvasTreeStore::refresh_row(Gtk::TreeModel::Row &row, bool do_children)
448 synfigapp::ValueDesc value_desc=row[model.value_desc];
452 if((bool)row[model.is_value_node] != value_desc.is_value_node() ||
453 (!bool(row[model.is_value_node]) && row[model.link_count]!=0))
455 set_row(row,value_desc,do_children);
459 if(row[model.is_value_node])
461 ValueNode::Handle value_node(value_desc.get_value_node());
463 if(ValueNode::Handle(row[model.value_node])!=value_node)
465 rebuild_row(row,do_children);
469 //row[model.id]=value_node->get_id();
471 // Setup the row's label
473 if(value_node->get_id().empty())
474 row[model.label] = Glib::ustring(row[model.name]);
475 else if(Glib::ustring(row[model.name]).empty())
476 row[model.label] = value_node->get_id();
478 row[model.label] = Glib::ustring(row[model.name])+" ("+value_node->get_id()+')';
481 LinkableValueNode::Handle linkable;
482 linkable=LinkableValueNode::Handle::cast_dynamic(value_node);
483 if(do_children && linkable && ((int)row[model.link_count] != linkable->link_count()))
485 // Gtk::TreeModel::Children children = row.children();
486 // while(!children.empty() && erase(children.begin()));
488 set_row(row,value_desc);
494 //row[model.label] = Glib::ustring(row[model.name]);
495 //row[model.is_value_node] = false;
496 //row[model.is_editable] = true;
502 Gtk::TreeModel::Children children = row.children();
503 Gtk::TreeModel::Children::iterator iter;
505 if(!children.empty())
506 for(iter = children.begin(); iter != children.end(); ++iter)
508 Gtk::TreeRow row=*iter;
514 CanvasTreeStore::rebuild_row(Gtk::TreeModel::Row &row, bool do_children)
516 synfigapp::ValueDesc value_desc=(synfigapp::ValueDesc)row[model.value_desc];
518 if(value_desc && value_desc.get_value_node())
520 ValueNode::Handle value_node;
521 value_node=value_desc.get_value_node();
523 assert(value_node);if(!value_node)return;
525 if(value_node && value_node!=(ValueNode::Handle)row[model.value_node])
527 // Gtk::TreeModel::Children children = row.children();
528 // while(!children.empty() && erase(children.begin()));
530 set_row(row,value_desc,do_children);
534 LinkableValueNode::Handle linkable;
535 linkable=LinkableValueNode::Handle::cast_dynamic(value_node);
537 if( do_children && linkable && (int)row[model.link_count] != linkable->link_count())
539 // Gtk::TreeModel::Children children = row.children();
540 // while(!children.empty() && erase(children.begin()));
542 set_row(row,value_desc);
547 // value_node=row[model.value_node];
549 row[model.id]=value_node->get_id();
551 // Setup the row's label
552 if(value_node->get_id().empty())
553 row[model.label] = Glib::ustring(row[model.name]);
554 else if(Glib::ustring(row[model.name]).empty())
555 row[model.label] = value_node->get_id();
557 row[model.label] = Glib::ustring(row[model.name])+" ("+value_node->get_id()+')';
561 row[model.label] = Glib::ustring(row[model.name]);
562 row[model.is_value_node] = false;
563 row[model.is_editable] = true;
564 Gtk::TreeModel::Children children = row.children();
565 while(!children.empty() && erase(children.begin()));
570 Gtk::TreeModel::Children children = row.children();
571 Gtk::TreeModel::Children::iterator iter;
572 if(!children.empty())
573 for(iter = children.begin(); iter != children.end(); ++iter)
575 Gtk::TreeRow row=*iter;
580 CellRenderer_ValueBase*
581 CanvasTreeStore::add_cell_renderer_value(Gtk::TreeView::Column* column)
583 const CanvasTreeStore::Model model;
585 CellRenderer_ValueBase* ret;
587 ret=Gtk::manage( new CellRenderer_ValueBase() );
589 column->pack_start(*ret,true);
590 column->add_attribute(ret->property_value(), model.value);
591 column->add_attribute(ret->property_editable(), model.is_editable);
592 column->add_attribute(ret->property_canvas(), model.canvas);
597 CellRenderer_TimeTrack*
598 CanvasTreeStore::add_cell_renderer_value_node(Gtk::TreeView::Column* column)
600 const CanvasTreeStore::Model model;
602 CellRenderer_TimeTrack* ret;
604 ret = Gtk::manage( new CellRenderer_TimeTrack() );
606 column->pack_start(*ret,true);
607 //column->add_attribute(ret->property_visible(), model.is_value_node);
608 column->add_attribute(ret->property_value_desc(), model.value_desc);
609 column->add_attribute(ret->property_canvas(), model.canvas);