Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-studio / tags / synfigstudio_0_61_03 / synfig-studio / src / synfigapp / instance.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file instance.cpp
3 **      \brief Instance
4 **
5 **      $Id: instance.cpp,v 1.1.1.1 2005/01/07 03:34:37 darco Exp $
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **
10 **      This package is free software; you can redistribute it and/or
11 **      modify it under the terms of the GNU General Public License as
12 **      published by the Free Software Foundation; either version 2 of
13 **      the License, or (at your option) any later version.
14 **
15 **      This package is distributed in the hope that it will be useful,
16 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
17 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 **      General Public License for more details.
19 **      \endlegal
20 */
21 /* ========================================================================= */
22
23 /* === H E A D E R S ======================================================= */
24
25 #ifdef USING_PCH
26 #       include "pch.h"
27 #else
28 #ifdef HAVE_CONFIG_H
29 #       include <config.h>
30 #endif
31
32 #include "instance.h"
33 #include "canvasinterface.h"
34 #include <iostream>
35 #include <synfig/loadcanvas.h>
36 #include <synfig/savecanvas.h>
37 #include <synfig/valuenode_composite.h>
38 #include <synfig/valuenode_radialcomposite.h>
39 #include <synfig/valuenode_reference.h>
40 #include <map>
41
42 #endif
43
44 /* === U S I N G =========================================================== */
45
46 using namespace std;
47 using namespace etl;
48 using namespace synfig;
49 using namespace synfigapp;
50
51 /* === M A C R O S ========================================================= */
52
53 /* === G L O B A L S ======================================================= */
54
55 static std::map<loose_handle<Canvas>, loose_handle<Instance> > instance_map_;
56
57 /* === P R O C E D U R E S ================================================= */
58
59 bool
60 synfigapp::is_editable(synfig::ValueNode::Handle value_node)
61 {
62         if(ValueNode_Const::Handle::cast_dynamic(value_node)
63                 || ValueNode_TimedSwap::Handle::cast_dynamic(value_node)
64                 || ValueNode_Animated::Handle::cast_dynamic(value_node)
65                 || ValueNode_Composite::Handle::cast_dynamic(value_node)
66                 || ValueNode_RadialComposite::Handle::cast_dynamic(value_node)
67                 || ValueNode_Reference::Handle::cast_dynamic(value_node)
68         )
69                 return true;
70         return false;
71 }
72
73 etl::handle<Instance>
74 synfigapp::find_instance(etl::handle<synfig::Canvas> canvas)
75 {
76         if(instance_map_.count(canvas)==0)
77                 return 0;
78         return instance_map_[canvas];
79 }
80
81 /* === M E T H O D S ======================================================= */
82
83 Instance::Instance(Canvas::Handle canvas):
84         CVSInfo(canvas->get_file_name()),
85         canvas_(canvas)
86 {
87         assert(canvas->is_root());
88
89         unset_selection_manager();
90         
91         instance_map_[canvas]=this;
92 } // END of synfigapp::Instance::Instance()
93
94 handle<Instance>
95 Instance::create(Canvas::Handle canvas)
96 {
97         // Construct a new instance
98         handle<Instance> instance(new Instance(canvas));
99
100         return instance;
101 } // END of synfigapp::Instance::create()
102
103 synfig::String
104 Instance::get_file_name()const
105 {
106         return get_canvas()->get_file_name();
107 }
108
109 void
110 Instance::set_file_name(const synfig::String &name)
111 {
112         get_canvas()->set_file_name(name);
113         CVSInfo::set_file_name(name);
114 }
115
116 Instance::~Instance()
117 {
118         instance_map_.erase(canvas_);
119         synfig::info("studio::Instance::~Instance(): Deleted");
120 } // END of studio::Instance::~Instance()
121
122 handle<CanvasInterface>
123 Instance::find_canvas_interface(handle<Canvas> canvas)
124 {
125         if(!canvas)
126                 return 0;
127         while(canvas->is_inline())
128                 canvas=canvas->parent();
129         
130         CanvasInterfaceList::iterator iter;
131
132         for(iter=canvas_interface_list().begin();iter!=canvas_interface_list().end();iter++)
133                 if((*iter)->get_canvas()==canvas)
134                         return *iter;
135
136         return CanvasInterface::create(this,canvas);
137 }
138
139 bool
140 Instance::save()const
141 {
142         bool ret=save_canvas(get_file_name(),canvas_);
143         if(ret)
144         {
145                 reset_action_count();
146                 const_cast<sigc::signal<void>& >(signal_saved_)();
147         }
148         return ret;
149 }
150
151 bool
152 Instance::save_as(const std::string &file_name)const
153 {
154         bool ret=save_canvas(file_name,canvas_);
155         if(ret)
156         {
157                 reset_action_count();
158                 const_cast<sigc::signal<void>& >(signal_saved_)();
159         }
160         return ret;
161 }
162
163 bool
164 Instance::save_as(const std::string &file_name)
165 {
166         bool ret;
167
168         String old_file_name(get_file_name());
169         
170         set_file_name(file_name);
171
172         ret=save_canvas(file_name,canvas_);
173
174         if(ret)
175         {
176                 reset_action_count();
177                 signal_saved_();
178         }
179         else
180         {
181                 set_file_name(old_file_name);
182         }
183
184         signal_filename_changed_();
185
186         return ret;
187 }