4c036afbb25324eb9c22f7c58161106666447f90
[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 static ID id_smart_object;
59
60 SMART_CB (delete);
61 SMART_CB (show);
62 SMART_CB (hide);
63 SMART_CB (clip_unset);
64 SMART_CB_OBJ (clip_set);
65 SMART_CB_COORD (move);
66 SMART_CB_COORD (resize);
67
68 static void smart_color_set (Evas_Object *o, int r, int g, int b, int a)
69 {
70         SMART_CB_BODY (color_set);
71
72         rb_funcall (self, id, 4, INT2FIX (r), INT2FIX (g), INT2FIX (b),
73                     INT2FIX (a));
74 }
75
76 static VALUE c_inherited (VALUE klass, VALUE child)
77 {
78         rb_const_set (child, id_smart_object, Qnil);
79
80         return Qnil;
81 }
82
83 static VALUE c_init (VALUE self, VALUE evas)
84 {
85         VALUE klass, smart, name;
86         Evas_Smart **s = NULL;
87
88         CHECK_CLASS (evas, cEvas);
89         GET_OBJ (evas, RbEvas, e);
90         GET_OBJ (self, RbEvasObject, s2);
91
92         klass = rb_obj_class (self);
93
94         /* check whether the smart object has been created already */
95         smart = rb_const_get (klass, id_smart_object);
96         if (!NIL_P (smart))
97                 Data_Get_Struct (smart, Evas_Smart *, s);
98         else {
99                 name = rb_class_path (klass);
100
101                 smart = Data_Make_Struct (rb_cObject, Evas_Smart *, NULL, NULL, s);
102
103                 *s = evas_smart_new (StringValuePtr (name),
104                                      NULL, smart_delete, NULL,
105                                      NULL, NULL, NULL, NULL,
106                                      smart_move, smart_resize,
107                                      smart_show, smart_hide,
108                                      smart_color_set,
109                                      smart_clip_set, smart_clip_unset,
110                                      NULL);
111
112                 rb_mod_remove_const(klass, ID2SYM (id_smart_object));
113                 rb_const_set (klass, id_smart_object, smart);
114         }
115
116         s2->real = evas_object_smart_add (e->real, *s);
117
118         rb_call_super (1, &evas);
119
120         return self;
121 }
122
123 static VALUE c_add_member (VALUE self, VALUE member)
124 {
125         GET_OBJ (self, RbEvasObject, e);
126         GET_OBJ (member, RbEvasObject, e2);
127
128         /* weird order of arguments */
129         evas_object_smart_member_add (e2->real, e->real);
130
131         return Qnil;
132 }
133
134 void Init_Smart (void)
135 {
136         VALUE c = rb_define_class_under (mEvas, "Smart", cEvasObject);
137
138         rb_define_singleton_method (c, "inherited", c_inherited, 1);
139         rb_define_method (c, "initialize", c_init, 1);
140         rb_define_method (c, "add_member", c_add_member, 1);
141
142         id_smart_object = rb_intern ("SMART_OBJECT");
143 }