Class instantiation fixes.
[ruby-evas.git] / src / rb_smart.c
1 /*
2  * $Id: rb_smart.c 354 2006-02-10 18:14:08Z 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 ("on_"#name); \
35 \
36         if (!rb_respond_to (self, id)) \
37                 return;
38
39 #define SMART_CB(name) \
40         static void on_##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 on_##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 on_##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 SMART_CB (delete);
61 SMART_CB (raise);
62 SMART_CB (lower);
63 SMART_CB (show);
64 SMART_CB (hide);
65 SMART_CB (clip_unset);
66 SMART_CB_OBJ (stack_above);
67 SMART_CB_OBJ (stack_below);
68 SMART_CB_OBJ (clip_set);
69 SMART_CB_COORD (move);
70 SMART_CB_COORD (resize);
71
72 static void on_layer_set (Evas_Object *o, int layer)
73 {
74         SMART_CB_BODY (layer_set);
75
76         rb_funcall (self, id, 1, FIX2INT (layer));
77 }
78
79 static void on_color_set (Evas_Object *o, int r, int g, int b, int a)
80 {
81         SMART_CB_BODY (color_set);
82
83         rb_funcall (self, id, 4, INT2FIX (r), INT2FIX (g), INT2FIX (b),
84                     INT2FIX (a));
85 }
86
87 static VALUE c_alloc (VALUE klass)
88 {
89         Evas_Smart **smart = NULL;
90
91         return Data_Make_Struct (klass, Evas_Smart *, NULL, free, smart);
92 }
93
94 /*
95  * call-seq:
96  *  Evas::Smart.new(name) => smart
97  *
98  * Creates a Evas::Smart object with the given name.
99  */
100 static VALUE c_init (VALUE self, VALUE name)
101 {
102         GET_OBJ (self, Evas_Smart *, smart);
103
104         Check_Type (name, T_STRING);
105
106         *smart = evas_smart_new (StringValuePtr (name),
107                                  NULL, on_delete, on_layer_set,
108                                  on_raise, on_lower, on_stack_above,
109                                  on_stack_below, on_move, on_resize,
110                                  on_show, on_hide, on_color_set,
111                                  on_clip_set, on_clip_unset, NULL);
112
113         return self;
114 }
115
116 /*
117  * call-seq:
118  *  smart.new_object(class, evas) => smart_obj
119  *
120  * Creates a Evas::EvasObject from <i>smart</i>.
121  */
122 static VALUE c_new_object (VALUE self, VALUE klass, VALUE evas)
123 {
124         VALUE obj;
125
126         CHECK_CLASS (evas, cEvas);
127         GET_OBJ (evas, RbEvas, e);
128
129         GET_OBJ (self, Evas_Smart *, s);
130
131         obj = rb_obj_alloc (klass);
132
133         GET_OBJ (obj, RbEvasObject, smart);
134
135         smart->real = evas_object_smart_add (e->real, *s);
136
137         rb_obj_call_init (obj, 1, &evas);
138
139         return obj;
140 }
141
142 void Init_Smart (void)
143 {
144         VALUE c = rb_define_class_under (mEvas, "Smart", rb_cObject);
145
146         rb_define_alloc_func (c, c_alloc);
147         rb_define_method (c, "initialize", c_init, 1);
148         rb_define_method (c, "new_object", c_new_object, 2);
149 }