Remove .gitignore do nothing is ignored.
[synfig.git] / synfig-studio / trunk / src / synfigapp / canvasinterface.cpp
index 9b1f6a9..cfc5db6 100644 (file)
@@ -6,6 +6,8 @@
 **
 **     \legal
 **     Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
+**     Copyright (c) 2007, 2008 Chris Moore
+**     Copyright (c) 2009 Carlos A. Sosa Navarro
 **
 **     This package is free software; you can redistribute it and/or
 **     modify it under the terms of the GNU General Public License as
@@ -40,6 +42,7 @@
 #include <synfig/valuenode_reference.h>
 #include <synfig/valuenode_twotone.h>
 #include <synfig/valuenode_stripes.h>
+#include <synfig/valuenode_bline.h>
 
 #include <synfig/waypoint.h>
 #include <synfig/loadcanvas.h>
@@ -50,6 +53,7 @@
 #include "instance.h"
 
 #include "actions/layeradd.h"
+#include "actions/layerremove.h"
 #include "actions/valuedescconvert.h"
 #include "actions/valuenodeadd.h"
 #include "actions/editmodeset.h"
@@ -59,6 +63,8 @@
 
 #include <synfig/gradient.h>
 
+#include "general.h"
+
 #endif
 
 /* === U S I N G =========================================================== */
@@ -76,7 +82,7 @@ using namespace synfigapp;
 
 /* === M E T H O D S ======================================================= */
 
-CanvasInterface::CanvasInterface(loose_handle<Instance> instance,handle<Canvas> canvas):
+CanvasInterface::CanvasInterface(etl::loose_handle<Instance> instance,etl::handle<synfig::Canvas> canvas):
        instance_(instance),
        canvas_(canvas),
        cur_time_(canvas->rend_desc().get_frame_start()),
@@ -88,7 +94,8 @@ CanvasInterface::CanvasInterface(loose_handle<Instance> instance,handle<Canvas>
 
 CanvasInterface::~CanvasInterface()
 {
-       synfig::info("synfigapp::CanvasInterface::~CanvasInterface(): Deleted");
+       if (getenv("SYNFIG_DEBUG_DESTRUCTORS"))
+               synfig::info("CanvasInterface::~CanvasInterface(): Deleted");
 }
 
 void
@@ -103,7 +110,14 @@ CanvasInterface::set_time(synfig::Time x)
        }
        if(cur_time_.is_equal(x))
                return;
-       cur_time_=x;
+       get_canvas()->set_time(cur_time_=x);
+
+       // update the time in all the child canvases
+       Canvas::Children children = get_canvas()->get_root()->children();
+       handle<CanvasInterface> interface;
+       for (Canvas::Children::iterator iter = children.begin(); iter != children.end(); iter++)
+               if ((interface = get_instance()->find_canvas_interface(*iter)) != this)
+                       interface->set_time(interface->get_canvas()->get_time());
 
        signal_time_changed()();
        signal_dirty_preview()();
@@ -124,7 +138,7 @@ CanvasInterface::refresh_current_values()
 }
 
 etl::handle<CanvasInterface>
-CanvasInterface::create(loose_handle<Instance> instance,handle<Canvas> canvas)
+CanvasInterface::create(etl::loose_handle<Instance> instance, etl::handle<synfig::Canvas> canvas)
 {
        etl::handle<CanvasInterface> intrfc;
        intrfc=new CanvasInterface(instance,canvas);
@@ -166,7 +180,7 @@ CanvasInterface::get_mode()const
 
 
 Layer::Handle
-CanvasInterface::add_layer_to(String name, Canvas::Handle canvas, int depth)
+CanvasInterface::add_layer_to(synfig::String name, synfig::Canvas::Handle canvas, int depth)
 {
        synfigapp::Action::PassiveGrouper group(get_instance().get(),_("Add Layer To"));
 
@@ -183,13 +197,31 @@ CanvasInterface::add_layer_to(String name, Canvas::Handle canvas, int depth)
                return 0;
        }
 
+       // automatically export the Index parameter of new Duplicate layers
+       if (name == "duplicate")
+               for (int i = 1; ; i++)
+               {
+                       String name = strprintf(_("Index %d"), i);
+                       try
+                       {
+                               canvas->find_value_node(name);
+                       }
+                       catch (Exception::IDNotFound x)
+                       {
+                               add_value_node(layer->dynamic_param_list().find("index")->second, name);
+                               break;
+                       }
+               }
+
        layer->set_canvas(canvas);
 
        // Apply some defaults
-       if(layer->set_param("fg",synfigapp::Main::get_foreground_color()))
-               layer->set_param("bg",synfigapp::Main::get_background_color());
+       if(layer->set_param("fg",synfigapp::Main::get_outline_color()))
+               layer->set_param("bg",synfigapp::Main::get_fill_color());
+       else if (name == "outline")
+               layer->set_param("color",synfigapp::Main::get_outline_color());
        else
-               layer->set_param("color",synfigapp::Main::get_foreground_color());
+               layer->set_param("color",synfigapp::Main::get_fill_color());
 
        layer->set_param("width",synfigapp::Main::get_bline_width().units(get_canvas()->rend_desc()));
        layer->set_param("gradient",synfigapp::Main::get_gradient());
@@ -198,18 +230,45 @@ CanvasInterface::add_layer_to(String name, Canvas::Handle canvas, int depth)
        layer->set_param("blend_method",synfigapp::Main::get_blend_method());
 
        {
-               // Grab the layer's list pf parameters
+               // Grab the layer's list of parameters
                Layer::ParamList paramlist=layer->get_param_list();
                Layer::ParamList::iterator iter;
+
+               // loop through the static parameters
                for(iter=paramlist.begin();iter!=paramlist.end();++iter)
                {
                        ValueNode::Handle value_node;
 
+                       // if we find any which are list values then make them
+                       // into dynamic list valuenodes, unless every element of
+                       // the list is a blinepoint, in which case convert it to a
+                       // bline
                        if(iter->second.get_type()==ValueBase::TYPE_LIST)
-                               value_node=LinkableValueNode::create("dynamic_list",iter->second);
+                       {
+                               // check whether it's a list of blinepoints only
+                               vector<ValueBase> list(iter->second.get_list());
+                               if (list.size())
+                               {
+                                       vector<ValueBase>::iterator iter2;
+                                       for (iter2 = list.begin(); iter2 != list.end(); iter2++)
+                                               if (iter2->get_type() != ValueBase::TYPE_BLINEPOINT)
+                                                       break;
+                                       if (iter2 == list.end())
+                                       {
+                                               value_node=LinkableValueNode::create("bline",iter->second);
+                                               ValueNode_BLine::Handle::cast_dynamic(value_node)->set_member_canvas(canvas);
+                                       }
+                               }
+
+                               if (!value_node)
+                                       value_node=LinkableValueNode::create("dynamic_list",iter->second);
+                       }
+                       // otherwise, if it's a type that can be converted to
+                       // 'composite' (other than the types that can be radial
+                       // composite) then do so
                        else if(LinkableValueNode::check_type("composite",iter->second.get_type()) &&
-                               (iter->second.get_type()!=ValueBase::TYPE_COLOR && iter->second.get_type()!=ValueBase::TYPE_VECTOR)
-                       )
+                                        (iter->second.get_type()!=ValueBase::TYPE_COLOR &&
+                                         iter->second.get_type()!=ValueBase::TYPE_VECTOR))
                                value_node=LinkableValueNode::create("composite",iter->second);
 
                        if(value_node)
@@ -240,11 +299,12 @@ CanvasInterface::add_layer_to(String name, Canvas::Handle canvas, int depth)
                return 0;
        }
 
-       synfig::info("DEPTH=%d",depth);
+       // synfig::info("DEPTH=%d",depth);
+
        // Action to move the layer (if necessary)
        if(depth>0)
        {
-               Action::Handle  action(Action::create("layer_move"));
+               Action::Handle  action(Action::create("LayerMove"));
 
                assert(action);
                if(!action)
@@ -274,7 +334,7 @@ CanvasInterface::add_layer_to(String name, Canvas::Handle canvas, int depth)
 
 
 bool
-CanvasInterface::convert(ValueDesc value_desc, String type)
+CanvasInterface::convert(ValueDesc value_desc, synfig::String type)
 {
        Action::Handle  action(Action::ValueDescConvert::create());
 
@@ -286,6 +346,7 @@ CanvasInterface::convert(ValueDesc value_desc, String type)
        action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
        action->set_param("value_desc",value_desc);
        action->set_param("type",type);
+       action->set_param("time",get_time());
 
        if(!action->is_ready())
        {
@@ -350,7 +411,18 @@ CanvasInterface::generate_param_list(const ValueDesc &value_desc)
                param_list.add("value_node",value_desc.get_value_node());
 
        if(value_desc.is_const())
-               param_list.add("value",value_desc.get_value());
+       {
+               // Fix 1868911: if we put a ValueBase holding a Canvas handle
+               // into the param_list and then export the canvas, the handle
+               // will miss out of having its reference count reduced,
+               // because by the time the handle is destructed the canvas
+               // will no longer be inline.  So let's not propogate that
+               // ValueBase any further than here.
+               if (value_desc.get_value_type() == ValueBase::TYPE_CANVAS)
+                       param_list.add("value",Canvas::LooseHandle(value_desc.get_value().get(Canvas::LooseHandle())));
+               else
+                       param_list.add("value",value_desc.get_value());
+       }
 
        if(value_desc.parent_is_layer_param())
        {
@@ -395,7 +467,7 @@ CanvasInterface::generate_param_list(const std::list<synfigapp::ValueDesc> &valu
 void
 CanvasInterface::set_rend_desc(const synfig::RendDesc &rend_desc)
 {
-       Action::Handle  action(Action::create("canvas_rend_desc_set"));
+       Action::Handle  action(Action::create("CanvasRendDescSet"));
 
        assert(action);
        if(!action)
@@ -409,30 +481,59 @@ CanvasInterface::set_rend_desc(const synfig::RendDesc &rend_desc)
                get_ui_interface()->error(_("Action Failed."));
 }
 
-bool
-CanvasInterface::set_name(const String &x)
+void
+CanvasInterface::set_name(const synfig::String &x)
 {
-       //! \todo This needs to be converted into an action
-       get_canvas()->set_name(x);
+       Action::Handle  action(Action::create("CanvasNameSet"));
+
+       assert(action);
+       if(!action)
+               return;
+
+       action->set_param("canvas",get_canvas());
+       action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
+       action->set_param("name",x);
+
+       if(!get_instance()->perform_action(action))
+               get_ui_interface()->error(_("Action Failed."));
+
        signal_id_changed_();
-       return true;
 }
 
-bool
-CanvasInterface::set_description(const String &x)
+void
+CanvasInterface::set_description(const synfig::String &x)
 {
-       //! \todo This needs to be converted into an action
-       get_canvas()->set_description(x);
-       return true;
+       Action::Handle  action(Action::create("CanvasDescriptionSet"));
+
+       assert(action);
+       if(!action)
+               return;
+
+       action->set_param("canvas",get_canvas());
+       action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
+       action->set_param("description",x);
+
+       if(!get_instance()->perform_action(action))
+               get_ui_interface()->error(_("Action Failed."));
 }
 
-bool
-CanvasInterface::set_id(const String &x)
+void
+CanvasInterface::set_id(const synfig::String &x)
 {
-       //! \todo This needs to be converted into an action
-       get_canvas()->set_id(x);
+       Action::Handle  action(Action::create("CanvasIdSet"));
+
+       assert(action);
+       if(!action)
+               return;
+
+       action->set_param("canvas",get_canvas());
+       action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
+       action->set_param("id",x);
+
+       if(!get_instance()->perform_action(action))
+               get_ui_interface()->error(_("Action Failed."));
+
        signal_id_changed_();
-       return true;
 }
 
 
@@ -463,35 +564,62 @@ CanvasInterface::jump_to_prev_keyframe()
 }
 
 bool
-CanvasInterface::import(const synfig::String &filename, bool copy)
+CanvasInterface::import(const synfig::String &filename, synfig::String &errors, synfig::String &warnings, bool resize_image)
 {
        Action::PassiveGrouper group(get_instance().get(),_("Import Image"));
 
        synfig::info("Attempting to import "+filename);
 
-
-       if(find(filename.begin(),filename.end(),'.')==filename.end())
+       if (filename_extension(filename) == "")
        {
-               get_ui_interface()->error("Filename must have an extension!");
+               get_ui_interface()->error(_("Filename must have an extension!"));
                return false;
        }
 
-       String ext(String(filename.begin()+filename.find_last_of('.')+1,filename.end()));
+       String ext(filename_extension(filename));
+       if (ext.size()) ext = ext.substr(1); // skip initial '.'
        std::transform(ext.begin(),ext.end(),ext.begin(),&::tolower);
 
+       if(ext=="svg"){//I don't like it, but worse is nothing
+               Layer::Handle _new_layer(add_layer_to("PasteCanvas",get_canvas()));
+               Layer::Handle _aux_layer(add_layer_to("svg_layer",get_canvas()));
+               if(_aux_layer){
+                       _aux_layer->set_param("filename",ValueBase(filename));
+                       _new_layer->set_param("canvas",ValueBase(_aux_layer->get_param("canvas")));
+                       //remove aux layer
+                       Action::Handle  action(Action::LayerRemove::create());
+                       assert(action);
+                       if(!action)
+                               return 0;
+                       action->set_param("canvas",get_canvas());
+                       action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
+                       action->set_param("layer",_aux_layer);
+                       if(!action->is_ready()){
+                               get_ui_interface()->error(_("Action Not Ready"));
+                               return 0;
+                       }
+                       if(!get_instance()->perform_action(action)){
+                               get_ui_interface()->error(_("Action Failed."));
+                               return 0;
+                       }
+               }
+               signal_layer_new_description()(_new_layer,filename);
+               return true;
+       }
+
        // If this is a SIF file, then we need to do things slightly differently
        if(ext=="sif" || ext=="sifz")try
        {
-
-               Canvas::Handle outside_canvas(synfig::open_canvas(filename));
+               Canvas::Handle outside_canvas(synfig::open_canvas(filename, errors, warnings));
                if(!outside_canvas)
-                       throw String(_("Unable to open this composition"));
+                       throw String(_("Unable to open this composition")) + ":\n\n" + errors;
 
                Layer::Handle layer(add_layer_to("PasteCanvas",get_canvas()));
                if(!layer)
-                       throw String(_("Unable to create \"PasteCanvas\" layer"));
+                       throw String(_("Unable to create \"Paste Canvas\" layer"));
                if(!layer->set_param("canvas",ValueBase(outside_canvas)))
                        throw int();
+               get_canvas()->register_external_canvas(filename, outside_canvas);
 
                //layer->set_description(basename(filename));
                signal_layer_new_description()(layer,filename);
@@ -499,7 +627,7 @@ CanvasInterface::import(const synfig::String &filename, bool copy)
        }
        catch(String x)
        {
-               get_ui_interface()->error(x+" -- "+filename);
+               get_ui_interface()->error(filename + ": " + x);
                return false;
        }
        catch(...)
@@ -508,8 +636,6 @@ CanvasInterface::import(const synfig::String &filename, bool copy)
                return false;
        }
 
-
-
        if(!Importer::book().count(ext))
        {
                get_ui_interface()->error(_("I don't know how to open images of this type -- ")+ext);
@@ -528,22 +654,34 @@ CanvasInterface::import(const synfig::String &filename, bool copy)
                h=layer->get_param("_height").get(int());
                if(w&&h)
                {
-                       Vector size=ValueBase(get_canvas()->rend_desc().get_br()-get_canvas()->rend_desc().get_tl());
-                       Vector x;
-                       if(size[0]<size[1])
+                       Vector x, size=ValueBase(get_canvas()->rend_desc().get_br()-get_canvas()->rend_desc().get_tl());
+
+                       // vector from top left of canvas to bottom right
+                       if (resize_image)
                        {
-                               x[0]=size[0];
-                               x[1]=size[0]/w*h;
-                               if(size[0]<0 ^ size[1]<0)
-                                       x[1]=-x[1];
+                               if(abs(size[0])<abs(size[1]))   // if canvas is tall and thin
+                               {
+                                       x[0]=size[0];   // use full width
+                                       x[1]=size[0]/w*h; // and scale for height
+                                       if((size[0]<0) ^ (size[1]<0))
+                                               x[1]=-x[1];
+                               }
+                               else                            // else canvas is short and fat (or maybe square)
+                               {
+                                       x[1]=size[1];   // use full height
+                                       x[0]=size[1]/h*w; // and scale for width
+                                       if((size[0]<0) ^ (size[1]<0))
+                                               x[0]=-x[0];
+                               }
                        }
                        else
                        {
-                               x[1]=size[1];
-                               x[0]=size[1]/h*w;
-                               if(size[0]<0 ^ size[1]<0)
-                                       x[0]=-x[0];
+                               x[0] = w/60.0;
+                               x[1] = h/60.0;
+                               if((size[0]<0)) x[0]=-x[0];
+                               if((size[1]<0)) x[1]=-x[1];
                        }
+
                        if(!layer->set_param("tl",ValueBase(-x/2)))
                                throw int();
                        if(!layer->set_param("br",ValueBase(x/2)))
@@ -574,7 +712,14 @@ CanvasInterface::import(const synfig::String &filename, bool copy)
 void
 CanvasInterface::waypoint_duplicate(synfigapp::ValueDesc value_desc,synfig::Waypoint waypoint)
 {
-       Action::Handle  action(Action::create("waypoint_set_smart"));
+       ValueNode::Handle value_node();
+       waypoint_duplicate(value_desc.get_value_node(), waypoint);
+}
+
+void
+CanvasInterface::waypoint_duplicate(ValueNode::Handle value_node,synfig::Waypoint waypoint)
+{
+       Action::Handle  action(Action::create("WaypointSetSmart"));
 
        assert(action);
        if(!action)
@@ -583,8 +728,6 @@ CanvasInterface::waypoint_duplicate(synfigapp::ValueDesc value_desc,synfig::Wayp
        waypoint.make_unique();
        waypoint.set_time(get_time());
 
-       ValueNode::Handle value_node(value_desc.get_value_node());
-
        action->set_param("canvas",get_canvas());
        action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
        action->set_param("waypoint",waypoint);
@@ -598,14 +741,19 @@ CanvasInterface::waypoint_duplicate(synfigapp::ValueDesc value_desc,synfig::Wayp
 void
 CanvasInterface::waypoint_remove(synfigapp::ValueDesc value_desc,synfig::Waypoint waypoint)
 {
-       Action::Handle  action(Action::create("waypoint_remove"));
+       ValueNode::Handle value_node();
+       waypoint_remove(value_desc.get_value_node(), waypoint);
+}
+
+void
+CanvasInterface::waypoint_remove(ValueNode::Handle value_node,synfig::Waypoint waypoint)
+{
+       Action::Handle  action(Action::create("WaypointRemove"));
 
        assert(action);
        if(!action)
                return;
 
-       ValueNode::Handle value_node(value_desc.get_value_node());
-
        action->set_param("canvas",get_canvas());
        action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
        action->set_param("waypoint",waypoint);
@@ -617,14 +765,14 @@ CanvasInterface::waypoint_remove(synfigapp::ValueDesc value_desc,synfig::Waypoin
 
 
 void
-CanvasInterface::auto_export(ValueNode::Handle value_node)
+CanvasInterface::auto_export(synfig::ValueNode::Handle /*value_node*/)
 {
 /*
        // Check to see if we are already exported.
        if(value_node->is_exported())
                return;
 
-       Action::Handle  action(Action::create("value_node_add"));
+       Action::Handle  action(Action::create("ValueNodeAdd"));
 
        assert(action);
        if(!action)
@@ -643,7 +791,7 @@ CanvasInterface::auto_export(ValueNode::Handle value_node)
 }
 
 void
-CanvasInterface::auto_export(const ValueDesc& value_desc)
+CanvasInterface::auto_export(const ValueDesc& /*value_desc*/)
 {
        // THIS FUNCTION IS DEPRECATED, AND IS NOW A STUB.
 #if 0
@@ -651,7 +799,7 @@ CanvasInterface::auto_export(const ValueDesc& value_desc)
        if(value_desc.is_exported())
                return;
 
-       Action::Handle  action(Action::create("value_desc_export"));
+       Action::Handle  action(Action::create("ValueDescExport"));
 
        assert(action);
        if(!action)
@@ -677,25 +825,28 @@ CanvasInterface::change_value(synfigapp::ValueDesc value_desc,synfig::ValueBase
                return true;
 
        // If this change needs to take place elsewhere, then so be it.
-       if(value_desc.get_canvas() && value_desc.get_canvas()->get_root()!=get_canvas()->get_root())do
+       if(value_desc.get_canvas())
        {
-               etl::handle<Instance> instance;
-               instance=find_instance(value_desc.get_canvas()->get_root());
-
-               if(instance)
-                       return instance->find_canvas_interface(value_desc.get_canvas())->change_value(value_desc,new_value);
-               else
+               if (value_desc.get_canvas()->get_root() != get_canvas()->get_root())
                {
-                       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."));
-                       return false;
+                       etl::handle<Instance> instance;
+                       instance=find_instance(value_desc.get_canvas()->get_root());
+
+                       if(instance)
+                               return instance->find_canvas_interface(value_desc.get_canvas())->change_value(value_desc,new_value);
+                       else
+                       {
+                               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."));
+                               return false;
+                       }
                }
-       }while(0);
+       }
 #ifdef _DEBUG
        else
        { synfig::warning("Can't get canvas from value desc...?"); }
 #endif
 
-       synfigapp::Action::Handle action(synfigapp::Action::create("value_desc_set"));
+       synfigapp::Action::Handle action(synfigapp::Action::create("ValueDescSet"));
        if(!action)
        {
                return false;
@@ -821,10 +972,10 @@ CanvasInterface::seek_frame(int frames)
        Time newtime(get_time()+(float)frames/fps);
        newtime=newtime.round(fps);
 
-       if(newtime<=get_canvas()->rend_desc().get_frame_start())
-               newtime=get_canvas()->rend_desc().get_frame_start();
-       if(newtime>=get_canvas()->rend_desc().get_frame_end())
-               newtime=get_canvas()->rend_desc().get_frame_end();
+       if(newtime<=get_canvas()->rend_desc().get_time_start())
+               newtime=get_canvas()->rend_desc().get_time_start();
+       if(newtime>=get_canvas()->rend_desc().get_time_end())
+               newtime=get_canvas()->rend_desc().get_time_end();
        set_time(newtime);
 }