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 //! don't define get_local_name() - allow the action code to define its own
78 #define ACTION_INIT_NO_GET_LOCAL_NAME(class) \
79 Action::Base* class::create() { return new class(); } \
80 synfig::String class::get_name()const { return name__; }
82 #define ACTION_INIT(class) \
83 ACTION_INIT_NO_GET_LOCAL_NAME(class) \
84 synfig::String class::get_local_name()const { return dgettext("synfigstudio",local_name__); }
86 /* === T Y P E D E F S ===================================================== */
88 /* === C L A S S E S & S T R U C T S ======================================= */
91 class ProgressCallback;
93 }; // END of namespace synfig
105 //! Exception class, thrown when redoing or undoing an action
123 synfig::String desc_;
127 Error(Type type, const char *format, ...):
131 va_start(args,format);
132 desc_=etl::vstrprintf(format,args);
135 Error(const char *format, ...):
139 va_start(args,format);
140 desc_=etl::vstrprintf(format,args);
143 Error(Type type=TYPE_UNABLE):
148 Type get_type()const { return type_; }
149 synfig::String get_desc()const { return desc_; }
151 }; // END of class Action::Error
162 CATEGORY_LAYER =(1<<0),
163 CATEGORY_CANVAS =(1<<1),
164 CATEGORY_WAYPOINT =(1<<2),
165 CATEGORY_ACTIVEPOINT =(1<<3),
166 CATEGORY_VALUEDESC =(1<<4),
167 CATEGORY_VALUENODE =(1<<5),
168 CATEGORY_KEYFRAME =(1<<6),
169 CATEGORY_GROUP =(1<<7),
171 CATEGORY_OTHER =(1<<12),
173 CATEGORY_DRAG =(1<<24),
175 CATEGORY_HIDDEN =(1<<31),
176 CATEGORY_ALL =(~0)-(1<<31) //!< All categories (EXCEPT HIDDEN)
177 }; // END of enum Category
179 inline Category operator|(Category lhs, Category rhs)
180 { return static_cast<Category>(int(lhs)|int(rhs)); }
184 //! Action Base Class
185 /*! An action should implement the following functions:
186 ** static bool is_candidate(const ParamList &x);
187 ** - Checks the ParamList to see if this action could be performed.
188 ** static ParamVocab get_param_vocab();
189 ** - Yields the ParamVocab object which describes what
190 ** this action needs before it can perform the act.
191 ** static Action::Base* create();
192 ** - Factory for creating this action from a ParamList
195 class Base : public etl::shared_object
203 //! This function will throw an Action::Error() on failure
204 virtual void perform()=0;
206 virtual bool set_param(const synfig::String& /*name*/, const Param &) { return false; }
207 virtual bool is_ready()const=0;
209 virtual synfig::String get_name()const =0;
210 virtual synfig::String get_local_name()const { return get_name(); }
212 void set_param_list(const ParamList &);
214 static synfig::String get_layer_descriptions(const std::list<synfig::Layer::Handle> layers, synfig::String singular_prefix = "", synfig::String plural_prefix = "");
215 static synfig::String get_layer_descriptions(const std::list<std::pair<synfig::Layer::Handle,int> > layers, synfig::String singular_prefix = "", synfig::String plural_prefix = "");
216 }; // END of class Action::Base
218 typedef Action::Base* (*Factory)();
219 typedef bool (*CandidateChecker)(const ParamList &x);
220 typedef ParamVocab (*GetParamVocab)();
222 typedef etl::handle<Base> Handle;
224 //! Undoable Action Base Class
225 class Undoable : public Base
231 Undoable():active_(true) { }
234 void set_active(bool x) { active_=x; }
238 //! This function will throw an Action::Error() on failure
239 virtual void undo()=0;
241 bool is_active()const { return active_; }
243 }; // END of class Action::Undoable
245 //! Action base class for canvas-specific actions
252 etl::loose_handle<synfigapp::CanvasInterface> canvas_interface_;
253 synfig::Canvas::Handle canvas_;
256 CanvasSpecific(const synfig::Canvas::Handle &canvas):is_dirty_(true),mode_(MODE_UNDEFINED),canvas_(canvas) { }
257 CanvasSpecific():is_dirty_(true), mode_(MODE_UNDEFINED) { }
259 virtual ~CanvasSpecific() { };
264 void set_canvas(synfig::Canvas::Handle x) { canvas_=x; }
265 void set_canvas_interface(etl::loose_handle<synfigapp::CanvasInterface> x) { canvas_interface_=x; }
267 synfig::Canvas::Handle get_canvas()const { return canvas_; }
268 etl::loose_handle<synfigapp::CanvasInterface> get_canvas_interface()const { return canvas_interface_; }
270 static ParamVocab get_param_vocab();
271 virtual bool set_param(const synfig::String& name, const Param &);
272 virtual bool is_ready()const;
274 EditMode get_edit_mode()const;
276 void set_edit_mode(EditMode x) { mode_=x; }
278 bool is_dirty()const { return is_dirty_; }
279 void set_dirty(bool x=true) { is_dirty_=x; }
281 }; // END of class Action::Undoable
283 typedef std::list< etl::handle<Action::Undoable> > ActionList;
285 /*! \class synfigapp::Action::Super
286 ** \brief Super-Action base class for actions composed of several other actions.
288 ** Actions deriving from this class should only implement prepare(), and
289 ** NOT implement perform() or undo().
291 class Super : public Undoable, public CanvasSpecific
293 ActionList action_list_;
297 ActionList &action_list() { return action_list_; }
298 const ActionList &action_list()const { return action_list_; }
300 virtual void prepare()=0;
302 void clear() { action_list().clear(); }
304 bool first_time()const { return action_list_.empty(); }
306 void add_action(etl::handle<Undoable> action);
308 void add_action_front(etl::handle<Undoable> action);
310 virtual void perform();
313 }; // END of class Action::Super
316 class Group : public Super
318 synfig::String name_;
320 ActionList action_list_;
324 Group(const synfig::String &str="Group");
327 virtual synfig::String get_name()const { return name_; }
329 virtual void prepare() { };
331 virtual bool set_param(const synfig::String& /*name*/, const Param &)const { return false; }
332 virtual bool is_ready()const { return ready_; }
334 void set_name(std::string&x) { name_=x; }
335 }; // END of class Action::Group
344 synfig::String local_name;
345 synfig::String version;
350 CandidateChecker is_candidate;
351 GetParamVocab get_param_vocab;
353 bool operator<(const BookEntry &rhs)const { return priority<rhs.priority; }
354 }; // END of struct BookEntry
356 typedef std::map<synfig::String,BookEntry> Book;
358 class CandidateList : public std::list<BookEntry>
361 iterator find(const synfig::String& x);
362 const_iterator find(const synfig::String& x)const { return const_cast<CandidateList*>(this)->find(x); }
367 Handle create(const synfig::String &name);
369 //! Compiles a list of potential candidate actions with the given \a param_list and \a category
370 CandidateList compile_candidate_list(const ParamList& param_list, Category category=CATEGORY_ALL);
372 /*! \class synfigapp::Action::Main
379 friend class synfigapp::Main;
386 }; // END of class Action::Main
388 }; // END of namespace Action
390 }; // END of namespace synfigapp
392 /* === E N D =============================================================== */