Fix some compiler warnings.
[synfig.git] / synfig-studio / trunk / src / synfigapp / canvasinterface.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file canvasinterface.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
33 //#include <iostream>
34
35 #include <ETL/clock>
36 #include <synfig/valuenode_scale.h>
37 #include <synfig/valuenode_timedswap.h>
38 #include <synfig/valuenode_composite.h>
39 #include <synfig/valuenode_subtract.h>
40 #include <synfig/valuenode_linear.h>
41 #include <synfig/valuenode_reference.h>
42 #include <synfig/valuenode_twotone.h>
43 #include <synfig/valuenode_stripes.h>
44 #include <synfig/valuenode_bline.h>
45
46 #include <synfig/waypoint.h>
47 #include <synfig/loadcanvas.h>
48 #include <synfig/importer.h>
49 #include <synfig/guidset.h>
50
51 #include "canvasinterface.h"
52 #include "instance.h"
53
54 #include "actions/layeradd.h"
55 #include "actions/valuedescconvert.h"
56 #include "actions/valuenodeadd.h"
57 #include "actions/editmodeset.h"
58 #include "action_system.h"
59
60 #include "main.h"
61
62 #include <synfig/gradient.h>
63
64 #include "general.h"
65
66 #endif
67
68 /* === U S I N G =========================================================== */
69
70 using namespace std;
71 using namespace etl;
72 using namespace synfig;
73 using namespace synfigapp;
74
75 /* === M A C R O S ========================================================= */
76
77 /* === G L O B A L S ======================================================= */
78
79 /* === P R O C E D U R E S ================================================= */
80
81 /* === M E T H O D S ======================================================= */
82
83 CanvasInterface::CanvasInterface(etl::loose_handle<Instance> instance,etl::handle<synfig::Canvas> canvas):
84         instance_(instance),
85         canvas_(canvas),
86         cur_time_(canvas->rend_desc().get_frame_start()),
87         mode_(MODE_NORMAL|MODE_ANIMATE_PAST|MODE_ANIMATE_FUTURE)
88 {
89         set_selection_manager(get_instance()->get_selection_manager());
90         set_ui_interface(get_instance()->get_ui_interface());
91 }
92
93 CanvasInterface::~CanvasInterface()
94 {
95         if (getenv("SYNFIG_DEBUG_DESTRUCTORS"))
96                 synfig::info("CanvasInterface::~CanvasInterface(): Deleted");
97 }
98
99 void
100 CanvasInterface::set_time(synfig::Time x)
101 {
102         if(get_canvas()->rend_desc().get_frame_rate())
103         {
104                 float fps(get_canvas()->rend_desc().get_frame_rate());
105                 Time r(x.round(fps));
106                 //synfig::info("CanvasInterface::set_time(): %s rounded to %s\n",x.get_string(fps).c_str(),r.get_string(fps).c_str());
107                 x=r;
108         }
109         if(cur_time_.is_equal(x))
110                 return;
111         get_canvas()->set_time(cur_time_=x);
112
113         // update the time in all the child canvases
114         Canvas::Children children = get_canvas()->get_root()->children();
115         handle<CanvasInterface> interface;
116         for (Canvas::Children::iterator iter = children.begin(); iter != children.end(); iter++)
117                 if ((interface = get_instance()->find_canvas_interface(*iter)) != this)
118                         interface->set_time(interface->get_canvas()->get_time());
119
120         signal_time_changed()();
121         signal_dirty_preview()();
122 }
123
124 synfig::Time
125 CanvasInterface::get_time()const
126 {
127         return cur_time_;
128 }
129
130 void
131 CanvasInterface::refresh_current_values()
132 {
133         get_canvas()->set_time(cur_time_);
134         signal_time_changed()();
135         signal_dirty_preview()();
136 }
137
138 etl::handle<CanvasInterface>
139 CanvasInterface::create(etl::loose_handle<Instance> instance, etl::handle<synfig::Canvas> canvas)
140 {
141         etl::handle<CanvasInterface> intrfc;
142         intrfc=new CanvasInterface(instance,canvas);
143         instance->canvas_interface_list().push_front(intrfc);
144         return intrfc;
145 }
146
147 void
148 CanvasInterface::set_mode(Mode x)
149 {
150         Action::Handle  action(Action::EditModeSet::create());
151
152         assert(action);
153
154         action->set_param("canvas",get_canvas());
155         action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
156         action->set_param("edit_mode",x);
157
158         if(!action->is_ready())
159         {
160                 get_ui_interface()->error(_("Action Not Ready, unable to change mode"));
161                 assert(0);
162                 return;
163         }
164
165         if(!get_instance()->perform_action(action))
166                 get_ui_interface()->error(_("Unable to change mode"));
167
168 //      mode_=x;
169 //      signal_mode_changed_(x);
170 }
171
172 CanvasInterface::Mode
173 CanvasInterface::get_mode()const
174 {
175         return mode_;
176 }
177
178
179
180 Layer::Handle
181 CanvasInterface::add_layer_to(synfig::String name, synfig::Canvas::Handle canvas, int depth)
182 {
183         synfigapp::Action::PassiveGrouper group(get_instance().get(),_("Add Layer To"));
184
185         Layer::Handle   layer(Layer::create(name));
186
187         assert(layer);
188
189         if(!layer)
190                 return 0;
191
192         if(canvas!=get_canvas() && !canvas->is_inline())
193         {
194                 synfig::error("Bad canvas passed to \"add_layer_to\"");
195                 return 0;
196         }
197
198         // automatically export the Index parameter of new Duplicate layers
199         if (name == "duplicate")
200                 for (int i = 1; ; i++)
201                 {
202                         String name = strprintf(_("Index %d"), i);
203                         try
204                         {
205                                 canvas->find_value_node(name);
206                         }
207                         catch (Exception::IDNotFound x)
208                         {
209                                 add_value_node(layer->dynamic_param_list().find("index")->second, name);
210                                 break;
211                         }
212                 }
213
214         layer->set_canvas(canvas);
215
216         // Apply some defaults
217         if(layer->set_param("fg",synfigapp::Main::get_foreground_color()))
218                 layer->set_param("bg",synfigapp::Main::get_background_color());
219         else
220                 layer->set_param("color",synfigapp::Main::get_foreground_color());
221
222         layer->set_param("width",synfigapp::Main::get_bline_width().units(get_canvas()->rend_desc()));
223         layer->set_param("gradient",synfigapp::Main::get_gradient());
224         if(name!="zoom")
225                 layer->set_param("amount",synfigapp::Main::get_opacity());
226         layer->set_param("blend_method",synfigapp::Main::get_blend_method());
227
228         {
229                 // Grab the layer's list of parameters
230                 Layer::ParamList paramlist=layer->get_param_list();
231                 Layer::ParamList::iterator iter;
232
233                 // loop through the static parameters
234                 for(iter=paramlist.begin();iter!=paramlist.end();++iter)
235                 {
236                         ValueNode::Handle value_node;
237
238                         // if we find any which are list values then make them
239                         // into dynamic list valuenodes, unless every element of
240                         // the list is a blinepoint, in which case convert it to a
241                         // bline
242                         if(iter->second.get_type()==ValueBase::TYPE_LIST)
243                         {
244                                 // check whether it's a list of blinepoints only
245                                 vector<ValueBase> list(iter->second.get_list());
246                                 if (list.size())
247                                 {
248                                         vector<ValueBase>::iterator iter2;
249                                         for (iter2 = list.begin(); iter2 != list.end(); iter2++)
250                                                 if (iter2->get_type() != ValueBase::TYPE_BLINEPOINT)
251                                                         break;
252                                         if (iter2 == list.end())
253                                         {
254                                                 value_node=LinkableValueNode::create("bline",iter->second);
255                                                 ValueNode_BLine::Handle::cast_dynamic(value_node)->set_member_canvas(canvas);
256                                         }
257                                 }
258
259                                 if (!value_node)
260                                         value_node=LinkableValueNode::create("dynamic_list",iter->second);
261                         }
262                         // otherwise, if it's a type that can be converted to
263                         // 'composite' (other than the types that can be radial
264                         // composite) then do so
265                         else if(LinkableValueNode::check_type("composite",iter->second.get_type()) &&
266                                          (iter->second.get_type()!=ValueBase::TYPE_COLOR &&
267                                           iter->second.get_type()!=ValueBase::TYPE_VECTOR))
268                                 value_node=LinkableValueNode::create("composite",iter->second);
269
270                         if(value_node)
271                                 layer->connect_dynamic_param(iter->first,value_node);
272                 }
273         }
274
275         // Action to add the layer
276         Action::Handle  action(Action::LayerAdd::create());
277
278         assert(action);
279         if(!action)
280                 return 0;
281
282         action->set_param("canvas",canvas);
283         action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
284         action->set_param("new",layer);
285
286         if(!action->is_ready())
287         {
288                 get_ui_interface()->error(_("Action Not Ready"));
289                 return 0;
290         }
291
292         if(!get_instance()->perform_action(action))
293         {
294                 get_ui_interface()->error(_("Action Failed."));
295                 return 0;
296         }
297
298         // synfig::info("DEPTH=%d",depth);
299
300         // Action to move the layer (if necessary)
301         if(depth>0)
302         {
303                 Action::Handle  action(Action::create("layer_move"));
304
305                 assert(action);
306                 if(!action)
307                         return 0;
308
309                 action->set_param("canvas",canvas);
310                 action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
311                 action->set_param("layer",layer);
312                 action->set_param("new_index",depth);
313
314                 if(!action->is_ready())
315                 {
316                         get_ui_interface()->error(_("Move Action Not Ready"));
317                         return 0;
318                 }
319
320                 if(!get_instance()->perform_action(action))
321                 {
322                         get_ui_interface()->error(_("Move Action Failed."));
323                         return 0;
324                 }
325         }
326
327
328         return layer;
329 }
330
331
332 bool
333 CanvasInterface::convert(ValueDesc value_desc, synfig::String type)
334 {
335         Action::Handle  action(Action::ValueDescConvert::create());
336
337         assert(action);
338         if(!action)
339                 return 0;
340
341         action->set_param("canvas",get_canvas());
342         action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
343         action->set_param("value_desc",value_desc);
344         action->set_param("type",type);
345         action->set_param("time",get_time());
346
347         if(!action->is_ready())
348         {
349                 get_ui_interface()->error(_("Action Not Ready"));
350                 return 0;
351         }
352
353         if(get_instance()->perform_action(action))
354                 return true;
355
356         get_ui_interface()->error(_("Action Failed."));
357         return false;
358 }
359
360 bool
361 CanvasInterface::add_value_node(synfig::ValueNode::Handle value_node, synfig::String name)
362 {
363         if(name.empty())
364         {
365                 get_ui_interface()->error(_("Empty name!"));
366                 return false;
367         }
368
369         Action::Handle  action(Action::ValueNodeAdd::create());
370
371         assert(action);
372         if(!action)
373                 return 0;
374
375         action->set_param("canvas",get_canvas());
376         action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
377         action->set_param("new",value_node);
378         action->set_param("name",name);
379
380         if(!action->is_ready())
381         {
382                 get_ui_interface()->error(_("Action Not Ready"));
383                 return 0;
384         }
385
386         if(get_instance()->perform_action(action))
387                 return true;
388
389         get_ui_interface()->error(_("Action Failed."));
390         return false;
391 }
392
393 Action::ParamList
394 CanvasInterface::generate_param_list(const ValueDesc &value_desc)
395 {
396         synfigapp::Action::ParamList param_list;
397         param_list.add("time",get_time());
398         param_list.add("canvas_interface",etl::handle<CanvasInterface>(this));
399         param_list.add("canvas",get_canvas());
400
401         param_list.add("value_desc",value_desc);
402
403         if(value_desc.parent_is_value_node())
404                 param_list.add("parent_value_node",value_desc.get_parent_value_node());
405
406         if(value_desc.is_value_node())
407                 param_list.add("value_node",value_desc.get_value_node());
408
409         if(value_desc.is_const())
410         {
411                 // Fix 1868911: if we put a ValueBase holding a Canvas handle
412                 // into the param_list and then export the canvas, the handle
413                 // will miss out of having its reference count reduced,
414                 // because by the time the handle is destructed the canvas
415                 // will no longer be inline.  So let's not propogate that
416                 // ValueBase any further than here.
417                 if (value_desc.get_value_type() == ValueBase::TYPE_CANVAS)
418                         param_list.add("value",Canvas::LooseHandle(value_desc.get_value().get(Canvas::LooseHandle())));
419                 else
420                         param_list.add("value",value_desc.get_value());
421         }
422
423         if(value_desc.parent_is_layer_param())
424         {
425                 param_list.add("parent_layer",value_desc.get_layer());
426                 param_list.add("parent_layer_param",value_desc.get_param_name());
427         }
428
429         {
430                 synfigapp::SelectionManager::ChildrenList children_list;
431                 children_list=get_selection_manager()->get_selected_children();
432                 if(!value_desc.parent_is_canvas() && children_list.size()==1)
433                 {
434                         param_list.add("dest",value_desc);
435                         param_list.add("src",children_list.front().get_value_node());
436                 }
437         }
438         return param_list;
439 }
440
441 Action::ParamList
442 CanvasInterface::generate_param_list(const std::list<synfigapp::ValueDesc> &value_desc_list)
443 {
444         synfigapp::Action::ParamList param_list;
445         param_list.add("time",get_time());
446         param_list.add("canvas_interface",etl::handle<CanvasInterface>(this));
447         param_list.add("canvas",get_canvas());
448
449         std::list<synfigapp::ValueDesc>::const_iterator iter;
450         for(iter=value_desc_list.begin();iter!=value_desc_list.end();++iter)
451         {
452                 param_list.add("value_desc",*iter);
453                 if(iter->is_value_node())
454                 {
455                         param_list.add("value_node",iter->get_value_node());
456                 }
457         }
458
459
460         return param_list;
461 }
462
463 void
464 CanvasInterface::set_rend_desc(const synfig::RendDesc &rend_desc)
465 {
466         Action::Handle  action(Action::create("canvas_rend_desc_set"));
467
468         assert(action);
469         if(!action)
470                 return;
471
472         action->set_param("canvas",get_canvas());
473         action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
474         action->set_param("rend_desc",rend_desc);
475
476         if(!get_instance()->perform_action(action))
477                 get_ui_interface()->error(_("Action Failed."));
478 }
479
480 void
481 CanvasInterface::set_name(const synfig::String &x)
482 {
483         Action::Handle  action(Action::create("canvas_name_set"));
484
485         assert(action);
486         if(!action)
487                 return;
488
489         action->set_param("canvas",get_canvas());
490         action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
491         action->set_param("name",x);
492
493         if(!get_instance()->perform_action(action))
494                 get_ui_interface()->error(_("Action Failed."));
495
496         signal_id_changed_();
497 }
498
499 void
500 CanvasInterface::set_description(const synfig::String &x)
501 {
502         Action::Handle  action(Action::create("canvas_description_set"));
503
504         assert(action);
505         if(!action)
506                 return;
507
508         action->set_param("canvas",get_canvas());
509         action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
510         action->set_param("description",x);
511
512         if(!get_instance()->perform_action(action))
513                 get_ui_interface()->error(_("Action Failed."));
514 }
515
516 void
517 CanvasInterface::set_id(const synfig::String &x)
518 {
519         Action::Handle  action(Action::create("canvas_id_set"));
520
521         assert(action);
522         if(!action)
523                 return;
524
525         action->set_param("canvas",get_canvas());
526         action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
527         action->set_param("id",x);
528
529         if(!get_instance()->perform_action(action))
530                 get_ui_interface()->error(_("Action Failed."));
531
532         signal_id_changed_();
533 }
534
535
536 void
537 CanvasInterface::jump_to_next_keyframe()
538 {
539         synfig::info("Current time: %s",get_time().get_string().c_str());
540         try
541         {
542                 synfig::Keyframe keyframe(*get_canvas()->keyframe_list().find_next(get_time()));
543                 synfig::info("Jumping to keyframe \"%s\" at %s",keyframe.get_description().c_str(),keyframe.get_time().get_string().c_str());
544                 set_time(keyframe.get_time());
545         }
546         catch(...) { synfig::warning("Unable to find next keyframe"); }
547 }
548
549 void
550 CanvasInterface::jump_to_prev_keyframe()
551 {
552         synfig::info("Current time: %s",get_time().get_string().c_str());
553         try
554         {
555                 synfig::Keyframe keyframe(*get_canvas()->keyframe_list().find_prev(get_time()));
556                 synfig::info("Jumping to keyframe \"%s\" at %s",keyframe.get_description().c_str(),keyframe.get_time().get_string().c_str());
557                 set_time(keyframe.get_time());
558         }
559         catch(...) { synfig::warning("Unable to find prev keyframe"); }
560 }
561
562 bool
563 CanvasInterface::import(const synfig::String &filename, synfig::String &errors, synfig::String &warnings, bool resize_image)
564 {
565         Action::PassiveGrouper group(get_instance().get(),_("Import Image"));
566
567         synfig::info("Attempting to import "+filename);
568
569         if (filename_extension(filename) == "")
570         {
571                 get_ui_interface()->error(_("Filename must have an extension!"));
572                 return false;
573         }
574
575         String ext(filename_extension(filename));
576         if (ext.size()) ext = ext.substr(1); // skip initial '.'
577         std::transform(ext.begin(),ext.end(),ext.begin(),&::tolower);
578
579         // If this is a SIF file, then we need to do things slightly differently
580         if(ext=="sif" || ext=="sifz")try
581         {
582                 Canvas::Handle outside_canvas(synfig::open_canvas(filename, errors, warnings));
583                 if(!outside_canvas)
584                         throw String(_("Unable to open this composition")) + ":\n\n" + errors;
585
586                 Layer::Handle layer(add_layer_to("PasteCanvas",get_canvas()));
587                 if(!layer)
588                         throw String(_("Unable to create \"Paste Canvas\" layer"));
589                 if(!layer->set_param("canvas",ValueBase(outside_canvas)))
590                         throw int();
591                 get_canvas()->register_external_canvas(filename, outside_canvas);
592
593                 //layer->set_description(basename(filename));
594                 signal_layer_new_description()(layer,filename);
595                 return true;
596         }
597         catch(String x)
598         {
599                 get_ui_interface()->error(filename + ": " + x);
600                 return false;
601         }
602         catch(...)
603         {
604                 get_ui_interface()->error(_("Uncaught exception when attempting\nto open this composition -- ")+filename);
605                 return false;
606         }
607
608         if(!Importer::book().count(ext))
609         {
610                 get_ui_interface()->error(_("I don't know how to open images of this type -- ")+ext);
611                 return false;
612         }
613
614         try
615         {
616                 Layer::Handle layer(add_layer_to("Import",get_canvas()));
617                 int w,h;
618                 if(!layer)
619                         throw int();
620                 if(!layer->set_param("filename",ValueBase(filename)))
621                         throw int();
622                 w=layer->get_param("_width").get(int());
623                 h=layer->get_param("_height").get(int());
624                 if(w&&h)
625                 {
626                         Vector x, size=ValueBase(get_canvas()->rend_desc().get_br()-get_canvas()->rend_desc().get_tl());
627
628                         // vector from top left of canvas to bottom right
629                         if (resize_image)
630                         {
631                                 if(abs(size[0])<abs(size[1]))   // if canvas is tall and thin
632                                 {
633                                         x[0]=size[0];   // use full width
634                                         x[1]=size[0]/w*h; // and scale for height
635                                         if((size[0]<0) ^ (size[1]<0))
636                                                 x[1]=-x[1];
637                                 }
638                                 else                            // else canvas is short and fat (or maybe square)
639                                 {
640                                         x[1]=size[1];   // use full height
641                                         x[0]=size[1]/h*w; // and scale for width
642                                         if((size[0]<0) ^ (size[1]<0))
643                                                 x[0]=-x[0];
644                                 }
645                         }
646                         else
647                         {
648                                 x[0] = w/60.0;
649                                 x[1] = h/60.0;
650                                 if((size[0]<0)) x[0]=-x[0];
651                                 if((size[1]<0)) x[1]=-x[1];
652                         }
653
654                         if(!layer->set_param("tl",ValueBase(-x/2)))
655                                 throw int();
656                         if(!layer->set_param("br",ValueBase(x/2)))
657                                 throw int();
658                 }
659                 else
660                 {
661                         if(!layer->set_param("tl",ValueBase(get_canvas()->rend_desc().get_tl())))
662                                 throw int();
663                         if(!layer->set_param("br",ValueBase(get_canvas()->rend_desc().get_br())))
664                                 throw int();
665                 }
666
667                 layer->set_description(basename(filename));
668                 signal_layer_new_description()(layer,filename);
669
670                 return true;
671         }
672         catch(...)
673         {
674                 get_ui_interface()->error("Unable to import "+filename);
675                 group.cancel();
676                 return false;
677         }
678 }
679
680
681 void
682 CanvasInterface::waypoint_duplicate(synfigapp::ValueDesc value_desc,synfig::Waypoint waypoint)
683 {
684         ValueNode::Handle value_node();
685         waypoint_duplicate(value_desc.get_value_node(), waypoint);
686 }
687
688 void
689 CanvasInterface::waypoint_duplicate(ValueNode::Handle value_node,synfig::Waypoint waypoint)
690 {
691         Action::Handle  action(Action::create("waypoint_set_smart"));
692
693         assert(action);
694         if(!action)
695                 return;
696
697         waypoint.make_unique();
698         waypoint.set_time(get_time());
699
700         action->set_param("canvas",get_canvas());
701         action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
702         action->set_param("waypoint",waypoint);
703         action->set_param("time",get_time());
704         action->set_param("value_node",value_node);
705
706         if(!get_instance()->perform_action(action))
707                 get_ui_interface()->error(_("Action Failed."));
708 }
709
710 void
711 CanvasInterface::waypoint_remove(synfigapp::ValueDesc value_desc,synfig::Waypoint waypoint)
712 {
713         ValueNode::Handle value_node();
714         waypoint_remove(value_desc.get_value_node(), waypoint);
715 }
716
717 void
718 CanvasInterface::waypoint_remove(ValueNode::Handle value_node,synfig::Waypoint waypoint)
719 {
720         Action::Handle  action(Action::create("waypoint_remove"));
721
722         assert(action);
723         if(!action)
724                 return;
725
726         action->set_param("canvas",get_canvas());
727         action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
728         action->set_param("waypoint",waypoint);
729         action->set_param("value_node",value_node);
730
731         if(!get_instance()->perform_action(action))
732                 get_ui_interface()->error(_("Action Failed."));
733 }
734
735
736 void
737 CanvasInterface::auto_export(synfig::ValueNode::Handle /*value_node*/)
738 {
739 /*
740         // Check to see if we are already exported.
741         if(value_node->is_exported())
742                 return;
743
744         Action::Handle  action(Action::create("value_node_add"));
745
746         assert(action);
747         if(!action)
748                 return;
749
750         String name(strprintf(_("Unnamed%08d"),synfig::UniqueID().get_uid()));
751
752         action->set_param("canvas",get_canvas());
753         action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
754         action->set_param("new",value_node);
755         action->set_param("name",name);
756
757         if(!get_instance()->perform_action(action))
758                 get_ui_interface()->error(_("Action Failed."));
759 */
760 }
761
762 void
763 CanvasInterface::auto_export(const ValueDesc& /*value_desc*/)
764 {
765         // THIS FUNCTION IS DEPRECATED, AND IS NOW A STUB.
766 #if 0
767         // Check to see if we are already exported.
768         if(value_desc.is_exported())
769                 return;
770
771         Action::Handle  action(Action::create("value_desc_export"));
772
773         assert(action);
774         if(!action)
775                 return;
776
777         String name(strprintf(_("Unnamed%08d"),synfig::UniqueID().get_uid()));
778
779         action->set_param("canvas",get_canvas());
780         action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
781         action->set_param("value_desc",value_desc);
782         action->set_param("name",name);
783
784         if(!get_instance()->perform_action(action))
785                 get_ui_interface()->error(_("Action Failed."));
786 #endif
787 }
788
789 bool
790 CanvasInterface::change_value(synfigapp::ValueDesc value_desc,synfig::ValueBase new_value)
791 {
792         // If this isn't really a change, then don't bother
793         if(new_value==value_desc.get_value(get_time()))
794                 return true;
795
796         // If this change needs to take place elsewhere, then so be it.
797         if(value_desc.get_canvas())
798         {
799                 if (value_desc.get_canvas()->get_root() != get_canvas()->get_root())
800                 {
801                         etl::handle<Instance> instance;
802                         instance=find_instance(value_desc.get_canvas()->get_root());
803
804                         if(instance)
805                                 return instance->find_canvas_interface(value_desc.get_canvas())->change_value(value_desc,new_value);
806                         else
807                         {
808                                 get_ui_interface()->error(_("The value you are trying to edit is in a composition\nwhich doesn't seem to be open. Open that composition and you\nshould be able to edit this value as normal."));
809                                 return false;
810                         }
811                 }
812         }
813 #ifdef _DEBUG
814         else
815         { synfig::warning("Can't get canvas from value desc...?"); }
816 #endif
817
818         synfigapp::Action::Handle action(synfigapp::Action::create("value_desc_set"));
819         if(!action)
820         {
821                 return false;
822         }
823
824         action->set_param("canvas",get_canvas());
825         action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
826         action->set_param("time",get_time());
827         action->set_param("value_desc",value_desc);
828         action->set_param("new_value",new_value);
829
830         return get_instance()->perform_action(action);
831 }
832
833 void
834 CanvasInterface::set_meta_data(const synfig::String& key,const synfig::String& data)
835 {
836         get_canvas()->set_meta_data(key,data);
837 }
838
839
840 // this function goes with find_important_value_descs()
841 static int
842 _process_value_desc(const synfigapp::ValueDesc& value_desc,std::vector<synfigapp::ValueDesc>& out, synfig::GUIDSet& guid_set)
843 {
844         int ret(0);
845
846         if(value_desc.get_value_type()==ValueBase::TYPE_CANVAS)
847         {
848                 Canvas::Handle canvas;
849                 canvas=value_desc.get_value().get(canvas);
850                 if(!canvas || !canvas->is_inline())
851                         return ret;
852                 ret+=CanvasInterface::find_important_value_descs(canvas,out,guid_set);
853         }
854
855         if(value_desc.is_value_node())
856         {
857                 ValueNode::Handle value_node(value_desc.get_value_node());
858
859                 if(guid_set.count(value_node->get_guid()))
860                         return ret;
861                 guid_set.insert(value_node->get_guid());
862
863                 if(LinkableValueNode::Handle::cast_dynamic(value_node))
864                 {
865                         if(ValueNode_DynamicList::Handle::cast_dynamic(value_node))
866                         {
867                                 out.push_back(value_desc);
868                                 ret++;
869                         }
870                         // Process the linkable ValueNode's children
871                         LinkableValueNode::Handle value_node_copy(LinkableValueNode::Handle::cast_dynamic(value_node));
872                         int i;
873                         for(i=0;i<value_node_copy->link_count();i++)
874                         {
875                                 ValueNode::Handle link(value_node_copy->get_link(i));
876                                 if(!link->is_exported())
877                                         ret+=_process_value_desc(ValueDesc(value_node_copy,i),out,guid_set);
878                         }
879                 }
880                 else if(ValueNode_Animated::Handle::cast_dynamic(value_node))
881                 {
882                         out.push_back(value_desc);
883                         ret++;
884                 }
885         }
886
887         return ret;
888 }
889
890 int
891 CanvasInterface::find_important_value_descs(synfig::Canvas::Handle canvas,std::vector<synfigapp::ValueDesc>& out,synfig::GUIDSet& guid_set)
892 {
893         int ret(0);
894         if(!canvas->is_inline())
895         {
896                 ValueNodeList::const_iterator iter;
897
898                 for(
899                         iter=canvas->value_node_list().begin();
900                         iter!=canvas->value_node_list().end();
901                         ++iter)
902                         ret+=_process_value_desc(ValueDesc(canvas,(*iter)->get_id()),out,guid_set);
903         }
904
905         Canvas::const_iterator iter;
906
907         for(iter=canvas->begin();iter!=canvas->end();++iter)
908         {
909                 Layer::Handle layer(*iter);
910
911                 Layer::DynamicParamList::const_iterator iter;
912                 for(
913                         iter=layer->dynamic_param_list().begin();
914                         iter!=layer->dynamic_param_list().end();
915                         ++iter)
916                 {
917                         if(!iter->second->is_exported())
918                                 ret+=_process_value_desc(ValueDesc(layer,iter->first),out,guid_set);
919                 }
920                 ValueBase value(layer->get_param("canvas"));
921                 if(value.is_valid())
922                         ret+=_process_value_desc(ValueDesc(layer,"canvas"),out,guid_set);
923         }
924
925         return ret;
926 }
927
928 int
929 CanvasInterface::find_important_value_descs(std::vector<synfigapp::ValueDesc>& out)
930 {
931         synfig::GUIDSet tmp;
932         return find_important_value_descs(get_canvas(),out,tmp);
933 }
934
935 void
936 CanvasInterface::seek_frame(int frames)
937 {
938         if(!frames)
939                 return;
940         float fps(get_canvas()->rend_desc().get_frame_rate());
941         Time newtime(get_time()+(float)frames/fps);
942         newtime=newtime.round(fps);
943
944         if(newtime<=get_canvas()->rend_desc().get_time_start())
945                 newtime=get_canvas()->rend_desc().get_time_start();
946         if(newtime>=get_canvas()->rend_desc().get_time_end())
947                 newtime=get_canvas()->rend_desc().get_time_end();
948         set_time(newtime);
949 }
950
951 void
952 CanvasInterface::seek_time(synfig::Time time)
953 {
954         if(!time)
955                 return;
956
957         float fps(get_canvas()->rend_desc().get_frame_rate());
958
959         if(time>=synfig::Time::end())
960         {
961                 set_time(get_canvas()->rend_desc().get_time_end());
962                 return;
963         }
964         if(time<=synfig::Time::begin())
965         {
966                 set_time(get_canvas()->rend_desc().get_time_start());
967                 return;
968         }
969
970         Time newtime(get_time()+time);
971         newtime=newtime.round(fps);
972
973         if(newtime<=get_canvas()->rend_desc().get_time_start())
974                 newtime=get_canvas()->rend_desc().get_time_start();
975         if(newtime>=get_canvas()->rend_desc().get_time_end())
976                 newtime=get_canvas()->rend_desc().get_time_end();
977         set_time(newtime);
978 }