Added Ecore::Evas::EcoreEvas#has_alpha? and #has_alpha=.
[ruby-ecore.git] / src / ecore / rb_event_handler.c
1 /*
2  * $Id: rb_event_handler.c 373 2006-02-16 21:37:43Z tilman $
3  *
4  * Copyright (C) 2004 ruby-ecore team (see AUTHORS)
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
26 #define __RB_EVENT_HANDLER_C
27 #include "rb_ecore.h"
28 #include "rb_event_handler.h"
29
30 typedef struct {
31         Ecore_Event_Handler *real;
32         VALUE event_class;
33         VALUE callback;
34         bool deleted;
35 } RbEventHandler;
36
37 static int on_ecore_event (void *data, int type, void *event);
38 static VALUE c_ev_raise (VALUE klass, VALUE argv);
39
40 VALUE event_classes, cEcoreEvent;
41 static VALUE event_args;
42
43 static void c_mark (RbEventHandler *h)
44 {
45         rb_gc_mark (h->callback);
46 }
47
48 static void c_free (RbEventHandler *h)
49 {
50         if (h->real && !h->deleted)
51                 ecore_event_handler_del (h->real);
52
53         free (h);
54 }
55
56 static VALUE c_alloc (VALUE klass)
57 {
58         RbEventHandler *h = NULL;
59
60         return Data_Make_Struct (klass, RbEventHandler, c_mark, c_free, h);
61 }
62
63 static VALUE c_init (VALUE self, VALUE klass)
64 {
65         RbEventHandler *h = NULL;
66         int t;
67
68         if (!rb_block_given_p ())
69                 rb_raise (rb_eStandardError, "block missing");
70
71         Data_Get_Struct (self, RbEventHandler, h);
72
73         if (rb_obj_is_kind_of (klass, rb_cModule) != Qtrue)
74                 rb_raise (rb_eArgError, "invalid argument");
75
76         t = NUM2INT (rb_const_get (klass, rb_intern ("TYPE")));
77         if (t <= ECORE_EVENT_NONE)
78                 rb_raise (rb_eStandardError, "invalid event");
79
80         h->event_class = klass;
81         h->callback = rb_block_proc ();
82         h->deleted = false;
83         h->real = ecore_event_handler_add (t, on_ecore_event, h);
84
85         return self;
86 }
87
88 static VALUE c_delete (VALUE self)
89 {
90         RbEventHandler *h = NULL;
91
92         Data_Get_Struct (self, RbEventHandler, h);
93
94         if (h->real && !h->deleted) {
95                 ecore_event_handler_del (h->real);
96                 h->real = NULL;
97                 h->deleted = true;
98         } else
99                 rb_raise (rb_eException, "EventHandler already deleted!");
100
101         return Qnil;
102 }
103
104 static int on_ecore_event (void *data, int type, void *event)
105 {
106         RbEventHandler *h = data;
107         VALUE obj, tmp = (VALUE) event;
108
109         obj = rb_obj_alloc (h->event_class);
110
111         if (rb_respond_to (h->event_class, rb_intern ("raise")))
112                 rb_apply (obj, rb_intern ("initialize"), tmp);
113         else
114                 rb_obj_call_init (obj, 1, &tmp);
115
116         tmp = rb_funcall (h->callback, rb_intern ("call"), 1, obj);
117
118         /* if the block returned false, don't call the other
119          * event handlers
120          */
121         return (tmp != Qfalse);
122 }
123
124 VALUE c_ev_inherited (VALUE klass, VALUE child)
125 {
126         VALUE t;
127
128         t = INT2FIX (ecore_event_type_new ());
129         rb_hash_aset (event_classes, t, child);
130
131         rb_define_const (child, "TYPE", t);
132         rb_define_singleton_method (child, "raise", c_ev_raise, -2);
133
134         return Qnil;
135 }
136
137 static VALUE c_ev_init (int argc, VALUE *argv, VALUE self)
138 {
139         return self;
140 }
141
142 static void free_event_args (void *data, void *argv)
143 {
144         rb_ary_delete (event_args, (VALUE) argv);
145 }
146
147 static VALUE c_ev_raise (VALUE klass, VALUE argv)
148 {
149         VALUE t;
150
151         rb_ary_push (event_args, argv);
152
153         t = rb_const_get (klass, rb_intern ("TYPE"));
154         ecore_event_add (FIX2INT (t), (void *) argv, free_event_args, NULL);
155
156         return Qnil;
157 }
158
159 void Init_EventHandler (void)
160 {
161         VALUE cEventHandler;
162
163         cEventHandler = rb_define_class_under (mEcore, "EventHandler",
164                                                rb_cObject);
165
166         rb_define_alloc_func (cEventHandler, c_alloc);
167         rb_define_method (cEventHandler, "initialize", c_init, 1);
168         rb_define_method (cEventHandler, "delete", c_delete, 0);
169
170         event_classes = rb_hash_new ();
171         rb_global_variable (&event_classes);
172
173         event_args = rb_ary_new ();
174         rb_global_variable (&event_args);
175
176         /* define a base event class */
177         cEcoreEvent = rb_define_class_under (mEcore, "Event", rb_cObject);
178         rb_define_singleton_method (cEcoreEvent, "inherited", c_ev_inherited, 1);
179         rb_define_method (cEcoreEvent, "initialize", c_ev_init, -1);
180 }