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