my log
[synfig.git] / synfig-studio / trunk / src / synfigapp / actions / layerencapsulate.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file layerraise.cpp
3 **      \brief Template File
4 **
5 **      $Id: layerencapsulate.cpp,v 1.1.1.1 2005/01/07 03:34:37 darco Exp $
6 **
7 **      \legal
8 **      Copyright (c) 2002 Robert B. Quattlebaum Jr.
9 **
10 **      This software and associated documentation
11 **      are CONFIDENTIAL and PROPRIETARY property of
12 **      the above-mentioned copyright holder.
13 **
14 **      You may not copy, print, publish, or in any
15 **      other way distribute this software without
16 **      a prior written agreement with
17 **      the copyright holder.
18 **      \endlegal
19 */
20 /* ========================================================================= */
21
22 /* === H E A D E R S ======================================================= */
23
24 #ifdef USING_PCH
25 #       include "pch.h"
26 #else
27 #ifdef HAVE_CONFIG_H
28 #       include <config.h>
29 #endif
30
31 #include "layerencapsulate.h"
32 #include "layeradd.h"
33 #include "layerremove.h"
34 #include <synfigapp/canvasinterface.h>
35
36 #endif
37
38 using namespace std;
39 using namespace etl;
40 using namespace synfig;
41 using namespace synfigapp;
42 using namespace Action;
43
44 /* === M A C R O S ========================================================= */
45
46 ACTION_INIT(Action::LayerEncapsulate);
47 ACTION_SET_NAME(Action::LayerEncapsulate,"layer_encapsulate");
48 ACTION_SET_LOCAL_NAME(Action::LayerEncapsulate,"Encapsulate");
49 ACTION_SET_TASK(Action::LayerEncapsulate,"encapsulate");
50 ACTION_SET_CATEGORY(Action::LayerEncapsulate,Action::CATEGORY_LAYER);
51 ACTION_SET_PRIORITY(Action::LayerEncapsulate,0);
52 ACTION_SET_VERSION(Action::LayerEncapsulate,"0.0");
53 ACTION_SET_CVS_ID(Action::LayerEncapsulate,"$Id: layerencapsulate.cpp,v 1.1.1.1 2005/01/07 03:34:37 darco Exp $");
54
55 /* === G L O B A L S ======================================================= */
56
57 /* === P R O C E D U R E S ================================================= */
58
59 /* === M E T H O D S ======================================================= */
60
61 Action::LayerEncapsulate::LayerEncapsulate()
62 {
63 }
64
65 Action::ParamVocab
66 Action::LayerEncapsulate::get_param_vocab()
67 {
68         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
69         
70         ret.push_back(ParamDesc("layer",Param::TYPE_LAYER)
71                 .set_local_name(_("Layer"))
72                 .set_desc(_("Layer to be encapsulated"))
73                 .set_supports_multiple()
74         );
75         
76         return ret;
77 }
78
79 bool
80 Action::LayerEncapsulate::is_canidate(const ParamList &x)
81 {
82         return canidate_check(get_param_vocab(),x);
83 }
84
85 bool
86 Action::LayerEncapsulate::set_param(const synfig::String& name, const Action::Param &param)
87 {
88         if(name=="layer" && param.get_type()==Param::TYPE_LAYER)
89         {
90                 layers.push_back(param.get_layer());
91                 
92                 return true;
93         }
94
95         return Action::CanvasSpecific::set_param(name,param);
96 }
97
98 bool
99 Action::LayerEncapsulate::is_ready()const
100 {
101         if(layers.empty())
102                 return false;
103         return Action::CanvasSpecific::is_ready();
104 }
105
106 int
107 Action::LayerEncapsulate::lowest_depth()const
108 {
109         std::list<synfig::Layer::Handle>::const_iterator iter;
110         int lowest_depth(0x7fffffff);
111         
112         for(iter=layers.begin();iter!=layers.end();++iter)
113         {
114                 int depth((*iter)->get_depth());
115                 if(depth<lowest_depth)
116                         lowest_depth=depth;
117         }
118         if(lowest_depth==0x7fffffff)
119                 return 0;
120         return lowest_depth;
121 }
122
123 void
124 Action::LayerEncapsulate::prepare()
125 {
126
127         if(!first_time())
128                 return;
129
130         if(layers.empty())
131                 throw Error("No layers to encapsulate");
132                 
133         // First create the new canvas and layer
134         if(!child_canvas)
135                 child_canvas=Canvas::create_inline(get_canvas());
136         
137         Layer::Handle new_layer(Layer::create("PasteCanvas"));
138         
139         new_layer->set_param("canvas",child_canvas);
140         
141         int target_depth(lowest_depth());
142         
143         // Add the layer
144         {
145                 Action::Handle action(LayerAdd::create());
146         
147                 action->set_param("canvas",get_canvas());
148                 action->set_param("canvas_interface",get_canvas_interface());
149                 action->set_param("new",new_layer);
150                 
151                 add_action(action);
152         }       
153         
154         // Move the layer
155         {
156                 Action::Handle action(Action::create("layer_move"));
157                 
158                 assert(action);
159         
160                 action->set_param("canvas",get_canvas());
161                 action->set_param("canvas_interface",get_canvas_interface());
162                 action->set_param("layer",new_layer);
163                 action->set_param("new_index",target_depth);
164                 
165                 add_action(action);
166         }               
167                 
168         std::list<synfig::Layer::Handle>::reverse_iterator iter;
169         
170         for(iter=layers.rbegin();iter!=layers.rend();++iter)
171         {
172                 Layer::Handle layer(*iter);
173                 
174                 Canvas::Handle subcanvas(layer->get_canvas());
175                 
176                 // Find the iterator for the layer
177                 Canvas::iterator iter=find(subcanvas->begin(),subcanvas->end(),layer);
178                 
179                 // If we couldn't find the layer in the canvas, then bail
180                 if(*iter!=layer)
181                         throw Error(_("This layer doesn't exist anymore."));
182         
183                 if(!subcanvas)
184                         throw Error(_("This layer doesn't have a parent canvas"));
185
186                 // If the subcanvas isn't the same as the canvas,
187                 // then it had better be an inline canvas. If not,
188                 // bail
189                 if(get_canvas()!=subcanvas && !subcanvas->is_inline())
190                         throw Error(_("This layer doesn't belong to this canvas anymore"));
191
192                 if(get_canvas()!=subcanvas)
193                         throw Error(_("get_canvas()!=subcanvas"));
194                 
195                 // Remove the layer from the old canvas
196                 {
197                         Action::Handle action(LayerRemove::create());
198                         
199                         action->set_param("canvas",subcanvas);
200                         action->set_param("canvas_interface",get_canvas_interface());
201                         action->set_param("layer",layer);
202                         
203                         add_action(action);
204                 }
205                 // Add the layer to the new canvas
206                 {
207                         Action::Handle action(LayerAdd::create());
208                         
209                         action->set_param("canvas",child_canvas);
210                         action->set_param("canvas_interface",get_canvas_interface());
211                         action->set_param("new",layer);
212                         
213                         add_action(action);
214                 }
215         }
216 }