Lots of new stuff.
[ruby-evas.git] / src / rb_evas_object.c
1 /*
2  * $Id: rb_evas_object.c 20 2004-06-22 20:46:56Z tilman $
3  *
4  * Copyright (C) 2004 Tilman Sauerbeck (tilman at code-monkey de)
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <ruby.h>
22
23 #include <Evas.h>
24
25 #include "rb_evas_main.h"
26 #include "rb_evas.h"
27 #include "rb_evas_object.h"
28
29 #define GET_OBJ(obj, type, o, desc) \
30         type **(o) = NULL; \
31 \
32         Data_Get_Struct ((obj), type *, (o)); \
33 \
34         if (!*(o)) { \
35                 rb_raise (rb_eException, desc " destroyed already"); \
36                 return Qnil; \
37         }
38
39 #define CHECK_BOOL(val) \
40         if (TYPE ((val)) != T_TRUE && TYPE ((val)) != T_FALSE) { \
41                 rb_raise (rb_eTypeError, \
42                           "wrong argument type %s (expected true or false)", \
43                           rb_obj_classname ((val))); \
44                 return Qnil; \
45         }
46
47 static VALUE parents;
48
49 /* called by the child classes */
50 void c_evas_object_free (Evas_Object **e)
51 {
52         if (*e)
53                 evas_object_del (*e);
54
55         free (e);
56 }
57
58 void c_evas_object_mark (Evas_Object **e)
59 {
60         VALUE parent;
61
62         parent = rb_hash_aref (parents, INT2NUM ((long) (e)));
63         if (!NIL_P (parent))
64                 rb_gc_mark (parent);
65 }
66
67 static VALUE c_init (VALUE self, VALUE parent)
68 {
69         GET_OBJ (self, Evas_Object, e, "EvasObject");
70
71         evas_object_data_set (*e, RUBY_EVAS_OBJECT_KEY, (void *) self);
72
73         rb_hash_aset (parents, INT2NUM ((long) e), parent);
74
75         return self;
76 }
77
78 static VALUE c_inspect (VALUE self)
79 {
80         char buf[128];
81
82         GET_OBJ (self, Evas_Object, e, "EvasObject");
83
84         snprintf (buf, sizeof (buf), "#<EvasObject:%p ptr=%p>",
85                   (void *) self, *e);
86
87         return rb_str_new2 (buf);
88 }
89
90 static VALUE c_delete (VALUE self)
91 {
92         GET_OBJ (self, Evas_Object, e, "EvasObject");
93
94         if (*e) {
95                 evas_object_del (*e);
96                 *e = NULL;
97         }
98
99         return Qnil;
100 }
101
102 static VALUE c_resize (VALUE self, VALUE w, VALUE h)
103 {
104         GET_OBJ (self, Evas_Object, e, "EvasObject");
105
106         Check_Type (w, T_FIXNUM);
107         Check_Type (h, T_FIXNUM);
108
109         evas_object_resize (*e, (Evas_Coord) FIX2INT (w),
110                                 (Evas_Coord) FIX2INT (h));
111
112         return Qnil;
113 }
114
115 static VALUE c_move (VALUE self, VALUE x, VALUE y)
116 {
117         GET_OBJ (self, Evas_Object, e, "EvasObject");
118
119         Check_Type (x, T_FIXNUM);
120         Check_Type (y, T_FIXNUM);
121
122         evas_object_move (*e, (Evas_Coord) FIX2INT (x),
123                               (Evas_Coord) FIX2INT (y));
124
125         return Qnil;
126 }
127
128 static VALUE c_geometry_get (VALUE self)
129 {
130         int x = 0, y = 0, w = 0, h = 0;
131
132         GET_OBJ (self, Evas_Object, e, "EvasObject");
133
134         evas_object_geometry_get (*e, (Evas_Coord *) &x, (Evas_Coord *) &y,
135                                   (Evas_Coord *) & w, (Evas_Coord *) &h);
136
137         return rb_ary_new3 (4, INT2FIX (x), INT2FIX (y), INT2FIX (w),
138                             INT2FIX (h));
139 }
140
141 static VALUE c_show (VALUE self)
142 {
143         GET_OBJ (self, Evas_Object, e, "EvasObject");
144
145         evas_object_show (*e);
146
147         return Qnil;
148 }
149
150 static VALUE c_hide (VALUE self)
151 {
152         GET_OBJ (self, Evas_Object, e, "EvasObject");
153
154         evas_object_hide (*e);
155
156         return Qnil;
157 }
158
159 static VALUE c_visible_get (VALUE self)
160 {
161         GET_OBJ (self, Evas_Object, e, "EvasObject");
162
163         return evas_object_visible_get (*e) ? Qtrue : Qfalse;
164 }
165
166 static VALUE c_evas_get (VALUE self)
167 {
168         GET_OBJ (self, Evas_Object, e, "EvasObject");
169
170         return rb_hash_aref (parents, INT2NUM ((long) (e)));
171 }
172
173 static VALUE c_name_get (VALUE self)
174 {
175         const char *name;
176
177         GET_OBJ (self, Evas_Object, e, "EvasObject");
178
179         if (!(name = evas_object_name_get (*e)))
180                 return Qnil;
181         else
182                 return rb_str_new2 (name);
183 }
184
185 static VALUE c_name_set (VALUE self, VALUE val)
186 {
187         GET_OBJ (self, Evas_Object, e, "EvasObject");
188
189         Check_Type (val, T_STRING);
190
191         evas_object_name_set (*e, StringValuePtr (val));
192
193         return Qnil;
194 }
195
196 static VALUE c_layer_get (VALUE self)
197 {
198         GET_OBJ (self, Evas_Object, e, "EvasObject");
199
200         return INT2FIX (evas_object_layer_get (*e));
201 }
202
203 static VALUE c_layer_set (VALUE self, VALUE val)
204 {
205         GET_OBJ (self, Evas_Object, e, "EvasObject");
206
207         Check_Type (val, T_FIXNUM);
208
209         evas_object_layer_set (*e, NUM2INT (val));
210
211         return Qnil;
212 }
213
214 static VALUE c_get_color (VALUE self)
215 {
216         int r = 0, g = 0, b = 0, a = 0;
217
218         GET_OBJ (self, Evas_Object, e, "EvasObject");
219
220         evas_object_color_get (*e, &r, &g, &b, &a);
221
222         return rb_ary_new3 (4, INT2FIX (r), INT2FIX (g), INT2FIX (b),
223                             INT2FIX (a));
224 }
225
226 static VALUE c_set_color (VALUE self, VALUE r, VALUE g, VALUE b,
227                           VALUE a)
228 {
229         GET_OBJ (self, Evas_Object, e, "EvasObject");
230
231         Check_Type (r, T_FIXNUM);
232         Check_Type (g, T_FIXNUM);
233         Check_Type (b, T_FIXNUM);
234         Check_Type (a, T_FIXNUM);
235
236         evas_object_color_set (*e, FIX2INT (r), FIX2INT (g), FIX2INT (b),
237                                FIX2INT (a));
238
239         return Qnil;
240 }
241
242 static VALUE c_pass_events_get (VALUE self)
243 {
244         GET_OBJ (self, Evas_Object, e, "EvasObject");
245
246         return evas_object_pass_events_get (*e) ? Qtrue : Qfalse;
247 }
248
249 static VALUE c_pass_events_set (VALUE self, VALUE val)
250 {
251         GET_OBJ (self, Evas_Object, e, "EvasObject");
252
253         CHECK_BOOL (val);
254
255         evas_object_pass_events_set (*e, val == Qtrue ? 1 : 0);
256
257         return Qnil;
258 }
259
260 static VALUE c_raise (VALUE self)
261 {
262         GET_OBJ (self, Evas_Object, e, "EvasObject");
263
264         evas_object_raise (*e);
265
266         return Qnil;
267 }
268
269 static VALUE c_lower (VALUE self)
270 {
271         GET_OBJ (self, Evas_Object, e, "EvasObject");
272
273         evas_object_lower (*e);
274
275         return Qnil;
276 }
277
278 static VALUE c_stack_above (VALUE self, VALUE target)
279 {
280         GET_OBJ (self, Evas_Object, e, "EvasObject");
281
282         if (!rb_obj_is_kind_of (target, cEvasObject)) {
283                 rb_raise (rb_eTypeError,
284                           "wrong argument type %s (expected EvasObject)",
285                           rb_obj_classname (target));
286                 return Qnil;
287         }
288
289         GET_OBJ (target, Evas_Object, target2, "EvasObject");
290
291         evas_object_stack_above (*e, *target2);
292
293         return Qnil;
294 }
295
296 static VALUE c_stack_below (VALUE self, VALUE target)
297 {
298         GET_OBJ (self, Evas_Object, e, "EvasObject");
299
300         if (!rb_obj_is_kind_of (target, cEvasObject)) {
301                 rb_raise (rb_eTypeError,
302                           "wrong argument type %s (expected EvasObject)",
303                           rb_obj_classname (target));
304                 return Qnil;
305         }
306
307         GET_OBJ (target, Evas_Object, target2, "EvasObject");
308
309         evas_object_stack_below (*e, *target2);
310
311         return Qnil;
312 }
313
314 static VALUE c_above_get (VALUE self)
315 {
316         Evas_Object *o;
317         void *obj;
318
319         GET_OBJ (self, Evas_Object, e, "EvasObject");
320
321         if (!(evas_object_above_get (*e)))
322                 return Qnil;
323
324         if (!(obj = evas_object_data_get (o, RUBY_EVAS_OBJECT_KEY))) {
325                 rb_raise (rb_eException, "EvasObject Ruby object key missing");
326                 return Qnil;
327         }
328
329         return (VALUE) obj;
330 }
331
332 static VALUE c_below_get (VALUE self)
333 {
334         Evas_Object *o;
335         void *obj;
336
337         GET_OBJ (self, Evas_Object, e, "EvasObject");
338
339         if (!(evas_object_below_get (*e)))
340                 return Qnil;
341
342         if (!(obj = evas_object_data_get (o, RUBY_EVAS_OBJECT_KEY))) {
343                 rb_raise (rb_eException, "EvasObject Ruby object key missing");
344                 return Qnil;
345         }
346
347         return (VALUE) obj;
348 }
349
350 void Init_EvasObject (void)
351 {
352         cEvasObject = rb_define_class_under (mEvas, "EvasObject",
353                                              rb_cObject);
354
355         rb_define_private_method (rb_singleton_class (cEvasObject),
356                                   "new", NULL, 0);
357         rb_define_method (cEvasObject, "initialize", c_init, 1);
358         rb_define_method (cEvasObject, "inspect", c_inspect, 0);
359         rb_define_method (cEvasObject, "delete", c_delete, 0);
360         rb_define_method (cEvasObject, "resize", c_resize, 2);
361         rb_define_method (cEvasObject, "move", c_move, 2);
362         rb_define_method (cEvasObject, "geometry", c_geometry_get, 0);
363         rb_define_method (cEvasObject, "show", c_show, 0);
364         rb_define_method (cEvasObject, "hide", c_hide, 0);
365         rb_define_method (cEvasObject, "visible?", c_visible_get, 0);
366         rb_define_method (cEvasObject, "evas", c_evas_get, 0);
367         rb_define_method (cEvasObject, "name", c_name_get, 0);
368         rb_define_method (cEvasObject, "name=", c_name_set, 1);
369         rb_define_method (cEvasObject, "layer", c_layer_get, 0);
370         rb_define_method (cEvasObject, "layer=", c_layer_set, 1);
371         rb_define_method (cEvasObject, "get_color", c_get_color, 0);
372         rb_define_method (cEvasObject, "set_color", c_set_color, 4);
373         rb_define_method (cEvasObject, "pass_events?", c_pass_events_get, 0);
374         rb_define_method (cEvasObject, "pass_events=", c_pass_events_set, 1);
375         rb_define_method (cEvasObject, "raise", c_raise, 0);
376         rb_define_method (cEvasObject, "lower", c_lower, 0);
377         rb_define_method (cEvasObject, "stack_above", c_stack_above, 1);
378         rb_define_method (cEvasObject, "stack_below", c_stack_below, 1);
379         rb_define_method (cEvasObject, "above", c_above_get, 0);
380         rb_define_method (cEvasObject, "below", c_below_get, 0);
381
382         parents = rb_hash_new ();
383         rb_global_variable (&parents);
384 }