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