5ede2ba65b0bebfeaf6b0da3b6e9dd081570fb1e
[ruby-evas.git] / src / rb_smart.c
1 /*
2  * $Id: rb_smart.c 387 2006-09-23 19:52:31Z 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 <Evas.h>
24
25 #include "rb_evas_main.h"
26 #include "rb_evas.h"
27 #include "rb_evas_object.h"
28
29 #define SMART_CB_BODY(name) \
30         VALUE self = TO_EVAS_OBJECT (o); \
31         static ID id; \
32 \
33         if (!id) \
34                 id = rb_intern ("smart_"#name); \
35 \
36         if (!rb_respond_to (self, id)) \
37                 return;
38
39 #define SMART_CB(name) \
40         static void smart_##name (Evas_Object *o) \
41         { \
42                 SMART_CB_BODY (name); \
43                 rb_funcall (self, id, 0); \
44         }
45
46 #define SMART_CB_OBJ(name) \
47         static void smart_##name (Evas_Object *o, Evas_Object *other) \
48         { \
49                 SMART_CB_BODY (name); \
50                 rb_funcall (self, id, 1, TO_EVAS_OBJECT (other)); \
51         }
52
53 #define SMART_CB_COORD(name) \
54         static void smart_##name (Evas_Object *o, Evas_Coord a, Evas_Coord b) \
55         { \
56                 SMART_CB_BODY (name); \
57                 rb_funcall (self, id, 2, INT2FIX ((int) a), INT2FIX ((int) b)); \
58         }
59
60 static ID id_smart_object;
61
62 SMART_CB (delete);
63 SMART_CB (show);
64 SMART_CB (hide);
65 SMART_CB (clip_unset);
66 SMART_CB_OBJ (clip_set);
67 SMART_CB_COORD (move);
68 SMART_CB_COORD (resize);
69
70 static void smart_color_set (Evas_Object *o, int r, int g, int b, int a)
71 {
72         SMART_CB_BODY (color_set);
73
74         rb_funcall (self, id, 4, INT2FIX (r), INT2FIX (g), INT2FIX (b),
75                     INT2FIX (a));
76 }
77
78 static VALUE c_inherited (VALUE klass, VALUE child)
79 {
80         rb_const_set (child, id_smart_object, Qnil);
81
82         return Qnil;
83 }
84
85 static VALUE c_init (VALUE self, VALUE evas)
86 {
87         VALUE klass, smart, name;
88         Evas_Smart **s = NULL;
89
90         CHECK_CLASS (evas, cEvas);
91         GET_OBJ (evas, RbEvas, e);
92         GET_OBJ (self, RbEvasObject, s2);
93
94         klass = rb_obj_class (self);
95
96         /* check whether the smart object has been created already */
97         smart = rb_const_get (klass, id_smart_object);
98         if (!NIL_P (smart))
99                 Data_Get_Struct (smart, Evas_Smart *, s);
100         else {
101                 name = rb_class_path (klass);
102
103                 smart = Data_Make_Struct (rb_cObject, Evas_Smart *, NULL, NULL, s);
104
105                 *s = evas_smart_new (StringValuePtr (name),
106                                      NULL, smart_delete, NULL,
107                                      NULL, NULL, NULL, NULL,
108                                      smart_move, smart_resize,
109                                      smart_show, smart_hide,
110                                      smart_color_set,
111                                      smart_clip_set, smart_clip_unset,
112                                      NULL);
113
114                 rb_mod_remove_const(klass, ID2SYM (id_smart_object));
115                 rb_const_set (klass, id_smart_object, smart);
116         }
117
118         s2->real = evas_object_smart_add (e->real, *s);
119
120         rb_call_super (1, &evas);
121
122         return self;
123 }
124
125 static VALUE c_add_member (VALUE self, VALUE member)
126 {
127         GET_OBJ (self, RbEvasObject, e);
128         GET_OBJ (member, RbEvasObject, e2);
129
130         /* weird order of arguments */
131         evas_object_smart_member_add (e2->real, e->real);
132
133         return Qnil;
134 }
135
136 void Init_Smart (void)
137 {
138         VALUE c = rb_define_class_under (mEvas, "Smart", cEvasObject);
139
140         rb_define_singleton_method (c, "inherited", c_inherited, 1);
141         rb_define_method (c, "initialize", c_init, 1);
142         rb_define_method (c, "add_member", c_add_member, 1);
143
144         id_smart_object = rb_intern ("SMART_OBJECT");
145 }