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