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