366c602f9625e37f83e9533b2b0b1d2768872d1b
[ruby-evas.git] / src / rb_smart.c
1 /*
2  * $Id: rb_smart.c 61 2004-08-12 10:04:07Z 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 RUBY_EVAS_SMART_OBJECT_KEY "__RB_EVAS_OBJECT_SMART_OBJECT"
30
31 #define GET_EVAS_OBJECT(obj, o) \
32         VALUE (obj); \
33 \
34         obj = (VALUE) evas_object_data_get (o, \
35                                             RUBY_EVAS_SMART_OBJECT_KEY); \
36         if (!obj) { \
37                 rb_raise (rb_eException, \
38                           "EvasObject Ruby object key missing"); \
39                 return; \
40         }
41
42 #define SMART_CB_BODY(name) \
43         static ID id; \
44 \
45         GET_EVAS_OBJECT (self, o); \
46 \
47         if (!id) \
48                 id = rb_intern ("on_"#name); \
49 \
50         if (!rb_respond_to (self, id)) \
51                 return;
52
53 #define SMART_CB(name) \
54         static void on_##name (Evas_Object *o) \
55         { \
56                 SMART_CB_BODY (name); \
57                 rb_funcall (self, id, 0); \
58         }
59
60 #define SMART_CB_OBJ(name) \
61         static void on_##name (Evas_Object *o, Evas_Object *other) \
62         { \
63                 void *obj; \
64 \
65                 SMART_CB_BODY (name); \
66 \
67                 obj = evas_object_data_get (other, RUBY_EVAS_OBJECT_KEY); \
68                 if (!obj) { \
69                         rb_raise (rb_eException, "EvasObject Ruby object key missing"); \
70                         return; \
71                 } \
72 \
73                 rb_funcall (self, id, 1, (VALUE) obj); \
74         }
75
76 #define SMART_CB_COORD(name) \
77         static void on_##name (Evas_Object *o, Evas_Coord a, Evas_Coord b) \
78         { \
79                 SMART_CB_BODY (name); \
80                 rb_funcall (self, id, 2, INT2FIX ((int) a), INT2FIX ((int) b)); \
81         }
82
83 SMART_CB (delete);
84 SMART_CB (raise);
85 SMART_CB (lower);
86 SMART_CB (show);
87 SMART_CB (hide);
88 SMART_CB (clip_unset);
89 SMART_CB_OBJ (stack_above);
90 SMART_CB_OBJ (stack_below);
91 SMART_CB_OBJ (clip_set);
92 SMART_CB_COORD (move);
93 SMART_CB_COORD (resize);
94
95 static void on_layer_set (Evas_Object *o, int layer)
96 {
97         SMART_CB_BODY (layer_set);
98
99         rb_funcall (self, id, 1, FIX2INT (layer));
100 }
101
102 static void on_color_set (Evas_Object *o, int r, int g, int b, int a)
103 {
104         SMART_CB_BODY (color_set);
105
106         rb_funcall (self, id, 4, INT2FIX (r), INT2FIX (g), INT2FIX (b),
107                     INT2FIX (a));
108 }
109
110 /*
111  * call-seq:
112  *  Evas::Smart.new(name) => smart
113  *
114  * Creates a Evas::Smart object with the given name.
115  */
116 static VALUE c_new (VALUE klass, VALUE name)
117 {
118         VALUE self;
119         Evas_Smart **smart;
120
121         Check_Type (name, T_STRING);
122
123         self = Data_Make_Struct (klass, Evas_Smart *, NULL, free, smart);
124         *smart = evas_smart_new (StringValuePtr (name),
125                                  NULL, on_delete, on_layer_set,
126                                  on_raise, on_lower, on_stack_above,
127                                  on_stack_below, on_move, on_resize,
128                                  on_show, on_hide, on_color_set,
129                                  on_clip_set, on_clip_unset, NULL);
130
131         rb_obj_call_init (self, 0, NULL);
132
133         return self;
134 }
135
136 static void c_object_free (RbEvasObject *e)
137 {
138         c_evas_object_free (e, true);
139 }
140
141 /*
142  * call-seq:
143  *  smart.new_object(class, evas) => smart_obj
144  *
145  * Creates a Evas::EvasObject from <i>smart</i>.
146  */
147 static VALUE c_new_object (VALUE self, VALUE klass, VALUE evas)
148 {
149         VALUE obj, argv[1];
150         RbEvasObject *smart;
151
152         CHECK_CLASS (evas, cEvas);
153         GET_OBJ (evas, RbEvas, e);
154
155         GET_OBJ (self, Evas_Smart *, s);
156
157         obj = Data_Make_Struct (klass, RbEvasObject, c_evas_object_mark,
158                                 c_object_free, smart);
159         smart->real = evas_object_smart_add (e->real, *s);
160
161         evas_object_data_set (smart->real, RUBY_EVAS_SMART_OBJECT_KEY,
162                               (void *) obj);
163
164         argv[0] = evas;
165         rb_obj_call_init (obj, 1, argv);
166
167         return obj;
168 }
169
170 void Init_Smart (void)
171 {
172         VALUE c = rb_define_class_under (mEvas, "Smart", rb_cObject);
173
174         rb_define_singleton_method (c, "new", c_new, 1);
175         rb_define_method (c, "new_object", c_new_object, 2);
176 }