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