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