7b96d9bb94f3acc7190a29f817829cabc44e92c9
[synfig.git] / synfig-studio / trunk / src / gtkmm / duck.h
1 /* === S Y N F I G ========================================================= */
2 /*!     \file duck.h
3 **      \brief Template Header
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_DUCKMATIC_DUCK_H
26 #define __SYNFIG_DUCKMATIC_DUCK_H
27
28 /* === H E A D E R S ======================================================= */
29
30 #include <list>
31
32 #include <ETL/smart_ptr>
33 #include <ETL/handle>
34
35 #include <synfig/vector.h>
36 #include <synfig/string.h>
37 #include <synfig/real.h>
38 #include <sigc++/signal.h>
39 #include <sigc++/object.h>
40 #include <synfig/time.h>
41 #include <ETL/smart_ptr>
42 #include <synfigapp/value_desc.h>
43 #include <synfig/transform.h>
44
45 /* === M A C R O S ========================================================= */
46
47 #ifdef HASH_MAP_H
48 #include HASH_MAP_H
49 #include FUNCTIONAL_H
50
51 #ifndef __STRING_HASH__
52 #define __STRING_HASH__
53 class StringHash
54 {
55 # ifdef FUNCTIONAL_HASH_ON_STRING
56         HASH_MAP_NAMESPACE::hash<synfig::String> hasher_;
57 # else  // FUNCTIONAL_HASH_ON_STRING
58         HASH_MAP_NAMESPACE::hash<const char*> hasher_;
59 # endif  // FUNCTIONAL_HASH_ON_STRING
60 public:
61         size_t operator()(const synfig::String& x)const
62         {
63 # ifdef FUNCTIONAL_HASH_ON_STRING
64                 return hasher_(x);
65 # else  // FUNCTIONAL_HASH_ON_STRING
66                 return hasher_(x.c_str());
67 # endif  // FUNCTIONAL_HASH_ON_STRING
68         }
69 };
70 #endif
71 #else
72 #include <map>
73 #endif
74
75 #include <set>
76
77 /* === T Y P E D E F S ===================================================== */
78
79 /* === C L A S S E S & S T R U C T S ======================================= */
80
81 namespace studio {
82 class Duckmatic;
83
84 /*! \class Duck
85 **      \writeme */
86 class Duck : public etl::shared_object
87 {
88         friend class Duckmatic;
89
90 public:
91         enum Type
92         {
93                 TYPE_NONE               =       (0),
94                 TYPE_POSITION   =       (1<<0),
95                 TYPE_TANGENT    =       (1<<1),
96                 TYPE_RADIUS             =       (1<<2),
97                 TYPE_WIDTH              =       (1<<3),
98                 TYPE_ANGLE              =       (1<<4),
99                 TYPE_VERTEX             =       (1<<5),
100
101                 TYPE_ALL                =       (~0),
102
103                 TYPE_DEFAULT    =       0xdefadefa
104         };
105
106         typedef etl::handle<Duck> Handle;
107         typedef etl::loose_handle<Duck> LooseHandle;
108
109 private:
110
111         sigc::signal<bool,const synfig::Point &> signal_edited_;
112         sigc::signal<bool,const synfig::Angle &> signal_edited_angle_;
113         sigc::signal<void> signal_user_click_[5];
114
115         Type type_;
116
117         synfig::Point point;
118         synfig::Angle rotations;
119
120         etl::smart_ptr<synfig::Point> shared_point;
121
122         synfig::Point origin;
123         synfig::String name;
124         synfig::Real scalar;
125
126         etl::handle<Duck> origin_duck;
127
128         etl::handle<Duck> connect_duck;
129         etl::handle<Duck> box_duck;
130
131         synfig::GUID guid_;
132
133         // Flags
134         bool editable;
135         bool radius_;
136         bool tangent_;
137         bool hover_;
138
139         synfig::TransformStack transform_stack_;
140
141         synfigapp::ValueDesc value_desc_;
142
143         static int duck_count;
144 public:
145         Duck();
146         Duck(const synfig::Point &point);
147         Duck(const synfig::Point &point,const synfig::Point &origin);
148         ~Duck();
149
150         sigc::signal<bool,const synfig::Point &> &signal_edited() { return signal_edited_; }
151         sigc::signal<bool,const synfig::Angle &> &signal_edited_angle() { return signal_edited_angle_; }
152         sigc::signal<void> &signal_user_click(int i=0) { assert(i>=0); assert(i<5); return signal_user_click_[i]; }
153
154         void set_guid(const synfig::GUID& x) { guid_=x; }
155         const synfig::GUID& get_guid()const { return guid_; }
156
157         synfig::GUID get_data_guid()const;
158
159         //! Changes the editable flag. If set, the duck will not be able to be moved.
160         void set_editable(bool x) { editable=x; }
161
162         //! Retrieves the status of the editable flag
163         bool get_editable()const { return editable; }
164
165         //! \writeme
166         void set_tangent(bool x) { tangent_=x; type_=TYPE_TANGENT; }
167
168         //! \writeme
169         bool get_tangent()const { return tangent_; }
170
171         //! Sets whether to show the duck as if it is being hovered over
172         void set_hover(bool h) { hover_=h; }
173
174         //! Retrieves whether to show the duck as if it is being hovered over
175         bool get_hover()const { return hover_; }
176
177         void set_connect_duck(const etl::handle<Duck>& x) { connect_duck=x; }
178         void set_box_duck(const etl::handle<Duck>& x) { box_duck=x; }
179
180         const etl::handle<Duck>& get_connect_duck()const { return connect_duck; }
181         const etl::handle<Duck>& get_box_duck()const { return box_duck; }
182
183         void set_value_desc(synfigapp::ValueDesc x) { value_desc_=x; }
184
185         synfigapp::ValueDesc& get_value_desc() { return value_desc_; }
186
187         void set_transform_stack(const synfig::TransformStack& x) { transform_stack_=x; }
188
189         const synfig::TransformStack& get_transform_stack()const { return transform_stack_; }
190
191         //! \writeme
192         void set_type(Type x) { type_=x; }
193
194         //! \writeme
195         Type get_type()const { return type_; }
196
197         //! Sets the scalar multiplier for the duck with respect to the origin
198         void set_scalar(synfig::Vector::value_type n) { scalar=n; }
199
200         //! Retrieves the scalar value
201         synfig::Vector::value_type get_scalar()const { return scalar; }
202
203         void set_shared_point(const etl::smart_ptr<synfig::Point>&x) { shared_point=x; }
204
205         //! Sets the location of the duck with respect to the origin
206         void set_point(const synfig::Point &x) { (shared_point?*shared_point:point)=x; }
207
208         //! Returns the location of the duck
209         synfig::Point get_point()const { return shared_point?*shared_point:point; }
210
211         synfig::Angle get_rotations()const { return rotations; };
212
213         synfig::Point get_trans_point()const;
214
215         void set_trans_point(const synfig::Point &x);
216
217         synfig::Point get_sub_trans_point()const;
218         void set_sub_trans_point(const synfig::Point &x);
219         synfig::Point get_sub_trans_origin()const;
220
221         //! Sets the origin point.
222         void set_origin(const synfig::Point &x);
223
224         //! Sets the origin point as another duck
225         void set_origin(const etl::handle<Duck> &x);
226
227         //! Retrieves the origin location
228         synfig::Point get_origin()const;
229
230         //! Retrieves the origin duck
231         const etl::handle<Duck> & get_origin_duck() const;
232
233         //! Retrieves the origin location
234         synfig::Point get_trans_origin()const;
235
236         void set_radius(bool r) { radius_=r; }
237         bool is_radius()const { return radius_; }
238
239         //! Sets the name of the duck
240         void set_name(const synfig::String &x);
241
242         //! Retrieves the name of the duck
243         synfig::String get_name()const { return name; }
244
245         bool operator==(const Duck &rhs)const;
246 }; // END of class Duck
247
248 //! Combine Flags
249 inline Duck::Type
250 operator|(Duck::Type lhs, const Duck::Type rhs)
251 { return static_cast<Duck::Type>(int(lhs)|int(rhs)); }
252
253 //! Exclude Flags
254 inline Duck::Type
255 operator-(Duck::Type lhs, const Duck::Type rhs)
256 { return static_cast<Duck::Type>(int(lhs)&~int(rhs)); }
257
258 inline Duck::Type&
259 operator|=(Duck::Type& lhs, const Duck::Type rhs)
260 { *reinterpret_cast<int*>(&lhs)|=int(rhs); return lhs; }
261
262 inline Duck::Type
263 operator&(const Duck::Type lhs, const Duck::Type rhs)
264 { return static_cast<Duck::Type>(int(lhs)&int(rhs)); }
265
266 class DuckMap : public
267 #ifdef HASH_MAP_H
268 HASH_MAP_CLASS<synfig::GUID,etl::handle<studio::Duck>,synfig::GUIDHash>
269 {
270         typedef HASH_MAP_CLASS<synfig::GUID,etl::handle<studio::Duck>,synfig::GUIDHash> PARENT_TYPE;
271 #else
272 std::map<synfig::GUID,etl::handle<studio::Duck> >
273 {
274         typedef std::map<synfig::GUID,etl::handle<studio::Duck> > PARENT_TYPE;
275 #endif
276 public:
277         void insert(const Duck::Handle& x) { operator[](x->get_guid())=x;  }
278 }; // END of class DuckMap
279
280 typedef std::list<Duck::Handle> DuckList;
281
282 }; // END of namespace studio
283
284 /* === E N D =============================================================== */
285
286 #endif