1 /* === S Y N F I G ========================================================= */
2 /*! \file layerencapsulate.cpp
3 ** \brief Template File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
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.
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.
21 /* ========================================================================= */
23 /* === H E A D E R S ======================================================= */
32 #include "layerencapsulate.h"
34 #include "layerremove.h"
35 #include <synfigapp/canvasinterface.h>
41 using namespace synfig;
42 using namespace synfigapp;
43 using namespace Action;
45 /* === M A C R O S ========================================================= */
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$");
56 /* === G L O B A L S ======================================================= */
58 /* === P R O C E D U R E S ================================================= */
60 /* === M E T H O D S ======================================================= */
62 Action::LayerEncapsulate::LayerEncapsulate()
67 Action::LayerEncapsulate::get_param_vocab()
69 ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
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()
81 Action::LayerEncapsulate::is_candidate(const ParamList &x)
83 return candidate_check(get_param_vocab(),x);
87 Action::LayerEncapsulate::set_param(const synfig::String& name, const Action::Param ¶m)
89 if(name=="layer" && param.get_type()==Param::TYPE_LAYER)
91 layers.push_back(param.get_layer());
96 return Action::CanvasSpecific::set_param(name,param);
100 Action::LayerEncapsulate::is_ready()const
104 return Action::CanvasSpecific::is_ready();
108 Action::LayerEncapsulate::lowest_depth()const
110 std::list<synfig::Layer::Handle>::const_iterator iter;
111 int lowest_depth(0x7fffffff);
113 for(iter=layers.begin();iter!=layers.end();++iter)
115 int depth((*iter)->get_depth());
116 if(depth<lowest_depth)
119 if(lowest_depth==0x7fffffff)
125 Action::LayerEncapsulate::prepare()
132 throw Error("No layers to encapsulate");
134 // First create the new canvas and layer
136 child_canvas=Canvas::create_inline(get_canvas());
138 Layer::Handle new_layer(Layer::create("paste_canvas"));
140 new_layer->set_param("canvas",child_canvas);
142 int target_depth(lowest_depth());
146 Action::Handle action(LayerAdd::create());
148 action->set_param("canvas",get_canvas());
149 action->set_param("canvas_interface",get_canvas_interface());
150 action->set_param("new",new_layer);
157 Action::Handle action(Action::create("layer_move"));
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);
169 std::list<synfig::Layer::Handle>::reverse_iterator iter;
171 for(iter=layers.rbegin();iter!=layers.rend();++iter)
173 Layer::Handle layer(*iter);
175 Canvas::Handle subcanvas(layer->get_canvas());
177 // Find the iterator for the layer
178 Canvas::iterator iter=find(subcanvas->begin(),subcanvas->end(),layer);
180 // If we couldn't find the layer in the canvas, then bail
182 throw Error(_("This layer doesn't exist anymore."));
185 throw Error(_("This layer doesn't have a parent canvas"));
187 // If the subcanvas isn't the same as the canvas,
188 // then it had better be an inline canvas. If not,
190 if(get_canvas()!=subcanvas && !subcanvas->is_inline())
191 throw Error(_("This layer doesn't belong to this canvas anymore"));
193 if(get_canvas()!=subcanvas)
194 throw Error(_("get_canvas()!=subcanvas"));
196 // Remove the layer from the old canvas
198 Action::Handle action(LayerRemove::create());
200 action->set_param("canvas",subcanvas);
201 action->set_param("canvas_interface",get_canvas_interface());
202 action->set_param("layer",layer);
206 // Add the layer to the new canvas
208 Action::Handle action(LayerAdd::create());
210 action->set_param("canvas",child_canvas);
211 action->set_param("canvas_interface",get_canvas_interface());
212 action->set_param("new",layer);