Initial attempt at i18n support using gettext
[synfig.git] / synfig-studio / trunk / src / synfigapp / action.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file action.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 "action.h"
33 #include "instance.h"
34
35
36 #include "actions/layerremove.h"
37 #include "actions/layermove.h"
38 #include "actions/layerraise.h"
39 #include "actions/layerlower.h"
40 #include "actions/layeradd.h"
41 #include "actions/layeractivate.h"
42 #include "actions/layerparamset.h"
43 #include "actions/layerparamconnect.h"
44 #include "actions/layerparamdisconnect.h"
45 #include "actions/layerencapsulate.h"
46 #include "actions/layerduplicate.h"
47 #include "actions/layersetdesc.h"
48
49 #include "actions/valuenodeconstset.h"
50 #include "actions/valuenodeadd.h"
51 #include "actions/valuenodereplace.h"
52 #include "actions/valuenodelinkconnect.h"
53 #include "actions/valuenodelinkdisconnect.h"
54 #include "actions/valuenodedynamiclistinsert.h"
55 #include "actions/valuenodedynamiclistremove.h"
56 #include "actions/valuenodedynamiclistinsertsmart.h"
57 #include "actions/valuenodedynamiclistremovesmart.h"
58 #include "actions/valuenodedynamiclistloop.h"
59 #include "actions/valuenodedynamiclistunloop.h"
60 #include "actions/valuenodedynamiclistrotateorder.h"
61 #include "actions/valuenoderename.h"
62 #include "actions/valuenoderemove.h"
63
64 #include "actions/valuedescset.h"
65 #include "actions/valuedescexport.h"
66 #include "actions/valuedescconvert.h"
67 #include "actions/valuedescconnect.h"
68 #include "actions/valuedescdisconnect.h"
69 #include "actions/valuedesclink.h"
70
71 #include "actions/waypointadd.h"
72 #include "actions/waypointset.h"
73 #include "actions/waypointsetsmart.h"
74 #include "actions/waypointremove.h"
75
76 #include "actions/activepointadd.h"
77 #include "actions/activepointset.h"
78 #include "actions/activepointsetsmart.h"
79 #include "actions/activepointsetoff.h"
80 #include "actions/activepointseton.h"
81 #include "actions/activepointremove.h"
82
83 #include "actions/keyframeadd.h"
84 #include "actions/keyframeset.h"
85 #include "actions/keyframeremove.h"
86 #include "actions/keyframeduplicate.h"
87 #include "actions/keyframewaypointset.h"
88 #include "actions/keyframesetdelta.h"
89
90 #include "actions/timepointsmove.h"
91 #include "actions/timepointscopy.h"
92 #include "actions/timepointsdelete.h"
93
94 #include "actions/canvasrenddescset.h"
95 #include "actions/canvasadd.h"
96 #include "actions/canvasremove.h"
97
98 #include "actions/editmodeset.h"
99
100 #include "actions/blinepointtangentmerge.h"
101 #include "actions/blinepointtangentsplit.h"
102
103 #include "actions/gradientset.h"
104 #include "actions/colorset.h"
105
106 #include "actions/groupaddlayers.h"
107 #include "actions/groupremovelayers.h"
108 #include "actions/groupremove.h"
109 #include "actions/grouprename.h"
110
111 #include "canvasinterface.h"
112
113 #include "general.h"
114
115 #endif
116
117 using namespace std;
118 using namespace etl;
119 using namespace synfig;
120 using namespace synfigapp;
121 using namespace Action;
122
123 /* === P R O C E D U R E S ================================================= */
124
125 /* === S T A T I C S ======================================================= */
126
127 synfigapp::Action::Book *book_;
128
129 /* === M E T H O D S ======================================================= */
130
131 #define ADD_ACTION(x) { BookEntry &be(book()[x::name__]); \
132         be.name=x::name__; \
133         be.local_name=gettext(x::local_name__); \
134         be.version=x::version__; \
135         be.task=x::task__; \
136         be.priority=x::priority__; \
137         be.category=x::category__; \
138         be.factory=x::create; \
139         be.get_param_vocab=x::get_param_vocab; \
140         be.is_candidate=x::is_candidate; \
141         }
142
143
144 Action::Main::Main()
145 {
146         book_=new synfigapp::Action::Book();
147
148         ADD_ACTION(Action::LayerRemove);
149         ADD_ACTION(Action::LayerMove);
150         ADD_ACTION(Action::LayerRaise);
151         ADD_ACTION(Action::LayerLower);
152         ADD_ACTION(Action::LayerAdd);
153         ADD_ACTION(Action::LayerActivate);
154         ADD_ACTION(Action::LayerParamSet);
155         ADD_ACTION(Action::LayerParamConnect);
156         ADD_ACTION(Action::LayerParamDisconnect);
157         ADD_ACTION(Action::LayerEncapsulate);
158         ADD_ACTION(Action::LayerDuplicate);
159         ADD_ACTION(Action::LayerSetDesc);
160
161         ADD_ACTION(Action::ValueNodeConstSet);
162         ADD_ACTION(Action::ValueNodeAdd);
163         ADD_ACTION(Action::ValueNodeReplace);
164         ADD_ACTION(Action::ValueNodeLinkConnect);
165         ADD_ACTION(Action::ValueNodeLinkDisconnect);
166         ADD_ACTION(Action::ValueNodeDynamicListInsert);
167         ADD_ACTION(Action::ValueNodeDynamicListRemove);
168         ADD_ACTION(Action::ValueNodeDynamicListInsertSmart);
169         ADD_ACTION(Action::ValueNodeDynamicListRemoveSmart);
170         ADD_ACTION(Action::ValueNodeDynamicListLoop);
171         ADD_ACTION(Action::ValueNodeDynamicListUnLoop);
172         ADD_ACTION(Action::ValueNodeDynamicListRotateOrder);
173         ADD_ACTION(Action::ValueNodeRename);
174         ADD_ACTION(Action::ValueNodeRemove);
175
176         ADD_ACTION(Action::ValueDescSet);
177         ADD_ACTION(Action::ValueDescExport);
178         ADD_ACTION(Action::ValueDescConvert);
179         ADD_ACTION(Action::ValueDescConnect);
180         ADD_ACTION(Action::ValueDescDisconnect);
181         ADD_ACTION(Action::ValueDescLink);
182
183         ADD_ACTION(Action::WaypointAdd);
184         ADD_ACTION(Action::WaypointSet);
185         ADD_ACTION(Action::WaypointSetSmart);
186         ADD_ACTION(Action::WaypointRemove);
187
188         ADD_ACTION(Action::ActivepointAdd);
189         ADD_ACTION(Action::ActivepointSet);
190         ADD_ACTION(Action::ActivepointSetSmart);
191         ADD_ACTION(Action::ActivepointSetOn);
192         ADD_ACTION(Action::ActivepointSetOff);
193         ADD_ACTION(Action::ActivepointRemove);
194
195         ADD_ACTION(Action::KeyframeAdd);
196         ADD_ACTION(Action::KeyframeSet);
197         ADD_ACTION(Action::KeyframeRemove);
198         ADD_ACTION(Action::KeyframeDuplicate);
199         ADD_ACTION(Action::KeyframeWaypointSet);
200         ADD_ACTION(Action::KeyframeSetDelta);
201
202         ADD_ACTION(Action::CanvasRendDescSet);
203         ADD_ACTION(Action::CanvasAdd);
204         ADD_ACTION(Action::CanvasRemove);
205
206         ADD_ACTION(Action::EditModeSet);
207
208         ADD_ACTION(Action::BLinePointTangentMerge);
209         ADD_ACTION(Action::BLinePointTangentSplit);
210
211         ADD_ACTION(Action::GradientSet);
212         ADD_ACTION(Action::ColorSet);
213
214         ADD_ACTION(Action::TimepointsMove);
215         ADD_ACTION(Action::TimepointsCopy);
216         ADD_ACTION(Action::TimepointsDelete);
217
218         ADD_ACTION(Action::GroupAddLayers);
219         ADD_ACTION(Action::GroupRemoveLayers);
220         ADD_ACTION(Action::GroupRemove);
221         ADD_ACTION(Action::GroupRename);
222 }
223
224 Action::Main::~Main()
225 {
226         delete book_;
227
228 }
229
230
231 Action::Book& Action::book() { return *book_; }
232
233
234 Action::Handle
235 Action::create(const String &name)
236 {
237         if(!book().count(name))
238                 return 0; //! \todo perhaps we should throw something instead?
239         return book()[name].factory();
240 }
241
242
243 Action::CandidateList
244 Action::compile_candidate_list(const ParamList& param_list, Category category)
245 {
246         Action::CandidateList ret;
247
248         Book::const_iterator iter;
249
250         //synfig::info("param_list.size()=%d",param_list.size());
251
252         for(iter=book().begin();iter!=book().end();++iter)
253         {
254                 if((iter->second.category&category))
255                 {
256                         if(iter->second.is_candidate(param_list))
257                                 ret.push_back(iter->second);
258                         else
259                         {
260                                 //synfig::info("Action \"%s\" is not a candidate",iter->second.name.c_str());
261                         }
262                 }
263         }
264
265         return ret;
266 }
267
268 Action::CandidateList::iterator
269 Action::CandidateList::find(const String& x)
270 {
271         iterator iter;
272         for(iter=begin();iter!=end();++iter)
273                 if(iter->name==x)
274                         break;
275         return iter;
276 }
277
278 void
279 Action::Base::set_param_list(const ParamList &param_list)
280 {
281         ParamList::const_iterator iter;
282
283         for(iter=param_list.begin();iter!=param_list.end();++iter)
284                 set_param(iter->first,iter->second);
285 }
286
287 void
288 Super::perform()
289 {
290         set_dirty(false);
291
292         prepare();
293
294         ActionList::const_iterator iter;
295         for(iter=action_list_.begin();iter!=action_list_.end();++iter)
296         {
297                 try
298                 {
299                         try
300                         {
301                                 (*iter)->perform();
302                                 CanvasSpecific* canvas_specific(dynamic_cast<CanvasSpecific*>(iter->get()));
303                                 if(canvas_specific && canvas_specific->is_dirty())
304                                         set_dirty(true);
305                         }
306                         catch(...)
307                         {
308                                 if(iter!=action_list_.begin())
309                                 {
310                                         for(--iter;iter!=action_list_.begin();--iter)
311                                                 (*iter)->undo();
312                                         (*iter)->undo();
313                                 }
314                                 throw;
315                         }
316                 }
317                 catch(Error x)
318                 {
319                         throw Error(x.get_type(),((*iter)->get_name()+": "+x.get_desc()).c_str());
320                 }
321         }
322 }
323
324 void
325 Super::undo()
326 {
327         set_dirty(false);
328
329         ActionList::const_reverse_iterator iter;
330         for(iter=const_cast<const ActionList &>(action_list_).rbegin();iter!=const_cast<const ActionList &>(action_list_).rend();++iter)
331         {
332                 try {
333                         (*iter)->undo();
334                         CanvasSpecific* canvas_specific(dynamic_cast<CanvasSpecific*>(iter->get()));
335                         if(canvas_specific && canvas_specific->is_dirty())
336                                 set_dirty(true);
337                 }
338                 catch(...)
339                 {
340                         if(iter!=const_cast<const ActionList &>(action_list_).rbegin())
341                         {
342                                 for(--iter;iter!=const_cast<const ActionList &>(action_list_).rbegin();--iter)
343                                         (*iter)->perform();
344                                 (*iter)->perform();
345                         }
346                         throw;
347                 }
348         }
349 }
350
351 void
352 Super::add_action(etl::handle<Undoable> action)
353 {
354         action_list_.push_back(action);
355         CanvasSpecific *specific_action=dynamic_cast<CanvasSpecific *>(action.get());
356         if(specific_action && !get_canvas())
357                 set_canvas(specific_action->get_canvas());
358 }
359
360 void
361 Super::add_action_front(etl::handle<Undoable> action)
362 {
363         action_list_.push_front(action);
364         CanvasSpecific *specific_action=dynamic_cast<CanvasSpecific *>(action.get());
365         if(specific_action && !get_canvas())
366                 set_canvas(specific_action->get_canvas());
367 }
368
369
370 Group::Group(const std::string &str):
371         name_(str),
372         ready_(true)
373 {
374 }
375
376 Group::~Group()
377 {
378 }
379
380
381
382
383 Action::ParamVocab
384 Action::CanvasSpecific::get_param_vocab()
385 {
386         ParamVocab ret;
387
388         ret.push_back(ParamDesc("canvas",Param::TYPE_CANVAS)
389                 .set_local_name(_("Canvas"))
390                 .set_desc(_("Selected Canvas"))
391         );
392
393         ret.push_back(ParamDesc("canvas_interface",Param::TYPE_CANVASINTERFACE)
394                 .set_local_name(_("Canvas Interface"))
395                 .set_desc(_("Canvas Interface"))
396                 .set_optional(true)
397         );
398
399
400         return ret;
401 }
402
403 bool
404 CanvasSpecific::set_param(const synfig::String& name, const Param &param)
405 {
406         if(name=="canvas" && param.get_type()==Param::TYPE_CANVAS)
407         {
408                 if(!param.get_canvas())
409                         return false;
410                 set_canvas(param.get_canvas());
411
412                 return true;
413         }
414         if(name=="canvas_interface" && param.get_type()==Param::TYPE_CANVASINTERFACE)
415         {
416                 if(!param.get_canvas_interface())
417                         return false;
418                 set_canvas_interface(param.get_canvas_interface());
419                 if(!get_canvas())
420                         set_canvas(get_canvas_interface()->get_canvas());
421
422                 return true;
423         }
424         if(name=="edit_mode" && param.get_type()==Param::TYPE_EDITMODE)
425         {
426                 set_edit_mode(param.get_edit_mode());
427
428                 return true;
429         }
430
431         return false;
432 }
433
434 bool
435 CanvasSpecific::is_ready()const
436 {
437         if(!get_canvas())
438                 return false;
439         return true;
440 }
441
442 EditMode
443 CanvasSpecific::get_edit_mode()const
444 {
445         if(mode_!=MODE_UNDEFINED)
446                 return mode_;
447
448         if(get_canvas_interface())
449                 return get_canvas_interface()->get_mode();
450
451         return MODE_NORMAL;
452 }