1 /* === S Y N F I G ========================================================= */
3 ** \brief Template File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
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.
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.
21 /* ========================================================================= */
23 /* === S T A R T =========================================================== */
25 #ifndef __SYNFIG_APP_ACTION_H
26 #define __SYNFIG_APP_ACTION_H
28 /* === H E A D E R S ======================================================= */
30 #include <synfig/string.h>
31 #include <synfig/canvas.h>
33 #include <ETL/stringf>
34 #include <ETL/trivial>
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>
49 #include "action_param.h"
52 /* === M A C R O S ========================================================= */
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;
63 #define ACTION_SET_NAME(class,x) const char class::name__[]=x
65 #define ACTION_SET_CATEGORY(class,x) const Category class::category__(x)
67 #define ACTION_SET_TASK(class,x) const char class::task__[]=x
69 #define ACTION_SET_PRIORITY(class,x) const int class::priority__=x
71 #define ACTION_SET_LOCAL_NAME(class,x) const char class::local_name__[]=x
73 #define ACTION_SET_VERSION(class,x) const char class::version__[]=x
75 #define ACTION_SET_CVS_ID(class,x) const char class::cvs_id__[]=x
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__; } \
82 /* === T Y P E D E F S ===================================================== */
84 /* === C L A S S E S & S T R U C T S ======================================= */
87 class ProgressCallback;
89 }; // END of namespace synfig
101 //! Exception class, thrown when redoing or undoing an action
119 synfig::String desc_;
123 Error(Type type, const char *format, ...):
127 va_start(args,format);
128 desc_=etl::vstrprintf(format,args);
131 Error(const char *format, ...):
135 va_start(args,format);
136 desc_=etl::vstrprintf(format,args);
139 Error(Type type=TYPE_UNABLE):
144 Type get_type()const { return type_; }
145 synfig::String get_desc()const { return desc_; }
147 }; // END of class Action::Error
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),
167 CATEGORY_OTHER =(1<<12),
169 CATEGORY_DRAG =(1<<24),
171 CATEGORY_HIDDEN =(1<<31),
172 CATEGORY_ALL =(~0)-(1<<31) //!< All categories (EXCEPT HIDDEN)
173 }; // END of enum Category
175 inline Category operator|(Category lhs, Category rhs)
176 { return static_cast<Category>(int(lhs)|int(rhs)); }
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
191 class Base : public etl::shared_object
199 //! This function will throw an Action::Error() on failure
200 virtual void perform()=0;
202 virtual bool set_param(const synfig::String& name, const Param &) { return false; }
203 virtual bool is_ready()const=0;
205 virtual synfig::String get_name()const =0;
206 virtual synfig::String get_local_name()const { return get_name(); }
208 void set_param_list(const ParamList &);
210 }; // END of class Action::Base
212 typedef Action::Base* (*Factory)();
213 typedef bool (*CandidateChecker)(const ParamList &x);
214 typedef ParamVocab (*GetParamVocab)();
216 typedef etl::handle<Base> Handle;
218 //! Undoable Action Base Class
219 class Undoable : public Base
225 Undoable():active_(true) { }
228 void set_active(bool x) { active_=x; }
232 //! This function will throw an Action::Error() on failure
233 virtual void undo()=0;
235 bool is_active()const { return active_; }
237 }; // END of class Action::Undoable
239 //! Action base class for canvas-specific actions
246 etl::loose_handle<synfigapp::CanvasInterface> canvas_interface_;
247 synfig::Canvas::Handle canvas_;
250 CanvasSpecific(const synfig::Canvas::Handle &canvas):is_dirty_(true),mode_(MODE_UNDEFINED),canvas_(canvas) { }
251 CanvasSpecific():is_dirty_(true), mode_(MODE_UNDEFINED) { }
253 virtual ~CanvasSpecific() { };
258 void set_canvas(synfig::Canvas::Handle x) { canvas_=x; }
259 void set_canvas_interface(etl::loose_handle<synfigapp::CanvasInterface> x) { canvas_interface_=x; }
261 synfig::Canvas::Handle get_canvas()const { return canvas_; }
262 etl::loose_handle<synfigapp::CanvasInterface> get_canvas_interface()const { return canvas_interface_; }
264 static ParamVocab get_param_vocab();
265 virtual bool set_param(const synfig::String& name, const Param &);
266 virtual bool is_ready()const;
268 EditMode get_edit_mode()const;
270 void set_edit_mode(EditMode x) { mode_=x; }
272 bool is_dirty()const { return is_dirty_; }
273 void set_dirty(bool x=true) { is_dirty_=x; }
275 }; // END of class Action::Undoable
277 typedef std::list< etl::handle<Action::Undoable> > ActionList;
279 /*! \class synfigapp::Action::Super
280 ** \brief Super-Action base class for actions composed of several other actions.
282 ** Actions deriving from this class should only implement prepare(), and
283 ** NOT implement perform() or undo().
285 class Super : public Undoable, public CanvasSpecific
287 ActionList action_list_;
291 ActionList &action_list() { return action_list_; }
292 const ActionList &action_list()const { return action_list_; }
294 virtual void prepare()=0;
296 void clear() { action_list().clear(); }
298 bool first_time()const { return action_list_.empty(); }
300 void add_action(etl::handle<Undoable> action);
302 void add_action_front(etl::handle<Undoable> action);
304 virtual void perform();
307 }; // END of class Action::Super
310 class Group : public Super
312 synfig::String name_;
314 ActionList action_list_;
318 Group(const synfig::String &str="Group");
321 virtual synfig::String get_name()const { return name_; }
323 virtual void prepare() { };
325 virtual bool set_param(const synfig::String& name, const Param &)const { return false; }
326 virtual bool is_ready()const { return ready_; }
328 void set_name(std::string&x) { name_=x; }
329 }; // END of class Action::Group
338 synfig::String local_name;
339 synfig::String version;
344 CandidateChecker is_candidate;
345 GetParamVocab get_param_vocab;
347 bool operator<(const BookEntry &rhs)const { return priority<rhs.priority; }
348 }; // END of struct BookEntry
350 typedef std::map<synfig::String,BookEntry> Book;
352 class CandidateList : public std::list<BookEntry>
355 iterator find(const synfig::String& x);
356 const_iterator find(const synfig::String& x)const { return const_cast<CandidateList*>(this)->find(x); }
361 Handle create(const synfig::String &name);
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);
366 /*! \class synfigapp::Action::Main
373 friend class synfigapp::Main;
380 }; // END of class Action::Main
382 }; // END of namespace Action
384 }; // END of namespace synfigapp
386 /* === E N D =============================================================== */