initial version
[synfig.git] / synfig-studio / trunk / src / gtkmm / duck.h
1 /* === S I N F G =========================================================== */
2 /*!     \file duck.h
3 **      \brief Template Header
4 **
5 **      $Id: duck.h,v 1.1.1.1 2005/01/07 03:34:36 darco Exp $
6 **
7 **      \legal
8 **      Copyright (c) 2002 Robert B. Quattlebaum Jr.
9 **
10 **      This software and associated documentation
11 **      are CONFIDENTIAL and PROPRIETARY property of
12 **      the above-mentioned copyright holder.
13 **
14 **      You may not copy, print, publish, or in any
15 **      other way distribute this software without
16 **      a prior written agreement with
17 **      the copyright holder.
18 **      \endlegal
19 */
20 /* ========================================================================= */
21
22 /* === S T A R T =========================================================== */
23
24 #ifndef __SINFG_DUCKMATIC_DUCK_H
25 #define __SINFG_DUCKMATIC_DUCK_H
26
27 /* === H E A D E R S ======================================================= */
28
29 #include <list>
30
31 #include <ETL/smart_ptr>
32 #include <ETL/handle>
33
34 #include <sinfg/vector.h>
35 #include <sinfg/string.h>
36 #include <sinfg/real.h>
37 #include <sigc++/signal.h>
38 #include <sigc++/object.h>
39 #include <sinfg/time.h>
40 #include <ETL/smart_ptr>
41 #include <sinfgapp/value_desc.h>
42 #include <sinfg/transform.h>
43
44 /* === M A C R O S ========================================================= */
45
46 #define HASH_MAP_H <ext/hash_map>
47 #define HASH_SET_H <ext/hash_set>
48
49 #ifdef HASH_MAP_H
50 #include HASH_MAP_H
51 #ifndef __STRING_HASH__
52 #define __STRING_HASH__
53 class StringHash
54 {
55         __gnu_cxx::hash<const char*> hasher_;
56 public:
57         size_t operator()(const sinfg::String& x)const
58         {
59                 return hasher_(x.c_str());
60         }
61 };
62 #endif
63 #else
64 #include <map>
65 #endif
66
67 #include <set>
68
69 /* === T Y P E D E F S ===================================================== */
70
71 /* === C L A S S E S & S T R U C T S ======================================= */
72
73 namespace studio {
74 class Duckmatic;
75         
76 /*! \class Duck
77 **      \writeme */
78 class Duck : public etl::shared_object
79 {
80         friend class Duckmatic;
81                 
82 public:
83         enum Type
84         {
85                 TYPE_NONE               =       (0),
86                 TYPE_POSITION   =       (1<<0),
87                 TYPE_TANGENT    =       (1<<1),
88                 TYPE_RADIUS             =       (1<<2),
89                 TYPE_WIDTH              =       (1<<3),
90                 TYPE_ANGLE              =       (1<<4),
91                 TYPE_VERTEX             =       (1<<5),
92
93                 TYPE_ALL                =       (~0),
94
95                 TYPE_DEFAULT    =       0xdefadefa
96         };
97
98         typedef etl::handle<Duck> Handle;
99         typedef etl::loose_handle<Duck> LooseHandle;
100         
101 private:
102
103         sigc::signal<bool,const sinfg::Point &> signal_edited_;
104         sigc::signal<void> signal_user_click_[5];
105         
106         Type type_;
107
108         sinfg::Point point;
109
110         etl::smart_ptr<sinfg::Point> shared_point;
111         
112         sinfg::Point origin;
113         sinfg::String name;
114         sinfg::Real scalar;
115
116         etl::handle<Duck> origin_duck;
117
118         etl::handle<Duck> connect_duck;
119         etl::handle<Duck> box_duck;
120
121         sinfg::GUID guid_;      
122
123         // Flags
124         bool editable;
125         bool radius_;
126         bool tangent_;
127         
128         sinfg::TransformStack transform_stack_;
129
130         sinfgapp::ValueDesc value_desc_;
131
132         static int duck_count;
133 public:
134         Duck();
135         Duck(const sinfg::Point &point);
136         Duck(const sinfg::Point &point,const sinfg::Point &origin);
137         ~Duck();
138                 
139         sigc::signal<bool,const sinfg::Point &> &signal_edited() { return signal_edited_; }
140         sigc::signal<void> &signal_user_click(int i=0) { assert(i>=0); assert(i<5); return signal_user_click_[i]; }
141
142         void set_guid(const sinfg::GUID& x) { guid_=x; }
143         const sinfg::GUID& get_guid()const { return guid_; }
144
145         sinfg::GUID get_data_guid()const;
146
147         //! Changes the editable flag. If set, the duck will not be able to be moved.
148         void set_editable(bool x) { editable=x; }
149
150         //! Retrieves the status of the editable flag
151         bool get_editable()const { return editable; }
152
153         //! \writeme
154         void set_tangent(bool x) { tangent_=x; type_=TYPE_TANGENT; }
155
156         //! \writeme
157         bool get_tangent()const { return tangent_; }
158
159         void set_connect_duck(const etl::handle<Duck>& x) { connect_duck=x; }
160         void set_box_duck(const etl::handle<Duck>& x) { box_duck=x; }
161
162         const etl::handle<Duck>& get_connect_duck()const { return connect_duck; }
163         const etl::handle<Duck>& get_box_duck()const { return box_duck; }
164
165         void set_value_desc(sinfgapp::ValueDesc x) { value_desc_=x; }
166
167         sinfgapp::ValueDesc& get_value_desc() { return value_desc_; }
168
169         void set_transform_stack(const sinfg::TransformStack& x) { transform_stack_=x; }
170
171         const sinfg::TransformStack& get_transform_stack()const { return transform_stack_; }
172         
173         //! \writeme
174         void set_type(Type x) { type_=x; }
175
176         //! \writeme
177         Type get_type()const { return type_; }
178
179         //! Sets the scalar multiplier for the duck with respect to the origin
180         void set_scalar(sinfg::Vector::value_type n) { scalar=n; }
181
182         //! Retrieves the scalar value
183         sinfg::Vector::value_type get_scalar()const { return scalar; }
184
185         void set_shared_point(const etl::smart_ptr<sinfg::Point>&x) { shared_point=x; }
186
187         //! Sets the location of the duck with respect to the origin
188         void set_point(const sinfg::Point &x) { (shared_point?*shared_point:point)=x; }
189                 
190         //! Returns the location of the duck
191         sinfg::Point get_point()const { return shared_point?*shared_point:point; }
192         
193         sinfg::Point get_trans_point()const;
194         
195         void set_trans_point(const sinfg::Point &x);
196
197         sinfg::Point get_sub_trans_point()const;
198         void set_sub_trans_point(const sinfg::Point &x);
199         sinfg::Point get_sub_trans_origin()const;
200
201         //! Sets the origin point.
202         void set_origin(const sinfg::Point &x);
203
204         //! Sets the origin point as another duck
205         void set_origin(const etl::handle<Duck> &x);
206
207         //! Retrieves the origin location
208         sinfg::Point get_origin()const;
209         
210         //! Retrieves the origin duck
211         const etl::handle<Duck> & get_origin_duck() const;      
212
213         //! Retrieves the origin location
214         sinfg::Point get_trans_origin()const;
215
216         void set_radius(bool r) { radius_=r; }
217         bool is_radius()const { return radius_; }
218         
219         //! Sets the name of the duck
220         void set_name(const sinfg::String &x);
221
222         //! Retrieves the name of the duck
223         sinfg::String get_name()const { return name; }
224         
225         bool operator==(const Duck &rhs)const;
226 }; // END of class Duck
227
228 //! Combine Flags
229 inline Duck::Type
230 operator|(Duck::Type lhs, const Duck::Type rhs)
231 { return static_cast<Duck::Type>(int(lhs)|int(rhs)); }
232
233 //! Exclude Flags
234 inline Duck::Type
235 operator-(Duck::Type lhs, const Duck::Type rhs)
236 { return static_cast<Duck::Type>(int(lhs)&~int(rhs)); }
237
238 inline Duck::Type&
239 operator|=(Duck::Type& lhs, const Duck::Type rhs)
240 { *reinterpret_cast<int*>(&lhs)|=int(rhs); return lhs; }
241
242 inline Duck::Type
243 operator&(const Duck::Type lhs, const Duck::Type rhs)
244 { return static_cast<Duck::Type>(int(lhs)&int(rhs)); }
245
246 class DuckMap : public
247 #ifdef HASH_MAP_H
248 __gnu_cxx::hash_map<sinfg::GUID,etl::handle<studio::Duck>,sinfg::GUIDHash>
249 {
250         typedef __gnu_cxx::hash_map<sinfg::GUID,etl::handle<studio::Duck>,sinfg::GUIDHash> PARENT_TYPE;
251 #else
252 std::map<sinfg::GUID,etl::handle<studio::Duck> >
253 {
254         typedef std::map<sinfg::GUID,etl::handle<studio::Duck> > PARENT_TYPE;
255 #endif
256 public:
257         void insert(const Duck::Handle& x) { operator[](x->get_guid())=x;  }
258 }; // END of class DuckMap
259
260 typedef std::list<Duck::Handle> DuckList;
261
262 }; // END of namespace studio
263
264 /* === E N D =============================================================== */
265
266 #endif