Macro updates.
[ruby-evas.git] / src / rb_evas_object.c
1 /*
2  * $Id: rb_evas_object.c 32 2004-07-10 14:07:49Z 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 static VALUE parents;
30
31 /* called by the child classes */
32 void c_evas_object_free (Evas_Object **e)
33 {
34         if (*e)
35                 evas_object_del (*e);
36
37         rb_hash_aset (parents, INT2NUM ((long) e), Qnil);
38
39         free (e);
40 }
41
42 void c_evas_object_mark (Evas_Object **e)
43 {
44         VALUE parent;
45
46         parent = rb_hash_aref (parents, INT2NUM ((long) (e)));
47         if (!NIL_P (parent))
48                 rb_gc_mark (parent);
49 }
50
51 static VALUE c_init (VALUE self, VALUE parent)
52 {
53         GET_OBJ (self, Evas_Object, e);
54
55         evas_object_data_set (*e, RUBY_EVAS_OBJECT_KEY, (void *) self);
56
57         rb_hash_aset (parents, INT2NUM ((long) e), parent);
58
59         return self;
60 }
61
62 static VALUE c_inspect (VALUE self)
63 {
64         INSPECT (self, Evas_Object);
65 }
66
67 static VALUE c_delete (VALUE self)
68 {
69         GET_OBJ (self, Evas_Object, e);
70
71         evas_object_del (*e);
72         *e = NULL;
73
74         return Qnil;
75 }
76
77 static VALUE c_resize (VALUE self, VALUE w, VALUE h)
78 {
79         GET_OBJ (self, Evas_Object, e);
80
81         Check_Type (w, T_FIXNUM);
82         Check_Type (h, T_FIXNUM);
83
84         evas_object_resize (*e, (Evas_Coord) FIX2INT (w),
85                                 (Evas_Coord) FIX2INT (h));
86
87         return Qnil;
88 }
89
90 static VALUE c_move (VALUE self, VALUE x, VALUE y)
91 {
92         GET_OBJ (self, Evas_Object, e);
93
94         Check_Type (x, T_FIXNUM);
95         Check_Type (y, T_FIXNUM);
96
97         evas_object_move (*e, (Evas_Coord) FIX2INT (x),
98                               (Evas_Coord) FIX2INT (y));
99
100         return Qnil;
101 }
102
103 static VALUE c_geometry_get (VALUE self)
104 {
105         int x = 0, y = 0, w = 0, h = 0;
106
107         GET_OBJ (self, Evas_Object, e);
108
109         evas_object_geometry_get (*e, (Evas_Coord *) &x, (Evas_Coord *) &y,
110                                   (Evas_Coord *) & w, (Evas_Coord *) &h);
111
112         return rb_ary_new3 (4, INT2FIX (x), INT2FIX (y), INT2FIX (w),
113                             INT2FIX (h));
114 }
115
116 static VALUE c_show (VALUE self)
117 {
118         GET_OBJ (self, Evas_Object, e);
119
120         evas_object_show (*e);
121
122         return Qnil;
123 }
124
125 static VALUE c_hide (VALUE self)
126 {
127         GET_OBJ (self, Evas_Object, e);
128
129         evas_object_hide (*e);
130
131         return Qnil;
132 }
133
134 static VALUE c_visible_get (VALUE self)
135 {
136         GET_OBJ (self, Evas_Object, e);
137
138         return evas_object_visible_get (*e) ? Qtrue : Qfalse;
139 }
140
141 static VALUE c_evas_get (VALUE self)
142 {
143         GET_OBJ (self, Evas_Object, e);
144
145         return rb_hash_aref (parents, INT2NUM ((long) (e)));
146 }
147
148 static VALUE c_name_get (VALUE self)
149 {
150         const char *name;
151
152         GET_OBJ (self, Evas_Object, e);
153
154         if (!(name = evas_object_name_get (*e)))
155                 return Qnil;
156         else
157                 return rb_str_new2 (name);
158 }
159
160 static VALUE c_name_set (VALUE self, VALUE val)
161 {
162         GET_OBJ (self, Evas_Object, e);
163
164         Check_Type (val, T_STRING);
165
166         evas_object_name_set (*e, StringValuePtr (val));
167
168         return Qnil;
169 }
170
171 static VALUE c_layer_get (VALUE self)
172 {
173         GET_OBJ (self, Evas_Object, e);
174
175         return INT2FIX (evas_object_layer_get (*e));
176 }
177
178 static VALUE c_layer_set (VALUE self, VALUE val)
179 {
180         GET_OBJ (self, Evas_Object, e);
181
182         Check_Type (val, T_FIXNUM);
183
184         evas_object_layer_set (*e, NUM2INT (val));
185
186         return Qnil;
187 }
188
189 static VALUE c_get_color (VALUE self)
190 {
191         int r = 0, g = 0, b = 0, a = 0;
192
193         GET_OBJ (self, Evas_Object, e);
194
195         evas_object_color_get (*e, &r, &g, &b, &a);
196
197         return rb_ary_new3 (4, INT2FIX (r), INT2FIX (g), INT2FIX (b),
198                             INT2FIX (a));
199 }
200
201 static VALUE c_set_color (VALUE self, VALUE r, VALUE g, VALUE b,
202                           VALUE a)
203 {
204         GET_OBJ (self, Evas_Object, e);
205
206         Check_Type (r, T_FIXNUM);
207         Check_Type (g, T_FIXNUM);
208         Check_Type (b, T_FIXNUM);
209         Check_Type (a, T_FIXNUM);
210
211         evas_object_color_set (*e, FIX2INT (r), FIX2INT (g), FIX2INT (b),
212                                FIX2INT (a));
213
214         return Qnil;
215 }
216
217 static VALUE c_pass_events_get (VALUE self)
218 {
219         GET_OBJ (self, Evas_Object, e);
220
221         return evas_object_pass_events_get (*e) ? Qtrue : Qfalse;
222 }
223
224 static VALUE c_pass_events_set (VALUE self, VALUE val)
225 {
226         GET_OBJ (self, Evas_Object, e);
227
228         CHECK_BOOL (val);
229
230         evas_object_pass_events_set (*e, val == Qtrue ? 1 : 0);
231
232         return Qnil;
233 }
234
235 static VALUE c_raise (VALUE self)
236 {
237         GET_OBJ (self, Evas_Object, e);
238
239         evas_object_raise (*e);
240
241         return Qnil;
242 }
243
244 static VALUE c_lower (VALUE self)
245 {
246         GET_OBJ (self, Evas_Object, e);
247
248         evas_object_lower (*e);
249
250         return Qnil;
251 }
252
253 static VALUE c_stack_above (VALUE self, VALUE target)
254 {
255         GET_OBJ (self, Evas_Object, e);
256
257         if (!rb_obj_is_kind_of (target, cEvasObject)) {
258                 rb_raise (rb_eTypeError,
259                           "wrong argument type %s (expected EvasObject)",
260                           rb_obj_classname (target));
261                 return Qnil;
262         }
263
264         GET_OBJ (target, Evas_Object, target2);
265
266         evas_object_stack_above (*e, *target2);
267
268         return Qnil;
269 }
270
271 static VALUE c_stack_below (VALUE self, VALUE target)
272 {
273         GET_OBJ (self, Evas_Object, e);
274
275         if (!rb_obj_is_kind_of (target, cEvasObject)) {
276                 rb_raise (rb_eTypeError,
277                           "wrong argument type %s (expected EvasObject)",
278                           rb_obj_classname (target));
279                 return Qnil;
280         }
281
282         GET_OBJ (target, Evas_Object, target2);
283
284         evas_object_stack_below (*e, *target2);
285
286         return Qnil;
287 }
288
289 static VALUE c_above_get (VALUE self)
290 {
291         Evas_Object *o;
292         void *obj;
293
294         GET_OBJ (self, Evas_Object, e);
295
296         if (!(evas_object_above_get (*e)))
297                 return Qnil;
298
299         if (!(obj = evas_object_data_get (o, RUBY_EVAS_OBJECT_KEY))) {
300                 rb_raise (rb_eException, "EvasObject Ruby object key missing");
301                 return Qnil;
302         }
303
304         return (VALUE) obj;
305 }
306
307 static VALUE c_below_get (VALUE self)
308 {
309         Evas_Object *o;
310         void *obj;
311
312         GET_OBJ (self, Evas_Object, e);
313
314         if (!(evas_object_below_get (*e)))
315                 return Qnil;
316
317         if (!(obj = evas_object_data_get (o, RUBY_EVAS_OBJECT_KEY))) {
318                 rb_raise (rb_eException, "EvasObject Ruby object key missing");
319                 return Qnil;
320         }
321
322         return (VALUE) obj;
323 }
324
325 void Init_EvasObject (void)
326 {
327         cEvasObject = rb_define_class_under (mEvas, "EvasObject",
328                                              rb_cObject);
329
330         rb_define_private_method (rb_singleton_class (cEvasObject),
331                                   "new", NULL, 0);
332         rb_define_method (cEvasObject, "initialize", c_init, 1);
333         rb_define_method (cEvasObject, "inspect", c_inspect, 0);
334         rb_define_method (cEvasObject, "delete", c_delete, 0);
335         rb_define_method (cEvasObject, "resize", c_resize, 2);
336         rb_define_method (cEvasObject, "move", c_move, 2);
337         rb_define_method (cEvasObject, "geometry", c_geometry_get, 0);
338         rb_define_method (cEvasObject, "show", c_show, 0);
339         rb_define_method (cEvasObject, "hide", c_hide, 0);
340         rb_define_method (cEvasObject, "visible?", c_visible_get, 0);
341         rb_define_method (cEvasObject, "evas", c_evas_get, 0);
342         rb_define_method (cEvasObject, "name", c_name_get, 0);
343         rb_define_method (cEvasObject, "name=", c_name_set, 1);
344         rb_define_method (cEvasObject, "layer", c_layer_get, 0);
345         rb_define_method (cEvasObject, "layer=", c_layer_set, 1);
346         rb_define_method (cEvasObject, "get_color", c_get_color, 0);
347         rb_define_method (cEvasObject, "set_color", c_set_color, 4);
348         rb_define_method (cEvasObject, "pass_events?", c_pass_events_get, 0);
349         rb_define_method (cEvasObject, "pass_events=", c_pass_events_set, 1);
350         rb_define_method (cEvasObject, "raise", c_raise, 0);
351         rb_define_method (cEvasObject, "lower", c_lower, 0);
352         rb_define_method (cEvasObject, "stack_above", c_stack_above, 1);
353         rb_define_method (cEvasObject, "stack_below", c_stack_below, 1);
354         rb_define_method (cEvasObject, "above", c_above_get, 0);
355         rb_define_method (cEvasObject, "below", c_below_get, 0);
356
357         parents = rb_hash_new ();
358         rb_global_variable (&parents);
359 }