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