Removed RCS-style IDs.
[ruby-evas.git] / src / rb_evas_object.c
1 /*
2  * Copyright (C) 2004 Tilman Sauerbeck (tilman at code-monkey de)
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #include <ruby.h>
20 #include <stdbool.h>
21
22 #include <Evas.h>
23
24 #define __RB_EVAS_OBJECT_C
25 #include "rb_evas_main.h"
26 #include "rb_evas.h"
27 #include "rb_evas_object.h"
28
29 VALUE cEvasObject;
30
31 VALUE TO_EVAS_OBJECT (Evas_Object *o)
32 {
33         void *obj;
34
35         if (!o)
36                 return Qnil;
37
38         if (!(obj = evas_object_data_get (o, RUBY_EVAS_OBJECT_KEY))) {
39                 rb_raise (rb_eException, "EvasObject Ruby object key missing");
40                 return Qnil;
41         }
42
43         return (VALUE) obj;
44 }
45
46 /* called by the child classes */
47 void c_evas_object_mark (RbEvasObject *e)
48 {
49         rb_gc_mark (e->parent);
50
51         if (!NIL_P (e->callbacks))
52                 rb_gc_mark (e->callbacks);
53
54         if (!NIL_P (e->userdata))
55                 rb_gc_mark (e->userdata);
56 }
57
58 void c_evas_object_free (RbEvasObject *e, bool free_mem)
59 {
60         if (e->real)
61                 evas_object_del (e->real);
62
63         if (free_mem)
64                 free (e);
65 }
66
67 static void c_free (RbEvasObject *e)
68 {
69         c_evas_object_free (e, true);
70 }
71
72 static VALUE c_alloc (VALUE klass)
73 {
74         RbEvasObject *e;
75
76         return Data_Make_Struct (klass, RbEvasObject, c_evas_object_mark,
77                                  c_free, e);
78 }
79
80 /* :nodoc: */
81 static VALUE c_init (VALUE self, VALUE parent)
82 {
83         GET_OBJ (self, RbEvasObject, e);
84
85         evas_object_data_set (e->real, RUBY_EVAS_OBJECT_KEY, (void *) self);
86
87         e->parent = parent;
88         e->callbacks = Qnil;
89
90         return self;
91 }
92
93 /* :nodoc: */
94 static VALUE c_inspect (VALUE self)
95 {
96         INSPECT (self, RbEvasObject);
97 }
98
99 static VALUE c_type_get (VALUE self)
100 {
101         const char *s;
102
103         GET_OBJ (self, RbEvasObject, e);
104
105         s = evas_object_type_get (e->real);
106
107         return s ? rb_str_new2 (s) : Qnil;
108 }
109
110 /*
111  * call-seq:
112  *  e.delete => nil
113  *
114  * Deletes <i>e</i>.
115  */
116 static VALUE c_delete (VALUE self)
117 {
118         GET_OBJ (self, RbEvasObject, e);
119
120         evas_object_del (e->real);
121         e->real = NULL;
122
123         return Qnil;
124 }
125
126 /*
127  * call-seq:
128  *  e.resize(width, height) => nil
129  *
130  * Resizes <i>e</i> to width x height.
131  *
132  *  e.resize(100, 200) #=> nil
133  */
134 static VALUE c_resize (VALUE self, VALUE w, VALUE h)
135 {
136         GET_OBJ (self, RbEvasObject, e);
137
138         Check_Type (w, T_FIXNUM);
139         Check_Type (h, T_FIXNUM);
140
141         evas_object_resize (e->real, (Evas_Coord) FIX2INT (w),
142                             (Evas_Coord) FIX2INT (h));
143
144         return Qnil;
145 }
146
147 /*
148  * call-seq:
149  *  e.move(x, y) => nil
150  *
151  * Moves <i>e</i> to the coordinates specified in
152  * <i>x</i> and <i>y</i>.
153  *
154  *  e.move(100, 200) #=> nil
155  */
156 static VALUE c_move (VALUE self, VALUE x, VALUE y)
157 {
158         GET_OBJ (self, RbEvasObject, e);
159
160         Check_Type (x, T_FIXNUM);
161         Check_Type (y, T_FIXNUM);
162
163         evas_object_move (e->real, (Evas_Coord) FIX2INT (x),
164                           (Evas_Coord) FIX2INT (y));
165
166         return Qnil;
167 }
168
169 /*
170  * call-seq:
171  *  e.geometry => array
172  *
173  * Returns an array containing the geometry of <i>e</i>.
174  *
175  *  e.move(150, 300)   #=> nil
176  *  e.resize(200, 200) #=> nil
177  *  e.geometry         #=> [150, 300, 200, 200]
178  */
179 static VALUE c_geometry_get (VALUE self)
180 {
181         int x = 0, y = 0, w = 0, h = 0;
182
183         GET_OBJ (self, RbEvasObject, e);
184
185         evas_object_geometry_get (e->real,
186                                   (Evas_Coord *) &x, (Evas_Coord *) &y,
187                                   (Evas_Coord *) &w, (Evas_Coord *) &h);
188
189         return rb_ary_new3 (4, INT2FIX (x), INT2FIX (y), INT2FIX (w),
190                             INT2FIX (h));
191 }
192
193 /*
194  * call-seq:
195  *  e.show => nil
196  *
197  * Shows <i>e</i>.
198  */
199 static VALUE c_show (VALUE self)
200 {
201         GET_OBJ (self, RbEvasObject, e);
202
203         evas_object_show (e->real);
204
205         return Qnil;
206 }
207
208 /*
209  * call-seq:
210  *  e.hide => nil
211  *
212  * Hides <i>e</i>.
213  */
214 static VALUE c_hide (VALUE self)
215 {
216         GET_OBJ (self, RbEvasObject, e);
217
218         evas_object_hide (e->real);
219
220         return Qnil;
221 }
222
223 /*
224  * call-seq:
225  *  e.visible? => true or false
226  *
227  * Returns true if <i>e</i> is visible, else returns false.
228  */
229 static VALUE c_visible_get (VALUE self)
230 {
231         GET_OBJ (self, RbEvasObject, e);
232
233         return evas_object_visible_get (e->real) ? Qtrue : Qfalse;
234 }
235
236 /*
237  * call-seq:
238  *  e.focused? => true or false
239  *
240  * Returns true if <i>e</i> is focused, else returns false.
241  */
242 static VALUE c_focused_get (VALUE self)
243 {
244         GET_OBJ (self, RbEvasObject, e);
245
246         return evas_object_focus_get (e->real) ? Qtrue : Qfalse;
247 }
248
249 /*
250  * call-seq:
251  *  e.focused(true or false)
252  *
253  * (Un)focuses <i>e</i>.
254  */
255 static VALUE c_focused_set (VALUE self, VALUE val)
256 {
257         GET_OBJ (self, RbEvasObject, e);
258
259         CHECK_BOOL (val);
260
261         evas_object_focus_set (e->real, val == Qtrue);
262
263         return Qnil;
264 }
265
266 /*
267  * call-seq:
268  *  e.evas => evas
269  *
270  * Returns the <code>Evas::Evas</code> for <i>e</i>.
271  */
272 static VALUE c_evas_get (VALUE self)
273 {
274         GET_OBJ (self, RbEvasObject, e);
275
276         return e->parent;
277 }
278
279 /*
280  * call-seq:
281  *  e.name => string
282  *
283  * Returns the name of <i>e</i>.
284  */
285 static VALUE c_name_get (VALUE self)
286 {
287         const char *name;
288
289         GET_OBJ (self, RbEvasObject, e);
290
291         if (!(name = evas_object_name_get (e->real)))
292                 return Qnil;
293         else
294                 return rb_str_new2 (name);
295 }
296
297 /*
298  * call-seq:
299  *  e.name(string)
300  *
301  * Sets the name of <i>e</i>.
302  */
303 static VALUE c_name_set (VALUE self, VALUE val)
304 {
305         GET_OBJ (self, RbEvasObject, e);
306
307         evas_object_name_set (e->real, StringValuePtr (val));
308
309         return Qnil;
310 }
311
312 /*
313  * call-seq:
314  *  e.layer => fixnum
315  *
316  * Returns the layer <i>e</i> is in.
317  */
318 static VALUE c_layer_get (VALUE self)
319 {
320         GET_OBJ (self, RbEvasObject, e);
321
322         return INT2FIX (evas_object_layer_get (e->real));
323 }
324
325 /*
326  * call-seq:
327  *  e.layer(fixnum)
328  *
329  * Sets the layer <i>e</i> is in.
330  */
331 static VALUE c_layer_set (VALUE self, VALUE val)
332 {
333         GET_OBJ (self, RbEvasObject, e);
334
335         Check_Type (val, T_FIXNUM);
336
337         evas_object_layer_set (e->real, NUM2INT (val));
338
339         return Qnil;
340 }
341
342 /*
343  * call-seq:
344  *  e.get_color => array
345  *
346  * Returns the color of <i>e</i>.
347  *
348  *  e.set_color(128, 128, 128, 0) #=> nil
349  *  e.get_color                   #=> [128, 128, 128, 0]
350  */
351 static VALUE c_get_color (VALUE self)
352 {
353         int r = 0, g = 0, b = 0, a = 0;
354
355         GET_OBJ (self, RbEvasObject, e);
356
357         evas_object_color_get (e->real, &r, &g, &b, &a);
358
359         return rb_ary_new3 (4, INT2FIX (r), INT2FIX (g), INT2FIX (b),
360                             INT2FIX (a));
361 }
362
363 /*
364  * call-seq:
365  *  e.set_color(r, g, b, a) => nil
366  *
367  * Sets the color of <i>e</i>.
368  *
369  *  e.set_color(128, 128, 128, 0) #=> nil
370  */
371 static VALUE c_set_color (VALUE self, VALUE r, VALUE g, VALUE b,
372                           VALUE a)
373 {
374         GET_OBJ (self, RbEvasObject, e);
375
376         Check_Type (r, T_FIXNUM);
377         Check_Type (g, T_FIXNUM);
378         Check_Type (b, T_FIXNUM);
379         Check_Type (a, T_FIXNUM);
380
381         evas_object_color_set (e->real, FIX2INT (r), FIX2INT (g),
382                                FIX2INT (b), FIX2INT (a));
383
384         return Qnil;
385 }
386
387 static VALUE c_get_clip (VALUE self)
388 {
389         GET_OBJ (self, RbEvasObject, e);
390
391         return TO_EVAS_OBJECT (evas_object_clip_get (e->real));
392 }
393
394 static VALUE c_set_clip (VALUE self, VALUE clip)
395 {
396         GET_OBJ (self, RbEvasObject, e);
397         GET_OBJ (clip, RbEvasObject, e2);
398
399         evas_object_clip_set (e->real, e2->real);
400
401         return Qnil;
402 }
403
404 /*
405  * call-seq:
406  *  e.pass_events? => true or false
407  *
408  * Returns true if <i>e</i> passes events on to EvasObjects that are
409  * below itself, else returns false.
410  */
411 static VALUE c_pass_events_get (VALUE self)
412 {
413         GET_OBJ (self, RbEvasObject, e);
414
415         return evas_object_pass_events_get (e->real) ? Qtrue : Qfalse;
416 }
417
418 /*
419  * call-seq:
420  *  e.pass_events(true or false)
421  *
422  * Sets whether <i>e</i> passes events on to EvasObjects that are
423  * below itself.
424  */
425 static VALUE c_pass_events_set (VALUE self, VALUE val)
426 {
427         GET_OBJ (self, RbEvasObject, e);
428
429         CHECK_BOOL (val);
430
431         evas_object_pass_events_set (e->real, val == Qtrue);
432
433         return Qnil;
434 }
435
436 /*
437  * call-seq:
438  *  e.repeat_events? => true or false
439  *
440  * Returns true if <i>e</i> repeats events to EvasObjects that are
441  * below itself, else returns false.
442  */
443 static VALUE c_repeat_events_get (VALUE self)
444 {
445         GET_OBJ (self, RbEvasObject, e);
446
447         return evas_object_repeat_events_get (e->real) ? Qtrue : Qfalse;
448 }
449
450 /*
451  * call-seq:
452  *  e.repeat_events(true or false)
453  *
454  * Sets whether <i>e</i> repeats events to EvasObjects that are
455  * below itself.
456  */
457 static VALUE c_repeat_events_set (VALUE self, VALUE val)
458 {
459         GET_OBJ (self, RbEvasObject, e);
460
461         CHECK_BOOL (val);
462
463         evas_object_repeat_events_set (e->real, val == Qtrue);
464
465         return Qnil;
466 }
467
468 static VALUE c_anti_alias_get (VALUE self)
469 {
470         GET_OBJ (self, RbEvasObject, e);
471
472         return evas_object_anti_alias_get (e->real) ? Qtrue : Qfalse;
473 }
474
475 static VALUE c_anti_alias_set (VALUE self, VALUE val)
476 {
477         GET_OBJ (self, RbEvasObject, e);
478
479         CHECK_BOOL (val);
480
481         evas_object_anti_alias_set (e->real, val == Qtrue);
482
483         return Qnil;
484 }
485
486 /*
487  * call-seq:
488  *   e.raise => nil
489  *
490  * Raises <i>e</i>.
491  */
492 static VALUE c_raise (VALUE self)
493 {
494         GET_OBJ (self, RbEvasObject, e);
495
496         evas_object_raise (e->real);
497
498         return Qnil;
499 }
500
501 /*
502  * call-seq:
503  *  e.lower => nil
504  *
505  * Lowers <i>e</i>.
506  */
507 static VALUE c_lower (VALUE self)
508 {
509         GET_OBJ (self, RbEvasObject, e);
510
511         evas_object_lower (e->real);
512
513         return Qnil;
514 }
515
516 /*
517  * call-seq:
518  *  e.stack_above(evasobject) => nil
519  *
520  * Positions <i>e</i> above <i>evasobject</i>.
521  */
522 static VALUE c_stack_above (VALUE self, VALUE target)
523 {
524         GET_OBJ (self, RbEvasObject, e);
525
526         CHECK_CLASS (target, cEvasObject);
527         GET_OBJ (target, RbEvasObject, t);
528
529         evas_object_stack_above (e->real, t->real);
530
531         return Qnil;
532 }
533
534 /*
535  * call-seq:
536  *  e.stack_below(evasobject) => nil
537  *
538  * Positions <i>e</i> below <i>evasobject</i>.
539  */
540 static VALUE c_stack_below (VALUE self, VALUE target)
541 {
542         GET_OBJ (self, RbEvasObject, e);
543
544         CHECK_CLASS (target, cEvasObject);
545         GET_OBJ (target, RbEvasObject, t);
546
547         evas_object_stack_below (e->real, t->real);
548
549         return Qnil;
550 }
551
552 /*
553  * call-seq:
554  *  e.above => evasobject
555  *
556  * Returns the <code>Evas::EvasObject</code> that's positioned above
557  * <i>e</i>.
558  */
559 static VALUE c_above_get (VALUE self)
560 {
561         GET_OBJ (self, RbEvasObject, e);
562
563         return TO_EVAS_OBJECT (evas_object_above_get (e->real));
564 }
565
566 /*
567  * call-seq:
568  *  e.below => evasobject
569  *
570  * Returns the <code>Evas::EvasObject</code> that's positioned below
571  * <i>e</i>.
572  */
573 static VALUE c_below_get (VALUE self)
574 {
575         GET_OBJ (self, RbEvasObject, e);
576
577         return TO_EVAS_OBJECT (evas_object_below_get (e->real));
578 }
579
580 static VALUE c_userdata_get (VALUE self)
581 {
582         GET_OBJ (self, RbEvasObject, e);
583
584         if (NIL_P (e->userdata))
585                 e->userdata = rb_hash_new ();
586
587         return e->userdata;
588 }
589
590 void Init_EvasObject (void)
591 {
592         cEvasObject = rb_define_class_under (mEvas, "EvasObject",
593                                              rb_cObject);
594
595         rb_define_alloc_func (cEvasObject, c_alloc);
596         rb_define_method (cEvasObject, "initialize", c_init, 1);
597         rb_define_method (cEvasObject, "inspect", c_inspect, 0);
598         rb_define_method (cEvasObject, "type", c_type_get, 0);
599         rb_define_method (cEvasObject, "delete", c_delete, 0);
600         rb_define_method (cEvasObject, "resize", c_resize, 2);
601         rb_define_method (cEvasObject, "move", c_move, 2);
602         rb_define_method (cEvasObject, "geometry", c_geometry_get, 0);
603         rb_define_method (cEvasObject, "show", c_show, 0);
604         rb_define_method (cEvasObject, "hide", c_hide, 0);
605         rb_define_method (cEvasObject, "visible?", c_visible_get, 0);
606         rb_define_method (cEvasObject, "focused?", c_focused_get, 0);
607         rb_define_method (cEvasObject, "focused=", c_focused_set, 1);
608         rb_define_method (cEvasObject, "evas", c_evas_get, 0);
609         rb_define_method (cEvasObject, "name", c_name_get, 0);
610         rb_define_method (cEvasObject, "name=", c_name_set, 1);
611         rb_define_method (cEvasObject, "layer", c_layer_get, 0);
612         rb_define_method (cEvasObject, "layer=", c_layer_set, 1);
613         rb_define_method (cEvasObject, "get_color", c_get_color, 0);
614         rb_define_method (cEvasObject, "set_color", c_set_color, 4);
615         rb_define_method (cEvasObject, "clip", c_get_clip, 0);
616         rb_define_method (cEvasObject, "clip=", c_set_clip, 1);
617         rb_define_method (cEvasObject, "pass_events?",
618                           c_pass_events_get, 0);
619         rb_define_method (cEvasObject, "pass_events=",
620                           c_pass_events_set, 1);
621         rb_define_method (cEvasObject, "repeat_events?",
622                           c_repeat_events_get, 0);
623         rb_define_method (cEvasObject, "repeat_events=",
624                           c_repeat_events_set, 1);
625         rb_define_method (cEvasObject, "anti_alias?", c_anti_alias_get, 0);
626         rb_define_method (cEvasObject, "anti_alias=", c_anti_alias_set, 1);
627         rb_define_method (cEvasObject, "raise", c_raise, 0);
628         rb_define_method (cEvasObject, "lower", c_lower, 0);
629         rb_define_method (cEvasObject, "stack_above", c_stack_above, 1);
630         rb_define_method (cEvasObject, "stack_below", c_stack_below, 1);
631         rb_define_method (cEvasObject, "above", c_above_get, 0);
632         rb_define_method (cEvasObject, "below", c_below_get, 0);
633         rb_define_method (cEvasObject, "userdata", c_userdata_get, 0);
634 }