2 * $Id: rb_edje.c 335 2005-05-02 17:28:37Z 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);
60 * Edje::Edje.new(evas) => edje
62 * Creates an Edje::Edje object.
64 static VALUE c_new (VALUE klass, VALUE evas)
69 CHECK_CLASS (evas, cEvas);
70 GET_OBJ (evas, RbEvas, e);
74 self = Data_Make_Struct (klass, RbEdje, c_mark, c_free, edje);
76 edje->real.real = edje_object_add (e->real);
78 edje->callbacks = Qnil;
79 edje->on_text_changed_cb = Qnil;
82 rb_obj_call_init (self, 1, argv);
87 static VALUE c_new_from_pointer (VALUE klass, VALUE evas, VALUE ptr)
94 self = Data_Make_Struct (klass, RbEdje, c_mark, c_free, edje);
96 edje->real.real = (Evas_Object *) ptr;
98 edje->callbacks = Qnil;
99 edje->on_text_changed_cb = Qnil;
102 rb_obj_call_init (self, 1, argv);
111 * Freezes <i>edje</i>.
113 static VALUE c_freeze (VALUE self)
115 GET_OBJ (self, RbEdje, e);
117 edje_object_freeze (e->real.real);
128 static VALUE c_thaw (VALUE self)
130 GET_OBJ (self, RbEdje, e);
132 edje_object_thaw (e->real.real);
139 * edje.load(eet, group) => nil
141 * Loads <i>eet</i> into <i>edje</i>. <i>group</i> is the
142 * name of the group to be displayed.
144 static VALUE c_load (VALUE self, VALUE eet, VALUE group)
146 GET_OBJ (self, RbEdje, e);
148 Check_Type (eet, T_STRING);
149 Check_Type (group, T_STRING);
151 if (!edje_object_file_set (e->real.real, StringValuePtr (eet),
152 StringValuePtr (group)))
153 rb_raise (rb_eException, "Cannot load eet");
160 * edje.get_size_min => array
162 * Returns an array that contains the minimum size
165 static VALUE c_get_size_min (VALUE self)
169 GET_OBJ (self, RbEdje, e);
171 edje_object_size_min_get (e->real.real, &w, &h);
173 return rb_ary_new3 (2, INT2FIX (w), INT2FIX (h));
178 * edje.get_size_max => array
180 * Returns an array that contains the maximum size
183 static VALUE c_get_size_max (VALUE self)
187 GET_OBJ (self, RbEdje, e);
189 edje_object_size_max_get (e->real.real, &w, &h);
191 return rb_ary_new3 (2, INT2FIX (w), INT2FIX (h));
196 * edje.part_exists?(part) => true or false
198 * Returns true if <i>edje</i> has a part called <i>part</i>,
199 * else returns false.
201 static VALUE c_part_exists_get (VALUE self, VALUE name)
205 GET_OBJ (self, RbEdje, e);
207 Check_Type (name, T_STRING);
209 r = edje_object_part_exists (e->real.real, StringValuePtr (name));
211 return r ? Qtrue : Qfalse;
216 * edje.part(part_name) => part
218 * Returns the <code>Edje::Part</code> object that corresponds to
219 * <i>part_name</i>. If there's no part with that name in <i>edje</i>,
220 * an exception is raised.
222 static VALUE c_part_get (VALUE self, VALUE name)
225 const char *cname = StringValuePtr (name);
227 GET_OBJ (self, RbEdje, e);
229 if (!edje_object_part_exists (e->real.real, cname)) {
230 rb_raise (rb_eException, "Unknown part name - %s", cname);
234 if (NIL_P (e->parts))
235 e->parts = rb_hash_new ();
237 if (NIL_P (part = rb_hash_aref (e->parts, name))) {
238 part = TO_PART (self, name);
239 rb_hash_aset (e->parts, name, part);
245 static void on_text_changed (void *data, Evas_Object *eo,
246 const char *part_name)
248 VALUE self = (VALUE) data, part, name;
250 GET_OBJ (self, RbEdje, e);
252 name = rb_str_new2 (part_name);
254 if (NIL_P (e->parts))
255 e->parts = rb_hash_new ();
257 if (NIL_P (part = rb_hash_aref (e->parts, name))) {
258 part = TO_PART (self, name);
259 rb_hash_aset (e->parts, name, part);
262 rb_funcall (e->on_text_changed_cb,
263 rb_intern ("call"), 1, part);
268 * edje.on_text_changed { |part_obj| block }
270 * Registers a callback that will get called when the text
271 * of any part is changed in <i>edje</i>.
272 * The block is passed the <code>Edje::Part</code> object
273 * of which the text changed.
275 static VALUE c_on_text_changed (VALUE self)
277 GET_OBJ (self, RbEdje, e);
279 if (!rb_block_given_p ())
282 e->on_text_changed_cb = rb_block_proc ();
284 edje_object_text_change_cb_set (e->real.real, on_text_changed,
292 * edje.emit_signal(signal, source) => nil
294 * Emits a signal to <i>edje</i>.
296 * edje.emit_signal("signal_foo", "part_bar") #=> nil
298 static VALUE c_emit_signal (VALUE self, VALUE signal, VALUE source)
300 GET_OBJ (self, RbEdje, e);
302 Check_Type (signal, T_STRING);
303 Check_Type (source, T_STRING);
305 edje_object_signal_emit (e->real.real, StringValuePtr (signal),
306 StringValuePtr (source));
311 static void on_signal (void *data, Evas_Object *o,
312 const char *signal, const char *src)
314 rb_funcall ((VALUE) data, rb_intern ("call"), 2,
315 rb_str_new2 (signal), rb_str_new2 (src));
320 * edje.on_signal(signal [, source]) { |signal, source| block } => nil
322 * Registers a callback that will get called when <i>signal</i>
323 * is emitted by <i>source</i>.
324 * If source is nil, "*" will be used instead.
325 * The block is passed two strings, signal and source, which identify
328 static VALUE c_on_signal (int argc, VALUE *argv, VALUE self)
330 VALUE signal, src, cb;
333 GET_OBJ (self, RbEdje, e);
335 rb_scan_args (argc, argv, "11", &signal, &src);
337 Check_Type (signal, T_STRING);
340 Check_Type (src, T_STRING);
341 ssrc = StringValuePtr (src);
344 if (!rb_block_given_p ())
347 cb = rb_block_proc ();
349 if (NIL_P (e->callbacks))
350 e->callbacks = rb_ary_new ();
352 rb_ary_push (e->callbacks, cb);
354 edje_object_signal_callback_add (e->real.real,
355 StringValuePtr (signal),
356 ssrc, on_signal, (void *) cb);
363 * edje.play? => true or false
365 * Returns true if <i>edje</i> is in play mode, else returns false.
367 static VALUE c_play_get (VALUE self)
369 GET_OBJ (self, RbEdje, e);
371 return edje_object_play_get (e->real.real) ? Qtrue : Qfalse;
376 * edje.play(true or false)
378 * Sets <i>edje</i> to play resp. pause mode.
380 static VALUE c_play_set (VALUE self, VALUE val)
382 GET_OBJ (self, RbEdje, e);
386 edje_object_play_set (e->real.real, val == Qtrue);
393 * edje.animation? => true or false
395 * Returns the animation state of <i>edje</i>.
397 static VALUE c_animation_get (VALUE self)
399 GET_OBJ (self, RbEdje, e);
401 return edje_object_animation_get (e->real.real) ? Qtrue : Qfalse;
406 * edje.animation(true or false)
408 * Sets the animation state of <i>edje</i>.
410 static VALUE c_animation_set (VALUE self, VALUE val)
412 GET_OBJ (self, RbEdje, e);
416 edje_object_animation_set (e->real.real, val == Qtrue);
421 static VALUE c_data_get (VALUE self, VALUE key)
425 GET_OBJ (self, RbEdje, e);
427 Check_Type (key, T_STRING);
429 s = edje_object_data_get (e->real.real, StringValuePtr (key));
431 return s ? rb_str_new2 (s) : Qnil;
434 static Edje_Message_Type get_msg_type (VALUE val)
437 Edje_Message_Type type;
441 return EDJE_MESSAGE_NONE;
443 if (!NIL_P (rb_check_string_type (val))) {
444 return EDJE_MESSAGE_STRING;
445 } else if (rb_obj_is_kind_of (val, rb_cFixnum)) {
446 return EDJE_MESSAGE_INT;
447 } else if (rb_obj_is_kind_of (val, rb_cFloat)) {
448 return EDJE_MESSAGE_FLOAT;
449 } else if (NIL_P (ary = rb_check_array_type (val)))
450 return EDJE_MESSAGE_NONE;
452 len = RARRAY (ary)->len;
454 return EDJE_MESSAGE_NONE;
456 entry = rb_ary_entry (ary, 0);
458 if (rb_obj_is_kind_of (entry, rb_cFixnum))
459 return EDJE_MESSAGE_INT_SET;
460 else if (rb_obj_is_kind_of (entry, rb_cFloat))
461 return EDJE_MESSAGE_FLOAT_SET;
462 else if (NIL_P (rb_check_string_type (entry)))
463 return EDJE_MESSAGE_NONE;
465 /* first entry is a string.
466 * so if we only have one entry, it's a string set
469 return EDJE_MESSAGE_STRING_SET;
471 entry = rb_ary_entry (ary, 1);
473 if (!NIL_P (rb_check_string_type (entry)))
474 type = EDJE_MESSAGE_STRING_SET;
475 else if (rb_obj_is_kind_of (entry, rb_cFixnum))
476 type = len == 2 ? EDJE_MESSAGE_STRING_INT : EDJE_MESSAGE_STRING_INT_SET;
477 else if (rb_obj_is_kind_of (entry, rb_cFloat))
478 type = len == 2 ? EDJE_MESSAGE_STRING_FLOAT :EDJE_MESSAGE_STRING_FLOAT_SET;
480 return EDJE_MESSAGE_NONE;
483 case EDJE_MESSAGE_STRING_SET:
484 for (i = 2; i < len; i++) {
485 entry = rb_ary_entry (ary, i);
486 if (NIL_P(rb_check_string_type (entry)))
487 return EDJE_MESSAGE_NONE;
491 case EDJE_MESSAGE_INT_SET:
492 case EDJE_MESSAGE_STRING_INT_SET:
493 for (i = 2; i < len; i++) {
494 entry = rb_ary_entry (ary, i);
495 if (!rb_obj_is_kind_of (entry, rb_cFixnum))
496 return EDJE_MESSAGE_NONE;
500 case EDJE_MESSAGE_FLOAT_SET:
501 case EDJE_MESSAGE_STRING_FLOAT_SET:
502 for (i = 2; i < len; i++) {
503 entry = rb_ary_entry (ary, 2);
504 if (!rb_obj_is_kind_of (entry, rb_cFloat))
505 return EDJE_MESSAGE_NONE;
516 static VALUE c_send_message (VALUE self, VALUE msg)
518 Edje_Message_String msg_s;
519 Edje_Message_Int msg_i;
520 Edje_Message_Float msg_f;
521 Edje_Message_String_Set *s_set = NULL;
522 Edje_Message_Int_Set *i_set = NULL;
523 Edje_Message_Float_Set *f_set = NULL;
524 Edje_Message_String_Int si;
525 Edje_Message_String_Float sf;
526 Edje_Message_String_Int_Set *si_set = NULL;
527 Edje_Message_String_Float_Set *sf_set = NULL;
528 Edje_Message_Type type;
531 bool free_data = false;
534 GET_OBJ (self, RbEdje, e);
536 CHECK_CLASS (msg, cMsg);
538 id = FIX2INT (rb_iv_get (msg, "@id"));
539 v = rb_iv_get (msg, "@value");
541 type = get_msg_type (v);
542 if (!NIL_P (ary = rb_check_array_type (v)))
543 len = RARRAY (ary)->len;
546 case EDJE_MESSAGE_NONE:
547 rb_raise (eEdjeError, "unsupported value");
549 case EDJE_MESSAGE_SIGNAL:
550 return Qnil; /* cannot happen */
551 case EDJE_MESSAGE_STRING:
552 msg_s.str = StringValuePtr (v);
555 case EDJE_MESSAGE_INT:
556 msg_i.val = FIX2INT (v);
559 case EDJE_MESSAGE_FLOAT:
560 msg_f.val = NUM2DBL (v);
563 case EDJE_MESSAGE_STRING_SET:
564 s_set = malloc (sizeof (Edje_Message_String_Set) + ((len - 1) * sizeof (char *)));
568 for (i = 0; i < len; i++) {
569 entry = rb_ary_entry (ary, i);
570 s_set->str[i] = StringValuePtr (entry);
576 case EDJE_MESSAGE_INT_SET:
577 i_set = malloc (sizeof (Edje_Message_Int_Set) + ((len - 1) * sizeof (int)));
581 for (i = 0; i < len; i++) {
582 entry = rb_ary_entry (ary, i);
583 i_set->val[i] = FIX2INT (entry);
589 case EDJE_MESSAGE_FLOAT_SET:
590 f_set = malloc (sizeof (Edje_Message_Float_Set) + ((len - 1) * sizeof (double)));
594 for (i = 0; i < len; i++) {
595 entry = rb_ary_entry (ary, i);
596 f_set->val[i] = NUM2DBL (entry);
602 case EDJE_MESSAGE_STRING_INT:
603 entry = rb_ary_entry (ary, 0);
604 si.str = StringValuePtr (entry);
605 entry = rb_ary_entry (ary, 1);
606 si.val = FIX2INT (entry);
611 case EDJE_MESSAGE_STRING_FLOAT:
612 entry = rb_ary_entry (ary, 0);
613 sf.str = StringValuePtr (entry);
614 entry = rb_ary_entry (ary, 1);
615 sf.val = NUM2DBL (entry);
620 case EDJE_MESSAGE_STRING_INT_SET:
621 si_set = malloc (sizeof (Edje_Message_String_Int_Set) + ((len - 1) * sizeof (int)));
622 si_set->count = len - 1;
625 entry = rb_ary_entry (ary, 0);
626 si_set->str = StringValuePtr (entry);
628 for (i = 1; i < len; i++) {
629 entry = rb_ary_entry (ary, i);
630 si_set->val[i - 1] = FIX2INT (entry);
636 case EDJE_MESSAGE_STRING_FLOAT_SET:
637 sf_set = malloc (sizeof (Edje_Message_String_Float_Set) + ((len - 1) * sizeof (double)));
638 sf_set->count = len - 1;
641 entry = rb_ary_entry (ary, 0);
642 sf_set->str = StringValuePtr (entry);
644 for (i = 1; i < len; i++) {
645 entry = rb_ary_entry (ary, i);
646 sf_set->val[i - 1] = NUM2DBL (entry);
654 edje_object_message_send (e->real.real, type, id, data);
662 static VALUE c_msg_init (int argc, VALUE *argv, VALUE self)
667 rb_scan_args (argc, argv, "11", &id, &val);
669 rb_scan_args (argc, argv, "1*", &id, &val);
671 Check_Type (id, T_FIXNUM);
673 rb_iv_set (self, "@id", id);
674 rb_iv_set (self, "@value", val);
679 void Init_Edje (void)
681 cEdje = rb_define_class_under (mEdje, "Edje", cEvasObject);
683 rb_define_singleton_method (cEdje, "new", c_new, 1);
684 rb_define_singleton_method (cEdje, "new_from_pointer",
685 c_new_from_pointer, 2);
686 rb_define_method (cEdje, "freeze", c_freeze, 0);
687 rb_define_method (cEdje, "thaw", c_thaw, 0);
688 rb_define_method (cEdje, "load", c_load, 2);
689 rb_define_method (cEdje, "get_size_min", c_get_size_min, 0);
690 rb_define_method (cEdje, "get_size_max", c_get_size_max, 0);
691 rb_define_method (cEdje, "part_exists?", c_part_exists_get, 1);
692 rb_define_method (cEdje, "part", c_part_get, 1);
693 rb_define_method (cEdje, "on_text_changed", c_on_text_changed, 0);
694 rb_define_method (cEdje, "emit_signal", c_emit_signal, 2);
695 rb_define_method (cEdje, "on_signal", c_on_signal, -1);
696 rb_define_method (cEdje, "play?", c_play_get, 0);
697 rb_define_method (cEdje, "play=", c_play_set, 1);
698 rb_define_method (cEdje, "animation?", c_animation_get, 0);
699 rb_define_method (cEdje, "animation=", c_animation_set, 1);
700 rb_define_method (cEdje, "data", c_data_get, 1);
701 rb_define_method (cEdje, "send_message", c_send_message, 1);
703 cMsg = rb_define_class_under (mEdje, "Message", rb_cObject);
705 rb_define_method (cMsg, "initialize", c_msg_init, -1);
707 rb_define_attr (cMsg, "id", 1, 1);
708 rb_define_attr (cMsg, "value", 1, 1);
710 eEdjeError = rb_define_class_under (mEdje, "EdjeError",