Implemented 'on_signal'.
[ruby-edje.git] / src / rb_edje.c
1 /*
2  * $Id: rb_edje.c 21 2004-06-25 17:41:17Z 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 #include "rb_edje_main.h"
28
29 #define GET_OBJ(obj, type, o, desc) \
30         type **(o) = NULL; \
31 \
32         Data_Get_Struct ((obj), type *, (o)); \
33 \
34         if (!*(o)) { \
35                 rb_raise (rb_eException, desc " destroyed already"); \
36                 return Qnil; \
37         }
38
39 #define CHECK_BOOL(val) \
40         if (TYPE ((val)) != T_TRUE && TYPE ((val)) != T_FALSE) { \
41                 rb_raise (rb_eTypeError, \
42                           "wrong argument type %s (expected true or false)", \
43                           rb_obj_classname ((val))); \
44                 return Qnil; \
45         }
46
47 VALUE cEdje;
48
49 static VALUE c_new (VALUE klass, VALUE evas)
50 {
51         VALUE self, argv[1];
52         Evas_Object **edje;
53
54         GET_OBJ (evas, Evas, e, "Evas");
55
56         self = Data_Make_Struct (klass, Evas_Object *, c_evas_object_mark,
57                                  c_evas_object_free, edje);
58         *edje = edje_object_add (*e);
59
60         argv[0] = evas;
61         rb_obj_call_init (self, 1, argv);
62
63         return self;
64 }
65
66 static VALUE c_load (VALUE self, VALUE eet, VALUE group)
67 {
68         GET_OBJ (self, Evas_Object, e, "Edje");
69
70         Check_Type (eet, T_STRING);
71         Check_Type (group, T_STRING);
72
73         if (!edje_object_file_set (*e, StringValuePtr (eet),
74                                    StringValuePtr (group)))
75                 rb_raise (rb_eException, "Cannot load eet");
76
77         return Qnil;
78 }
79
80 static VALUE c_get_size_min (VALUE self)
81 {
82         int w = 0, h = 0;
83
84         GET_OBJ (self, Evas_Object, e, "Edje");
85
86         edje_object_size_min_get (*e, &w, &h);
87
88         return rb_ary_new3 (2, INT2FIX (w), INT2FIX (h));
89 }
90
91 static VALUE c_get_size_max (VALUE self)
92 {
93         int w = 0, h = 0;
94
95         GET_OBJ (self, Evas_Object, e, "Edje");
96
97         edje_object_size_max_get (*e, &w, &h);
98
99         return rb_ary_new3 (2, INT2FIX (w), INT2FIX (h));
100 }
101
102 static VALUE c_part_exists_get (VALUE self, VALUE part)
103 {
104         int r;
105
106         GET_OBJ (self, Evas_Object, e, "Edje");
107
108         Check_Type (part, T_STRING);
109
110         r = edje_object_part_exists (*e, StringValuePtr (part));
111
112         return r ? Qtrue : Qfalse;
113 }
114
115 static VALUE c_part_swallow (VALUE self, VALUE part, VALUE target)
116 {
117         GET_OBJ (self, Evas_Object, e, "Edje");
118
119         Check_Type (part, T_STRING);
120
121         if (!rb_obj_is_kind_of (target, cEvasObject)) {
122                 rb_raise (rb_eTypeError,
123                           "wrong argument type %s (expected EvasObject)",
124                           rb_obj_classname (target));
125                 return Qnil;
126         }
127
128         GET_OBJ (target, Evas_Object, target2, "EvasObject");
129
130         edje_object_part_swallow (*e, StringValuePtr (part), *target2);
131
132         return Qnil;
133 }
134
135 static VALUE c_part_unswallow (VALUE self, VALUE target)
136 {
137         GET_OBJ (self, Evas_Object, e, "Edje");
138
139         if (!rb_obj_is_kind_of (target, cEvasObject)) {
140                 rb_raise (rb_eTypeError,
141                           "wrong argument type %s (expected EvasObject)",
142                           rb_obj_classname (target));
143                 return Qnil;
144         }
145
146         GET_OBJ (target, Evas_Object, target2, "EvasObject");
147
148         edje_object_part_unswallow (*e, *target2);
149
150         return Qnil;
151 }
152
153 static VALUE c_get_part_swallow (VALUE self, VALUE part)
154 {
155         Evas_Object *o;
156         void *obj;
157
158         GET_OBJ (self, Evas_Object, e, "Edje");
159
160         Check_Type (part, T_STRING);
161
162         if (!(o = edje_object_part_swallow_get (*e, StringValuePtr (part))))
163                 return Qnil;
164
165         if (!(obj = evas_object_data_get (o, RUBY_EVAS_OBJECT_KEY))) {
166                 rb_raise (rb_eException, "EvasObject Ruby object key missing");
167                 return Qnil;
168         }
169
170         return (VALUE) obj;
171 }
172
173 static VALUE c_get_part_text (VALUE self, VALUE part)
174 {
175         const char *s;
176
177         GET_OBJ (self, Evas_Object, e, "Edje");
178
179         Check_Type (part, T_STRING);
180
181         if (!(s = edje_object_part_text_get (*e, StringValuePtr (part))))
182                 return Qnil;
183         else
184                 return rb_str_new2 (s);
185 }
186
187 static VALUE c_set_part_text (VALUE self, VALUE part, VALUE text)
188 {
189         GET_OBJ (self, Evas_Object, e, "Edje");
190
191         Check_Type (part, T_STRING);
192         Check_Type (text, T_STRING);
193
194         edje_object_part_text_set (*e, StringValuePtr (part),
195                                    StringValuePtr (text));
196
197         return Qnil;
198 }
199
200 static void on_text_changed (void *data, Evas_Object *e,
201                              const char *part)
202 {
203         rb_funcall ((VALUE) data, rb_intern ("call"), 1,
204                     rb_str_new2 (part));
205 }
206
207 static VALUE c_on_text_changed (VALUE self)
208 {
209         GET_OBJ (self, Evas_Object, e, "Edje");
210
211         if (!rb_block_given_p ())
212                 return Qnil;
213
214         edje_object_text_change_cb_set (*e, on_text_changed,
215                                         (void *) rb_block_proc ());
216
217         return Qnil;
218 }
219
220 static VALUE c_signal_emit (VALUE self, VALUE emission, VALUE source)
221 {
222         GET_OBJ (self, Evas_Object, e, "Edje");
223
224         Check_Type (emission, T_STRING);
225         Check_Type (source, T_STRING);
226
227         edje_object_signal_emit (*e, StringValuePtr (emission),
228                                  StringValuePtr (source));
229
230         return Qnil;
231 }
232
233 static void on_signal (void *data, Evas_Object *o,
234                        const char *emission, const char *src) {
235         rb_funcall ((VALUE) data, rb_intern ("call"), 1,
236                     rb_str_new2 (emission), rb_str_new2 (src));
237 }
238
239 static VALUE c_on_signal (VALUE self, VALUE signal, VALUE src)
240 {
241         GET_OBJ (self, Evas_Object, e, "Edje");
242
243         Check_Type (signal, T_STRING);
244         Check_Type (src, T_STRING);
245
246         if (!rb_block_given_p ())
247                 return Qnil;
248
249         edje_object_signal_callback_add (*e, StringValuePtr (signal),
250                                          StringValuePtr (src), on_signal,
251                                          (void *) rb_block_proc ());
252
253         return Qnil;
254 }
255
256 static VALUE c_play_get (VALUE self)
257 {
258         GET_OBJ (self, Evas_Object, e, "Edje");
259
260         return edje_object_play_get (*e) ? Qtrue : Qfalse;
261 }
262
263 static VALUE c_play_set (VALUE self, VALUE val)
264 {
265         GET_OBJ (self, Evas_Object, e, "Edje");
266
267         CHECK_BOOL(val);
268
269         edje_object_play_set (*e, val == Qtrue ? 1 : 0);
270
271         return Qnil;
272 }
273
274 static VALUE c_animation_get (VALUE self)
275 {
276         GET_OBJ (self, Evas_Object, e, "Edje");
277
278         return edje_object_animation_get (*e) ? Qtrue : Qfalse;
279 }
280
281 static VALUE c_animation_set (VALUE self, VALUE val)
282 {
283         GET_OBJ (self, Evas_Object, e, "Edje");
284
285         CHECK_BOOL(val);
286
287         edje_object_animation_set (*e, val == Qtrue ? 1 : 0);
288
289         return Qnil;
290 }
291
292 void Init_Edje (void)
293 {
294         cEdje = rb_define_class_under (mEdje, "Edje", cEvasObject);
295
296         rb_define_singleton_method (cEdje, "new", c_new, 1);
297         rb_define_method (cEdje, "load", c_load, 2);
298         rb_define_method (cEdje, "get_size_min", c_get_size_min, 0);
299         rb_define_method (cEdje, "get_size_max", c_get_size_max, 0);
300         rb_define_method (cEdje, "part_exists?", c_part_exists_get, 1);
301         rb_define_method (cEdje, "part_swallow", c_part_swallow, 2);
302         rb_define_method (cEdje, "part_unswallow", c_part_unswallow, 1);
303         rb_define_method (cEdje, "get_part_swallow", c_get_part_swallow, 1);
304         rb_define_method (cEdje, "get_part_text", c_get_part_text, 1);
305         rb_define_method (cEdje, "set_part_text", c_set_part_text, 2);
306         rb_define_method (cEdje, "on_text_changed", c_on_text_changed, 0);
307         rb_define_method (cEdje, "signal_emit", c_signal_emit, 1);
308         rb_define_method (cEdje, "on_signal", c_on_signal, 2);
309         rb_define_method (cEdje, "play?", c_play_get, 0);
310         rb_define_method (cEdje, "play=", c_play_set, 1);
311         rb_define_method (cEdje, "animation?", c_animation_get, 0);
312         rb_define_method (cEdje, "animation=", c_animation_set, 1);
313 }
314