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