Class instantiation fixes.
[ruby-esmart.git] / src / esmart_container / rb_esmart_container.c
1 /*
2  * $Id: rb_esmart_container.c 356 2006-02-10 18:27:31Z 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 <Esmart/Esmart_Container.h>
24 #include <evas/rb_evas.h>
25 #include <evas/rb_evas_object.h>
26
27 #include "../rb_esmart.h"
28
29 typedef struct {
30         RbEvasObject real;
31         VALUE elements;
32 } RbContainer;
33
34 static void c_mark (RbContainer *e)
35 {
36         c_evas_object_mark (&e->real);
37
38         rb_gc_mark (e->elements);
39 }
40
41 static void c_free (RbContainer *e)
42 {
43         c_evas_object_free (&e->real, false);
44
45         free (e);
46 }
47
48 static VALUE c_alloc (VALUE klass)
49 {
50         RbContainer *cont = NULL;
51
52         return Data_Make_Struct (klass, RbContainer, c_mark,
53                                  c_free, cont);
54 }
55
56 static VALUE c_init (VALUE self, VALUE evas)
57 {
58         CHECK_CLASS (evas, cEvas);
59         GET_OBJ (evas, RbEvas, e);
60         GET_OBJ (self, RbContainer, cont);
61
62         cont->real.real = esmart_container_new (e->real);
63
64         rb_call_super (1, &evas);
65
66         cont->elements = rb_ary_new ();
67
68         return self;
69 }
70
71 static VALUE c_append_element (VALUE self, VALUE element)
72 {
73         GET_OBJ (self, RbContainer, e);
74
75         CHECK_CLASS (element, cEvasObject);
76         GET_OBJ (element, RbEvasObject, o);
77
78         esmart_container_element_append (e->real.real, o->real);
79         rb_ary_push (e->elements, element);
80
81         return Qnil;
82 }
83
84 static VALUE c_prepend_element (VALUE self, VALUE element)
85 {
86         GET_OBJ (self, RbContainer, e);
87
88         CHECK_CLASS (element, cEvasObject);
89         GET_OBJ (element, RbEvasObject, o);
90
91         esmart_container_element_prepend (e->real.real, o->real);
92         rb_ary_unshift (e->elements, element);
93
94         return Qnil;
95 }
96
97 static VALUE c_remove_element (VALUE self, VALUE element)
98 {
99         GET_OBJ (self, RbContainer, e);
100
101         CHECK_CLASS (element, cEvasObject);
102         GET_OBJ (element, RbEvasObject, o);
103
104         esmart_container_element_remove (e->real.real, o->real);
105         rb_ary_delete (e->elements, element);
106
107         return Qnil;
108 }
109
110 static VALUE c_elements_get (VALUE self)
111 {
112         VALUE ary;
113
114         GET_OBJ (self, RbContainer, e);
115
116         ary = rb_ary_dup (e->elements);
117         OBJ_FREEZE (ary);
118
119         return ary;
120 }
121
122 static VALUE c_elements_length_get (VALUE self)
123 {
124         double l;
125
126         GET_OBJ (self, RbContainer, e);
127
128         l = esmart_container_elements_length_get (e->real.real);
129
130         return rb_float_new (l);
131 }
132
133 static VALUE c_elements_orig_length_get (VALUE self)
134 {
135         double l;
136
137         GET_OBJ (self, RbContainer, e);
138
139         l = esmart_container_elements_orig_length_get (e->real.real);
140
141         return rb_float_new (l);
142 }
143
144 static VALUE c_direction_get (VALUE self)
145 {
146         GET_OBJ (self, RbContainer, e);
147
148         return INT2FIX (esmart_container_direction_get (e->real.real));
149 }
150
151 static VALUE c_direction_set (VALUE self, VALUE val)
152 {
153         GET_OBJ (self, RbContainer, e);
154
155         Check_Type (val, T_FIXNUM);
156
157         esmart_container_direction_set (e->real.real, FIX2INT (val));
158
159         return Qnil;
160 }
161
162 static VALUE c_spacing_get (VALUE self)
163 {
164         GET_OBJ (self, RbContainer, e);
165
166         return INT2FIX (esmart_container_spacing_get (e->real.real));
167 }
168
169 static VALUE c_spacing_set (VALUE self, VALUE val)
170 {
171         GET_OBJ (self, RbContainer, e);
172
173         Check_Type (val, T_FIXNUM);
174
175         esmart_container_spacing_set (e->real.real, FIX2INT (val));
176
177         return Qnil;
178 }
179
180 static VALUE c_fill_policy_get (VALUE self)
181 {
182         GET_OBJ (self, RbContainer, e);
183
184         return INT2FIX (esmart_container_fill_policy_get (e->real.real));
185 }
186
187 static VALUE c_fill_policy_set (VALUE self, VALUE val)
188 {
189         GET_OBJ (self, RbContainer, e);
190
191         Check_Type (val, T_FIXNUM);
192
193         esmart_container_fill_policy_set (e->real.real, FIX2INT (val));
194
195         return Qnil;
196 }
197
198 static VALUE c_alignment_get (VALUE self)
199 {
200         GET_OBJ (self, RbContainer, e);
201
202         return INT2FIX (esmart_container_alignment_get (e->real.real));
203 }
204
205 static VALUE c_alignment_set (VALUE self, VALUE val)
206 {
207         GET_OBJ (self, RbContainer, e);
208
209         Check_Type (val, T_FIXNUM);
210
211         esmart_container_alignment_set (e->real.real, FIX2INT (val));
212
213         return Qnil;
214 }
215
216
217 static VALUE c_get_padding (VALUE self)
218 {
219         double l = 0, r = 0, t = 0, b = 0;
220
221         GET_OBJ (self, RbContainer, e);
222
223         esmart_container_padding_get (e->real.real, &l, &r, &t, &b);
224
225         return rb_ary_new3 (4, rb_float_new (l), rb_float_new (r),
226                             rb_float_new (t), rb_float_new (b));
227 }
228
229 static VALUE c_set_padding (VALUE self, VALUE l, VALUE r,
230                             VALUE t, VALUE b)
231 {
232         GET_OBJ (self, RbContainer, e);
233
234         Check_Type (l, T_FLOAT);
235         Check_Type (r, T_FLOAT);
236         Check_Type (t, T_FLOAT);
237         Check_Type (b, T_FLOAT);
238
239         esmart_container_padding_set (e->real.real, NUM2DBL (l), NUM2DBL (r),
240                                       NUM2DBL (t), NUM2DBL (b));
241
242         return Qnil;
243 }
244
245 static VALUE c_scroll (VALUE self, VALUE val)
246 {
247         GET_OBJ (self, RbContainer, e);
248
249         Check_Type (val, T_FIXNUM);
250
251         esmart_container_scroll (e->real.real, FIX2INT (val));
252
253         return Qnil;
254 }
255
256 static VALUE c_scroll_percent_get (VALUE self)
257 {
258         double val;
259
260         GET_OBJ (self, RbContainer, e);
261
262         val = esmart_container_scroll_percent_get (e->real.real);
263
264         return rb_float_new (val);
265 }
266
267 static VALUE c_scroll_percent_set (VALUE self, VALUE val)
268 {
269         GET_OBJ (self, RbContainer, e);
270
271         Check_Type (val, T_FLOAT);
272
273         esmart_container_scroll_percent_set (e->real.real, NUM2DBL (val));
274
275         return Qnil;
276 }
277
278 void Init_esmart_container (void)
279 {
280         VALUE c;
281
282         rb_require ("esmart");
283
284         c = rb_define_class_under (mEsmart, "Container", cEvasObject);
285
286         rb_define_alloc_func (c, c_alloc);
287         rb_define_method (c, "initialize", c_init, 1);
288         rb_define_method (c, "direction", c_direction_get, 0);
289         rb_define_method (c, "direction=", c_direction_set, 1);
290         rb_define_method (c, "spacing", c_spacing_get, 0);
291         rb_define_method (c, "spacing=", c_spacing_set, 1);
292         rb_define_method (c, "fill_policy", c_fill_policy_get, 0);
293         rb_define_method (c, "fill_policy=", c_fill_policy_set, 1);
294         rb_define_method (c, "alignment", c_alignment_get, 0);
295         rb_define_method (c, "alignment=", c_alignment_set, 1);
296         rb_define_method (c, "get_padding", c_get_padding, 0);
297         rb_define_method (c, "set_padding", c_set_padding, 4);
298         rb_define_method (c, "append_element", c_append_element, 1);
299         rb_define_method (c, "prepend_element", c_prepend_element, 1);
300         rb_define_method (c, "remove_element", c_remove_element, 1);
301         rb_define_method (c, "elements", c_elements_get, 0);
302         rb_define_method (c, "elements_length", c_elements_length_get, 0);
303         rb_define_method (c, "elements_orig_length",
304                           c_elements_orig_length_get, 0);
305         rb_define_method (c, "scroll", c_scroll, 1);
306         rb_define_method (c, "scroll_percent", c_scroll_percent_get, 0);
307         rb_define_method (c, "scroll_percent=", c_scroll_percent_set, 1);
308
309         rb_define_const (c, "HORIZONTAL",
310                          INT2FIX (CONTAINER_DIRECTION_HORIZONTAL));
311         rb_define_const (c, "VERTICAL",
312                          INT2FIX (CONTAINER_DIRECTION_VERTICAL));
313
314         rb_define_const (c, "NONE",
315                          INT2FIX (CONTAINER_FILL_POLICY_NONE));
316         rb_define_const (c, "KEEP_ASPECT",
317                          INT2FIX (CONTAINER_FILL_POLICY_KEEP_ASPECT));
318         rb_define_const (c, "FILL_X",
319                          INT2FIX (CONTAINER_FILL_POLICY_FILL_X));
320         rb_define_const (c, "FILL_Y",
321                          INT2FIX (CONTAINER_FILL_POLICY_FILL_Y));
322         rb_define_const (c, "HOMOGENOUS",
323                          INT2FIX (CONTAINER_FILL_POLICY_HOMOGENOUS));
324
325         rb_define_const (c, "CENTER",
326                          INT2FIX (CONTAINER_ALIGN_CENTER));
327         rb_define_const (c, "LEFT",
328                          INT2FIX (CONTAINER_ALIGN_LEFT));
329         rb_define_const (c, "RIGHT",
330                          INT2FIX (CONTAINER_ALIGN_RIGHT));
331         rb_define_const (c, "BOTTOM",
332                          INT2FIX (CONTAINER_ALIGN_BOTTOM));
333         rb_define_const (c, "TOP",
334                          INT2FIX (CONTAINER_ALIGN_TOP));
335 }