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