Initial attempt at i18n support using gettext
[synfig.git] / synfig-studio / trunk / src / synfigapp / actions / layermove.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file layermove.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 "layermove.h"
33 #include <synfigapp/canvasinterface.h>
34
35 #include <synfigapp/general.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::LayerMove);
48 ACTION_SET_NAME(Action::LayerMove,"layer_move");
49 ACTION_SET_LOCAL_NAME(Action::LayerMove,N_("Move Layer"));
50 ACTION_SET_TASK(Action::LayerMove,"move");
51 ACTION_SET_CATEGORY(Action::LayerMove,Action::CATEGORY_LAYER);
52 ACTION_SET_PRIORITY(Action::LayerMove,0);
53 ACTION_SET_VERSION(Action::LayerMove,"0.0");
54 ACTION_SET_CVS_ID(Action::LayerMove,"$Id$");
55
56 /* === G L O B A L S ======================================================= */
57
58 static const int nindex=-1;
59
60 /* === P R O C E D U R E S ================================================= */
61
62 /* === M E T H O D S ======================================================= */
63
64 Action::LayerMove::LayerMove():
65         new_index(0xdeadbeef)
66 {
67 }
68
69 Action::ParamVocab
70 Action::LayerMove::get_param_vocab()
71 {
72         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
73
74         ret.push_back(ParamDesc("layer",Param::TYPE_LAYER)
75                 .set_local_name(_("Layer"))
76                 .set_desc(_("Layer to be moved"))
77         );
78
79         ret.push_back(ParamDesc("new_index",Param::TYPE_INTEGER)
80                 .set_local_name(_("New Index"))
81                 .set_desc(_("Where the layer is to be moved to"))
82         );
83
84         ret.push_back(ParamDesc("dest_canvas",Param::TYPE_CANVAS)
85                 .set_local_name(_("Destination Canvas"))
86                 .set_desc(_("The canvas the layer is to be moved to"))
87                 .set_optional()
88         );
89
90         return ret;
91 }
92
93 bool
94 Action::LayerMove::is_candidate(const ParamList &x)
95 {
96         return candidate_check(get_param_vocab(),x);
97 }
98
99 bool
100 Action::LayerMove::set_param(const synfig::String& name, const Action::Param &param)
101 {
102         if(name=="layer" && param.get_type()==Param::TYPE_LAYER)
103         {
104
105                 layer=param.get_layer();
106
107                 return true;
108         }
109
110         if(name=="new_index" && param.get_type()==Param::TYPE_INTEGER)
111         {
112                 new_index=param.get_integer();
113
114                 return true;
115         }
116
117         if(name=="dest_canvas" && param.get_type()==Param::TYPE_CANVAS)
118         {
119                 dest_canvas=param.get_canvas();
120
121                 return true;
122         }
123
124         return Action::CanvasSpecific::set_param(name,param);
125 }
126
127 bool
128 Action::LayerMove::is_ready()const
129 {
130         synfig::info(__FILE__":%d: layer->count()=%d",__LINE__,layer.count());
131         if(!layer || (unsigned)new_index==0xdeadbeef)
132                 return false;
133         return Action::CanvasSpecific::is_ready();
134 }
135
136 void
137 Action::LayerMove::perform()
138 {
139         synfig::info(__FILE__":%d: layer->count()=%d",__LINE__,layer.count());
140
141         Canvas::Handle subcanvas(layer->get_canvas());
142         src_canvas=subcanvas;
143         if(!dest_canvas)
144                 dest_canvas=subcanvas;
145
146         // Find the iterator for the layer
147         Canvas::iterator iter=find(src_canvas->begin(),src_canvas->end(),layer);
148
149         // If we couldn't find the layer in the canvas, then bail
150         if(*iter!=layer)
151                 throw Error(_("This layer doesn't exist anymore."));
152
153         synfig::info(__FILE__":%d: layer->count()=%d",__LINE__,layer.count());
154
155         // If the subcanvas isn't the same as the canvas,
156         // then it had better be an inline canvas. If not,
157         // bail
158         //if(get_canvas()!=subcanvas && !subcanvas->is_inline())
159         if(get_canvas()->get_root()!=dest_canvas->get_root() || get_canvas()->get_root()!=src_canvas->get_root())
160                 throw Error(_("You cannot directly move layers across compositions"));
161
162         old_index=iter-src_canvas->begin();
163         int depth;
164
165         if(new_index<0)
166                 depth=dest_canvas->size()+new_index+1;
167         else
168                 depth=new_index;
169
170         set_dirty(layer->active());
171
172         synfig::info(__FILE__":%d: layer->count()=%d",__LINE__,layer.count());
173
174         // If we were to move it to where it is
175         if(old_index==depth && src_canvas==dest_canvas)
176                 return;
177
178         if(depth>dest_canvas->size())
179                 depth=dest_canvas->size();
180         if(depth<0)
181                 depth=0;
182
183         src_canvas->erase(iter);
184
185         dest_canvas->insert(dest_canvas->begin()+depth,layer);
186         layer->set_canvas(dest_canvas);
187
188         layer->changed();
189         dest_canvas->changed(); if(dest_canvas!=src_canvas) src_canvas->changed();
190
191         synfig::info(__FILE__":%d: layer->count()=%d",__LINE__,layer.count());
192
193         if(get_canvas_interface())
194         {
195                 if(src_canvas==dest_canvas)
196                 {
197                         if(new_index==old_index-1)      // Raise
198                                 get_canvas_interface()->signal_layer_raised()(layer);
199                         else if(new_index==old_index+1) // Lower
200                                 get_canvas_interface()->signal_layer_lowered()(layer);
201                         else            // Moved
202                         {
203                                 get_canvas_interface()->signal_layer_moved()(layer,depth,dest_canvas);
204                         }
205                 }
206                 else
207                 {
208                         get_canvas_interface()->signal_layer_moved()(layer,depth,dest_canvas);
209                 }
210         }
211         else synfig::warning("CanvasInterface not set on action");
212
213         synfig::info(__FILE__":%d: layer->count()=%d",__LINE__,layer.count());
214 }
215
216 void
217 Action::LayerMove::undo()
218 {
219         // Find the iterator for the layer
220         Canvas::iterator iter=find(dest_canvas->begin(),dest_canvas->end(),layer);
221
222         // If we couldn't find the layer in the canvas, then bail
223         if(*iter!=layer || (get_canvas()!=dest_canvas && !dest_canvas->is_inline()))
224                 throw Error(_("This layer doesn't exist anymore."));
225
226         // If we were to move it to where it is
227         if(old_index==new_index && src_canvas==dest_canvas)
228                 return;
229
230         // Mark ourselves as dirty if necessary
231         set_dirty(layer->active());
232
233         dest_canvas->erase(iter);
234
235         src_canvas->insert(src_canvas->begin()+old_index,layer);
236         layer->set_canvas(src_canvas);
237
238         layer->changed();
239         dest_canvas->changed(); if(dest_canvas!=src_canvas) src_canvas->changed();
240
241         // Execute any signals
242         if(get_canvas_interface())
243         {
244                 if(src_canvas==dest_canvas)
245                 {
246                         if(new_index==old_index+1)      // Raise
247                                 get_canvas_interface()->signal_layer_raised()(layer);
248                         else if(new_index==old_index-1) // Lower
249                                 get_canvas_interface()->signal_layer_lowered()(layer);
250                         else            // Moved
251                         {
252                         get_canvas_interface()->signal_layer_moved()(layer,old_index,src_canvas);
253                         }
254                 }
255                 else
256                 {
257                         get_canvas_interface()->signal_layer_moved()(layer,old_index,src_canvas);
258                         //get_canvas_interface()->signal_layer_removed()(layer);
259                         //get_canvas_interface()->signal_layer_inserted()(layer,old_index);
260                 }
261         }
262         else synfig::warning("CanvasInterface not set on action");
263 }