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