Allow PasteCanvas layers with unset canvas parameters to be exported.
[synfig.git] / synfig-studio / trunk / src / gtkmm / duckmatic.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file duckmatic.cpp
3 **      \brief Template File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007, 2008 Chris Moore
10 **
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.
15 **
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.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === H E A D E R S ======================================================= */
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32 #include <fstream>
33 #include <iostream>
34 #include <algorithm>
35
36 #include <ETL/hermite>
37
38 #include "duckmatic.h"
39 #include <synfigapp/value_desc.h>
40 #include <synfig/general.h>
41 #include <synfig/paramdesc.h>
42 #include <synfig/valuenode_timedswap.h>
43 #include <synfig/valuenode_animated.h>
44 #include <synfig/valuenode_composite.h>
45 #include <synfig/valuenode_scale.h>
46 #include <synfig/valuenode_bline.h>
47 #include <synfig/valuenode_blinecalctangent.h>
48 #include <synfig/valuenode_blinecalcvertex.h>
49 #include <synfig/valuenode_blinecalcwidth.h>
50
51 #include <synfig/curve_helper.h>
52
53 #include <sigc++/retype_return.h>
54 #include <sigc++/retype.h>
55 #include <sigc++/hide.h>
56 #include <sigc++/bind.h>
57
58 #include "canvasview.h"
59
60 #include "onemoment.h"
61
62 #include "general.h"
63
64 #endif
65
66 /* === U S I N G =========================================================== */
67
68 using namespace std;
69 using namespace etl;
70 using namespace synfig;
71 using namespace studio;
72
73 /* === M A C R O S ========================================================= */
74
75 /* 0.33333333333333333 makes for nice short tangent handles,
76    1.0 makes them draw as their real length */
77 #define TANGENT_HANDLE_SCALE 0.33333333333333333
78
79 /* leave this alone or the bezier won't lie on top of the bline */
80 #define TANGENT_BEZIER_SCALE 0.33333333333333333
81
82 /* === G L O B A L S ======================================================= */
83
84 /* === P R O C E D U R E S ================================================= */
85
86 /* === M E T H O D S ======================================================= */
87
88 /* === E N T R Y P O I N T ================================================= */
89
90 Duckmatic::Duckmatic():
91         type_mask(Duck::TYPE_ALL-Duck::TYPE_WIDTH),
92         grid_snap(false),
93         guide_snap(false),
94         grid_size(1.0/4.0,1.0/4.0),
95         show_persistent_strokes(true)
96 {
97         axis_lock=false;
98         drag_offset_=Point(0,0);
99         clear_duck_dragger();
100 }
101
102 Duckmatic::~Duckmatic()
103 {
104         clear_ducks();
105
106         if (Duck::duck_count)
107                 synfig::error("%d ducks not yet deleted!", Duck::duck_count);
108
109         if (getenv("SYNFIG_DEBUG_DESTRUCTORS"))
110                 synfig::info("Duckmatic::~Duckmatic(): Deleted");
111 }
112
113 void
114 Duckmatic::clear_ducks()
115 {
116         duck_data_share_map.clear();
117         duck_map.clear();
118
119         //duck_list_.clear();
120         bezier_list_.clear();
121         stroke_list_.clear();
122
123         if(show_persistent_strokes)
124                 stroke_list_=persistent_stroke_list_;
125 }
126
127 //! Returns \a true if the given duck is currently selected
128 bool
129 Duckmatic::duck_is_selected(const etl::handle<Duck> &duck)const
130 {
131         return duck && selected_ducks.count(duck->get_guid());
132 }
133
134 void
135 Duckmatic::set_grid_size(const synfig::Vector &s)
136 {
137         if(grid_size!=s)
138         {
139                 grid_size=s;
140                 signal_grid_changed();
141         }
142 }
143
144 void
145 Duckmatic::set_grid_snap(bool x)
146 {
147         if(grid_snap!=x)
148         {
149                 grid_snap=x;
150                 signal_grid_changed();
151         }
152 }
153
154 void
155 Duckmatic::set_guide_snap(bool x)
156 {
157         if(guide_snap!=x)
158         {
159                 guide_snap=x;
160                 signal_grid_changed();
161         }
162 }
163
164 Duckmatic::GuideList::iterator
165 Duckmatic::find_guide_x(synfig::Point pos, float radius)
166 {
167         GuideList::iterator iter,best(guide_list_x_.end());
168         float dist(radius);
169         for(iter=guide_list_x_.begin();iter!=guide_list_x_.end();++iter)
170         {
171                 float amount(abs(*iter-pos[0]));
172                 if(amount<dist)
173                 {
174                         dist=amount;
175                         best=iter;
176                 }
177         }
178         return best;
179 }
180
181 Duckmatic::GuideList::iterator
182 Duckmatic::find_guide_y(synfig::Point pos, float radius)
183 {
184         GuideList::iterator iter,best(guide_list_y_.end());
185         float dist(radius);
186         for(iter=guide_list_y_.begin();iter!=guide_list_y_.end();++iter)
187         {
188                 float amount(abs(*iter-pos[1]));
189                 if(amount<=dist)
190                 {
191                         dist=amount;
192                         best=iter;
193                 }
194         }
195         return best;
196 }
197
198 void
199 Duckmatic::clear_selected_ducks()
200 {
201         selected_ducks.clear();
202         signal_duck_selection_changed_();
203 }
204
205 etl::handle<Duckmatic::Duck>
206 Duckmatic::get_selected_duck()const
207 {
208         if(selected_ducks.empty() || duck_map.empty())
209                 return 0;
210         return duck_map.find(*selected_ducks.begin())->second;
211 }
212
213 void
214 Duckmatic::refresh_selected_ducks()
215 {
216 /*
217         std::set<etl::handle<Duck> >::iterator iter;
218         std::set<etl::handle<Duck> > new_set;
219         if(duck_list().empty())
220         {
221                 selected_duck_list.clear();
222                 signal_duck_selection_changed_();
223                 return;
224         }
225
226         for(iter=selected_duck_list.begin();iter!=selected_duck_list.end();++iter)
227         {
228                 etl::handle<Duck> similar(find_similar_duck(*iter));
229                 if(similar)
230                 {
231                         new_set.insert(similar);
232                 }
233         }
234         selected_duck_list=new_set;
235 */
236         GUIDSet old_set(selected_ducks);
237         GUIDSet::const_iterator iter;
238
239         for(iter=old_set.begin();iter!=old_set.end();++iter)
240         {
241                 if(duck_map.count(*iter)==0)
242                         selected_ducks.erase(*iter);
243         }
244
245         signal_duck_selection_changed_();
246 }
247
248 bool
249 Duckmatic::is_duck_group_selectable(const etl::handle<Duck>& x)const
250 {
251         const Type type(get_type_mask());
252
253         if (((x->get_type() && (!(type & x->get_type()))) ||
254                  !x->get_editable()))
255                 return false;
256
257         synfigapp::ValueDesc value_desc(x->get_value_desc());
258         if(value_desc.parent_is_layer_param() && type & Duck::TYPE_POSITION)
259         {
260                 Layer::Handle layer(value_desc.get_layer());
261                 String layer_name(layer->get_name());
262
263                 if (layer_name == "outline" || layer_name == "region" || layer_name == "plant" ||
264                         layer_name == "polygon" || layer_name == "curve_gradient")
265                         return false;
266
267                 if((layer_name=="PasteCanvas"|| layer_name=="paste_canvas") &&
268                    !layer->get_param("children_lock").get(bool()))
269                         return false;
270         }
271         else if (value_desc.parent_is_value_node())
272         {
273                 if (ValueNode_BLineCalcVertex::Handle::cast_dynamic(value_desc.get_value_node()))
274                         return false;
275                 if (value_desc.parent_is_linkable_value_node())
276                 {
277                         LinkableValueNode::Handle parent_value_node(value_desc.get_parent_value_node());
278                         if (ValueNode_Composite::Handle::cast_dynamic(parent_value_node))
279                         {
280                                 if (parent_value_node->get_type() == ValueBase::TYPE_BLINEPOINT &&
281                                         ValueNode_BLineCalcVertex::Handle::cast_dynamic(
282                                                 parent_value_node->get_link("point")))
283                                         return false;
284                         }
285                         else if (ValueNode_BLine::Handle::cast_dynamic(parent_value_node))
286                         {
287                                 ValueNode_Composite::Handle composite(ValueNode_Composite::Handle::cast_dynamic(
288                                                                                                                   value_desc.get_value_node()));
289                                 if (composite &&
290                                         ValueNode_BLineCalcVertex::Handle::cast_dynamic(composite->get_link("point")))
291                                         return false;
292                         }
293                 }
294         }
295         return true;
296 }
297
298 void
299 Duckmatic::select_all_ducks()
300 {
301         DuckMap::const_iterator iter;
302         for(iter=duck_map.begin();iter!=duck_map.end();++iter)
303                 if(is_duck_group_selectable(iter->second))
304                         select_duck(iter->second);
305                 else
306                         unselect_duck(iter->second);
307 }
308
309 void
310 Duckmatic::unselect_all_ducks()
311 {
312         DuckMap::const_iterator iter;
313         for(iter=duck_map.begin();iter!=duck_map.end();++iter)
314                 unselect_duck(iter->second);
315 }
316
317 void
318 Duckmatic::toggle_select_ducks_in_box(const synfig::Vector& tl,const synfig::Vector& br)
319 {
320         Vector vmin, vmax;
321         vmin[0]=std::min(tl[0],br[0]);
322         vmin[1]=std::min(tl[1],br[1]);
323         vmax[0]=std::max(tl[0],br[0]);
324         vmax[1]=std::max(tl[1],br[1]);
325
326         DuckMap::const_iterator iter;
327         for(iter=duck_map.begin();iter!=duck_map.end();++iter)
328         {
329                 Point p(iter->second->get_trans_point());
330                 if(p[0]<=vmax[0] && p[0]>=vmin[0] && p[1]<=vmax[1] && p[1]>=vmin[1] &&
331                    is_duck_group_selectable(iter->second))
332                         toggle_select_duck(iter->second);
333         }
334 }
335
336 void
337 Duckmatic::select_ducks_in_box(const synfig::Vector& tl,const synfig::Vector& br)
338 {
339         Vector vmin, vmax;
340         vmin[0]=std::min(tl[0],br[0]);
341         vmin[1]=std::min(tl[1],br[1]);
342         vmax[0]=std::max(tl[0],br[0]);
343         vmax[1]=std::max(tl[1],br[1]);
344
345 //      Type type(get_type_mask());
346
347         DuckMap::const_iterator iter;
348         for(iter=duck_map.begin();iter!=duck_map.end();++iter)
349         {
350                 Point p(iter->second->get_trans_point());
351                 if(p[0]<=vmax[0] && p[0]>=vmin[0] && p[1]<=vmax[1] && p[1]>=vmin[1])
352                 {
353                         if(is_duck_group_selectable(iter->second))
354                                 select_duck(iter->second);
355                 }
356         }
357 }
358
359 int
360 Duckmatic::count_selected_ducks()const
361 {
362         return selected_ducks.size();
363 }
364
365 void
366 Duckmatic::select_duck(const etl::handle<Duck> &duck)
367 {
368         if(duck)
369         {
370                 selected_ducks.insert(duck->get_guid());
371                 signal_duck_selection_changed_();
372         }
373 }
374
375 DuckList
376 Duckmatic::get_selected_ducks()const
377 {
378         DuckList ret;
379         GUIDSet::const_iterator iter;
380         const Type type(get_type_mask());
381
382         for(iter=selected_ducks.begin();iter!=selected_ducks.end();++iter)
383         {
384                 const DuckMap::const_iterator d_iter(duck_map.find(*iter));
385
386                 if(d_iter==duck_map.end())
387                         continue;
388
389                 if(( d_iter->second->get_type() && (!(type & d_iter->second->get_type())) ) )
390                         continue;
391
392                 ret.push_back(d_iter->second);
393         }
394         return ret;
395 }
396
397 DuckList
398 Duckmatic::get_duck_list()const
399 {
400         DuckList ret;
401         DuckMap::const_iterator iter;
402         for(iter=duck_map.begin();iter!=duck_map.end();++iter)
403                 ret.push_back(iter->second);
404         return ret;
405 }
406
407 void
408 Duckmatic::unselect_duck(const etl::handle<Duck> &duck)
409 {
410         if(duck && selected_ducks.count(duck->get_guid()))
411         {
412                 selected_ducks.erase(duck->get_guid());
413                 signal_duck_selection_changed_();
414         }
415 }
416
417 void
418 Duckmatic::toggle_select_duck(const etl::handle<Duck> &duck)
419 {
420         if(duck_is_selected(duck))
421                 unselect_duck(duck);
422         else
423                 select_duck(duck);
424 }
425
426 void
427 Duckmatic::translate_selected_ducks(const synfig::Vector& vector)
428 {
429         if(duck_dragger_)
430                 duck_dragger_->duck_drag(this,vector);
431 }
432
433 void
434 Duckmatic::start_duck_drag(const synfig::Vector& offset)
435 {
436         if(duck_dragger_)
437                 duck_dragger_->begin_duck_drag(this,offset);
438
439         //drag_offset_=offset;
440         drag_offset_=find_duck(offset)->get_trans_point();
441 }
442
443 bool
444 Duckmatic::end_duck_drag()
445 {
446         if(duck_dragger_)
447                 return duck_dragger_->end_duck_drag(this);
448         return false;
449 }
450
451 Point
452 Duckmatic::snap_point_to_grid(const synfig::Point& x, float radius)const
453 {
454         Point ret(x);
455
456         GuideList::const_iterator guide_x,guide_y;
457         bool has_guide_x(false), has_guide_y(false);
458
459         guide_x=find_guide_x(ret,radius);
460         if(guide_x!=guide_list_x_.end())
461                 has_guide_x=true;
462
463         guide_y=find_guide_y(ret,radius);
464         if(guide_y!=guide_list_y_.end())
465                 has_guide_y=true;
466
467         if(get_grid_snap())
468         {
469                 Point snap(
470                         floor(ret[0]/get_grid_size()[0]+0.5)*get_grid_size()[0],
471                         floor(ret[1]/get_grid_size()[1]+0.5)*get_grid_size()[1]);
472
473                 if(abs(snap[0]-ret[0])<=radius && (!has_guide_x || abs(snap[0]-ret[0])<=abs(*guide_x-ret[0])))
474                         ret[0]=snap[0],has_guide_x=false;
475                 if(abs(snap[1]-ret[1])<=radius && (!has_guide_y || abs(snap[1]-ret[1])<=abs(*guide_y-ret[1])))
476                         ret[1]=snap[1],has_guide_y=false;
477         }
478
479         if(guide_snap)
480         {
481                 if(has_guide_x)
482                         ret[0]=*guide_x;
483                 if(has_guide_y)
484                         ret[1]=*guide_y;
485         }
486
487         if(axis_lock)
488         {
489                 ret-=drag_offset_;
490                 if(abs(ret[0])<abs(ret[1]))
491                         ret[0]=0;
492                 else
493                         ret[1]=0;
494                 ret+=drag_offset_;
495         }
496
497         return ret;
498 }
499
500 void
501 DuckDrag_Translate::begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& offset)
502 {
503         last_translate_=Vector(0,0);
504         {
505                 drag_offset_=duckmatic->find_duck(offset)->get_trans_point();
506
507                 snap=Vector(0,0);
508         }
509
510         const DuckList selected_ducks(duckmatic->get_selected_ducks());
511         DuckList::const_iterator iter;
512
513         positions.clear();
514         for(iter=selected_ducks.begin();iter!=selected_ducks.end();++iter)
515         {
516                 Point p((*iter)->get_trans_point());
517                 positions.push_back(p);
518         }
519 }
520
521 bool
522 DuckDrag_Translate::end_duck_drag(Duckmatic* duckmatic)
523 {
524         if(last_translate_.mag()>0.0001)
525         {
526                 duckmatic->signal_edited_selected_ducks();
527                 return true;
528         }
529         else
530         {
531                 duckmatic->signal_user_click_selected_ducks(0);
532                 return false;
533         }
534 }
535
536 void
537 DuckDrag_Translate::duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector)
538 {
539         const DuckList selected_ducks(duckmatic->get_selected_ducks());
540         DuckList::const_iterator iter;
541
542         synfig::Vector vect(duckmatic->snap_point_to_grid(vector)-drag_offset_);
543         int i;
544         Time time(duckmatic->get_time());
545
546         // drag the vertex and position ducks first
547         for (i=0,iter=selected_ducks.begin(); iter!=selected_ducks.end(); ++iter,i++)
548         {
549                 etl::handle<Duck> duck(*iter);
550                 if(duck->get_type() == Duck::TYPE_VERTEX || duck->get_type() == Duck::TYPE_POSITION)
551                 {
552                         duck->set_trans_point(positions[i]+vect);
553
554                         ValueNode_BLineCalcVertex::Handle bline_vertex;
555                         ValueNode_Composite::Handle composite;
556
557                         if ((bline_vertex = ValueNode_BLineCalcVertex::Handle::cast_dynamic(duck->get_value_desc().get_value_node())) ||
558                                 ((composite = ValueNode_Composite::Handle::cast_dynamic(duck->get_value_desc().get_value_node())) &&
559                                  composite->get_type() == ValueBase::TYPE_BLINEPOINT &&
560                                  (bline_vertex = ValueNode_BLineCalcVertex::Handle::cast_dynamic(composite->get_link("point")))))
561                         {
562                                 synfig::Point closest_point = duck->get_point();
563                                 synfig::Real radius = 0.0;
564                                 ValueNode_BLine::Handle bline = ValueNode_BLine::Handle::cast_dynamic(bline_vertex->get_link(bline_vertex->get_link_index_from_name("bline")));
565                                 synfig::find_closest_point(
566                                         (*bline)(time),
567                                         duck->get_point(),
568                                         radius,
569                                         bline->get_loop(),
570                                         &closest_point);
571                                 duck->set_point(closest_point);
572                         }
573                 }
574         }
575
576         // then drag the others
577         for (i=0,iter=selected_ducks.begin(); iter!=selected_ducks.end(); ++iter,i++)
578                 if ((*iter)->get_type() != Duck::TYPE_VERTEX && (*iter)->get_type() != Duck::TYPE_POSITION)
579                         (*iter)->set_trans_point(positions[i]+vect);
580
581         // then patch up the tangents for the vertices we've moved
582         DuckList duck_list(duckmatic->get_duck_list());
583         for (iter=selected_ducks.begin(); iter!=selected_ducks.end(); ++iter)
584         {
585                 etl::handle<Duck> duck(*iter);
586                 if (duck->get_type() == Duck::TYPE_VERTEX || duck->get_type() == Duck::TYPE_POSITION)
587                 {
588                         ValueNode_BLineCalcVertex::Handle bline_vertex;
589                         ValueNode_Composite::Handle composite;
590
591                         if ((bline_vertex = ValueNode_BLineCalcVertex::Handle::cast_dynamic(duck->get_value_desc().get_value_node())) ||
592                                 ((composite = ValueNode_Composite::Handle::cast_dynamic(duck->get_value_desc().get_value_node())) &&
593                                  composite->get_type() == ValueBase::TYPE_BLINEPOINT &&
594                                  (bline_vertex = ValueNode_BLineCalcVertex::Handle::cast_dynamic(composite->get_link("point")))))
595                         {
596                                 synfig::Real radius = 0.0;
597                                 ValueNode_BLine::Handle bline(ValueNode_BLine::Handle::cast_dynamic(bline_vertex->get_link(bline_vertex->get_link_index_from_name("bline"))));
598                                 Real amount = synfig::find_closest_point((*bline)(time), duck->get_point(), radius, bline->get_loop());
599
600                                 int vertex_amount_index(bline_vertex->get_link_index_from_name("amount"));
601                                 ValueNode::Handle vertex_amount_value_node(bline_vertex->get_link(vertex_amount_index));
602
603                                 DuckList::iterator iter;
604                                 for (iter=duck_list.begin(); iter!=duck_list.end(); iter++)
605                                 {
606                                         ValueNode::Handle duck_value_node((*iter)->get_value_desc().get_value_node());
607                                         if (ValueNode_BLineCalcTangent::Handle bline_tangent = ValueNode_BLineCalcTangent::Handle::cast_dynamic(duck_value_node))
608                                         {
609                                                 if (bline_tangent->get_link(bline_tangent->get_link_index_from_name("amount")) == vertex_amount_value_node)
610                                                 {
611                                                         switch (bline_tangent->get_type())
612                                                         {
613                                                         case ValueBase::TYPE_ANGLE:
614                                                         {
615                                                                 Angle angle((*bline_tangent)(time, amount).get(Angle()));
616                                                                 (*iter)->set_point(Point(Angle::cos(angle).get(), Angle::sin(angle).get()));
617                                                                 break;
618                                                         }
619                                                         case ValueBase::TYPE_REAL:
620                                                                 (*iter)->set_point(Point((*bline_tangent)(time, amount).get(Real()), 0));
621                                                                 break;
622                                                         case ValueBase::TYPE_VECTOR:
623                                                                 (*iter)->set_point((*bline_tangent)(time, amount).get(Vector()));
624                                                                 break;
625                                                         default:
626                                                                 break;
627                                                         }
628                                                 }
629                                         }
630                                         else if (ValueNode_BLineCalcWidth::Handle bline_width = ValueNode_BLineCalcWidth::Handle::cast_dynamic(duck_value_node))
631                                         {
632                                                 if (bline_width->get_link(bline_width->get_link_index_from_name("amount")) == vertex_amount_value_node)
633                                                         (*iter)->set_point(Point((*bline_width)(time, amount).get(Real()), 0));
634                                         }
635                                 }
636                         }
637                 }
638         }
639
640         last_translate_=vect;
641 }
642
643 void
644 Duckmatic::signal_edited_selected_ducks()
645 {
646         const DuckList ducks(get_selected_ducks());
647         DuckList::const_iterator iter;
648
649         synfig::GUIDSet old_set(selected_ducks);
650
651         // If we have more than 20 things to move, then display
652         // something to explain that it may take a moment
653         smart_ptr<OneMoment> wait; if(ducks.size()>20)wait.spawn();
654
655         // Go ahead and call everyone's signals
656         for(iter=ducks.begin();iter!=ducks.end();++iter)
657         {
658                 if ((*iter)->get_type() == Duck::TYPE_ANGLE)
659                 {
660                         if(!(*iter)->signal_edited_angle()((*iter)->get_rotations()))
661                         {
662                                 selected_ducks=old_set;
663                                 throw String("Bad edit");
664                         }
665                 }
666                 else if (App::restrict_radius_ducks &&
667                                  (*iter)->is_radius())
668                 {
669                         Point point((*iter)->get_point());
670                         bool changed = false;
671
672                         if (point[0] < 0)
673                         {
674                                 point[0] = 0;
675                                 changed = true;
676                         }
677                         if (point[1] < 0)
678                         {
679                                 point[1] = 0;
680                                 changed = true;
681                         }
682
683                         if (changed) (*iter)->set_point(point);
684
685                         if(!(*iter)->signal_edited()(point))
686                         {
687                                 selected_ducks=old_set;
688                                 throw String("Bad edit");
689                         }
690                 }
691                 else
692                 {
693                         if(!(*iter)->signal_edited()((*iter)->get_point()))
694                         {
695                                 selected_ducks=old_set;
696                                 throw String("Bad edit");
697                         }
698                 }
699         }
700         selected_ducks=old_set;
701 }
702
703 void
704 Duckmatic::signal_user_click_selected_ducks(int button)
705 {
706         const DuckList ducks(get_selected_ducks());
707         DuckList::const_iterator iter;
708
709         for(iter=ducks.begin();iter!=ducks.end();++iter)
710         {
711                 (*iter)->signal_user_click(button)();
712         }
713 }
714
715 void
716 Duckmatic::add_duck(const etl::handle<Duck> &duck)
717 {
718         //if(!duck_map.count(duck->get_guid()))
719         {
720                 if(duck_data_share_map.count(duck->get_data_guid()))
721                 {
722                         duck->set_shared_point(duck_data_share_map[duck->get_data_guid()]);
723                 }
724                 else
725                 {
726                         etl::smart_ptr<synfig::Point> point(new Point(duck->get_point()));
727                         duck->set_shared_point(point);
728                         duck_data_share_map[duck->get_data_guid()]=point;
729                 }
730
731                 duck_map.insert(duck);
732         }
733
734         last_duck_guid=duck->get_guid();
735 }
736
737 void
738 Duckmatic::add_bezier(const etl::handle<Bezier> &bezier)
739 {
740         bezier_list_.push_back(bezier);
741 }
742
743 void
744 Duckmatic::add_stroke(etl::smart_ptr<std::list<synfig::Point> > stroke_point_list, const synfig::Color& color)
745 {
746         assert(stroke_point_list);
747
748         std::list<etl::handle<Stroke> >::iterator iter;
749
750         for(iter=stroke_list_.begin();iter!=stroke_list_.end();++iter)
751         {
752                 if((*iter)->stroke_data==stroke_point_list)
753                         return;
754         }
755
756         etl::handle<Stroke> stroke(new Stroke());
757
758         stroke->stroke_data=stroke_point_list;
759         stroke->color=color;
760
761         stroke_list_.push_back(stroke);
762 }
763
764 void
765 Duckmatic::add_persistent_stroke(etl::smart_ptr<std::list<synfig::Point> > stroke_point_list, const synfig::Color& color)
766 {
767         add_stroke(stroke_point_list,color);
768         persistent_stroke_list_.push_back(stroke_list_.back());
769 }
770
771 void
772 Duckmatic::clear_persistent_strokes()
773 {
774         persistent_stroke_list_.clear();
775 }
776
777 void
778 Duckmatic::set_show_persistent_strokes(bool x)
779 {
780         if(x!=show_persistent_strokes)
781         {
782                 show_persistent_strokes=x;
783                 if(x)
784                         stroke_list_=persistent_stroke_list_;
785                 else
786                         stroke_list_.clear();
787         }
788 }
789
790 void
791 Duckmatic::erase_duck(const etl::handle<Duck> &duck)
792 {
793         duck_map.erase(duck->get_guid());
794 }
795
796 etl::handle<Duckmatic::Duck>
797 Duckmatic::find_similar_duck(etl::handle<Duck> duck)
798 {
799         DuckMap::const_iterator iter(duck_map.find(duck->get_guid()));
800         if(iter!=duck_map.end())
801                 return iter->second;
802         return 0;
803
804 /*      std::list<handle<Duck> >::reverse_iterator iter;
805
806         for(iter=duck_list_.rbegin();iter!=duck_list_.rend();++iter)
807         {
808                 if(*iter!=duck && **iter==*duck)
809                 {
810                         //synfig::info("Found similar duck! (iter:%08x vs. duck:%08x)",iter->get(), duck.get());
811                         return *iter;
812                 }
813         }
814         return 0;
815 */
816 }
817
818 etl::handle<Duckmatic::Duck>
819 Duckmatic::add_similar_duck(etl::handle<Duck> duck)
820 {
821         etl::handle<Duck> similar(find_similar_duck(duck));
822         if(!similar)
823         {
824                 add_duck(duck);
825                 return duck;
826         }
827         return similar;
828 }
829
830 void
831 Duckmatic::erase_bezier(const etl::handle<Bezier> &bezier)
832 {
833         std::list<handle<Bezier> >::iterator iter;
834
835         for(iter=bezier_list_.begin();iter!=bezier_list_.end();++iter)
836         {
837                 if(*iter==bezier)
838                 {
839                         bezier_list_.erase(iter);
840                         return;
841                 }
842         }
843         synfig::warning("Unable to find bezier to erase!");
844 }
845
846 etl::handle<Duckmatic::Duck>
847 Duckmatic::last_duck()const
848 {
849         DuckMap::const_iterator iter(duck_map.find(last_duck_guid));
850         if(iter!=duck_map.end())
851                 return iter->second;
852         return 0;
853 }
854
855 etl::handle<Duckmatic::Bezier>
856 Duckmatic::last_bezier()const
857 {
858         return bezier_list_.back();
859 }
860
861 etl::handle<Duckmatic::Duck>
862 Duckmatic::find_duck(synfig::Point point, synfig::Real radius, Duck::Type type)
863 {
864         if(radius==0)radius=10000000;
865
866         if(type==Duck::TYPE_DEFAULT)
867                 type=get_type_mask();
868
869         Real closest(10000000);
870         etl::handle<Duck> ret;
871
872         DuckMap::const_iterator iter;
873
874         for(iter=duck_map.begin();iter!=duck_map.end();++iter)
875         {
876                 const Duck::Handle& duck(iter->second);
877
878                 if(duck->get_ignore())
879                         continue;
880                 Real dist((duck->get_trans_point()-point).mag_squared());
881
882                 if(duck->get_type()&Duck::TYPE_VERTEX)
883                         dist*=1.0001;
884                 else if(duck->get_type()&Duck::TYPE_RADIUS)
885                         dist*=0.9999;
886
887                 if(dist<=closest && !( duck->get_type() && (!(type & duck->get_type())) ) )
888                 {
889                         {
890                                 closest=dist;
891                                 ret=duck;
892                         }
893                 }
894         }
895
896         if(radius==0 || closest<radius*radius)
897                 return ret;
898
899         return 0;
900 }
901
902 etl::handle<Duckmatic::Bezier>
903 Duckmatic::find_bezier(synfig::Point point, synfig::Real radius,float* location)
904 {
905         return find_bezier(point,radius,radius,location);
906 }
907
908 etl::handle<Duckmatic::Bezier>
909 Duckmatic::find_bezier(synfig::Point pos, synfig::Real scale, synfig::Real radius, float* location)
910 {
911         if(radius==0)radius=10000000;
912         Real closest(10000000);
913         etl::handle<Bezier> ret;
914
915         bezier<Point>   curve;
916
917         Real    d,step;
918         float   time = 0;
919         float   best_time = 0;
920
921         for(std::list<handle<Bezier> >::const_iterator iter=bezier_list().begin();iter!=bezier_list().end();++iter)
922         {
923                 curve[0] = (*iter)->p1->get_trans_point();
924                 curve[1] = (*iter)->c1->get_trans_point();
925                 curve[2] = (*iter)->c2->get_trans_point();
926                 curve[3] = (*iter)->p2->get_trans_point();
927                 curve.sync();
928
929 #if 0
930                 // I don't know why this doesn't work
931                 time=curve.find_closest(pos,6);
932                 d=((curve(time)-pos).mag_squared());
933
934 #else
935                 //set the step size based on the size of the picture
936                 d = (curve[1] - curve[0]).mag() + (curve[2]-curve[1]).mag()     + (curve[3]-curve[2]).mag();
937
938                 step = d/(2*scale); //want to make the distance between lines happy
939
940                 step = max(step,0.01); //100 samples should be plenty
941                 step = min(step,0.1); //10 is minimum
942
943                 d = find_closest(curve,pos,step,&closest,&time);
944 #endif
945
946                 if(d < closest)
947                 {
948                         closest = d;
949                         ret = *iter;
950                         best_time=time;
951                 }
952         }
953
954         if(closest < radius*radius)
955         {
956                 if(location)
957                         *location = best_time;  // We need to square-root this because we were dealing with squared distances
958
959                 return ret;
960         }
961
962         return 0;
963 }
964
965 bool
966 Duckmatic::save_sketch(const synfig::String& filename)const
967 {
968         ChangeLocale change_locale(LC_NUMERIC, "C");
969         std::ofstream file(filename.c_str());
970
971         if(!file)return false;
972
973         file<<"SKETCH"<<endl;
974
975         std::list<etl::handle<Stroke> >::const_iterator iter;
976
977         for(iter=persistent_stroke_list_.begin();iter!=persistent_stroke_list_.end();++iter)
978         {
979                 file<<"C "
980                         <<(*iter)->color.get_r()<<' '
981                         <<(*iter)->color.get_g()<<' '
982                         <<(*iter)->color.get_b()
983                 <<endl;
984                 std::list<synfig::Point>::const_iterator viter;
985                 for(viter=(*iter)->stroke_data->begin();viter!=(*iter)->stroke_data->end();++viter)
986                 {
987                         file<<"V "
988                                 <<(*viter)[0]<<' '
989                                 <<(*viter)[1]
990                         <<endl;
991                 }
992         }
993         if(!file)return false;
994         sketch_filename_=filename;
995         signal_sketch_saved_();
996         return true;
997 }
998
999 bool
1000 Duckmatic::load_sketch(const synfig::String& filename)
1001 {
1002         ChangeLocale change_locale(LC_NUMERIC, "C");
1003         std::ifstream file(filename.c_str());
1004
1005         if(!file)
1006                 return false;
1007
1008         std::string line;
1009         getline(file,line);
1010
1011         if(line!="SKETCH")
1012         {
1013                 synfig::error("Not a sketch");
1014                 return false;
1015         }
1016
1017         etl::smart_ptr<std::list<synfig::Point> > stroke_data;
1018
1019         while(file)
1020         {
1021                 getline(file,line);
1022
1023                 if(line.empty())
1024                         continue;
1025
1026                 switch(line[0])
1027                 {
1028                 case 'C':
1029                 case 'c':
1030                         {
1031                                 stroke_data.spawn();
1032                                 float r,g,b;
1033                                 if(!strscanf(line,"C %f %f %f",&r, &g, &b))
1034                                 {
1035                                         synfig::warning("Bad color line \"%s\"",line.c_str());
1036                                         r=0;g=0;b=0;
1037                                 }
1038                                 add_persistent_stroke(stroke_data, synfig::Color(r,g,b));
1039                         }
1040                         break;
1041                 case 'V':
1042                 case 'v':
1043                         if(!stroke_data)
1044                         {
1045                                 stroke_data.spawn();
1046                                 add_persistent_stroke(stroke_data, synfig::Color(0,0,0));
1047                         }
1048                         float x,y;
1049                         if(!strscanf(line,"V %f %f",&x, &y))
1050                                 synfig::warning("Bad vertex \"%s\"",line.c_str());
1051                         else
1052                                 stroke_data->push_back(synfig::Vector(x,y));
1053                         break;
1054                 default:
1055                         synfig::warning("Unexpected sketch token '%c'",line[0]);
1056                         break;
1057                 }
1058         }
1059
1060         sketch_filename_=filename;
1061         return true;
1062 }
1063
1064 Duckmatic::Push::Push(Duckmatic *duckmatic_):
1065         duckmatic_(duckmatic_)
1066 {
1067         duck_map=duckmatic_->duck_map;
1068         bezier_list_=duckmatic_->bezier_list_;
1069         duck_data_share_map=duckmatic_->duck_data_share_map;
1070         stroke_list_=duckmatic_->stroke_list_;
1071         duck_dragger_=duckmatic_->duck_dragger_;
1072         needs_restore=true;
1073 }
1074
1075 Duckmatic::Push::~Push()
1076 {
1077         if(needs_restore)
1078                 restore();
1079 }
1080
1081 void
1082 Duckmatic::Push::restore()
1083 {
1084         duckmatic_->duck_map=duck_map;
1085         duckmatic_->bezier_list_=bezier_list_;
1086         duckmatic_->duck_data_share_map=duck_data_share_map;
1087         duckmatic_->stroke_list_=stroke_list_;
1088         duckmatic_->duck_dragger_=duck_dragger_;
1089         needs_restore=false;
1090 }
1091
1092 inline String guid_string(const synfigapp::ValueDesc& x)
1093 {
1094         if(x.parent_is_layer_param())
1095                 return strprintf("%s",x.get_layer()->get_guid().get_string().c_str())+x.get_param_name();
1096         //if(x.is_value_node())
1097                 return strprintf("%s",x.get_value_node()->get_guid().get_string().c_str());
1098 }
1099
1100 inline synfig::GUID calc_duck_guid(const synfigapp::ValueDesc& x,const synfig::TransformStack& transform_stack)
1101 {
1102         synfig::GUID ret(0);
1103
1104         if(x.parent_is_layer_param())
1105         {
1106                 ret=x.get_layer()->get_guid()^synfig::GUID::hasher(x.get_param_name());
1107         }
1108         else
1109         {
1110                 ret=x.get_value_node()->get_guid();
1111         }
1112
1113         ret^=transform_stack.get_guid();
1114         return ret;
1115 }
1116
1117 /*
1118 Duck::Handle
1119 Duckmatic::create_duck_from(const synfigapp::ValueDesc& value_desc,etl::handle<CanvasView> canvas_view, const synfig::TransformStack& transform_stack, int modifier, synfig::ParamDesc *param_desc)
1120 {
1121         synfig::GUID duck_guid(calc_duck_guid(value_desc,transform_stack)^synfig::GUID::hasher(modifier));
1122         etl::handle<Duck> duck=new Duck();
1123
1124         return duck;
1125 }
1126 */
1127
1128 bool
1129 Duckmatic::add_to_ducks(const synfigapp::ValueDesc& value_desc,etl::handle<CanvasView> canvas_view, const synfig::TransformStack& transform_stack, synfig::ParamDesc *param_desc, int multiple)
1130 {
1131         ValueBase::Type type=value_desc.get_value_type();
1132 #define REAL_COOKIE             reinterpret_cast<synfig::ParamDesc*>(28)
1133         switch(type)
1134         {
1135         case ValueBase::TYPE_REAL:
1136
1137                 if(!param_desc || param_desc==REAL_COOKIE || !param_desc->get_origin().empty())
1138                 {
1139                         etl::handle<Duck> duck=new Duck();
1140                         duck->set_transform_stack(transform_stack);
1141                         duck->set_radius(true);
1142                         duck->set_type(Duck::TYPE_RADIUS);
1143
1144                         // put the duck on the right hand side of the center
1145                         duck->set_point(Point(value_desc.get_value(get_time()).get(Real()), 0));
1146                         duck->set_name(guid_string(value_desc));
1147                         if(value_desc.is_value_node())
1148                         {
1149                                 // If the ValueNode can be directly manipulated,
1150                                 // then set it as so.
1151                                 duck->set_editable(synfigapp::is_editable(value_desc.get_value_node()));
1152                         }
1153                         else
1154                         {
1155                                 duck->set_editable(true);
1156                         }
1157
1158                         if(param_desc && param_desc!=REAL_COOKIE)
1159                         {
1160                                 if(!param_desc->get_origin().empty())
1161                                 {
1162                                         synfigapp::ValueDesc value_desc_origin(value_desc.get_layer(),param_desc->get_origin());
1163                                         /*
1164                                         duck->set_origin(value_desc_origin.get_value(get_time()).get(synfig::Point()));
1165                                         */
1166                                         add_to_ducks(value_desc_origin,canvas_view, transform_stack);
1167                                         duck->set_origin(last_duck());
1168                                 }
1169                                 duck->set_scalar(param_desc->get_scalar());
1170                         }
1171
1172                         duck->signal_edited().clear(); // value_desc.get_value_type() == ValueBase::TYPE_REAL:
1173                         duck->signal_edited().connect(
1174                                 sigc::bind(
1175                                         sigc::mem_fun(
1176                                                 *canvas_view,
1177                                                 &studio::CanvasView::on_duck_changed),
1178                                         value_desc));
1179                         duck->set_value_desc(value_desc);
1180
1181                         duck->signal_user_click(2).connect(
1182                                 sigc::bind(
1183                                         sigc::bind(
1184                                                 sigc::bind(
1185                                                         sigc::mem_fun(
1186                                                                 *canvas_view,
1187                                                                 &studio::CanvasView::popup_param_menu),
1188                                                         false),
1189                                                 0.0f),
1190                                         value_desc));
1191
1192                         duck->set_guid(calc_duck_guid(value_desc,transform_stack)^synfig::GUID::hasher(multiple));
1193
1194                         add_duck(duck);
1195
1196                         return true;
1197                 }
1198                 break;
1199
1200         case ValueBase::TYPE_ANGLE:
1201
1202                 if(!param_desc || param_desc==REAL_COOKIE || !param_desc->get_origin().empty())
1203                 {
1204                         etl::handle<Duck> duck=new Duck();
1205                         duck->set_type(Duck::TYPE_ANGLE);
1206                         duck->set_transform_stack(transform_stack);
1207                         synfig::Angle angle;
1208
1209                         angle=value_desc.get_value(get_time()).get(Angle());
1210                         duck->set_point(Point(Angle::cos(angle).get(),Angle::sin(angle).get()));
1211                         duck->set_name(guid_string(value_desc));
1212                         if(value_desc.is_value_node())
1213                         {
1214                                 ValueNode::Handle value_node=value_desc.get_value_node();
1215                                 //duck->set_name(strprintf("%x",value_node.get()));
1216
1217                                 // If the ValueNode can be directly manipulated,
1218                                 // then set it as so.
1219                                 duck->set_editable(synfigapp::is_editable(value_desc.get_value_node()));
1220                         }
1221                         else
1222                         {
1223                                 //angle=(value_desc.get_value().get(Angle()));
1224                                 //duck->set_point(Point(Angle::cos(angle).get(),Angle::sin(angle).get()));
1225                                 //duck->set_name(strprintf("%x",value_desc.get_layer().get())+value_desc.get_param_name());
1226                                 duck->set_editable(true);
1227                         }
1228
1229                         if(param_desc && param_desc!=REAL_COOKIE)
1230                         {
1231                                 if(!param_desc->get_origin().empty())
1232                                 {
1233                                         synfigapp::ValueDesc value_desc_origin(value_desc.get_layer(),param_desc->get_origin());
1234                                         /*
1235                                         duck->set_origin(value_desc_origin.get_value(get_time()).get(synfig::Point()));
1236                                         */
1237                                         add_to_ducks(value_desc_origin,canvas_view, transform_stack);
1238                                         duck->set_origin(last_duck());
1239                                 }
1240                                 duck->set_scalar(param_desc->get_scalar());
1241                         }
1242
1243                         duck->signal_edited().clear(); // value_desc.get_value_type() == ValueBase::TYPE_ANGLE:
1244                         duck->signal_edited_angle().clear();
1245                         duck->signal_edited_angle().connect(
1246                                 sigc::bind(
1247                                         sigc::mem_fun(
1248                                                 *canvas_view,
1249                                                 &studio::CanvasView::on_duck_angle_changed),
1250                                         value_desc));
1251                         duck->set_value_desc(value_desc);
1252
1253                         duck->signal_user_click(2).connect(
1254                                 sigc::bind(
1255                                         sigc::bind(
1256                                                 sigc::bind(
1257                                                         sigc::mem_fun(
1258                                                                 *canvas_view,
1259                                                                 &studio::CanvasView::popup_param_menu),
1260                                                         false),
1261                                                 0.0f),
1262                                         value_desc));
1263                         duck->set_guid(calc_duck_guid(value_desc,transform_stack)^synfig::GUID::hasher(multiple));
1264
1265                         add_duck(duck);
1266
1267                         return true;
1268                 }
1269                 break;
1270
1271         case ValueBase::TYPE_VECTOR:
1272                 {
1273                         etl::handle<Duck> duck=new Duck();
1274                         duck->set_transform_stack(transform_stack);
1275
1276                         duck->set_point(value_desc.get_value(get_time()).get(Point()));
1277                         duck->set_name(guid_string(value_desc));
1278                         if(value_desc.is_value_node())
1279                         {
1280                                 //duck->set_name(strprintf("%x",value_desc.get_value_node().get()));
1281
1282                                 // If the ValueNode can be directly manipulated,
1283                                 // then set it as so.
1284                                 duck->set_editable(synfigapp::is_editable(value_desc.get_value_node()));
1285                         }
1286                         else
1287                         {
1288                                 //duck->set_point(value_desc.get_value().get(Point()));
1289                                 //duck->set_name(strprintf("%x",value_desc.get_layer().get())+value_desc.get_param_name());
1290                                 duck->set_editable(true);
1291                         }
1292
1293                         // If we were passed a parameter description
1294                         if(param_desc)
1295                         {
1296                                 if(!param_desc->get_connect().empty())
1297                                 {
1298                                         synfigapp::ValueDesc value_desc_origin(value_desc.get_layer(),param_desc->get_connect());
1299                                         Duck::Handle connect_duck;
1300                                         if(duck_map.find(calc_duck_guid(value_desc_origin,transform_stack)^synfig::GUID::hasher(0))!=duck_map.end())
1301                                         {
1302                                                 connect_duck=duck_map[calc_duck_guid(value_desc_origin,transform_stack)^synfig::GUID::hasher(0)];
1303                                         }
1304                                         else
1305                                         {
1306                                                 add_to_ducks(value_desc_origin,canvas_view, transform_stack);
1307                                                 connect_duck=last_duck();
1308                                         }
1309                                         duck->set_connect_duck(connect_duck);
1310                                 }
1311                                 if(!param_desc->get_box().empty())
1312                                 {
1313                                         synfigapp::ValueDesc value_desc_origin(value_desc.get_layer(),param_desc->get_box());
1314                                         add_to_ducks(value_desc_origin,canvas_view, transform_stack);
1315                                         duck->set_box_duck(last_duck());
1316                                 }
1317
1318                                 // If we have an origin
1319                                 if(!param_desc->get_origin().empty())
1320                                 {
1321                                         synfigapp::ValueDesc value_desc_origin(value_desc.get_layer(),param_desc->get_origin());
1322                                         /*
1323                                         duck->set_origin(value_desc_origin.get_value(get_time()).get(synfig::Point()));
1324                                         */
1325                                         add_to_ducks(value_desc_origin,canvas_view, transform_stack);
1326                                         duck->set_origin(last_duck());
1327                                         duck->set_type(Duck::TYPE_VERTEX);
1328                                 }
1329                                 else
1330                                         duck->set_type(Duck::TYPE_POSITION);
1331
1332                                 duck->set_scalar(param_desc->get_scalar());
1333                         }
1334                         else
1335                                 duck->set_type(Duck::TYPE_POSITION);
1336
1337                         duck->signal_edited().clear(); // value_desc.get_value_type() == ValueBase::TYPE_VECTOR:
1338                         duck->signal_edited().connect(
1339                                 sigc::bind(
1340                                         sigc::mem_fun(
1341                                                 *canvas_view,
1342                                                 &studio::CanvasView::on_duck_changed),
1343                                         value_desc));
1344                         duck->set_value_desc(value_desc);
1345
1346                         duck->signal_user_click(2).connect(
1347                                 sigc::bind(
1348                                         sigc::bind(
1349                                                 sigc::bind(
1350                                                         sigc::mem_fun(
1351                                                                 *canvas_view,
1352                                                                 &studio::CanvasView::popup_param_menu),
1353                                                         false),
1354                                                 1.0f),
1355                                         value_desc));
1356                         duck->set_guid(calc_duck_guid(value_desc,transform_stack)^synfig::GUID::hasher(multiple));
1357                         add_duck(duck);
1358
1359                         return true;
1360                 }
1361                 break;
1362         case ValueBase::TYPE_SEGMENT:
1363                 {
1364                         etl::handle<Bezier> bezier(new Bezier());
1365                         ValueNode_Composite::Handle value_node;
1366
1367                         if(value_desc.is_value_node() &&
1368                                 (value_node=ValueNode_Composite::Handle::cast_dynamic(value_desc.get_value_node())))
1369                         {
1370                                 if(!add_to_ducks(synfigapp::ValueDesc(value_node,0),canvas_view,transform_stack))
1371                                         return false;
1372                                 bezier->p1=last_duck();
1373                                 bezier->p1->set_type(Duck::TYPE_VERTEX);
1374                                 if(!add_to_ducks(synfigapp::ValueDesc(value_node,1),canvas_view,transform_stack))
1375                                         return false;
1376                                 bezier->c1=last_duck();
1377                                 bezier->c1->set_type(Duck::TYPE_TANGENT);
1378                                 bezier->c1->set_origin(bezier->p1);
1379                                 bezier->c1->set_scalar(TANGENT_BEZIER_SCALE);
1380                                 bezier->c1->set_tangent(true);
1381
1382                                 if(!add_to_ducks(synfigapp::ValueDesc(value_node,2),canvas_view,transform_stack))
1383                                         return false;
1384                                 bezier->p2=last_duck();
1385                                 bezier->p2->set_type(Duck::TYPE_VERTEX);
1386                                 if(!add_to_ducks(synfigapp::ValueDesc(value_node,3),canvas_view,transform_stack))
1387                                         return false;
1388                                 bezier->c2=last_duck();
1389                                 bezier->c2->set_type(Duck::TYPE_TANGENT);
1390                                 bezier->c2->set_origin(bezier->p2);
1391                                 bezier->c2->set_scalar(-TANGENT_BEZIER_SCALE);
1392                                 bezier->c2->set_tangent(true);
1393
1394                                 bezier->signal_user_click(2).connect(
1395                                         sigc::bind(
1396                                                 sigc::mem_fun(
1397                                                         *canvas_view,
1398                                                         &studio::CanvasView::popup_param_menu_bezier),
1399                                                 value_desc));
1400
1401                                 add_bezier(bezier);
1402                         }
1403                         else if(value_desc.get_value().is_valid())
1404                         {
1405                                 Segment segment=value_desc.get_value();
1406                                 etl::handle<Duck> duck_p,duck_c;
1407                                 synfig::String name;
1408                                 if(param_desc)
1409                                 {
1410                                         name=param_desc->get_local_name();
1411                                 }
1412                                 else
1413                                 {
1414                                         name=guid_string(value_desc);
1415                                 }
1416
1417                                 duck_p=new Duck(segment.p1);
1418                                 duck_p->set_name(name+".P1");
1419                                 duck_p->set_type(Duck::TYPE_VERTEX);
1420                                 add_duck(duck_p);
1421
1422                                 duck_c=new Duck(segment.t1);
1423                                 duck_c->set_name(name+".T1");
1424                                 duck_c->set_type(Duck::TYPE_TANGENT);
1425                                 add_duck(duck_c);
1426                                 duck_c->set_origin(duck_p);
1427                                 duck_c->set_scalar(TANGENT_HANDLE_SCALE);
1428                                 duck_c->set_tangent(true);
1429
1430                                 bezier->p1=duck_p;
1431                                 bezier->c1=duck_c;
1432
1433                                 duck_p=new Duck(segment.p2);
1434                                 duck_p->set_name(name+".P2");
1435                                 duck_p->set_type(Duck::TYPE_VERTEX);
1436                                 add_duck(duck_p);
1437
1438                                 duck_c=new Duck(segment.t2);
1439                                 duck_c->set_type(Duck::TYPE_TANGENT);
1440                                 duck_c->set_name(name+".T2");
1441                                 add_duck(duck_c);
1442                                 duck_c->set_origin(duck_p);
1443                                 duck_c->set_scalar(-TANGENT_HANDLE_SCALE);
1444                                 duck_c->set_tangent(true);
1445
1446                                 bezier->p2=duck_p;
1447                                 bezier->c2=duck_c;
1448                                 add_bezier(bezier);
1449                         }
1450
1451                         return true;
1452                 }
1453                 break;
1454         case ValueBase::TYPE_BLINEPOINT:
1455         {
1456
1457                 if(value_desc.is_value_node() &&
1458                         ValueNode_Composite::Handle::cast_dynamic(value_desc.get_value_node()))
1459                 {
1460                         ValueNode_Composite::Handle value_node;
1461                         value_node=ValueNode_Composite::Handle::cast_dynamic(value_desc.get_value_node());
1462
1463                         if(!add_to_ducks(synfigapp::ValueDesc(value_node,0),canvas_view,transform_stack))
1464                                 return false;
1465                         etl::handle<Duck> vertex_duck(last_duck());
1466                         vertex_duck->set_type(Duck::TYPE_VERTEX);
1467                         if(!add_to_ducks(synfigapp::ValueDesc(value_node,4),canvas_view,transform_stack))
1468                                 return false;
1469                         etl::handle<Duck> t1_duck(last_duck());
1470
1471                         t1_duck->set_origin(vertex_duck);
1472                         t1_duck->set_scalar(-TANGENT_HANDLE_SCALE);
1473                         t1_duck->set_tangent(true);
1474
1475                         etl::handle<Duck> t2_duck;
1476
1477                         // If the tangents are split
1478                         if((*value_node->get_link("split"))(get_time()).get(bool()))
1479                         {
1480                                 if(!add_to_ducks(synfigapp::ValueDesc(value_node,5),canvas_view,transform_stack))
1481                                         return false;
1482                                 t2_duck=last_duck();
1483                                 t2_duck->set_origin(vertex_duck);
1484                                 t2_duck->set_scalar(TANGENT_HANDLE_SCALE);
1485                                 t2_duck->set_tangent(true);
1486                         }
1487                         else
1488                         {
1489                                 if(!add_to_ducks(synfigapp::ValueDesc(value_node,4),canvas_view,transform_stack))
1490                                         return false;
1491                                 t2_duck=last_duck();
1492                                 t2_duck->set_origin(vertex_duck);
1493                                 t2_duck->set_scalar(TANGENT_HANDLE_SCALE);
1494                                 t2_duck->set_tangent(true);
1495                         }
1496                         return true;
1497                 }
1498
1499         }
1500         break;
1501         case ValueBase::TYPE_LIST:
1502         {
1503                 // Check for BLine
1504                 if (value_desc.is_value_node() &&
1505                         ValueNode_BLine::Handle::cast_dynamic(value_desc.get_value_node()))
1506                 {
1507                         ValueNode_BLine::Handle value_node;
1508                         value_node=ValueNode_BLine::Handle::cast_dynamic(value_desc.get_value_node());
1509
1510                         int i,first=-1;
1511
1512                         etl::handle<Bezier> bezier;
1513                         etl::handle<Duck> first_duck;
1514                         etl::handle<Duck> duck, tduck;
1515
1516                         for (i = 0; i < value_node->link_count(); i++)
1517                         {
1518                                 float amount(value_node->list[i].amount_at_time(get_time()));
1519
1520                                 // skip vertices that aren't fully on
1521                                 if (amount < 0.9999f)
1522                                         continue;
1523
1524                                 // remember the index of the first vertex we didn't skip
1525                                 if (first == -1)
1526                                         first = i;
1527
1528                                 BLinePoint bline_point((*value_node->get_link(i))(get_time()));
1529
1530                                 // try casting the vertex to Composite - this tells us whether it is composite or not
1531                                 ValueNode_Composite::Handle composite_vertex_value_node(
1532                                         ValueNode_Composite::Handle::cast_dynamic(value_node->get_link(i)));
1533
1534                                 // add the vertex duck - it's a composite
1535                                 if(composite_vertex_value_node)
1536                                 {
1537                                         if (add_to_ducks(synfigapp::ValueDesc(composite_vertex_value_node,0),canvas_view,transform_stack))
1538                                         {
1539                                                 duck=last_duck();
1540                                                 if(i==first)
1541                                                         first_duck=duck;
1542                                                 duck->set_type(Duck::TYPE_VERTEX);
1543
1544                                                 duck->signal_user_click(2).clear();
1545                                                 duck->signal_user_click(2).connect(
1546                                                         sigc::bind(
1547                                                                 sigc::bind(
1548                                                                         sigc::bind(
1549                                                                                 sigc::mem_fun(
1550                                                                                         *canvas_view,
1551                                                                                         &studio::CanvasView::popup_param_menu),
1552                                                                                 false),
1553                                                                         1.0f),
1554                                                                 synfigapp::ValueDesc(value_node,i)));
1555                                                 duck->set_value_desc(synfigapp::ValueDesc(value_node,i));
1556
1557                                                 if(param_desc)
1558                                                 {
1559                                                         if(!param_desc->get_origin().empty())
1560                                                         {
1561                                                                 synfigapp::ValueDesc value_desc_origin(value_desc.get_layer(),param_desc->get_origin());
1562                                                                 add_to_ducks(value_desc_origin,canvas_view, transform_stack);
1563                                                                 duck->set_origin(last_duck());
1564 /*
1565                                                                 ValueBase value(synfigapp::ValueDesc(value_desc.get_layer(),param_desc->get_origin()).get_value(get_time()));
1566                                                                 if(value.same_type_as(synfig::Point()))
1567                                                                         duck->set_origin(value.get(synfig::Point()));
1568 */
1569                                                         }
1570                                                 }
1571                                         }
1572                                         else
1573                                                 return false;
1574                                 }
1575                                 // else it's not a composite
1576                                 else
1577                                 {
1578                                         duck=new Duck(bline_point.get_vertex());
1579                                         if(i==first)
1580                                                 first_duck=duck;
1581                                         duck->set_transform_stack(transform_stack);
1582                                         duck->set_editable(false);
1583                                         //duck->set_name(strprintf("%x-vertex",value_node->get_link(i).get()));
1584                                         duck->set_name(guid_string(synfigapp::ValueDesc(value_node,i))+".v");
1585
1586                                         duck->set_type(Duck::TYPE_VERTEX);
1587                                         if(param_desc)
1588                                         {
1589                                                 if(!param_desc->get_origin().empty())
1590                                                 {
1591                                                                 synfigapp::ValueDesc value_desc_origin(value_desc.get_layer(),param_desc->get_origin());
1592                                                                 add_to_ducks(value_desc_origin,canvas_view, transform_stack);
1593                                                                 duck->set_origin(last_duck());
1594 /*
1595                                                                 ValueBase value(synfigapp::ValueDesc(value_desc.get_layer(),param_desc->get_origin()).get_value(get_time()));
1596                                                                 if(value.same_type_as(synfig::Point()))
1597                                                                         duck->set_origin(value.get(synfig::Point()));
1598 */
1599                                                 }
1600                                         }
1601                                         duck->set_guid(calc_duck_guid(synfigapp::ValueDesc(value_node,i),transform_stack)^synfig::GUID::hasher(".v"));
1602                                         duck=add_similar_duck(duck);
1603 //                                      add_duck(duck);
1604                                 }
1605
1606                                 // Add the width duck if it is a parameter with a hint (ie. "width") or if it isn't a parameter
1607                                 if ((param_desc && !param_desc->get_hint().empty()) ||
1608                                         !param_desc)
1609                                 {
1610                                         etl::handle<Duck> width;
1611                                         if (add_to_ducks(synfigapp::ValueDesc(composite_vertex_value_node,1),canvas_view,transform_stack,REAL_COOKIE))
1612                                         {
1613                                                 width=last_duck();
1614                                                 width->set_origin(duck);
1615                                                 width->set_type(Duck::TYPE_WIDTH);
1616                                                 width->set_name(guid_string(synfigapp::ValueDesc(value_node,i))+".w");
1617
1618                                                 // if the bline is a layer's parameter, scale the width duck by the layer's "width" parameter
1619                                                 if (param_desc)
1620                                                 {
1621                                                         ValueBase value(synfigapp::ValueDesc(value_desc.get_layer(),param_desc->get_hint()).get_value(get_time()));
1622                                                         if(value.same_type_as(synfig::Real()))
1623                                                                 width->set_scalar(value.get(synfig::Real())*0.5f);
1624                                                         // if it doesn't have a "width" parameter, scale by 0.5f instead
1625                                                         else
1626                                                                 width->set_scalar(0.5f);
1627                                                 }
1628                                                 // otherwise just present the raw unscaled width
1629                                                 else
1630                                                         width->set_scalar(0.5f);
1631                                         }
1632                                         else
1633                                                 synfig::error("Unable to add width duck!");
1634                                 }
1635
1636                                 // each bezier uses t2 of one point and t1 of the next
1637                                 // the first time through this loop we won't have the t2 duck from the previous vertex
1638                                 // and so we don't make a bezier.  instead we skip on to t2 for this point
1639                                 if(bezier)
1640                                 {
1641                                         // Add the tangent1 duck
1642                                         if(composite_vertex_value_node)
1643                                         {
1644                                                 if(!add_to_ducks(synfigapp::ValueDesc(composite_vertex_value_node,4),canvas_view,transform_stack))
1645                                                         return false;
1646                                                 tduck=last_duck();
1647                                         }
1648                                         else
1649                                         {
1650                                                 tduck=new Duck(bline_point.get_tangent1());
1651                                                 tduck->set_transform_stack(transform_stack);
1652                                                 tduck->set_editable(false);
1653                                                 tduck->set_name(guid_string(synfigapp::ValueDesc(value_node,i))+".t1");
1654 //                                              tduck->set_name(strprintf("%x-tangent1",value_node->get_link(i).get()));
1655                                                 tduck->set_guid(calc_duck_guid(synfigapp::ValueDesc(value_node,i),transform_stack)^synfig::GUID::hasher(".t1"));
1656                                                 tduck=add_similar_duck(tduck);
1657 //                                              add_duck(duck);
1658                                         }
1659
1660                                         tduck->set_origin(duck);
1661                                         tduck->set_scalar(-TANGENT_BEZIER_SCALE);
1662                                         tduck->set_tangent(true);
1663
1664                                         bezier->p2=duck;
1665                                         bezier->c2=tduck;
1666
1667                                         bezier->signal_user_click(2).connect(
1668                                                 sigc::bind(
1669                                                         sigc::mem_fun(
1670                                                                 *canvas_view,
1671                                                                 &studio::CanvasView::popup_param_menu_bezier),
1672                                                         synfigapp::ValueDesc(value_node,i)));
1673
1674                                         duck->signal_user_click(2).clear();
1675                                         duck->signal_user_click(2).connect(
1676                                                 sigc::bind(
1677                                                         sigc::bind(
1678                                                                 sigc::bind(
1679                                                                         sigc::mem_fun(
1680                                                                                 *canvas_view,
1681                                                                                 &studio::CanvasView::popup_param_menu),
1682                                                                         false),
1683                                                                 1.0f),
1684                                                         synfigapp::ValueDesc(value_node,i)));
1685                                         duck->set_value_desc(synfigapp::ValueDesc(value_node,i));
1686
1687                                         add_bezier(bezier);
1688                                         bezier=0;
1689                                 }
1690
1691                                 // don't start a new bezier for the last point in the line if we're not looped
1692                                 if(i+1>=value_node->link_count() && !value_node->get_loop())
1693                                         continue;
1694
1695                                 bezier=new Bezier();
1696
1697                                 // Add the tangent2 duck
1698                                 if(composite_vertex_value_node)
1699                                 {
1700                                         int i=bline_point.get_split_tangent_flag()?5:4;
1701                                         if(!add_to_ducks(synfigapp::ValueDesc(composite_vertex_value_node,i),canvas_view,transform_stack,0,2))
1702                                                 return false;
1703                                         tduck=last_duck();
1704                                 }
1705                                 else
1706                                 {
1707                                         if(bline_point.get_split_tangent_flag())
1708                                                 tduck=new Duck(bline_point.get_tangent2());
1709                                         else
1710                                                 tduck=new Duck(bline_point.get_tangent1());
1711
1712                                         tduck->set_transform_stack(transform_stack);
1713                                         tduck->set_name(guid_string(synfigapp::ValueDesc(value_node,i))+".t2");
1714                                         tduck->set_guid(calc_duck_guid(synfigapp::ValueDesc(value_node,i),transform_stack)^synfig::GUID::hasher(".t2"));
1715                                         tduck->set_editable(false);
1716                                         tduck=add_similar_duck(tduck);
1717 //                                      add_duck(duck);
1718                                         if(param_desc)
1719                                         {
1720                                                                 synfigapp::ValueDesc value_desc_origin(value_desc.get_layer(),param_desc->get_origin());
1721                                                                 add_to_ducks(value_desc_origin,canvas_view, transform_stack);
1722                                                                 duck->set_origin(last_duck());
1723 /*
1724                                                                 ValueBase value(synfigapp::ValueDesc(value_desc.get_layer(),param_desc->get_origin()).get_value(get_time()));
1725                                                                 if(value.same_type_as(synfig::Point()))
1726                                                                         duck->set_origin(value.get(synfig::Point()));
1727 */
1728 //                                              if(!param_desc->get_origin().empty())
1729 //                                                      duck->set_origin(synfigapp::ValueDesc(value_desc.get_layer(),param_desc->get_origin()).get_value(get_time()).get(synfig::Point()));
1730                                         }
1731                                         duck->signal_user_click(2).clear();
1732                                         duck->signal_user_click(2).connect(
1733                                                 sigc::bind(
1734                                                         sigc::bind(
1735                                                                 sigc::bind(
1736                                                                         sigc::mem_fun(
1737                                                                                 *canvas_view,
1738                                                                                 &studio::CanvasView::popup_param_menu),
1739                                                                         false),
1740                                                                 1.0f),
1741                                                         synfigapp::ValueDesc(value_node,i)));
1742                                         duck->set_value_desc(synfigapp::ValueDesc(value_node,i));
1743
1744                                 }
1745
1746                                 tduck->set_origin(duck);
1747                                 tduck->set_scalar(TANGENT_BEZIER_SCALE);
1748                                 tduck->set_tangent(true);
1749
1750                                 bezier->p1=duck;
1751                                 bezier->c1=tduck;
1752                         }
1753
1754                         // Loop if necessary
1755                         if(bezier && value_node->get_loop())
1756                         {
1757                                 BLinePoint bline_point((*value_node->get_link(first))(get_time()));
1758
1759                                 ValueNode_Composite::Handle composite_vertex_value_node(
1760                                         ValueNode_Composite::Handle::cast_dynamic(
1761                                                 value_node->get_link(first)));
1762
1763                                 // Add the vertex duck
1764                                 duck=first_duck;
1765
1766                                 // Add the tangent1 duck
1767                                 if(composite_vertex_value_node)
1768                                 {
1769                                         if(!add_to_ducks(synfigapp::ValueDesc(composite_vertex_value_node,4),canvas_view,transform_stack))
1770                                                 return false;
1771                                         tduck=last_duck();
1772                                 }
1773                                 else
1774                                 {
1775                                         tduck=new Duck(bline_point.get_tangent1());
1776                                         tduck->set_transform_stack(transform_stack);
1777                                         tduck->set_editable(false);
1778                                         tduck->set_name(guid_string(synfigapp::ValueDesc(value_node,first))+".t1");
1779                                         //tduck->set_name(strprintf("%x-tangent1",value_node->get_link(first).get()));
1780                                         tduck=add_similar_duck(tduck);
1781                                         tduck->set_guid(calc_duck_guid(synfigapp::ValueDesc(value_node,first),transform_stack)^synfig::GUID::hasher(".t1"));
1782                                         //add_duck(duck);
1783                                 }
1784
1785                                 tduck->set_origin(duck);
1786                                 tduck->set_scalar(-TANGENT_BEZIER_SCALE);
1787                                 tduck->set_tangent(true);
1788
1789                                 bezier->p2=duck;
1790                                 bezier->c2=tduck;
1791
1792                                 bezier->signal_user_click(2).connect(
1793                                         sigc::bind(
1794                                                 sigc::mem_fun(
1795                                                         *canvas_view,
1796                                                         &studio::CanvasView::popup_param_menu_bezier),
1797                                                 synfigapp::ValueDesc(value_node,first)));
1798
1799                                 duck->signal_user_click(2).clear();
1800                                 duck->signal_user_click(2).connect(
1801                                         sigc::bind(
1802                                                 sigc::bind(
1803                                                         sigc::bind(
1804                                                                 sigc::mem_fun(
1805                                                                         *canvas_view,
1806                                                                         &studio::CanvasView::popup_param_menu),
1807                                                                 false),
1808                                                         1.0f),
1809                                                 synfigapp::ValueDesc(value_node,first)));
1810                                 duck->set_value_desc(synfigapp::ValueDesc(value_node,first));
1811
1812                                 add_bezier(bezier);
1813                                 bezier=0;
1814                         }
1815                         return true;
1816                 }
1817                 else // Check for DynamicList
1818                 if(value_desc.is_value_node() &&
1819                         ValueNode_DynamicList::Handle::cast_dynamic(value_desc.get_value_node()))
1820                 {
1821                         ValueNode_DynamicList::Handle value_node;
1822                         value_node=ValueNode_DynamicList::Handle::cast_dynamic(value_desc.get_value_node());
1823                         int i;
1824
1825                         if(value_node->get_contained_type()==ValueBase::TYPE_VECTOR)
1826                         {
1827                                 Bezier bezier;
1828                                 etl::handle<Duck> first_duck, duck;
1829                                 int first = -1;
1830                                 for(i=0;i<value_node->link_count();i++)
1831                                 {
1832                                         if(!value_node->list[i].status_at_time(get_time()))
1833                                                 continue;
1834                                         if(!add_to_ducks(synfigapp::ValueDesc(value_node,i),canvas_view,transform_stack))
1835                                                 return false;
1836                                         duck = last_duck();
1837
1838                                         // remember the index of the first vertex we didn't skip
1839                                         if (first == -1)
1840                                         {
1841                                                 first = i;
1842                                                 first_duck = duck;
1843                                         }
1844
1845                                         if(param_desc)
1846                                         {
1847                                                                 synfigapp::ValueDesc value_desc_origin(value_desc.get_layer(),param_desc->get_origin());
1848                                                                 add_to_ducks(value_desc_origin,canvas_view, transform_stack);
1849                                                                 duck->set_origin(last_duck());
1850 /*
1851                                                                 ValueBase value(synfigapp::ValueDesc(value_desc.get_layer(),param_desc->get_origin()).get_value(get_time()));
1852                                                                 if(value.same_type_as(synfig::Point()))
1853                                                                         duck->set_origin(value.get(synfig::Point()));
1854 */
1855 //                                              if(!param_desc->get_origin().empty())
1856 //                                                      last_duck()->set_origin(synfigapp::ValueDesc(value_desc.get_layer(),param_desc->get_origin()).get_value(get_time()).get(synfig::Point()));
1857                                         }
1858                                         duck->set_type(Duck::TYPE_VERTEX);
1859                                         bezier.p1=bezier.p2;bezier.c1=bezier.c2;
1860                                         bezier.p2=bezier.c2=duck;
1861
1862                                         if (first != i)
1863                                         {
1864                                                 handle<Bezier> bezier_(new Bezier());
1865                                                 bezier_->p1=bezier.p1;
1866                                                 bezier_->c1=bezier.c1;
1867                                                 bezier_->p2=bezier.p2;
1868                                                 bezier_->c2=bezier.c2;
1869                                                 add_bezier(bezier_);
1870                                                 last_bezier()->signal_user_click(2).connect(
1871                                                         sigc::bind(
1872                                                                 sigc::mem_fun(
1873                                                                         *canvas_view,
1874                                                                         &studio::CanvasView::popup_param_menu_bezier),
1875                                                                 synfigapp::ValueDesc(value_node,i)));
1876                                         }
1877                                 }
1878
1879                                 if (value_node->get_loop() && first != -1 && first_duck != duck)
1880                                 {
1881                                         duck = first_duck;
1882
1883                                         bezier.p1=bezier.p2;bezier.c1=bezier.c2;
1884                                         bezier.p2=bezier.c2=duck;
1885
1886                                         handle<Bezier> bezier_(new Bezier());
1887                                         bezier_->p1=bezier.p1;
1888                                         bezier_->c1=bezier.c1;
1889                                         bezier_->p2=bezier.p2;
1890                                         bezier_->c2=bezier.c2;
1891                                         add_bezier(bezier_);
1892                                         last_bezier()->signal_user_click(2).connect(
1893                                                 sigc::bind(
1894                                                         sigc::mem_fun(
1895                                                                 *canvas_view,
1896                                                                 &studio::CanvasView::popup_param_menu_bezier),
1897                                                         synfigapp::ValueDesc(value_node,first)));
1898                                 }
1899                         }
1900                         else if(value_node->get_contained_type()==ValueBase::TYPE_SEGMENT)
1901                         {
1902                                 for(i=0;i<value_node->link_count();i++)
1903                                 {
1904                                         if(!value_node->list[i].status_at_time(get_time()))
1905                                                 continue;
1906                                         if(!add_to_ducks(synfigapp::ValueDesc(value_node,i),canvas_view,transform_stack))
1907                                                 return false;
1908                                 }
1909                         }
1910                         else
1911                                 return false;
1912                 }
1913                 else
1914                 {
1915                         // WRITEME
1916                 }
1917
1918                 return true;
1919         }
1920
1921         break;
1922         default:
1923                 break;
1924         }
1925         return false;
1926 }