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