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