Introduced TO_EVAS_OBJECT.
[ruby-evas.git] / src / rb_evas_object.c
1 /*
2  * $Id: rb_evas_object.c 68 2004-08-16 15:42:19Z 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 #include <stdbool.h>
23
24 #include <Evas.h>
25
26 #define __RB_EVAS_OBJECT_C
27 #include "rb_evas_main.h"
28 #include "rb_evas.h"
29 #include "rb_evas_object.h"
30
31 VALUE cEvasObject;
32
33 VALUE TO_EVAS_OBJECT (Evas_Object *o)
34 {
35         void *obj;
36
37         if (!o)
38                 return Qnil;
39
40         if (!(obj = evas_object_data_get (o, RUBY_EVAS_OBJECT_KEY))) {
41                 rb_raise (rb_eException, "EvasObject Ruby object key missing");
42                 return Qnil;
43         }
44
45         return (VALUE) obj;
46 }
47
48 /* called by the child classes */
49 void c_evas_object_mark (RbEvasObject *e)
50 {
51         rb_gc_mark (e->parent);
52 }
53
54 void c_evas_object_free (RbEvasObject *e, bool free_mem)
55 {
56         if (e->real)
57                 evas_object_del (e->real);
58
59         if (free_mem)
60                 free (e);
61 }
62
63 /* :nodoc: */
64 static VALUE c_init (VALUE self, VALUE parent)
65 {
66         GET_OBJ (self, RbEvasObject, e);
67
68         evas_object_data_set (e->real, RUBY_EVAS_OBJECT_KEY, (void *) self);
69
70         e->parent = parent;
71
72         return self;
73 }
74
75 /* :nodoc: */
76 static VALUE c_inspect (VALUE self)
77 {
78         INSPECT (self, RbEvasObject);
79 }
80
81 /*
82  * call-seq:
83  *  e.delete => nil
84  *
85  * Deletes <i>e</i>.
86  */
87 static VALUE c_delete (VALUE self)
88 {
89         GET_OBJ (self, RbEvasObject, e);
90
91         evas_object_del (e->real);
92         e->real = NULL;
93
94         return Qnil;
95 }
96
97 /*
98  * call-seq:
99  *  e.resize(width, height) => nil
100  *
101  * Resizes <i>e</i> to width x height.
102  *
103  *  e.resize(100, 200) #=> nil
104  */
105 static VALUE c_resize (VALUE self, VALUE w, VALUE h)
106 {
107         GET_OBJ (self, RbEvasObject, e);
108
109         Check_Type (w, T_FIXNUM);
110         Check_Type (h, T_FIXNUM);
111
112         evas_object_resize (e->real, (Evas_Coord) FIX2INT (w),
113                             (Evas_Coord) FIX2INT (h));
114
115         return Qnil;
116 }
117
118 /*
119  * call-seq:
120  *  e.move(x, y) => nil
121  *
122  * Moves <i>e</i> to the coordinates specified in
123  * <i>x</i> and <i>y</i>.
124  *
125  *  e.move(100, 200) #=> nil
126  */
127 static VALUE c_move (VALUE self, VALUE x, VALUE y)
128 {
129         GET_OBJ (self, RbEvasObject, e);
130
131         Check_Type (x, T_FIXNUM);
132         Check_Type (y, T_FIXNUM);
133
134         evas_object_move (e->real, (Evas_Coord) FIX2INT (x),
135                           (Evas_Coord) FIX2INT (y));
136
137         return Qnil;
138 }
139
140 /*
141  * call-seq:
142  *  e.geometry => array
143  *
144  * Returns an array containing the geometry of <i>e</i>.
145  *
146  *  e.move(150, 300)   #=> nil
147  *  e.resize(200, 200) #=> nil
148  *  e.geometry         #=> [150, 300, 200, 200]
149  */
150 static VALUE c_geometry_get (VALUE self)
151 {
152         int x = 0, y = 0, w = 0, h = 0;
153
154         GET_OBJ (self, RbEvasObject, e);
155
156         evas_object_geometry_get (e->real,
157                                   (Evas_Coord *) &x, (Evas_Coord *) &y,
158                                   (Evas_Coord *) &w, (Evas_Coord *) &h);
159
160         return rb_ary_new3 (4, INT2FIX (x), INT2FIX (y), INT2FIX (w),
161                             INT2FIX (h));
162 }
163
164 /*
165  * call-seq:
166  *  e.show => nil
167  *
168  * Shows <i>e</i>.
169  */
170 static VALUE c_show (VALUE self)
171 {
172         GET_OBJ (self, RbEvasObject, e);
173
174         evas_object_show (e->real);
175
176         return Qnil;
177 }
178
179 /*
180  * call-seq:
181  *  e.hide => nil
182  *
183  * Hides <i>e</i>.
184  */
185 static VALUE c_hide (VALUE self)
186 {
187         GET_OBJ (self, RbEvasObject, e);
188
189         evas_object_hide (e->real);
190
191         return Qnil;
192 }
193
194 /*
195  * call-seq:
196  *  e.visible? => true or false
197  *
198  * Returns true if <i>e</i> is visible, else returns false.
199  */
200 static VALUE c_visible_get (VALUE self)
201 {
202         GET_OBJ (self, RbEvasObject, e);
203
204         return evas_object_visible_get (e->real) ? Qtrue : Qfalse;
205 }
206
207 /*
208  * call-seq:
209  *  e.evas => evas
210  *
211  * Returns the <code>Evas::Evas</code> for <i>e</i>.
212  */
213 static VALUE c_evas_get (VALUE self)
214 {
215         GET_OBJ (self, RbEvasObject, e);
216
217         return e->parent;
218 }
219
220 /*
221  * call-seq:
222  *  e.name => string
223  *
224  * Returns the name of <i>e</i>.
225  */
226 static VALUE c_name_get (VALUE self)
227 {
228         const char *name;
229
230         GET_OBJ (self, RbEvasObject, e);
231
232         if (!(name = evas_object_name_get (e->real)))
233                 return Qnil;
234         else
235                 return rb_str_new2 (name);
236 }
237
238 /*
239  * call-seq:
240  *  e.name(string)
241  *
242  * Sets the name of <i>e</i>.
243  */
244 static VALUE c_name_set (VALUE self, VALUE val)
245 {
246         GET_OBJ (self, RbEvasObject, e);
247
248         Check_Type (val, T_STRING);
249
250         evas_object_name_set (e->real, StringValuePtr (val));
251
252         return Qnil;
253 }
254
255 /*
256  * call-seq:
257  *  e.layer => fixnum
258  *
259  * Returns the layer <i>e</i> is in.
260  */
261 static VALUE c_layer_get (VALUE self)
262 {
263         GET_OBJ (self, RbEvasObject, e);
264
265         return INT2FIX (evas_object_layer_get (e->real));
266 }
267
268 /*
269  * call-seq:
270  *  e.layer(fixnum)
271  *
272  * Sets the layer <i>e</i> is in.
273  */
274 static VALUE c_layer_set (VALUE self, VALUE val)
275 {
276         GET_OBJ (self, RbEvasObject, e);
277
278         Check_Type (val, T_FIXNUM);
279
280         evas_object_layer_set (e->real, NUM2INT (val));
281
282         return Qnil;
283 }
284
285 /*
286  * call-seq:
287  *  e.get_color => array
288  *
289  * Returns the color of <i>e</i>.
290  *
291  *  e.set_color(128, 128, 128, 0) #=> nil
292  *  e.get_color                   #=> [128, 128, 128, 0]
293  */
294 static VALUE c_get_color (VALUE self)
295 {
296         int r = 0, g = 0, b = 0, a = 0;
297
298         GET_OBJ (self, RbEvasObject, e);
299
300         evas_object_color_get (e->real, &r, &g, &b, &a);
301
302         return rb_ary_new3 (4, INT2FIX (r), INT2FIX (g), INT2FIX (b),
303                             INT2FIX (a));
304 }
305
306 /*
307  * call-seq:
308  *  e.set_color(r, g, b, a) => nil
309  *
310  * Sets the color of <i>e</i>.
311  *
312  *  e.set_color(128, 128, 128, 0) #=> nil
313  */
314 static VALUE c_set_color (VALUE self, VALUE r, VALUE g, VALUE b,
315                           VALUE a)
316 {
317         GET_OBJ (self, RbEvasObject, e);
318
319         Check_Type (r, T_FIXNUM);
320         Check_Type (g, T_FIXNUM);
321         Check_Type (b, T_FIXNUM);
322         Check_Type (a, T_FIXNUM);
323
324         evas_object_color_set (e->real, FIX2INT (r), FIX2INT (g),
325                                FIX2INT (b), FIX2INT (a));
326
327         return Qnil;
328 }
329
330 /*
331  * call-seq:
332  *  e.pass_events? => true or false
333  *
334  * Returns true if <i>e</i> passes events on to EvasObjects that are
335  * below itself, else returns false.
336  */
337 static VALUE c_pass_events_get (VALUE self)
338 {
339         GET_OBJ (self, RbEvasObject, e);
340
341         return evas_object_pass_events_get (e->real) ? Qtrue : Qfalse;
342 }
343
344 /*
345  * call-seq:
346  *  e.pass_events(true or false)
347  *
348  * Sets whether <i>e</i> passes events on to EvasObjects that are
349  * below itself.
350  */
351 static VALUE c_pass_events_set (VALUE self, VALUE val)
352 {
353         GET_OBJ (self, RbEvasObject, e);
354
355         CHECK_BOOL (val);
356
357         evas_object_pass_events_set (e->real, val == Qtrue);
358
359         return Qnil;
360 }
361
362 /*
363  * call-seq:
364  *   e.raise => nil
365  *
366  * Raises <i>e</i>.
367  */
368 static VALUE c_raise (VALUE self)
369 {
370         GET_OBJ (self, RbEvasObject, e);
371
372         evas_object_raise (e->real);
373
374         return Qnil;
375 }
376
377 /*
378  * call-seq:
379  *  e.lower => nil
380  *
381  * Lowers <i>e</i>.
382  */
383 static VALUE c_lower (VALUE self)
384 {
385         GET_OBJ (self, RbEvasObject, e);
386
387         evas_object_lower (e->real);
388
389         return Qnil;
390 }
391
392 /*
393  * call-seq:
394  *  e.stack_above(evasobject) => nil
395  *
396  * Positions <i>e</i> above <i>evasobject</i>.
397  */
398 static VALUE c_stack_above (VALUE self, VALUE target)
399 {
400         GET_OBJ (self, RbEvasObject, e);
401
402         CHECK_CLASS (target, cEvasObject);
403         GET_OBJ (target, RbEvasObject, t);
404
405         evas_object_stack_above (e->real, t->real);
406
407         return Qnil;
408 }
409
410 /*
411  * call-seq:
412  *  e.stack_below(evasobject) => nil
413  *
414  * Positions <i>e</i> below <i>evasobject</i>.
415  */
416 static VALUE c_stack_below (VALUE self, VALUE target)
417 {
418         GET_OBJ (self, RbEvasObject, e);
419
420         CHECK_CLASS (target, cEvasObject);
421         GET_OBJ (target, RbEvasObject, t);
422
423         evas_object_stack_below (e->real, t->real);
424
425         return Qnil;
426 }
427
428 /*
429  * call-seq:
430  *  e.above => evasobject
431  *
432  * Returns the <code>Evas::EvasObject</code> that's positioned above
433  * <i>e</i>.
434  */
435 static VALUE c_above_get (VALUE self)
436 {
437         Evas_Object *o;
438
439         GET_OBJ (self, RbEvasObject, e);
440
441         if (!(o = evas_object_above_get (e->real)))
442                 return Qnil;
443
444         return TO_EVAS_OBJECT (o);
445 }
446
447 /*
448  * call-seq:
449  *  e.below => evasobject
450  *
451  * Returns the <code>Evas::EvasObject</code> that's positioned below
452  * <i>e</i>.
453  */
454 static VALUE c_below_get (VALUE self)
455 {
456         Evas_Object *o;
457
458         GET_OBJ (self, RbEvasObject, e);
459
460         if (!(o = evas_object_below_get (e->real)))
461                 return Qnil;
462
463         return TO_EVAS_OBJECT (o);
464 }
465
466 void Init_EvasObject (void)
467 {
468         cEvasObject = rb_define_class_under (mEvas, "EvasObject",
469                                              rb_cObject);
470
471         rb_define_private_method (rb_singleton_class (cEvasObject),
472                                   "new", NULL, 0);
473         rb_define_method (cEvasObject, "initialize", c_init, 1);
474         rb_define_method (cEvasObject, "inspect", c_inspect, 0);
475         rb_define_method (cEvasObject, "delete", c_delete, 0);
476         rb_define_method (cEvasObject, "resize", c_resize, 2);
477         rb_define_method (cEvasObject, "move", c_move, 2);
478         rb_define_method (cEvasObject, "geometry", c_geometry_get, 0);
479         rb_define_method (cEvasObject, "show", c_show, 0);
480         rb_define_method (cEvasObject, "hide", c_hide, 0);
481         rb_define_method (cEvasObject, "visible?", c_visible_get, 0);
482         rb_define_method (cEvasObject, "evas", c_evas_get, 0);
483         rb_define_method (cEvasObject, "name", c_name_get, 0);
484         rb_define_method (cEvasObject, "name=", c_name_set, 1);
485         rb_define_method (cEvasObject, "layer", c_layer_get, 0);
486         rb_define_method (cEvasObject, "layer=", c_layer_set, 1);
487         rb_define_method (cEvasObject, "get_color", c_get_color, 0);
488         rb_define_method (cEvasObject, "set_color", c_set_color, 4);
489         rb_define_method (cEvasObject, "pass_events?", c_pass_events_get, 0);
490         rb_define_method (cEvasObject, "pass_events=", c_pass_events_set, 1);
491         rb_define_method (cEvasObject, "raise", c_raise, 0);
492         rb_define_method (cEvasObject, "lower", c_lower, 0);
493         rb_define_method (cEvasObject, "stack_above", c_stack_above, 1);
494         rb_define_method (cEvasObject, "stack_below", c_stack_below, 1);
495         rb_define_method (cEvasObject, "above", c_above_get, 0);
496         rb_define_method (cEvasObject, "below", c_below_get, 0);
497 }