Introduced TO_EVAS_OBJECT.
[ruby-evas.git] / src / rb_smart.c
1 /*
2  * $Id: rb_smart.c 68 2004-08-16 15:42:19Z 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                 SMART_CB_BODY (name); \
64                 rb_funcall (self, id, 1, TO_EVAS_OBJECT (other)); \
65         }
66
67 #define SMART_CB_COORD(name) \
68         static void on_##name (Evas_Object *o, Evas_Coord a, Evas_Coord b) \
69         { \
70                 SMART_CB_BODY (name); \
71                 rb_funcall (self, id, 2, INT2FIX ((int) a), INT2FIX ((int) b)); \
72         }
73
74 SMART_CB (delete);
75 SMART_CB (raise);
76 SMART_CB (lower);
77 SMART_CB (show);
78 SMART_CB (hide);
79 SMART_CB (clip_unset);
80 SMART_CB_OBJ (stack_above);
81 SMART_CB_OBJ (stack_below);
82 SMART_CB_OBJ (clip_set);
83 SMART_CB_COORD (move);
84 SMART_CB_COORD (resize);
85
86 static void on_layer_set (Evas_Object *o, int layer)
87 {
88         SMART_CB_BODY (layer_set);
89
90         rb_funcall (self, id, 1, FIX2INT (layer));
91 }
92
93 static void on_color_set (Evas_Object *o, int r, int g, int b, int a)
94 {
95         SMART_CB_BODY (color_set);
96
97         rb_funcall (self, id, 4, INT2FIX (r), INT2FIX (g), INT2FIX (b),
98                     INT2FIX (a));
99 }
100
101 /*
102  * call-seq:
103  *  Evas::Smart.new(name) => smart
104  *
105  * Creates a Evas::Smart object with the given name.
106  */
107 static VALUE c_new (VALUE klass, VALUE name)
108 {
109         VALUE self;
110         Evas_Smart **smart;
111
112         Check_Type (name, T_STRING);
113
114         self = Data_Make_Struct (klass, Evas_Smart *, NULL, free, smart);
115         *smart = evas_smart_new (StringValuePtr (name),
116                                  NULL, on_delete, on_layer_set,
117                                  on_raise, on_lower, on_stack_above,
118                                  on_stack_below, on_move, on_resize,
119                                  on_show, on_hide, on_color_set,
120                                  on_clip_set, on_clip_unset, NULL);
121
122         rb_obj_call_init (self, 0, NULL);
123
124         return self;
125 }
126
127 static void c_object_free (RbEvasObject *e)
128 {
129         c_evas_object_free (e, true);
130 }
131
132 /*
133  * call-seq:
134  *  smart.new_object(class, evas) => smart_obj
135  *
136  * Creates a Evas::EvasObject from <i>smart</i>.
137  */
138 static VALUE c_new_object (VALUE self, VALUE klass, VALUE evas)
139 {
140         VALUE obj, argv[1];
141         RbEvasObject *smart;
142
143         CHECK_CLASS (evas, cEvas);
144         GET_OBJ (evas, RbEvas, e);
145
146         GET_OBJ (self, Evas_Smart *, s);
147
148         obj = Data_Make_Struct (klass, RbEvasObject, c_evas_object_mark,
149                                 c_object_free, smart);
150         smart->real = evas_object_smart_add (e->real, *s);
151
152         evas_object_data_set (smart->real, RUBY_EVAS_SMART_OBJECT_KEY,
153                               (void *) obj);
154
155         argv[0] = evas;
156         rb_obj_call_init (obj, 1, argv);
157
158         return obj;
159 }
160
161 void Init_Smart (void)
162 {
163         VALUE c = rb_define_class_under (mEvas, "Smart", rb_cObject);
164
165         rb_define_singleton_method (c, "new", c_new, 1);
166         rb_define_method (c, "new_object", c_new_object, 2);
167 }