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