2 * $Id: rb_edje.c 355 2006-02-10 18:15:13Z tilman $
4 * Copyright (C) 2004 Tilman Sauerbeck (tilman at code-monkey de)
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.
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.
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
25 #include <evas/rb_evas.h>
26 #include <evas/rb_evas_object.h>
30 #include "rb_edje_main.h"
34 static VALUE cMsg, eEdjeError;
36 static void c_mark (RbEdje *e)
38 c_evas_object_mark (&e->real);
40 if (!NIL_P (e->parts))
41 rb_gc_mark (e->parts);
43 if (!NIL_P (e->callbacks))
44 rb_gc_mark (e->callbacks);
46 if (!NIL_P (e->on_text_changed_cb))
47 rb_gc_mark (e->on_text_changed_cb);
50 static void c_free (RbEdje *e)
52 c_evas_object_free (&e->real, false);
58 static VALUE c_alloc (VALUE klass)
64 return Data_Make_Struct (klass, RbEdje, c_mark, c_free, e);
69 * Edje::Edje.new(evas) => edje
71 * Creates an Edje::Edje object.
73 static VALUE c_init (VALUE self, VALUE evas)
75 CHECK_CLASS (evas, cEvas);
76 GET_OBJ (evas, RbEvas, e);
77 GET_OBJ (self, RbEdje, edje);
79 /* c_new_from_pointer() might already have initialized the pointer */
81 edje->real.real = edje_object_add (e->real);
84 edje->callbacks = Qnil;
85 edje->on_text_changed_cb = Qnil;
87 rb_call_super (1, &evas);
92 static VALUE c_new_from_pointer (VALUE klass, VALUE evas, VALUE ptr)
94 VALUE self = rb_obj_alloc (klass);
96 GET_OBJ (self, RbEdje, edje);
98 edje->real.real = (Evas_Object *) ptr;
100 rb_obj_call_init (self, 1, &evas);
109 * Freezes <i>edje</i>.
111 static VALUE c_freeze (VALUE self)
113 GET_OBJ (self, RbEdje, e);
115 edje_object_freeze (e->real.real);
126 static VALUE c_thaw (VALUE self)
128 GET_OBJ (self, RbEdje, e);
130 edje_object_thaw (e->real.real);
137 * edje.load(eet, group) => nil
139 * Loads <i>eet</i> into <i>edje</i>. <i>group</i> is the
140 * name of the group to be displayed.
142 static VALUE c_load (VALUE self, VALUE eet, VALUE group)
144 GET_OBJ (self, RbEdje, e);
146 Check_Type (eet, T_STRING);
147 Check_Type (group, T_STRING);
149 if (!edje_object_file_set (e->real.real, StringValuePtr (eet),
150 StringValuePtr (group)))
151 rb_raise (rb_eException, "Cannot load eet");
158 * edje.get_size_min => array
160 * Returns an array that contains the minimum size
163 static VALUE c_get_size_min (VALUE self)
167 GET_OBJ (self, RbEdje, e);
169 edje_object_size_min_get (e->real.real, &w, &h);
171 return rb_ary_new3 (2, INT2FIX (w), INT2FIX (h));
176 * edje.get_size_max => array
178 * Returns an array that contains the maximum size
181 static VALUE c_get_size_max (VALUE self)
185 GET_OBJ (self, RbEdje, e);
187 edje_object_size_max_get (e->real.real, &w, &h);
189 return rb_ary_new3 (2, INT2FIX (w), INT2FIX (h));
194 * edje.part_exists?(part) => true or false
196 * Returns true if <i>edje</i> has a part called <i>part</i>,
197 * else returns false.
199 static VALUE c_part_exists_get (VALUE self, VALUE name)
203 GET_OBJ (self, RbEdje, e);
205 Check_Type (name, T_STRING);
207 r = edje_object_part_exists (e->real.real, StringValuePtr (name));
209 return r ? Qtrue : Qfalse;
214 * edje.part(part_name) => part
216 * Returns the <code>Edje::Part</code> object that corresponds to
217 * <i>part_name</i>. If there's no part with that name in <i>edje</i>,
218 * an exception is raised.
220 static VALUE c_part_get (VALUE self, VALUE name)
223 const char *cname = StringValuePtr (name);
225 GET_OBJ (self, RbEdje, e);
227 if (!edje_object_part_exists (e->real.real, cname)) {
228 rb_raise (rb_eException, "Unknown part name - %s", cname);
232 if (NIL_P (e->parts))
233 e->parts = rb_hash_new ();
235 if (NIL_P (part = rb_hash_aref (e->parts, name))) {
236 part = TO_PART (self, name);
237 rb_hash_aset (e->parts, name, part);
243 static void on_text_changed (void *data, Evas_Object *eo,
244 const char *part_name)
246 VALUE self = (VALUE) data, part, name;
248 GET_OBJ (self, RbEdje, e);
250 name = rb_str_new2 (part_name);
252 if (NIL_P (e->parts))
253 e->parts = rb_hash_new ();
255 if (NIL_P (part = rb_hash_aref (e->parts, name))) {
256 part = TO_PART (self, name);
257 rb_hash_aset (e->parts, name, part);
260 rb_funcall (e->on_text_changed_cb,
261 rb_intern ("call"), 1, part);
266 * edje.on_text_changed { |part_obj| block }
268 * Registers a callback that will get called when the text
269 * of any part is changed in <i>edje</i>.
270 * The block is passed the <code>Edje::Part</code> object
271 * of which the text changed.
273 static VALUE c_on_text_changed (VALUE self)
275 GET_OBJ (self, RbEdje, e);
277 if (!rb_block_given_p ())
280 e->on_text_changed_cb = rb_block_proc ();
282 edje_object_text_change_cb_set (e->real.real, on_text_changed,
290 * edje.emit_signal(signal, source) => nil
292 * Emits a signal to <i>edje</i>.
294 * edje.emit_signal("signal_foo", "part_bar") #=> nil
296 static VALUE c_emit_signal (VALUE self, VALUE signal, VALUE source)
298 GET_OBJ (self, RbEdje, e);
300 Check_Type (signal, T_STRING);
301 Check_Type (source, T_STRING);
303 edje_object_signal_emit (e->real.real, StringValuePtr (signal),
304 StringValuePtr (source));
309 static void on_signal (void *data, Evas_Object *o,
310 const char *signal, const char *src)
312 rb_funcall ((VALUE) data, rb_intern ("call"), 2,
313 rb_str_new2 (signal), rb_str_new2 (src));
318 * edje.on_signal(signal [, source]) { |signal, source| block } => nil
320 * Registers a callback that will get called when <i>signal</i>
321 * is emitted by <i>source</i>.
322 * If source is nil, "*" will be used instead.
323 * The block is passed two strings, signal and source, which identify
326 static VALUE c_on_signal (int argc, VALUE *argv, VALUE self)
328 VALUE signal, src, cb;
331 GET_OBJ (self, RbEdje, e);
333 rb_scan_args (argc, argv, "11", &signal, &src);
335 Check_Type (signal, T_STRING);
338 Check_Type (src, T_STRING);
339 ssrc = StringValuePtr (src);
342 if (!rb_block_given_p ())
345 cb = rb_block_proc ();
347 if (NIL_P (e->callbacks))
348 e->callbacks = rb_ary_new ();
350 rb_ary_push (e->callbacks, cb);
352 edje_object_signal_callback_add (e->real.real,
353 StringValuePtr (signal),
354 ssrc, on_signal, (void *) cb);
361 * edje.play? => true or false
363 * Returns true if <i>edje</i> is in play mode, else returns false.
365 static VALUE c_play_get (VALUE self)
367 GET_OBJ (self, RbEdje, e);
369 return edje_object_play_get (e->real.real) ? Qtrue : Qfalse;
374 * edje.play(true or false)
376 * Sets <i>edje</i> to play resp. pause mode.
378 static VALUE c_play_set (VALUE self, VALUE val)
380 GET_OBJ (self, RbEdje, e);
384 edje_object_play_set (e->real.real, val == Qtrue);
391 * edje.animation? => true or false
393 * Returns the animation state of <i>edje</i>.
395 static VALUE c_animation_get (VALUE self)
397 GET_OBJ (self, RbEdje, e);
399 return edje_object_animation_get (e->real.real) ? Qtrue : Qfalse;
404 * edje.animation(true or false)
406 * Sets the animation state of <i>edje</i>.
408 static VALUE c_animation_set (VALUE self, VALUE val)
410 GET_OBJ (self, RbEdje, e);
414 edje_object_animation_set (e->real.real, val == Qtrue);
419 static VALUE c_data_get (VALUE self, VALUE key)
423 GET_OBJ (self, RbEdje, e);
425 Check_Type (key, T_STRING);
427 s = edje_object_data_get (e->real.real, StringValuePtr (key));
429 return s ? rb_str_new2 (s) : Qnil;
432 static Edje_Message_Type get_msg_type (VALUE val)
435 Edje_Message_Type type;
439 return EDJE_MESSAGE_NONE;
441 if (!NIL_P (rb_check_string_type (val))) {
442 return EDJE_MESSAGE_STRING;
443 } else if (rb_obj_is_kind_of (val, rb_cFixnum)) {
444 return EDJE_MESSAGE_INT;
445 } else if (rb_obj_is_kind_of (val, rb_cFloat)) {
446 return EDJE_MESSAGE_FLOAT;
447 } else if (NIL_P (ary = rb_check_array_type (val)))
448 return EDJE_MESSAGE_NONE;
450 len = RARRAY (ary)->len;
452 return EDJE_MESSAGE_NONE;
454 entry = rb_ary_entry (ary, 0);
456 if (rb_obj_is_kind_of (entry, rb_cFixnum))
457 return EDJE_MESSAGE_INT_SET;
458 else if (rb_obj_is_kind_of (entry, rb_cFloat))
459 return EDJE_MESSAGE_FLOAT_SET;
460 else if (NIL_P (rb_check_string_type (entry)))
461 return EDJE_MESSAGE_NONE;
463 /* first entry is a string.
464 * so if we only have one entry, it's a string set
467 return EDJE_MESSAGE_STRING_SET;
469 entry = rb_ary_entry (ary, 1);
471 if (!NIL_P (rb_check_string_type (entry)))
472 type = EDJE_MESSAGE_STRING_SET;
473 else if (rb_obj_is_kind_of (entry, rb_cFixnum))
474 type = len == 2 ? EDJE_MESSAGE_STRING_INT : EDJE_MESSAGE_STRING_INT_SET;
475 else if (rb_obj_is_kind_of (entry, rb_cFloat))
476 type = len == 2 ? EDJE_MESSAGE_STRING_FLOAT :EDJE_MESSAGE_STRING_FLOAT_SET;
478 return EDJE_MESSAGE_NONE;
481 case EDJE_MESSAGE_STRING_SET:
482 for (i = 2; i < len; i++) {
483 entry = rb_ary_entry (ary, i);
484 if (NIL_P(rb_check_string_type (entry)))
485 return EDJE_MESSAGE_NONE;
489 case EDJE_MESSAGE_INT_SET:
490 case EDJE_MESSAGE_STRING_INT_SET:
491 for (i = 2; i < len; i++) {
492 entry = rb_ary_entry (ary, i);
493 if (!rb_obj_is_kind_of (entry, rb_cFixnum))
494 return EDJE_MESSAGE_NONE;
498 case EDJE_MESSAGE_FLOAT_SET:
499 case EDJE_MESSAGE_STRING_FLOAT_SET:
500 for (i = 2; i < len; i++) {
501 entry = rb_ary_entry (ary, 2);
502 if (!rb_obj_is_kind_of (entry, rb_cFloat))
503 return EDJE_MESSAGE_NONE;
514 static VALUE c_send_message (VALUE self, VALUE msg)
516 Edje_Message_String msg_s;
517 Edje_Message_Int msg_i;
518 Edje_Message_Float msg_f;
519 Edje_Message_String_Set *s_set = NULL;
520 Edje_Message_Int_Set *i_set = NULL;
521 Edje_Message_Float_Set *f_set = NULL;
522 Edje_Message_String_Int si;
523 Edje_Message_String_Float sf;
524 Edje_Message_String_Int_Set *si_set = NULL;
525 Edje_Message_String_Float_Set *sf_set = NULL;
526 Edje_Message_Type type;
529 bool free_data = false;
532 GET_OBJ (self, RbEdje, e);
534 CHECK_CLASS (msg, cMsg);
536 id = FIX2INT (rb_iv_get (msg, "@id"));
537 v = rb_iv_get (msg, "@value");
539 type = get_msg_type (v);
540 if (!NIL_P (ary = rb_check_array_type (v)))
541 len = RARRAY (ary)->len;
544 case EDJE_MESSAGE_NONE:
545 rb_raise (eEdjeError, "unsupported value");
547 case EDJE_MESSAGE_SIGNAL:
548 return Qnil; /* cannot happen */
549 case EDJE_MESSAGE_STRING:
550 msg_s.str = StringValuePtr (v);
553 case EDJE_MESSAGE_INT:
554 msg_i.val = FIX2INT (v);
557 case EDJE_MESSAGE_FLOAT:
558 msg_f.val = NUM2DBL (v);
561 case EDJE_MESSAGE_STRING_SET:
562 s_set = malloc (sizeof (Edje_Message_String_Set) + ((len - 1) * sizeof (char *)));
566 for (i = 0; i < len; i++) {
567 entry = rb_ary_entry (ary, i);
568 s_set->str[i] = StringValuePtr (entry);
574 case EDJE_MESSAGE_INT_SET:
575 i_set = malloc (sizeof (Edje_Message_Int_Set) + ((len - 1) * sizeof (int)));
579 for (i = 0; i < len; i++) {
580 entry = rb_ary_entry (ary, i);
581 i_set->val[i] = FIX2INT (entry);
587 case EDJE_MESSAGE_FLOAT_SET:
588 f_set = malloc (sizeof (Edje_Message_Float_Set) + ((len - 1) * sizeof (double)));
592 for (i = 0; i < len; i++) {
593 entry = rb_ary_entry (ary, i);
594 f_set->val[i] = NUM2DBL (entry);
600 case EDJE_MESSAGE_STRING_INT:
601 entry = rb_ary_entry (ary, 0);
602 si.str = StringValuePtr (entry);
603 entry = rb_ary_entry (ary, 1);
604 si.val = FIX2INT (entry);
609 case EDJE_MESSAGE_STRING_FLOAT:
610 entry = rb_ary_entry (ary, 0);
611 sf.str = StringValuePtr (entry);
612 entry = rb_ary_entry (ary, 1);
613 sf.val = NUM2DBL (entry);
618 case EDJE_MESSAGE_STRING_INT_SET:
619 si_set = malloc (sizeof (Edje_Message_String_Int_Set) + ((len - 1) * sizeof (int)));
620 si_set->count = len - 1;
623 entry = rb_ary_entry (ary, 0);
624 si_set->str = StringValuePtr (entry);
626 for (i = 1; i < len; i++) {
627 entry = rb_ary_entry (ary, i);
628 si_set->val[i - 1] = FIX2INT (entry);
634 case EDJE_MESSAGE_STRING_FLOAT_SET:
635 sf_set = malloc (sizeof (Edje_Message_String_Float_Set) + ((len - 1) * sizeof (double)));
636 sf_set->count = len - 1;
639 entry = rb_ary_entry (ary, 0);
640 sf_set->str = StringValuePtr (entry);
642 for (i = 1; i < len; i++) {
643 entry = rb_ary_entry (ary, i);
644 sf_set->val[i - 1] = NUM2DBL (entry);
652 edje_object_message_send (e->real.real, type, id, data);
660 static VALUE c_msg_init (int argc, VALUE *argv, VALUE self)
665 rb_scan_args (argc, argv, "11", &id, &val);
667 rb_scan_args (argc, argv, "1*", &id, &val);
669 Check_Type (id, T_FIXNUM);
671 rb_iv_set (self, "@id", id);
672 rb_iv_set (self, "@value", val);
677 void Init_Edje (void)
679 cEdje = rb_define_class_under (mEdje, "Edje", cEvasObject);
681 rb_define_alloc_func (cEdje, c_alloc);
682 rb_define_singleton_method (cEdje, "new_from_pointer",
683 c_new_from_pointer, 2);
684 rb_define_method (cEdje, "initialize", c_init, 1);
685 rb_define_method (cEdje, "freeze", c_freeze, 0);
686 rb_define_method (cEdje, "thaw", c_thaw, 0);
687 rb_define_method (cEdje, "load", c_load, 2);
688 rb_define_method (cEdje, "get_size_min", c_get_size_min, 0);
689 rb_define_method (cEdje, "get_size_max", c_get_size_max, 0);
690 rb_define_method (cEdje, "part_exists?", c_part_exists_get, 1);
691 rb_define_method (cEdje, "part", c_part_get, 1);
692 rb_define_method (cEdje, "on_text_changed", c_on_text_changed, 0);
693 rb_define_method (cEdje, "emit_signal", c_emit_signal, 2);
694 rb_define_method (cEdje, "on_signal", c_on_signal, -1);
695 rb_define_method (cEdje, "play?", c_play_get, 0);
696 rb_define_method (cEdje, "play=", c_play_set, 1);
697 rb_define_method (cEdje, "animation?", c_animation_get, 0);
698 rb_define_method (cEdje, "animation=", c_animation_set, 1);
699 rb_define_method (cEdje, "data", c_data_get, 1);
700 rb_define_method (cEdje, "send_message", c_send_message, 1);
702 cMsg = rb_define_class_under (mEdje, "Message", rb_cObject);
704 rb_define_method (cMsg, "initialize", c_msg_init, -1);
706 rb_define_attr (cMsg, "id", 1, 1);
707 rb_define_attr (cMsg, "value", 1, 1);
709 eEdjeError = rb_define_class_under (mEdje, "EdjeError",