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