The previous commit was missing this change to make BLineCalcTangent ducks editable.
[synfig.git] / synfig-studio / trunk / src / synfigapp / instance.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file synfigapp/instance.cpp
3 **      \brief Instance
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 "instance.h"
34 #include "canvasinterface.h"
35 #include <iostream>
36 #include <synfig/loadcanvas.h>
37 #include <synfig/savecanvas.h>
38 #include <synfig/valuenode_composite.h>
39 #include <synfig/valuenode_radialcomposite.h>
40 #include <synfig/valuenode_reference.h>
41 #include <synfig/valuenode_blinecalcvertex.h>
42 #include <synfig/valuenode_blinecalctangent.h>
43 #include <map>
44
45 #include "general.h"
46
47 #endif
48
49 /* === U S I N G =========================================================== */
50
51 using namespace std;
52 using namespace etl;
53 using namespace synfig;
54 using namespace synfigapp;
55
56 /* === M A C R O S ========================================================= */
57
58 /* === G L O B A L S ======================================================= */
59
60 static std::map<loose_handle<Canvas>, loose_handle<Instance> > instance_map_;
61
62 /* === P R O C E D U R E S ================================================= */
63
64 bool
65 synfigapp::is_editable(synfig::ValueNode::Handle value_node)
66 {
67         if(ValueNode_Const::Handle::cast_dynamic(value_node)
68                 || ValueNode_Animated::Handle::cast_dynamic(value_node)
69                 || ValueNode_Composite::Handle::cast_dynamic(value_node)
70                 || ValueNode_RadialComposite::Handle::cast_dynamic(value_node)
71                 || ValueNode_Reference::Handle::cast_dynamic(value_node)
72                 || ValueNode_BLineCalcVertex::Handle::cast_dynamic(value_node)
73                 || ValueNode_BLineCalcTangent::Handle::cast_dynamic(value_node)
74         )
75                 return true;
76         return false;
77 }
78
79 etl::handle<Instance>
80 synfigapp::find_instance(etl::handle<synfig::Canvas> canvas)
81 {
82         if(instance_map_.count(canvas)==0)
83                 return 0;
84         return instance_map_[canvas];
85 }
86
87 /* === M E T H O D S ======================================================= */
88
89 Instance::Instance(etl::handle<synfig::Canvas> canvas):
90         CVSInfo(canvas->get_file_name()),
91         canvas_(canvas)
92 {
93         assert(canvas->is_root());
94
95         unset_selection_manager();
96
97         instance_map_[canvas]=this;
98 } // END of synfigapp::Instance::Instance()
99
100 handle<Instance>
101 Instance::create(etl::handle<synfig::Canvas> canvas)
102 {
103         // Construct a new instance
104         handle<Instance> instance(new Instance(canvas));
105
106         return instance;
107 } // END of synfigapp::Instance::create()
108
109 synfig::String
110 Instance::get_file_name()const
111 {
112         return get_canvas()->get_file_name();
113 }
114
115 void
116 Instance::set_file_name(const synfig::String &name)
117 {
118         get_canvas()->set_file_name(name);
119         CVSInfo::set_file_name(name);
120 }
121
122 Instance::~Instance()
123 {
124         instance_map_.erase(canvas_);
125
126         if (getenv("SYNFIG_DEBUG_DESTRUCTORS"))
127                 synfig::info("Instance::~Instance(): Deleted");
128 }
129
130 handle<CanvasInterface>
131 Instance::find_canvas_interface(synfig::Canvas::Handle canvas)
132 {
133         if(!canvas)
134                 return 0;
135         while(canvas->is_inline())
136                 canvas=canvas->parent();
137
138         CanvasInterfaceList::iterator iter;
139
140         for(iter=canvas_interface_list().begin();iter!=canvas_interface_list().end();iter++)
141                 if((*iter)->get_canvas()==canvas)
142                         return *iter;
143
144         return CanvasInterface::create(this,canvas);
145 }
146
147 bool
148 Instance::save()const
149 {
150         bool ret=save_canvas(get_file_name(),canvas_);
151         if(ret)
152         {
153                 reset_action_count();
154                 const_cast<sigc::signal<void>& >(signal_saved_)();
155         }
156         return ret;
157 }
158
159 bool
160 Instance::save_as(const synfig::String &file_name)
161 {
162         bool ret;
163
164         String old_file_name(get_file_name());
165
166         set_file_name(file_name);
167
168         ret=save_canvas(file_name,canvas_);
169
170         if(ret)
171         {
172                 reset_action_count();
173                 signal_saved_();
174         }
175         else
176                 set_file_name(old_file_name);
177
178         signal_filename_changed_();
179
180         return ret;
181 }