Implemented Edje#send_message.
[ruby-edje.git] / src / rb_edje.c
1 /*
2  * $Id: rb_edje.c 139 2004-10-30 09: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 <evas/rb_evas.h>
25 #include <evas/rb_evas_object.h>
26
27 #define __RB_EDJE_C
28 #include "rb_edje.h"
29 #include "rb_edje_main.h"
30 #include "rb_part.h"
31 #include "rb_messages.h"
32
33 VALUE cEdje;
34
35 static void c_mark (RbEdje *e)
36 {
37         c_evas_object_mark (&e->real);
38
39         rb_gc_mark (e->parts);
40         rb_gc_mark (e->callbacks);
41
42         if (!NIL_P (e->on_text_changed_cb))
43                 rb_gc_mark (e->on_text_changed_cb);
44 }
45
46 static void c_free (RbEdje *e)
47 {
48         c_evas_object_free (&e->real, false);
49
50         edje_shutdown ();
51 }
52
53 /*
54  * call-seq:
55  *  Edje::Edje.new(evas) => edje
56  *
57  * Creates an Edje::Edje object.
58  */
59 static VALUE c_new (VALUE klass, VALUE evas)
60 {
61         VALUE self, argv[1];
62         RbEdje *edje = NULL;
63
64         CHECK_CLASS (evas, cEvas);
65         GET_OBJ (evas, RbEvas, e);
66
67         edje_init ();
68
69         self = Data_Make_Struct (klass, RbEdje, c_mark, c_free, edje);
70
71         edje->real.real = edje_object_add (e->real);
72         edje->parts = rb_hash_new ();
73         edje->callbacks = rb_ary_new ();
74         edje->on_text_changed_cb = Qnil;
75
76         argv[0] = evas;
77         rb_obj_call_init (self, 1, argv);
78
79         return self;
80 }
81
82 /*
83  * call-seq:
84  *  edje.freeze => nil
85  *
86  * Freezes <i>edje</i>.
87  */
88 static VALUE c_freeze (VALUE self)
89 {
90         GET_OBJ (self, RbEdje, e);
91
92         edje_object_freeze (e->real.real);
93
94         return Qnil;
95 }
96
97 /*
98  * call-seq:
99  *  edje.thaw => nil
100  *
101  * Thaws <i>edje</i>.
102  */
103 static VALUE c_thaw (VALUE self)
104 {
105         GET_OBJ (self, RbEdje, e);
106
107         edje_object_thaw (e->real.real);
108
109         return Qnil;
110 }
111
112 /*
113  * call-seq:
114  *  edje.load(eet, group) => nil
115  *
116  * Loads <i>eet</i> into <i>edje</i>. <i>group</i> is the
117  * name of the group to be displayed.
118  */
119 static VALUE c_load (VALUE self, VALUE eet, VALUE group)
120 {
121         GET_OBJ (self, RbEdje, e);
122
123         Check_Type (eet, T_STRING);
124         Check_Type (group, T_STRING);
125
126         if (!edje_object_file_set (e->real.real, StringValuePtr (eet),
127                                    StringValuePtr (group)))
128                 rb_raise (rb_eException, "Cannot load eet");
129
130         return Qnil;
131 }
132
133 /*
134  * call-seq:
135  *  edje.get_size_min => array
136  *
137  * Returns an array that contains the minimum size
138  * of <i>edje</i>.
139  */
140 static VALUE c_get_size_min (VALUE self)
141 {
142         int w = 0, h = 0;
143
144         GET_OBJ (self, RbEdje, e);
145
146         edje_object_size_min_get (e->real.real, &w, &h);
147
148         return rb_ary_new3 (2, INT2FIX (w), INT2FIX (h));
149 }
150
151 /*
152  * call-seq:
153  *  edje.get_size_max => array
154  *
155  * Returns an array that contains the maximum size
156  * of <i>edje</i>.
157  */
158 static VALUE c_get_size_max (VALUE self)
159 {
160         int w = 0, h = 0;
161
162         GET_OBJ (self, RbEdje, e);
163
164         edje_object_size_max_get (e->real.real, &w, &h);
165
166         return rb_ary_new3 (2, INT2FIX (w), INT2FIX (h));
167 }
168
169 /*
170  * call-seq:
171  *  edje.part_exists?(part) => true or false
172  *
173  * Returns true if <i>edje</i> has a part called <i>part</i>,
174  * else returns false.
175  */
176 static VALUE c_part_exists_get (VALUE self, VALUE name)
177 {
178         int r;
179
180         GET_OBJ (self, RbEdje, e);
181
182         Check_Type (name, T_STRING);
183
184         r = edje_object_part_exists (e->real.real, StringValuePtr (name));
185
186         return r ? Qtrue : Qfalse;
187 }
188
189 /*
190  * call-seq:
191  *  edje.part(part_name) => part
192  *
193  * Returns the <code>Edje::Part</code> object that corresponds to
194  * <i>part_name</i>. If there's no part with that name in <i>edje</i>,
195  * an exception is raised.
196  */
197 static VALUE c_part_get (VALUE self, VALUE name)
198 {
199         VALUE part;
200
201         GET_OBJ (self, RbEdje, e);
202         Check_Type (name, T_STRING);
203
204         if (!edje_object_part_exists (e->real.real, StringValuePtr (name))) {
205                 rb_raise (rb_eException, "Unknown part name");
206                 return Qnil;
207         }
208
209         if (NIL_P (part = rb_hash_aref (e->parts, name))) {
210                 part = TO_PART (self, name);
211                 rb_hash_aset (e->parts, name, part);
212         }
213
214         return part;
215 }
216
217 static void on_text_changed (void *data, Evas_Object *eo,
218                              const char *part_name)
219 {
220         VALUE self = (VALUE) data, part, name;
221
222         GET_OBJ (self, RbEdje, e);
223
224         name = rb_str_new2 (part_name);
225
226         if (NIL_P (part = rb_hash_aref (e->parts, name))) {
227                 part = TO_PART (self, name);
228                 rb_hash_aset (e->parts, name, part);
229         }
230
231         rb_funcall (e->on_text_changed_cb,
232                     rb_intern ("call"), 1, part);
233 }
234
235 /*
236  * call-seq:
237  *  edje.on_text_changed { |part_obj| block }
238  *
239  * Registers a callback that will get called when the text
240  * of any part is changed in <i>edje</i>.
241  * The block is passed the <code>Edje::Part</code> object
242  * of which the text changed.
243  */
244 static VALUE c_on_text_changed (VALUE self)
245 {
246         GET_OBJ (self, RbEdje, e);
247
248         if (!rb_block_given_p ())
249                 return Qnil;
250
251         e->on_text_changed_cb = rb_block_proc ();
252
253         edje_object_text_change_cb_set (e->real.real, on_text_changed,
254                                         (void *) self);
255
256         return Qnil;
257 }
258
259 /*
260  * call-seq:
261  *  edje.emit_signal(signal, source) => nil
262  *
263  * Emits a signal to <i>edje</i>.
264  *
265  *  edje.emit_signal("signal_foo", "part_bar") #=> nil
266  */
267 static VALUE c_emit_signal (VALUE self, VALUE signal, VALUE source)
268 {
269         GET_OBJ (self, RbEdje, e);
270
271         Check_Type (signal, T_STRING);
272         Check_Type (source, T_STRING);
273
274         edje_object_signal_emit (e->real.real, StringValuePtr (signal),
275                                  StringValuePtr (source));
276
277         return Qnil;
278 }
279
280 static void on_signal (void *data, Evas_Object *o,
281                        const char *signal, const char *src)
282 {
283         rb_funcall ((VALUE) data, rb_intern ("call"), 2,
284                     rb_str_new2 (signal), rb_str_new2 (src));
285 }
286
287 /*
288  * call-seq:
289  *  edje.on_signal(signal [, source]) { |signal, source| block } => nil
290  *
291  * Registers a callback that will get called when <i>signal</i>
292  * is emitted by <i>source</i>.
293  * If source is nil, "*" will be used instead.
294  * The block is passed two strings, signal and source, which identify
295  * the emission.
296  */
297 static VALUE c_on_signal (int argc, VALUE *argv, VALUE self)
298 {
299         VALUE signal, src, cb;
300         char *ssrc = "*";
301
302         GET_OBJ (self, RbEdje, e);
303
304         rb_scan_args (argc, argv, "11", &signal, &src);
305
306         Check_Type (signal, T_STRING);
307
308         if (!NIL_P (src)) {
309                 Check_Type (src, T_STRING);
310                 ssrc = StringValuePtr (src);
311         }
312
313         if (!rb_block_given_p ())
314                 return Qnil;
315
316         cb = rb_block_proc ();
317         rb_ary_push (e->callbacks, cb);
318
319         edje_object_signal_callback_add (e->real.real,
320                                          StringValuePtr (signal),
321                                          ssrc, on_signal, (void *) cb);
322
323         return Qnil;
324 }
325
326 /*
327  * call-seq:
328  *  edje.play? => true or false
329  *
330  * Returns true if <i>edje</i> is in play mode, else returns false.
331  */
332 static VALUE c_play_get (VALUE self)
333 {
334         GET_OBJ (self, RbEdje, e);
335
336         return edje_object_play_get (e->real.real) ? Qtrue : Qfalse;
337 }
338
339 /*
340  * call-seq:
341  *  edje.play(true or false)
342  *
343  * Sets <i>edje</i> to play resp. pause mode.
344  */
345 static VALUE c_play_set (VALUE self, VALUE val)
346 {
347         GET_OBJ (self, RbEdje, e);
348
349         CHECK_BOOL(val);
350
351         edje_object_play_set (e->real.real, val == Qtrue);
352
353         return Qnil;
354 }
355
356 /*
357  * call-seq:
358  *  edje.animation? => true or false
359  *
360  * Returns the animation state of <i>edje</i>.
361  */
362 static VALUE c_animation_get (VALUE self)
363 {
364         GET_OBJ (self, RbEdje, e);
365
366         return edje_object_animation_get (e->real.real) ? Qtrue : Qfalse;
367 }
368
369 /*
370  * call-seq:
371  *  edje.animation(true or false)
372  *
373  * Sets the animation state of <i>edje</i>.
374  */
375 static VALUE c_animation_set (VALUE self, VALUE val)
376 {
377         GET_OBJ (self, RbEdje, e);
378
379         CHECK_BOOL(val);
380
381         edje_object_animation_set (e->real.real, val == Qtrue);
382
383         return Qnil;
384 }
385
386 static VALUE c_data_get (VALUE self, VALUE key)
387 {
388         const char *s;
389
390         GET_OBJ (self, RbEdje, e);
391
392         Check_Type (key, T_STRING);
393
394         s = edje_object_data_get (e->real.real, StringValuePtr (key));
395
396         return s ? rb_str_new2 (s) : Qnil;
397 }
398
399 static VALUE c_send_message (VALUE self, VALUE msg)
400 {
401         VALUE m;
402         Edje_Message_Type type;
403         int id;
404
405         GET_OBJ (self, RbEdje, e);
406
407         CHECK_CLASS (msg, cMsg);
408
409         type = NUM2INT (rb_iv_get (msg, "@type"));
410         id = NUM2INT (rb_iv_get (msg, "@id"));
411         m = rb_funcall (msg, rb_intern ("serialize"), 0, NULL);
412
413         edje_object_message_send (e->real.real, type, id, (void *) m);
414
415         return Qnil;
416 }
417
418 void Init_Edje (void)
419 {
420         cEdje = rb_define_class_under (mEdje, "Edje", cEvasObject);
421
422         rb_define_singleton_method (cEdje, "new", c_new, 1);
423         rb_define_method (cEdje, "freeze", c_freeze, 0);
424         rb_define_method (cEdje, "thaw", c_thaw, 0);
425         rb_define_method (cEdje, "load", c_load, 2);
426         rb_define_method (cEdje, "get_size_min", c_get_size_min, 0);
427         rb_define_method (cEdje, "get_size_max", c_get_size_max, 0);
428         rb_define_method (cEdje, "part_exists?", c_part_exists_get, 1);
429         rb_define_method (cEdje, "part", c_part_get, 1);
430         rb_define_method (cEdje, "on_text_changed", c_on_text_changed, 0);
431         rb_define_method (cEdje, "emit_signal", c_emit_signal, 2);
432         rb_define_method (cEdje, "on_signal", c_on_signal, -1);
433         rb_define_method (cEdje, "play?", c_play_get, 0);
434         rb_define_method (cEdje, "play=", c_play_set, 1);
435         rb_define_method (cEdje, "animation?", c_animation_get, 0);
436         rb_define_method (cEdje, "animation=", c_animation_set, 1);
437         rb_define_method (cEdje, "data", c_data_get, 1);
438         rb_define_method (cEdje, "send_message", c_send_message, 1);
439 }