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