40d14df59b8a164123b20fa9649605ecb1e5c1bf
[synfig.git] /
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, bool /*copy*/)
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
583                 Canvas::Handle outside_canvas(synfig::open_canvas(filename));
584                 if(!outside_canvas)
585                         throw String(_("Unable to open this composition"));
586
587                 Layer::Handle layer(add_layer_to("PasteCanvas",get_canvas()));
588                 if(!layer)
589                         throw String(_("Unable to create \"Paste Canvas\" layer"));
590                 if(!layer->set_param("canvas",ValueBase(outside_canvas)))
591                         throw int();
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(x+" -- "+filename);
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 size=ValueBase(get_canvas()->rend_desc().get_br()-get_canvas()->rend_desc().get_tl());
627                         Vector x;
628                         if(size[0]<size[1])
629                         {
630                                 x[0]=size[0];
631                                 x[1]=size[0]/w*h;
632                                 if((size[0]<0) ^ (size[1]<0))
633                                         x[1]=-x[1];
634                         }
635                         else
636                         {
637                                 x[1]=size[1];
638                                 x[0]=size[1]/h*w;
639                                 if((size[0]<0) ^ (size[1]<0))
640                                         x[0]=-x[0];
641                         }
642                         if(!layer->set_param("tl",ValueBase(-x/2)))
643                                 throw int();
644                         if(!layer->set_param("br",ValueBase(x/2)))
645                                 throw int();
646                 }
647                 else
648                 {
649                         if(!layer->set_param("tl",ValueBase(get_canvas()->rend_desc().get_tl())))
650                                 throw int();
651                         if(!layer->set_param("br",ValueBase(get_canvas()->rend_desc().get_br())))
652                                 throw int();
653                 }
654
655                 layer->set_description(basename(filename));
656                 signal_layer_new_description()(layer,filename);
657
658                 return true;
659         }
660         catch(...)
661         {
662                 get_ui_interface()->error("Unable to import "+filename);
663                 group.cancel();
664                 return false;
665         }
666 }
667
668
669 void
670 CanvasInterface::waypoint_duplicate(synfigapp::ValueDesc value_desc,synfig::Waypoint waypoint)
671 {
672         ValueNode::Handle value_node();
673         waypoint_duplicate(value_desc.get_value_node(), waypoint);
674 }
675
676 void
677 CanvasInterface::waypoint_duplicate(ValueNode::Handle value_node,synfig::Waypoint waypoint)
678 {
679         Action::Handle  action(Action::create("waypoint_set_smart"));
680
681         assert(action);
682         if(!action)
683                 return;
684
685         waypoint.make_unique();
686         waypoint.set_time(get_time());
687
688         action->set_param("canvas",get_canvas());
689         action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
690         action->set_param("waypoint",waypoint);
691         action->set_param("time",get_time());
692         action->set_param("value_node",value_node);
693
694         if(!get_instance()->perform_action(action))
695                 get_ui_interface()->error(_("Action Failed."));
696 }
697
698 void
699 CanvasInterface::waypoint_remove(synfigapp::ValueDesc value_desc,synfig::Waypoint waypoint)
700 {
701         ValueNode::Handle value_node();
702         waypoint_remove(value_desc.get_value_node(), waypoint);
703 }
704
705 void
706 CanvasInterface::waypoint_remove(ValueNode::Handle value_node,synfig::Waypoint waypoint)
707 {
708         Action::Handle  action(Action::create("waypoint_remove"));
709
710         assert(action);
711         if(!action)
712                 return;
713
714         action->set_param("canvas",get_canvas());
715         action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
716         action->set_param("waypoint",waypoint);
717         action->set_param("value_node",value_node);
718
719         if(!get_instance()->perform_action(action))
720                 get_ui_interface()->error(_("Action Failed."));
721 }
722
723
724 void
725 CanvasInterface::auto_export(synfig::ValueNode::Handle /*value_node*/)
726 {
727 /*
728         // Check to see if we are already exported.
729         if(value_node->is_exported())
730                 return;
731
732         Action::Handle  action(Action::create("value_node_add"));
733
734         assert(action);
735         if(!action)
736                 return;
737
738         String name(strprintf(_("Unnamed%08d"),synfig::UniqueID().get_uid()));
739
740         action->set_param("canvas",get_canvas());
741         action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
742         action->set_param("new",value_node);
743         action->set_param("name",name);
744
745         if(!get_instance()->perform_action(action))
746                 get_ui_interface()->error(_("Action Failed."));
747 */
748 }
749
750 void
751 CanvasInterface::auto_export(const ValueDesc& /*value_desc*/)
752 {
753         // THIS FUNCTION IS DEPRECATED, AND IS NOW A STUB.
754 #if 0
755         // Check to see if we are already exported.
756         if(value_desc.is_exported())
757                 return;
758
759         Action::Handle  action(Action::create("value_desc_export"));
760
761         assert(action);
762         if(!action)
763                 return;
764
765         String name(strprintf(_("Unnamed%08d"),synfig::UniqueID().get_uid()));
766
767         action->set_param("canvas",get_canvas());
768         action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
769         action->set_param("value_desc",value_desc);
770         action->set_param("name",name);
771
772         if(!get_instance()->perform_action(action))
773                 get_ui_interface()->error(_("Action Failed."));
774 #endif
775 }
776
777 bool
778 CanvasInterface::change_value(synfigapp::ValueDesc value_desc,synfig::ValueBase new_value)
779 {
780         // If this isn't really a change, then don't bother
781         if(new_value==value_desc.get_value(get_time()))
782                 return true;
783
784         // If this change needs to take place elsewhere, then so be it.
785         if(value_desc.get_canvas())
786         {
787                 if (value_desc.get_canvas()->get_root() != get_canvas()->get_root())
788                 {
789                         etl::handle<Instance> instance;
790                         instance=find_instance(value_desc.get_canvas()->get_root());
791
792                         if(instance)
793                                 return instance->find_canvas_interface(value_desc.get_canvas())->change_value(value_desc,new_value);
794                         else
795                         {
796                                 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."));
797                                 return false;
798                         }
799                 }
800         }
801 #ifdef _DEBUG
802         else
803         { synfig::warning("Can't get canvas from value desc...?"); }
804 #endif
805
806         synfigapp::Action::Handle action(synfigapp::Action::create("value_desc_set"));
807         if(!action)
808         {
809                 return false;
810         }
811
812         action->set_param("canvas",get_canvas());
813         action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
814         action->set_param("time",get_time());
815         action->set_param("value_desc",value_desc);
816         action->set_param("new_value",new_value);
817
818         return get_instance()->perform_action(action);
819 }
820
821 void
822 CanvasInterface::set_meta_data(const synfig::String& key,const synfig::String& data)
823 {
824         get_canvas()->set_meta_data(key,data);
825 }
826
827
828 // this function goes with find_important_value_descs()
829 static int
830 _process_value_desc(const synfigapp::ValueDesc& value_desc,std::vector<synfigapp::ValueDesc>& out, synfig::GUIDSet& guid_set)
831 {
832         int ret(0);
833
834         if(value_desc.get_value_type()==ValueBase::TYPE_CANVAS)
835         {
836                 Canvas::Handle canvas;
837                 canvas=value_desc.get_value().get(canvas);
838                 if(!canvas || !canvas->is_inline())
839                         return ret;
840                 ret+=CanvasInterface::find_important_value_descs(canvas,out,guid_set);
841         }
842
843         if(value_desc.is_value_node())
844         {
845                 ValueNode::Handle value_node(value_desc.get_value_node());
846
847                 if(guid_set.count(value_node->get_guid()))
848                         return ret;
849                 guid_set.insert(value_node->get_guid());
850
851                 if(LinkableValueNode::Handle::cast_dynamic(value_node))
852                 {
853                         if(ValueNode_DynamicList::Handle::cast_dynamic(value_node))
854                         {
855                                 out.push_back(value_desc);
856                                 ret++;
857                         }
858                         // Process the linkable ValueNode's children
859                         LinkableValueNode::Handle value_node_copy(LinkableValueNode::Handle::cast_dynamic(value_node));
860                         int i;
861                         for(i=0;i<value_node_copy->link_count();i++)
862                         {
863                                 ValueNode::Handle link(value_node_copy->get_link(i));
864                                 if(!link->is_exported())
865                                         ret+=_process_value_desc(ValueDesc(value_node_copy,i),out,guid_set);
866                         }
867                 }
868                 else if(ValueNode_Animated::Handle::cast_dynamic(value_node))
869                 {
870                         out.push_back(value_desc);
871                         ret++;
872                 }
873         }
874
875         return ret;
876 }
877
878 int
879 CanvasInterface::find_important_value_descs(synfig::Canvas::Handle canvas,std::vector<synfigapp::ValueDesc>& out,synfig::GUIDSet& guid_set)
880 {
881         int ret(0);
882         if(!canvas->is_inline())
883         {
884                 ValueNodeList::const_iterator iter;
885
886                 for(
887                         iter=canvas->value_node_list().begin();
888                         iter!=canvas->value_node_list().end();
889                         ++iter)
890                         ret+=_process_value_desc(ValueDesc(canvas,(*iter)->get_id()),out,guid_set);
891         }
892
893         Canvas::const_iterator iter;
894
895         for(iter=canvas->begin();iter!=canvas->end();++iter)
896         {
897                 Layer::Handle layer(*iter);
898
899                 Layer::DynamicParamList::const_iterator iter;
900                 for(
901                         iter=layer->dynamic_param_list().begin();
902                         iter!=layer->dynamic_param_list().end();
903                         ++iter)
904                 {
905                         if(!iter->second->is_exported())
906                                 ret+=_process_value_desc(ValueDesc(layer,iter->first),out,guid_set);
907                 }
908                 ValueBase value(layer->get_param("canvas"));
909                 if(value.is_valid())
910                         ret+=_process_value_desc(ValueDesc(layer,"canvas"),out,guid_set);
911         }
912
913         return ret;
914 }
915
916 int
917 CanvasInterface::find_important_value_descs(std::vector<synfigapp::ValueDesc>& out)
918 {
919         synfig::GUIDSet tmp;
920         return find_important_value_descs(get_canvas(),out,tmp);
921 }
922
923 void
924 CanvasInterface::seek_frame(int frames)
925 {
926         if(!frames)
927                 return;
928         float fps(get_canvas()->rend_desc().get_frame_rate());
929         Time newtime(get_time()+(float)frames/fps);
930         newtime=newtime.round(fps);
931
932         if(newtime<=get_canvas()->rend_desc().get_time_start())
933                 newtime=get_canvas()->rend_desc().get_time_start();
934         if(newtime>=get_canvas()->rend_desc().get_time_end())
935                 newtime=get_canvas()->rend_desc().get_time_end();
936         set_time(newtime);
937 }
938
939 void
940 CanvasInterface::seek_time(synfig::Time time)
941 {
942         if(!time)
943                 return;
944
945         float fps(get_canvas()->rend_desc().get_frame_rate());
946
947         if(time>=synfig::Time::end())
948         {
949                 set_time(get_canvas()->rend_desc().get_time_end());
950                 return;
951         }
952         if(time<=synfig::Time::begin())
953         {
954                 set_time(get_canvas()->rend_desc().get_time_start());
955                 return;
956         }
957
958         Time newtime(get_time()+time);
959         newtime=newtime.round(fps);
960
961         if(newtime<=get_canvas()->rend_desc().get_time_start())
962                 newtime=get_canvas()->rend_desc().get_time_start();
963         if(newtime>=get_canvas()->rend_desc().get_time_end())
964                 newtime=get_canvas()->rend_desc().get_time_end();
965         set_time(newtime);
966 }