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