Updated for new smart object API.
[ruby-evas.git] / src / rb_smart.c
1 /*
2  * Copyright (C) 2004 Tilman Sauerbeck (tilman at code-monkey de)
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #include <ruby.h>
20
21 #include <Evas.h>
22
23 #include "rb_evas_main.h"
24 #include "rb_evas.h"
25 #include "rb_evas_object.h"
26
27 #define SMART_CB_BODY(name) \
28         VALUE self = TO_EVAS_OBJECT (o); \
29         static ID id; \
30 \
31         if (!id) \
32                 id = rb_intern ("smart_"#name); \
33 \
34         if (!rb_respond_to (self, id)) \
35                 return;
36
37 #define SMART_CB(name) \
38         static void smart_##name (Evas_Object *o) \
39         { \
40                 SMART_CB_BODY (name); \
41                 rb_funcall (self, id, 0); \
42         }
43
44 #define SMART_CB_OBJ(name) \
45         static void smart_##name (Evas_Object *o, Evas_Object *other) \
46         { \
47                 SMART_CB_BODY (name); \
48                 rb_funcall (self, id, 1, TO_EVAS_OBJECT (other)); \
49         }
50
51 #define SMART_CB_COORD(name) \
52         static void smart_##name (Evas_Object *o, Evas_Coord a, Evas_Coord b) \
53         { \
54                 SMART_CB_BODY (name); \
55                 rb_funcall (self, id, 2, INT2FIX ((int) a), INT2FIX ((int) b)); \
56         }
57
58 typedef struct {
59         Evas_Smart_Class smart_class;
60         Evas_Smart *smart;
61
62         VALUE name;
63 } SmartData;
64
65 static ID id_smart_data;
66
67 SMART_CB (delete);
68 SMART_CB (show);
69 SMART_CB (hide);
70 SMART_CB (clip_unset);
71 SMART_CB_OBJ (clip_set);
72 SMART_CB_COORD (move);
73 SMART_CB_COORD (resize);
74
75 static void smart_color_set (Evas_Object *o, int r, int g, int b, int a)
76 {
77         SMART_CB_BODY (color_set);
78
79         rb_funcall (self, id, 4, INT2FIX (r), INT2FIX (g), INT2FIX (b),
80                     INT2FIX (a));
81 }
82
83 static VALUE c_inherited (VALUE klass, VALUE child)
84 {
85         rb_const_set (child, id_smart_data, Qnil);
86
87         return Qnil;
88 }
89
90 static void
91 c_data_mark (SmartData *sd)
92 {
93         rb_gc_mark (sd->name);
94 }
95
96 static VALUE c_init (VALUE self, VALUE evas)
97 {
98         VALUE klass, data;
99         SmartData *sd;
100
101         CHECK_CLASS (evas, cEvas);
102         GET_OBJ (evas, RbEvas, e);
103         GET_OBJ (self, RbEvasObject, s2);
104
105         klass = rb_obj_class (self);
106
107         /* check whether the smart object has been created already */
108         data = rb_const_get (klass, id_smart_data);
109         if (!NIL_P (data))
110                 Data_Get_Struct (data, SmartData, sd);
111         else {
112                 data = Data_Make_Struct (rb_cObject, SmartData,
113                                          c_data_mark, NULL, sd);
114
115                 sd->name = rb_class_path (klass);
116
117                 sd->smart_class.name = StringValuePtr (sd->name);
118                 sd->smart_class.version = EVAS_SMART_CLASS_VERSION;
119
120                 sd->smart_class.add = NULL;
121                 sd->smart_class.del = smart_delete;
122                 sd->smart_class.move = smart_move;
123                 sd->smart_class.resize = smart_resize;
124                 sd->smart_class.show = smart_show;
125                 sd->smart_class.hide = smart_hide;
126                 sd->smart_class.color_set = smart_color_set;
127                 sd->smart_class.clip_set = smart_clip_set;
128                 sd->smart_class.clip_unset = smart_clip_unset;
129
130                 sd->smart = evas_smart_class_new (&sd->smart_class);
131
132                 rb_mod_remove_const (klass, ID2SYM (id_smart_data));
133                 rb_const_set (klass, id_smart_data, data);
134         }
135
136         s2->real = evas_object_smart_add (e->real, sd->smart);
137
138         rb_call_super (1, &evas);
139
140         return self;
141 }
142
143 static VALUE c_add_member (VALUE self, VALUE member)
144 {
145         GET_OBJ (self, RbEvasObject, e);
146         GET_OBJ (member, RbEvasObject, e2);
147
148         /* weird order of arguments */
149         evas_object_smart_member_add (e2->real, e->real);
150
151         return Qnil;
152 }
153
154 void Init_Smart (void)
155 {
156         VALUE c = rb_define_class_under (mEvas, "Smart", cEvasObject);
157
158         rb_define_singleton_method (c, "inherited", c_inherited, 1);
159         rb_define_method (c, "initialize", c_init, 1);
160         rb_define_method (c, "add_member", c_add_member, 1);
161
162         id_smart_data = rb_intern ("SMART_DATA");
163 }