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