Reworked the call system.
[ruby-edje.git] / src / rb_edje.c
1 /*
2  * $Id: rb_edje.c 48 2004-07-31 13:46:07Z 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
23 #include <Edje.h>
24 #include <rb_evas.h>
25 #include <rb_evas_object.h>
26
27 #define __RB_EDJE_C
28 #include "rb_edje_main.h"
29 #include "rb_part.h"
30
31 typedef struct {
32         Evas_Object *real;
33         VALUE parts;
34         VALUE callbacks;
35 } RbEdje;
36
37 VALUE cEdje;
38
39 static void c_mark (RbEdje *e)
40 {
41         c_evas_object_mark (&e->real);
42
43         rb_gc_mark (e->parts);
44         rb_gc_mark (e->callbacks);
45 }
46
47 static void c_free (RbEdje *e)
48 {
49         c_evas_object_free (&e->real);
50
51         edje_shutdown ();
52 }
53
54 static VALUE c_new (VALUE klass, VALUE evas)
55 {
56         VALUE self, argv[1];
57         RbEdje *edje;
58
59         CHECK_CLASS (evas, cEvas);
60         GET_OBJ (evas, Evas *, e);
61
62         edje_init ();
63
64         self = Data_Make_Struct (klass, RbEdje, c_mark, c_free, edje);
65
66         edje->real = edje_object_add (*e);
67         edje->parts = rb_hash_new ();
68         edje->callbacks = rb_ary_new ();
69
70         argv[0] = evas;
71         rb_obj_call_init (self, 1, argv);
72
73         return self;
74 }
75
76 static VALUE c_freeze (VALUE self)
77 {
78         GET_OBJ (self, RbEdje, e);
79
80         edje_object_freeze (e->real);
81
82         return Qnil;
83 }
84
85 static VALUE c_thaw (VALUE self)
86 {
87         GET_OBJ (self, RbEdje, e);
88
89         edje_object_thaw (e->real);
90
91         return Qnil;
92 }
93
94 static VALUE c_load (VALUE self, VALUE eet, VALUE group)
95 {
96         GET_OBJ (self, RbEdje, e);
97
98         Check_Type (eet, T_STRING);
99         Check_Type (group, T_STRING);
100
101         if (!edje_object_file_set (e->real, StringValuePtr (eet),
102                                    StringValuePtr (group)))
103                 rb_raise (rb_eException, "Cannot load eet");
104
105         return Qnil;
106 }
107
108 static VALUE c_get_size_min (VALUE self)
109 {
110         int w = 0, h = 0;
111
112         GET_OBJ (self, RbEdje, e);
113
114         edje_object_size_min_get (e->real, &w, &h);
115
116         return rb_ary_new3 (2, INT2FIX (w), INT2FIX (h));
117 }
118
119 static VALUE c_get_size_max (VALUE self)
120 {
121         int w = 0, h = 0;
122
123         GET_OBJ (self, RbEdje, e);
124
125         edje_object_size_max_get (e->real, &w, &h);
126
127         return rb_ary_new3 (2, INT2FIX (w), INT2FIX (h));
128 }
129
130 static VALUE c_part_exists_get (VALUE self, VALUE name)
131 {
132         int r;
133
134         GET_OBJ (self, RbEdje, e);
135
136         Check_Type (name, T_STRING);
137
138         r = edje_object_part_exists (e->real, StringValuePtr (name));
139
140         return r ? Qtrue : Qfalse;
141 }
142
143 static VALUE c_part_get (VALUE self, VALUE name)
144 {
145         VALUE part;
146
147         GET_OBJ (self, RbEdje, e);
148
149         Check_Type (name, T_STRING);
150
151         if (!edje_object_part_exists (e->real, StringValuePtr (name)))
152                 return Qnil;
153
154         if (NIL_P (part = rb_hash_aref (e->parts, name))) {
155                 part = TO_PART (self, name);
156                 rb_hash_aset (e->parts, name, part);
157         }
158
159         return part;
160 }
161
162 static void on_text_changed (void *data, Evas_Object *e,
163                              const char *part)
164 {
165         rb_funcall ((VALUE) data, rb_intern ("call"), 1,
166                     rb_str_new2 (part));
167 }
168
169 static VALUE c_on_text_changed (VALUE self)
170 {
171         GET_OBJ (self, RbEdje, e);
172
173         if (!rb_block_given_p ())
174                 return Qnil;
175
176         edje_object_text_change_cb_set (e->real, on_text_changed,
177                                         (void *) rb_block_proc ());
178
179         return Qnil;
180 }
181
182 static VALUE c_emit_signal (VALUE self, VALUE emission, VALUE source)
183 {
184         GET_OBJ (self, RbEdje, e);
185
186         Check_Type (emission, T_STRING);
187         Check_Type (source, T_STRING);
188
189         edje_object_signal_emit (e->real, StringValuePtr (emission),
190                                  StringValuePtr (source));
191
192         return Qnil;
193 }
194
195 static void on_signal (void *data, Evas_Object *o,
196                        const char *emission, const char *src)
197 {
198         rb_funcall ((VALUE) data, rb_intern ("call"), 2,
199                     rb_str_new2 (emission), rb_str_new2 (src));
200 }
201
202 static VALUE c_on_signal (VALUE self, VALUE signal, VALUE src)
203 {
204         VALUE cb;
205
206         GET_OBJ (self, RbEdje, e);
207
208         Check_Type (signal, T_STRING);
209         Check_Type (src, T_STRING);
210
211         if (!rb_block_given_p ())
212                 return Qnil;
213
214         cb = rb_block_proc ();
215         rb_ary_push (e->callbacks, cb);
216
217         edje_object_signal_callback_add (e->real, StringValuePtr (signal),
218                                          StringValuePtr (src), on_signal,
219                                          (void *) cb);
220
221         return Qnil;
222 }
223
224 static VALUE c_play_get (VALUE self)
225 {
226         GET_OBJ (self, RbEdje, e);
227
228         return edje_object_play_get (e->real) ? Qtrue : Qfalse;
229 }
230
231 static VALUE c_play_set (VALUE self, VALUE val)
232 {
233         GET_OBJ (self, RbEdje, e);
234
235         CHECK_BOOL(val);
236
237         edje_object_play_set (e->real, val == Qtrue);
238
239         return Qnil;
240 }
241
242 static VALUE c_animation_get (VALUE self)
243 {
244         GET_OBJ (self, RbEdje, e);
245
246         return edje_object_animation_get (e->real) ? Qtrue : Qfalse;
247 }
248
249 static VALUE c_animation_set (VALUE self, VALUE val)
250 {
251         GET_OBJ (self, RbEdje, e);
252
253         CHECK_BOOL(val);
254
255         edje_object_animation_set (e->real, val == Qtrue);
256
257         return Qnil;
258 }
259
260 void Init_Edje (void)
261 {
262         cEdje = rb_define_class_under (mEdje, "Edje", cEvasObject);
263
264         rb_define_singleton_method (cEdje, "new", c_new, 1);
265         rb_define_method (cEdje, "freeze", c_freeze, 0);
266         rb_define_method (cEdje, "thaw", c_thaw, 0);
267         rb_define_method (cEdje, "load", c_load, 2);
268         rb_define_method (cEdje, "get_size_min", c_get_size_min, 0);
269         rb_define_method (cEdje, "get_size_max", c_get_size_max, 0);
270         rb_define_method (cEdje, "part_exists?", c_part_exists_get, 1);
271         rb_define_method (cEdje, "part", c_part_get, 1);
272         rb_define_method (cEdje, "on_text_changed", c_on_text_changed, 0);
273         rb_define_method (cEdje, "emit_signal", c_emit_signal, 2);
274         rb_define_method (cEdje, "on_signal", c_on_signal, 2);
275         rb_define_method (cEdje, "play?", c_play_get, 0);
276         rb_define_method (cEdje, "play=", c_play_set, 1);
277         rb_define_method (cEdje, "animation?", c_animation_get, 0);
278         rb_define_method (cEdje, "animation=", c_animation_set, 1);
279 }