Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-studio / tags / 0.61.08 / src / synfigapp / action.h
1 /* === S Y N F I G ========================================================= */
2 /*!     \file action.h
3 **      \brief Template File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2008 Chris Moore
10 **
11 **      This package is free software; you can redistribute it and/or
12 **      modify it under the terms of the GNU General Public License as
13 **      published by the Free Software Foundation; either version 2 of
14 **      the License, or (at your option) any later version.
15 **
16 **      This package is distributed in the hope that it will be useful,
17 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
18 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 **      General Public License for more details.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === S T A R T =========================================================== */
25
26 #ifndef __SYNFIG_APP_ACTION_H
27 #define __SYNFIG_APP_ACTION_H
28
29 /* === H E A D E R S ======================================================= */
30
31 #include <synfig/string.h>
32 #include <synfig/canvas.h>
33 #include <ETL/handle>
34 #include <ETL/stringf>
35 #include <ETL/trivial>
36
37 #include <map>
38 #include <list>
39
40 #include <synfig/layer.h>
41 #include <synfig/canvas.h>
42 #include <synfig/valuenode.h>
43 #include <synfigapp/value_desc.h>
44 #include <synfig/value.h>
45 #include <synfig/activepoint.h>
46 #include <synfig/valuenode_animated.h>
47 #include <synfig/string.h>
48 #include <synfig/keyframe.h>
49
50 #include "action_param.h"
51 #include "editmode.h"
52
53 /* === M A C R O S ========================================================= */
54
55 #define ACTION_MODULE_EXT public: \
56         static const char name__[], local_name__[], version__[], cvs_id__[], task__[]; \
57         static const Category category__; \
58         static const int priority__; \
59         static Action::Base *create(); \
60         virtual synfig::String get_name()const; \
61         virtual synfig::String get_local_name()const;
62
63
64 #define ACTION_SET_NAME(class,x) const char class::name__[]=x
65
66 #define ACTION_SET_CATEGORY(class,x) const Category class::category__(x)
67
68 #define ACTION_SET_TASK(class,x) const char class::task__[]=x
69
70 #define ACTION_SET_PRIORITY(class,x) const int class::priority__=x
71
72 #define ACTION_SET_LOCAL_NAME(class,x) const char class::local_name__[]=x
73
74 #define ACTION_SET_VERSION(class,x) const char class::version__[]=x
75
76 #define ACTION_SET_CVS_ID(class,x) const char class::cvs_id__[]=x
77
78 //! don't define get_local_name() - allow the action code to define its own
79 #define ACTION_INIT_NO_GET_LOCAL_NAME(class)                      \
80         Action::Base* class::create() { return new class(); } \
81         synfig::String class::get_name()const { return name__; }
82
83 #define ACTION_INIT(class)                               \
84         ACTION_INIT_NO_GET_LOCAL_NAME(class) \
85         synfig::String class::get_local_name()const { return dgettext("synfigstudio",local_name__); }
86
87 /* === T Y P E D E F S ===================================================== */
88
89 /* === C L A S S E S & S T R U C T S ======================================= */
90
91 namespace synfig {
92 class ProgressCallback;
93 class Canvas;
94 }; // END of namespace synfig
95
96 namespace synfigapp {
97
98 class Instance;
99 class Main;
100
101 namespace Action {
102
103 class System;
104
105
106 //! Exception class, thrown when redoing or undoing an action
107 class Error
108 {
109 public:
110         enum Type
111         {
112                 TYPE_UNKNOWN,
113                 TYPE_UNABLE,
114                 TYPE_BADPARAM,
115                 TYPE_CRITICAL,
116                 TYPE_NOTREADY,
117                 TYPE_BUG,
118
119                 TYPE_END
120         };
121 private:
122
123         Type type_;
124         synfig::String desc_;
125
126 public:
127
128         Error(Type type, const char *format, ...):
129                 type_(type)
130         {
131                 va_list args;
132                 va_start(args,format);
133                 desc_=etl::vstrprintf(format,args);
134         }
135
136         Error(const char *format, ...):
137                 type_(TYPE_UNKNOWN)
138         {
139                 va_list args;
140                 va_start(args,format);
141                 desc_=etl::vstrprintf(format,args);
142         }
143
144         Error(Type type=TYPE_UNABLE):
145                 type_(type)
146         {
147         }
148
149         Type get_type()const { return type_; }
150         synfig::String get_desc()const { return desc_; }
151
152 }; // END of class Action::Error
153
154 class Param;
155 class ParamList;
156 class ParamDesc;
157 class ParamVocab;
158
159 // Action Category
160 enum Category
161 {
162         CATEGORY_NONE                   =0,
163         CATEGORY_LAYER                  =(1<<0),
164         CATEGORY_CANVAS                 =(1<<1),
165         CATEGORY_WAYPOINT               =(1<<2),
166         CATEGORY_ACTIVEPOINT    =(1<<3),
167         CATEGORY_VALUEDESC              =(1<<4),
168         CATEGORY_VALUENODE              =(1<<5),
169         CATEGORY_KEYFRAME               =(1<<6),
170         CATEGORY_GROUP                  =(1<<7),
171
172         CATEGORY_OTHER                  =(1<<12),
173
174         CATEGORY_DRAG                   =(1<<24),
175
176         CATEGORY_HIDDEN                 =(1<<31),
177         CATEGORY_ALL                    =(~0)-(1<<31)           //!< All categories (EXCEPT HIDDEN)
178 }; // END of enum Category
179
180 inline Category operator|(Category lhs, Category rhs)
181 { return static_cast<Category>(int(lhs)|int(rhs)); }
182
183
184
185 //! Action Base Class
186 /*!     An action should implement the following functions:
187 **      static bool is_candidate(const ParamList &x);
188 **              -       Checks the ParamList to see if this action could be performed.
189 **      static ParamVocab get_param_vocab();
190 **              -       Yields the ParamVocab object which describes what
191 **                      this action needs before it can perform the act.
192 **      static Action::Base* create();
193 **              -       Factory for creating this action from a ParamList
194 **
195 */
196 class Base : public etl::shared_object
197 {
198 protected:
199         Base() { }
200
201 public:
202         virtual ~Base() { };
203
204         //! This function will throw an Action::Error() on failure
205         virtual void perform()=0;
206
207         virtual bool set_param(const synfig::String& /*name*/, const Param &) { return false; }
208         virtual bool is_ready()const=0;
209
210         virtual synfig::String get_name()const =0;
211         virtual synfig::String get_local_name()const { return get_name(); }
212
213         void set_param_list(const ParamList &);
214
215         static synfig::String get_layer_descriptions(const std::list<synfig::Layer::Handle> layers, synfig::String singular_prefix = "", synfig::String plural_prefix = "");
216         static synfig::String get_layer_descriptions(const std::list<std::pair<synfig::Layer::Handle,int> > layers, synfig::String singular_prefix = "", synfig::String plural_prefix = "");
217 }; // END of class Action::Base
218
219 typedef Action::Base* (*Factory)();
220 typedef bool (*CandidateChecker)(const ParamList &x);
221 typedef ParamVocab (*GetParamVocab)();
222
223 typedef etl::handle<Base> Handle;
224
225 //! Undoable Action Base Class
226 class Undoable : public Base
227 {
228         friend class System;
229         bool active_;
230
231 protected:
232         Undoable():active_(true) { }
233
234 private:
235         void set_active(bool x) { active_=x; }
236
237 public:
238
239         //! This function will throw an Action::Error() on failure
240         virtual void undo()=0;
241
242         bool is_active()const { return active_; }
243
244 }; // END of class Action::Undoable
245
246 //! Action base class for canvas-specific actions
247 class CanvasSpecific
248 {
249 private:
250         bool is_dirty_;
251         EditMode        mode_;
252
253         etl::loose_handle<synfigapp::CanvasInterface> canvas_interface_;
254         synfig::Canvas::Handle canvas_;
255
256 protected:
257         CanvasSpecific(const synfig::Canvas::Handle &canvas):is_dirty_(true),mode_(MODE_UNDEFINED),canvas_(canvas) { }
258         CanvasSpecific():is_dirty_(true), mode_(MODE_UNDEFINED) { }
259
260         virtual ~CanvasSpecific() { };
261
262
263 public:
264
265         void set_canvas(synfig::Canvas::Handle x) { canvas_=x; }
266         void set_canvas_interface(etl::loose_handle<synfigapp::CanvasInterface> x) { canvas_interface_=x; }
267
268         synfig::Canvas::Handle get_canvas()const { return canvas_; }
269         etl::loose_handle<synfigapp::CanvasInterface> get_canvas_interface()const { return canvas_interface_; }
270
271         static ParamVocab get_param_vocab();
272         virtual bool set_param(const synfig::String& name, const Param &);
273         virtual bool is_ready()const;
274
275         EditMode get_edit_mode()const;
276
277         void set_edit_mode(EditMode x) { mode_=x; }
278
279         bool is_dirty()const { return is_dirty_; }
280         void set_dirty(bool x=true) { is_dirty_=x; }
281
282 }; // END of class Action::Undoable
283
284 typedef std::list< etl::handle<Action::Undoable> > ActionList;
285
286 /*!     \class synfigapp::Action::Super
287 **      \brief Super-Action base class for actions composed of several other actions.
288 **
289 **      Actions deriving from this class should only implement prepare(), and
290 **      NOT implement perform() or undo().
291 */
292 class Super : public Undoable, public CanvasSpecific
293 {
294         ActionList action_list_;
295
296 public:
297
298         ActionList &action_list() { return action_list_; }
299         const ActionList &action_list()const { return action_list_; }
300
301         virtual void prepare()=0;
302
303         void clear() { action_list().clear(); }
304
305         bool first_time()const { return action_list_.empty(); }
306
307         void add_action(etl::handle<Undoable> action);
308
309         void add_action_front(etl::handle<Undoable> action);
310
311         virtual void perform();
312         virtual void undo();
313
314 }; // END of class Action::Super
315
316
317 class Group : public Super
318 {
319         synfig::String name_;
320
321         ActionList action_list_;
322 protected:
323         bool ready_;
324 public:
325         Group(const synfig::String &str="Group");
326         virtual ~Group();
327
328         virtual synfig::String get_name()const { return name_; }
329
330         virtual void prepare() { };
331
332         virtual bool set_param(const synfig::String& /*name*/, const Param &)const { return false; }
333         virtual bool is_ready()const { return ready_; }
334
335         void set_name(std::string&x) { name_=x; }
336 }; // END of class Action::Group
337
338
339
340
341
342 struct BookEntry
343 {
344         synfig::String  name;
345         synfig::String  local_name;
346         synfig::String  version;
347         synfig::String  task;
348         int                     priority;
349         Category                category;
350         Factory                 factory;
351         CandidateChecker        is_candidate;
352         GetParamVocab   get_param_vocab;
353
354         bool operator<(const BookEntry &rhs)const { return priority<rhs.priority; }
355 }; // END of struct BookEntry
356
357 typedef std::map<synfig::String,BookEntry> Book;
358
359 class CandidateList : public std::list<BookEntry>
360 {
361 public:
362         iterator find(const synfig::String& x);
363         const_iterator find(const synfig::String& x)const { return const_cast<CandidateList*>(this)->find(x); }
364 };
365
366 Book& book();
367
368 Handle create(const synfig::String &name);
369
370 //! Compiles a list of potential candidate actions with the given \a param_list and \a category
371 CandidateList compile_candidate_list(const ParamList& param_list, Category category=CATEGORY_ALL);
372
373 /*!     \class synfigapp::Action::Main
374 **      \brief \writeme
375 **
376 **      \writeme
377 */
378 class Main
379 {
380         friend class synfigapp::Main;
381
382         Main();
383
384 public:
385         ~Main();
386
387 }; // END of class Action::Main
388
389 }; // END of namespace Action
390
391 }; // END of namespace synfigapp
392
393 /* === E N D =============================================================== */
394
395 #endif