Added copyright lines for files I've edited this year.
[synfig.git] / synfig-studio / trunk / src / synfigapp / actions / layerencapsulate.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file layerencapsulate.cpp
3 **      \brief Template File
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 "layerencapsulate.h"
34 #include "layeradd.h"
35 #include "layerremove.h"
36 #include <synfigapp/canvasinterface.h>
37
38 #include <synfigapp/general.h>
39
40 #endif
41
42 using namespace std;
43 using namespace etl;
44 using namespace synfig;
45 using namespace synfigapp;
46 using namespace Action;
47
48 /* === M A C R O S ========================================================= */
49
50 ACTION_INIT_NO_GET_LOCAL_NAME(Action::LayerEncapsulate);
51 ACTION_SET_NAME(Action::LayerEncapsulate,"layer_encapsulate");
52 ACTION_SET_LOCAL_NAME(Action::LayerEncapsulate,N_("Encapsulate"));
53 ACTION_SET_TASK(Action::LayerEncapsulate,"encapsulate");
54 ACTION_SET_CATEGORY(Action::LayerEncapsulate,Action::CATEGORY_LAYER);
55 ACTION_SET_PRIORITY(Action::LayerEncapsulate,0);
56 ACTION_SET_VERSION(Action::LayerEncapsulate,"0.0");
57 ACTION_SET_CVS_ID(Action::LayerEncapsulate,"$Id$");
58
59 /* === G L O B A L S ======================================================= */
60
61 /* === P R O C E D U R E S ================================================= */
62
63 /* === M E T H O D S ======================================================= */
64
65 Action::LayerEncapsulate::LayerEncapsulate()
66 {
67 }
68
69 synfig::String
70 Action::LayerEncapsulate::get_local_name()const
71 {
72         return get_layer_descriptions(layers, _("Encapsulate Layer"), _("Encapsulate Layers"));
73 }
74
75 Action::ParamVocab
76 Action::LayerEncapsulate::get_param_vocab()
77 {
78         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
79
80         ret.push_back(ParamDesc("layer",Param::TYPE_LAYER)
81                 .set_local_name(_("Layer"))
82                 .set_desc(_("Layer to be encapsulated"))
83                 .set_supports_multiple()
84         );
85
86         return ret;
87 }
88
89 bool
90 Action::LayerEncapsulate::is_candidate(const ParamList &x)
91 {
92         return candidate_check(get_param_vocab(),x);
93 }
94
95 bool
96 Action::LayerEncapsulate::set_param(const synfig::String& name, const Action::Param &param)
97 {
98         if(name=="layer" && param.get_type()==Param::TYPE_LAYER)
99         {
100                 layers.push_back(param.get_layer());
101
102                 return true;
103         }
104
105         return Action::CanvasSpecific::set_param(name,param);
106 }
107
108 bool
109 Action::LayerEncapsulate::is_ready()const
110 {
111         if(layers.empty())
112                 return false;
113         return Action::CanvasSpecific::is_ready();
114 }
115
116 int
117 Action::LayerEncapsulate::lowest_depth()const
118 {
119         std::list<synfig::Layer::Handle>::const_iterator iter;
120         int lowest_depth(0x7fffffff);
121
122         for(iter=layers.begin();iter!=layers.end();++iter)
123         {
124                 int depth((*iter)->get_depth());
125                 if(depth<lowest_depth)
126                         lowest_depth=depth;
127         }
128         if(lowest_depth==0x7fffffff)
129                 return 0;
130         return lowest_depth;
131 }
132
133 void
134 Action::LayerEncapsulate::prepare()
135 {
136
137         if(!first_time())
138                 return;
139
140         if(layers.empty())
141                 throw Error(_("No layers to encapsulate"));
142
143         // First create the new canvas and layer
144         if(!child_canvas)
145                 child_canvas=Canvas::create_inline(get_canvas());
146
147         Layer::Handle new_layer(Layer::create("PasteCanvas"));
148
149         new_layer->set_param("canvas",child_canvas);
150
151         int target_depth(lowest_depth());
152
153         // Add the layer
154         {
155                 Action::Handle action(LayerAdd::create());
156
157                 action->set_param("canvas",get_canvas());
158                 action->set_param("canvas_interface",get_canvas_interface());
159                 action->set_param("new",new_layer);
160
161                 add_action(action);
162         }
163
164         // Move the layer
165         {
166                 Action::Handle action(Action::create("layer_move"));
167
168                 assert(action);
169
170                 action->set_param("canvas",get_canvas());
171                 action->set_param("canvas_interface",get_canvas_interface());
172                 action->set_param("layer",new_layer);
173                 action->set_param("new_index",target_depth);
174
175                 add_action(action);
176         }
177
178         std::list<synfig::Layer::Handle>::reverse_iterator iter;
179
180         for(iter=layers.rbegin();iter!=layers.rend();++iter)
181         {
182                 Layer::Handle layer(*iter);
183
184                 Canvas::Handle subcanvas(layer->get_canvas());
185
186                 // Find the iterator for the layer
187                 Canvas::iterator iter=find(subcanvas->begin(),subcanvas->end(),layer);
188
189                 // If we couldn't find the layer in the canvas, then bail
190                 if(*iter!=layer)
191                         throw Error(_("This layer doesn't exist anymore."));
192
193                 if(!subcanvas)
194                         throw Error(_("This layer doesn't have a parent canvas"));
195
196                 // If the subcanvas isn't the same as the canvas,
197                 // then it had better be an inline canvas. If not,
198                 // bail
199                 if(get_canvas()!=subcanvas && !subcanvas->is_inline())
200                         throw Error(_("This layer doesn't belong to this canvas anymore"));
201
202                 if(get_canvas()!=subcanvas)
203                         throw Error(_("get_canvas()!=subcanvas"));
204
205                 // Remove the layer from the old canvas
206                 {
207                         Action::Handle action(LayerRemove::create());
208
209                         action->set_param("canvas",subcanvas);
210                         action->set_param("canvas_interface",get_canvas_interface());
211                         action->set_param("layer",layer);
212
213                         add_action(action);
214                 }
215                 // Add the layer to the new canvas
216                 {
217                         Action::Handle action(LayerAdd::create());
218
219                         action->set_param("canvas",child_canvas);
220                         action->set_param("canvas_interface",get_canvas_interface());
221                         action->set_param("new",layer);
222
223                         add_action(action);
224                 }
225         }
226 }