Minor doc improvements.
[ruby-ecore.git] / src / ecore_evas / rb_ecore_evas.c
1 /*
2  * $Id: rb_ecore_evas.c 65 2004-08-12 19:37:37Z 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 <Ecore.h>
25 #include <Ecore_Evas.h>
26 #include <evas/rb_evas.h>
27
28 #define __RB_ECORE_EVAS_C
29 #include "../ecore/rb_ecore.h"
30 #include "rb_ecore_evas_main.h"
31 #include "rb_ecore_evas.h"
32
33 #define CALLBACK_DEFINE_HANDLER(name) \
34         static void on_##name (Ecore_Evas *real) \
35         { \
36                 VALUE self = rb_hash_aref (objects, INT2NUM ((long) real)); \
37                 VALUE cb; \
38 \
39                 GET_OBJ (self, RbEcoreEvas, ee); \
40 \
41                 cb = rb_hash_aref (ee->callbacks, rb_str_new2 (#name)); \
42                 rb_funcall (cb, rb_intern ("call"), 0); \
43         } \
44
45 #define CALLBACK_REG_HANDLER(name) \
46                 GET_OBJ (self, RbEcoreEvas, ee); \
47 \
48                 if (!rb_block_given_p ()) \
49                         return Qnil; \
50 \
51                 rb_hash_aset (ee->callbacks, rb_str_new2 (#name), \
52                               rb_block_proc ()); \
53 \
54                 ecore_evas_callback_##name##_set (ee->real, on_##name); \
55 \
56                 return Qnil;
57
58 VALUE cEcoreEvas;
59 static VALUE objects;
60
61 /* called by the child classes */
62 void c_ecore_evas_mark (RbEcoreEvas *ee)
63 {
64         if (!NIL_P (ee->evas))
65                 rb_gc_mark (ee->evas);
66
67         rb_gc_mark (ee->callbacks);
68 }
69
70 void c_ecore_evas_free (RbEcoreEvas *ee, bool free_mem)
71 {
72         if (ee->real)
73                 ecore_evas_free (ee->real);
74
75         ecore_evas_shutdown ();
76         ecore_shutdown ();
77
78         if (free_mem)
79                 free (ee);
80 }
81
82 /* :nodoc: */
83 static VALUE c_init (int argc, VALUE *argv, VALUE self)
84 {
85         GET_OBJ (self, RbEcoreEvas, ee);
86
87         ee->evas = Qnil;
88         ee->callbacks = rb_hash_new ();
89
90         rb_hash_aset (objects, INT2NUM ((long) ee->real), self);
91
92         return Qnil;
93 }
94
95 /* :nodoc: */
96 static VALUE c_inspect (VALUE self)
97 {
98         INSPECT (self, RbEcoreEvas);
99 }
100
101 /*
102  * call-seq:
103  *  ee.show => nil
104  *
105  * Shows <i>ee</i>.
106  */
107 static VALUE c_show (VALUE self)
108 {
109         GET_OBJ (self, RbEcoreEvas, ee);
110
111         ecore_evas_show (ee->real);
112
113         return Qnil;
114 }
115
116 /*
117  * call-seq:
118  *  ee.hide => nil
119  *
120  * Hides <i>ee</i>.
121  */
122 static VALUE c_hide (VALUE self)
123 {
124         GET_OBJ (self, RbEcoreEvas, ee);
125
126         ecore_evas_hide (ee->real);
127
128         return Qnil;
129 }
130
131 /*
132  * call-seq:
133  *  ee.visible? => true or false
134  *
135  * Returns true if <i>ee</i> is visible, else returns false.
136  */
137 static VALUE c_visible_get (VALUE self)
138 {
139         GET_OBJ (self, RbEcoreEvas, ee);
140
141         return ecore_evas_visibility_get (ee->real) ? Qtrue : Qfalse;
142 }
143
144 /*
145  * call-seq:
146  *   ee.raise => nil
147  *
148  * Raises <i>ee</i>.
149  */
150 static VALUE c_raise (VALUE self)
151 {
152         GET_OBJ (self, RbEcoreEvas, ee);
153
154         ecore_evas_raise (ee->real);
155
156         return Qnil;
157 }
158
159 /*
160  * call-seq:
161  *  ee.lower => nil
162  *
163  * Lowers <i>ee</i>.
164  */
165 static VALUE c_lower (VALUE self)
166 {
167         GET_OBJ (self, RbEcoreEvas, ee);
168
169         ecore_evas_lower (ee->real);
170
171         return Qnil;
172 }
173
174 /*
175  * call-seq:
176  *   ee.layer => fixnum
177  *
178  * Returns the layer of <i>ee</i>.
179  */
180 static VALUE c_layer_get (VALUE self)
181 {
182         GET_OBJ (self, RbEcoreEvas, ee);
183
184         return INT2FIX (ecore_evas_layer_get (ee->real));
185 }
186
187 /*
188  * call-seq:
189  *  ee.layer(fixnum) => fixnum
190  *
191  * Sets the layer of <i>ee</i>.
192  */
193 static VALUE c_layer_set (VALUE self, VALUE val)
194 {
195         GET_OBJ (self, RbEcoreEvas, ee);
196
197         Check_Type (val, T_FIXNUM);
198
199         ecore_evas_layer_set (ee->real, FIX2INT (val));
200
201         return Qnil;
202 }
203
204 /*
205  * call-seq:
206  *  ee.evas => evas
207  *
208  * Returns the <code>Evas::Evas</code> object for <i>ee</i>.
209  */
210 static VALUE c_evas_get (VALUE self)
211 {
212         GET_OBJ (self, RbEcoreEvas, ee);
213
214         if (NIL_P (ee->evas))
215                 ee->evas = TO_EVAS (self, ecore_evas_get (ee->real));
216
217         return ee->evas;
218 }
219
220 /*
221  * call-seq:
222  *  ee.geometry => array
223  *
224  * Returns an array containing the geometry of <i>ee</i>.
225  *
226  *  ee.move(150, 300)   #=> nil
227  *  ee.resize(200, 200) #=> nil
228  *  ee.geometry         #=> [150, 300, 200, 200]
229  */
230 static VALUE c_geometry_get (VALUE self)
231 {
232         int x = 0, y = 0, w = 0, h = 0;
233
234         GET_OBJ (self, RbEcoreEvas, ee);
235
236         ecore_evas_geometry_get (ee->real, &x, &y, &w, &h);
237
238         return rb_ary_new3 (4, INT2FIX (x), INT2FIX (y),
239                             INT2FIX (w), INT2FIX (h));
240 }
241
242 /*
243  * call-seq:
244  *  ee.get_size_min => array
245  *
246  * Returns an array containing the minimum size of <i>ee</i>.
247  *
248  *  ee.set_size_min(100, 200) #=> nil
249  *  ee.get_size_min           #=> [100, 200]
250  */
251 static VALUE c_get_size_min (VALUE self)
252 {
253         int w = 0, h = 0;
254
255         GET_OBJ (self, RbEcoreEvas, ee);
256
257         ecore_evas_size_min_get (ee->real, &w, &h);
258
259         return rb_ary_new3 (2, INT2FIX (w), INT2FIX (h));
260 }
261
262 /*
263  * call-seq:
264  *  ee.set_size_min(width, height) => nil
265  *
266  * Sets the minimum size of <i>ee</i>.
267  *
268  *  ee.set_size_min(100, 200) #=> nil
269  */
270 static VALUE c_set_size_min (VALUE self, VALUE w, VALUE h)
271 {
272         GET_OBJ (self, RbEcoreEvas, ee);
273
274         Check_Type (w, T_FIXNUM);
275         Check_Type (h, T_FIXNUM);
276
277         ecore_evas_size_min_set (ee->real, FIX2INT (w), FIX2INT (h));
278
279         return Qnil;
280 }
281
282 /*
283  * call-seq:
284  *  ee.get_size_max => array
285  *
286  * Returns an array containing the maximum size of <i>ee</i>.
287  *
288  *  ee.set_size_max(100, 200) #=> nil
289  *  ee.get_size_max           #=> [100, 200]
290  */
291 static VALUE c_get_size_max (VALUE self)
292 {
293         int w = 0, h = 0;
294
295         GET_OBJ (self, RbEcoreEvas, ee);
296
297         ecore_evas_size_max_get (ee->real, &w, &h);
298
299         return rb_ary_new3 (2, INT2FIX (w), INT2FIX (h));
300 }
301
302 /*
303  * call-seq:
304  *  ee.set_size_max(width, height) => nil
305  *
306  * Sets the maximum size of <i>ee</i>.
307  *
308  *  ee.set_size_max(100, 200) #=> nil
309  */
310 static VALUE c_set_size_max (VALUE self, VALUE w, VALUE h)
311 {
312         GET_OBJ (self, RbEcoreEvas, ee);
313
314         Check_Type (w, T_FIXNUM);
315         Check_Type (h, T_FIXNUM);
316
317         ecore_evas_size_max_set (ee->real, FIX2INT (w), FIX2INT (h));
318
319         return Qnil;
320 }
321
322 /*
323  * call-seq:
324  *  ee.move(x, y) => nil
325  *
326  * Moves <i>ee</i> to the coordinates specified in
327  * <i>x</i> and <i>y</i>.
328  *
329  *  ee.move(100, 200) #=> nil
330  */
331 static VALUE c_move (VALUE self, VALUE x, VALUE y)
332 {
333         GET_OBJ (self, RbEcoreEvas, ee);
334
335         Check_Type (x, T_FIXNUM);
336         Check_Type (y, T_FIXNUM);
337
338         ecore_evas_move (ee->real, FIX2INT (x), FIX2INT (y));
339
340         return Qnil;
341 }
342
343 /*
344  * call-seq:
345  *  ee.resize(width, height) => nil
346  *
347  * Resizes <i>ee</i> to width x height.
348  *
349  *  ee.resize(100, 200) #=> nil
350  */
351 static VALUE c_resize (VALUE self, VALUE w, VALUE h)
352 {
353         GET_OBJ (self, RbEcoreEvas, ee);
354
355         Check_Type (w, T_FIXNUM);
356         Check_Type (h, T_FIXNUM);
357
358         ecore_evas_resize (ee->real, FIX2INT (w), FIX2INT (h));
359
360         return Qnil;
361 }
362
363 /*
364  * call-seq:
365  *  ee.title => string
366  *
367  * Returns the title of <i>ee</i>.
368  */
369 static VALUE c_title_get (VALUE self)
370 {
371         const char *tmp;
372
373         GET_OBJ (self, RbEcoreEvas, ee);
374
375         if (!(tmp = ecore_evas_title_get (ee->real)))
376                 return Qnil;
377         else
378                 return rb_str_new2 (tmp);
379 }
380
381 /*
382  * call-seq:
383  *  ee.title(string)
384  *
385  * Sets the title of <i>ee</i>.
386  */
387 static VALUE c_title_set (VALUE self, VALUE val)
388 {
389         GET_OBJ (self, RbEcoreEvas, ee);
390
391         Check_Type (val, T_STRING);
392
393         ecore_evas_title_set (ee->real, StringValuePtr (val));
394
395         return Qnil;
396 }
397
398 /*
399  * call-seq:
400  *  ee.borderless? => true or false
401  *
402  * Returns true if <i>ee</i> is borderless, else returns false.
403  */
404 static VALUE c_borderless_get (VALUE self)
405 {
406         GET_OBJ (self, RbEcoreEvas, ee);
407
408         return ecore_evas_borderless_get (ee->real) ? Qtrue : Qfalse;
409 }
410
411 /*
412  * call-seq:
413  *  ee.borderless(true or false)
414  *
415  * Sets whether <i>ee</i> is borderless or not.
416  */
417 static VALUE c_borderless_set (VALUE self, VALUE val)
418 {
419         GET_OBJ (self, RbEcoreEvas, ee);
420
421         CHECK_BOOL (val);
422
423         ecore_evas_borderless_set (ee->real, val == Qtrue);
424
425         return Qnil;
426 }
427
428 /*
429  * call-seq:
430  *  ee.shaped? => true or false
431  *
432  * Returns true if <i>ee</i> is shaped, else returns false.
433  */
434 static VALUE c_shaped_get (VALUE self)
435 {
436         GET_OBJ (self, RbEcoreEvas, ee);
437
438         return ecore_evas_shaped_get (ee->real) ? Qtrue : Qfalse;
439 }
440
441 /*
442  * call-seq:
443  *  ee.shaped(true or false)
444  *
445  * Sets whether <i>ee</i> is shaped or not.
446  */
447 static VALUE c_shaped_set (VALUE self, VALUE val)
448 {
449         GET_OBJ (self, RbEcoreEvas, ee);
450
451         CHECK_BOOL (val);
452
453         ecore_evas_shaped_set (ee->real, val == Qtrue);
454
455         return Qnil;
456 }
457
458 /*
459  * call-seq:
460  *  ee.sticky? => true or false
461  *
462  * Returns true if <i>ee</i> is sticky, else returns false.
463  */
464 static VALUE c_sticky_get (VALUE self)
465 {
466         GET_OBJ (self, RbEcoreEvas, ee);
467
468         return ecore_evas_sticky_get (ee->real) ? Qtrue : Qfalse;
469 }
470
471 /*
472  * call-seq:
473  *  ee.sticky(true or false)
474  *
475  * Sets whether <i>ee</i> is sticky or not.
476  */
477 static VALUE c_sticky_set (VALUE self, VALUE val)
478 {
479         GET_OBJ (self, RbEcoreEvas, ee);
480
481         CHECK_BOOL (val);
482
483         ecore_evas_sticky_set (ee->real, val == Qtrue);
484
485         return Qnil;
486 }
487
488 /*
489  * call-seq:
490  *  ee.rotation => fixnum
491  *
492  * Returns the rotation of <i>ee</i>.
493  */
494 static VALUE c_rotation_get (VALUE self)
495 {
496         GET_OBJ (self, RbEcoreEvas, ee);
497
498         return INT2FIX (ecore_evas_rotation_get (ee->real));
499 }
500
501 /*
502  * call-seq:
503  *  ee.rotation(fixnum)
504  *
505  * Sets the rotation of <i>ee</i>.
506  */
507 static VALUE c_rotation_set (VALUE self, VALUE val)
508 {
509         GET_OBJ (self, RbEcoreEvas, ee);
510
511         Check_Type (val, T_FIXNUM);
512
513         ecore_evas_rotation_set (ee->real, FIX2INT (val));
514
515         return Qnil;
516 }
517
518 /* FIXME: this is unsafe!
519  * :nodoc:
520  */
521 static VALUE c_delete (VALUE self)
522 {
523         GET_OBJ (self, RbEcoreEvas, ee);
524
525         /* reap our children */
526         rb_gc_start ();
527
528         ecore_evas_free (ee->real);
529         rb_hash_aset (objects, INT2NUM ((long) ee->real), Qnil);
530         ee->real = NULL;
531
532         return Qnil;
533 }
534
535 CALLBACK_DEFINE_HANDLER (resize);
536 CALLBACK_DEFINE_HANDLER (move);
537 CALLBACK_DEFINE_HANDLER (show);
538 CALLBACK_DEFINE_HANDLER (hide);
539 CALLBACK_DEFINE_HANDLER (delete_request);
540 CALLBACK_DEFINE_HANDLER (destroy);
541 CALLBACK_DEFINE_HANDLER (focus_in);
542 CALLBACK_DEFINE_HANDLER (focus_out);
543 CALLBACK_DEFINE_HANDLER (mouse_in);
544 CALLBACK_DEFINE_HANDLER (mouse_out);
545 CALLBACK_DEFINE_HANDLER (pre_render);
546 CALLBACK_DEFINE_HANDLER (post_render);
547
548 /*
549  * call-seq:
550  *  ee.on_resize { block } => nil
551  *
552  * Sets the handler for the resize event.
553  */
554 static VALUE c_on_resize (VALUE self)
555 {
556         CALLBACK_REG_HANDLER (resize);
557 }
558
559 /*
560  * call-seq:
561  *  ee.on_move { block } => nil
562  *
563  * Sets the handler for the move event.
564  */
565 static VALUE c_on_move (VALUE self)
566 {
567         CALLBACK_REG_HANDLER (move);
568 }
569
570 /*
571  * call-seq:
572  *  ee.on_show { block } => nil
573  *
574  * Sets the handler for the show event.
575  */
576 static VALUE c_on_show (VALUE self)
577 {
578         CALLBACK_REG_HANDLER (show);
579 }
580
581 /*
582  * call-seq:
583  *  ee.on_hide { block } => nil
584  *
585  * Sets the handler for the hide event.
586  */
587 static VALUE c_on_hide (VALUE self)
588 {
589         CALLBACK_REG_HANDLER (hide);
590 }
591
592 /*
593  * call-seq:
594  *  ee.on_delete_request { block } => nil
595  *
596  * Sets the handler for the delete request event.
597  */
598 static VALUE c_on_delete_request (VALUE self)
599 {
600         CALLBACK_REG_HANDLER (delete_request);
601 }
602
603 /*
604  * call-seq:
605  *  ee.on_destroy { block } => nil
606  *
607  * Sets the handler for the destroy event.
608  */
609 static VALUE c_on_destroy (VALUE self)
610 {
611         CALLBACK_REG_HANDLER (destroy);
612 }
613
614 /*
615  * call-seq:
616  *  ee.on_focus_in { block } => nil
617  *
618  * Sets the handler for the focus in event.
619  */
620 static VALUE c_on_focus_in (VALUE self)
621 {
622         CALLBACK_REG_HANDLER (focus_in);
623 }
624
625 /*
626  * call-seq:
627  *  ee.on_focus_out { block } => nil
628  *
629  * Sets the handler for the focus out event.
630  */
631 static VALUE c_on_focus_out (VALUE self)
632 {
633         CALLBACK_REG_HANDLER (focus_out);
634 }
635
636 /*
637  * call-seq:
638  *  ee.on_mouse_in { block } => nil
639  *
640  * Sets the handler for the mouse in event.
641  */
642 static VALUE c_on_mouse_in (VALUE self)
643 {
644         CALLBACK_REG_HANDLER (mouse_in);
645 }
646
647 /*
648  * call-seq:
649  *  ee.on_mouse_out { block } => nil
650  *
651  * Sets the handler for the mouse out event.
652  */
653 static VALUE c_on_mouse_out (VALUE self)
654 {
655         CALLBACK_REG_HANDLER (mouse_out);
656 }
657
658 /*
659  * call-seq:
660  *  ee.on_pre_render { block } => nil
661  *
662  * Sets the handler for the pre render event.
663  */
664 static VALUE c_on_pre_render (VALUE self)
665 {
666         CALLBACK_REG_HANDLER (pre_render);
667 }
668
669 /*
670  * call-seq:
671  *  ee.on_post_render { block } => nil
672  *
673  * Sets the handler for the post render event.
674  */
675 static VALUE c_on_post_render (VALUE self)
676 {
677         CALLBACK_REG_HANDLER (post_render);
678 }
679
680 void Init_EcoreEvas (void)
681 {
682         cEcoreEvas = rb_define_class_under (mEvas, "EcoreEvas", rb_cObject);
683
684         rb_define_private_method (rb_singleton_class (cEcoreEvas),
685                                   "new", NULL, 0);
686         rb_define_method (cEcoreEvas, "initialize", c_init, -1);
687         rb_define_method (cEcoreEvas, "inspect", c_inspect, 0);
688         rb_define_method (cEcoreEvas, "delete", c_delete, 0);
689         rb_define_method (cEcoreEvas, "show", c_show, 0);
690         rb_define_method (cEcoreEvas, "hide", c_hide, 0);
691         rb_define_method (cEcoreEvas, "visible?", c_visible_get, 0);
692         rb_define_method (cEcoreEvas, "raise", c_raise, 0);
693         rb_define_method (cEcoreEvas, "lower", c_lower, 0);
694         rb_define_method (cEcoreEvas, "layer", c_layer_get, 0);
695         rb_define_method (cEcoreEvas, "layer=", c_layer_set, 1);
696         rb_define_method (cEcoreEvas, "evas", c_evas_get, 0);
697         rb_define_method (cEcoreEvas, "geometry", c_geometry_get, 0);
698         rb_define_method (cEcoreEvas, "get_size_min", c_get_size_min, 0);
699         rb_define_method (cEcoreEvas, "set_size_min", c_set_size_min, 2);
700         rb_define_method (cEcoreEvas, "get_size_max", c_get_size_max, 0);
701         rb_define_method (cEcoreEvas, "set_size_max", c_set_size_max, 2);
702         rb_define_method (cEcoreEvas, "move", c_move, 2);
703         rb_define_method (cEcoreEvas, "resize", c_resize, 2);
704         rb_define_method (cEcoreEvas, "title", c_title_get, 0);
705         rb_define_method (cEcoreEvas, "title=", c_title_set, 1);
706         rb_define_method (cEcoreEvas, "borderless?", c_borderless_get, 0);
707         rb_define_method (cEcoreEvas, "borderless=", c_borderless_set, 1);
708         rb_define_method (cEcoreEvas, "shaped?", c_shaped_get, 0);
709         rb_define_method (cEcoreEvas, "shaped=", c_shaped_set, 1);
710         rb_define_method (cEcoreEvas, "sticky?", c_sticky_get, 0);
711         rb_define_method (cEcoreEvas, "sticky=", c_sticky_set, 1);
712         rb_define_method (cEcoreEvas, "rotation", c_rotation_get, 0);
713         rb_define_method (cEcoreEvas, "rotation=", c_rotation_set, 1);
714
715         rb_define_method (cEcoreEvas, "on_resize", c_on_resize, 0);
716         rb_define_method (cEcoreEvas, "on_move", c_on_move, 0);
717         rb_define_method (cEcoreEvas, "on_show", c_on_show, 0);
718         rb_define_method (cEcoreEvas, "on_hide", c_on_hide, 0);
719         rb_define_method (cEcoreEvas, "on_delete_request", c_on_delete_request, 0);
720         rb_define_method (cEcoreEvas, "on_destroy", c_on_destroy, 0);
721         rb_define_method (cEcoreEvas, "on_focus_in", c_on_focus_in, 0);
722         rb_define_method (cEcoreEvas, "on_focus_out", c_on_focus_out, 0);
723         rb_define_method (cEcoreEvas, "on_mouse_in", c_on_mouse_in, 0);
724         rb_define_method (cEcoreEvas, "on_mouse_out", c_on_mouse_out, 0);
725         rb_define_method (cEcoreEvas, "on_pre_render", c_on_pre_render, 0);
726         rb_define_method (cEcoreEvas, "on_post_render", c_on_post_render, 0);
727
728         objects = rb_hash_new ();
729         rb_global_variable (&objects);
730 }