8336cd02c22e77042a65be3ce4a579e421feaabe
[ruby-esmart.git] / src / esmart_container / rb_esmart_container.c
1 /*
2  * $Id$
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 <rb_evas.h>
25 #include <rb_evas_object.h>
26
27 #include "../rb_esmart.h"
28
29 static VALUE c_new (VALUE klass, VALUE evas)
30 {
31         VALUE self, argv[1];
32         Evas_Object **cont;
33
34         CHECK_CLASS (evas, cEvas);
35         GET_OBJ (evas, Evas *, e);
36
37         self = Data_Make_Struct (klass, Evas_Object *, c_evas_object_mark,
38                                  c_evas_object_free, cont);
39         *cont = esmart_container_new (*e);
40
41         argv[0] = evas;
42         rb_obj_call_init (self, 1, argv);
43
44         return self;
45 }
46
47 static VALUE c_element_append (VALUE self, VALUE element)
48 {
49         GET_OBJ (self, Evas_Object *, e);
50
51         CHECK_CLASS (element, cEvasObject);
52         GET_OBJ (element, Evas_Object *, o);
53
54         esmart_container_element_append (*e, *o);
55
56         return Qnil;
57 }
58
59 static VALUE c_element_prepend (VALUE self, VALUE element)
60 {
61         GET_OBJ (self, Evas_Object *, e);
62
63         CHECK_CLASS (element, cEvasObject);
64         GET_OBJ (element, Evas_Object *, o);
65
66         esmart_container_element_prepend (*e, *o);
67
68         return Qnil;
69 }
70
71 static VALUE c_element_remove (VALUE self, VALUE element)
72 {
73         GET_OBJ (self, Evas_Object *, e);
74
75         CHECK_CLASS (element, cEvasObject);
76         GET_OBJ (element, Evas_Object *, o);
77
78         esmart_container_element_remove (*e, *o);
79
80         return Qnil;
81 }
82
83 static VALUE c_direction_get (VALUE self)
84 {
85         GET_OBJ (self, Evas_Object *, e);
86
87         return INT2FIX (esmart_container_direction_get (*e));
88 }
89
90 static VALUE c_direction_set (VALUE self, VALUE val)
91 {
92         GET_OBJ (self, Evas_Object *, e);
93
94         Check_Type (val, T_FIXNUM);
95
96         esmart_container_direction_set (*e, FIX2INT (val));
97
98         return Qnil;
99 }
100
101 static VALUE c_spacing_get (VALUE self)
102 {
103         GET_OBJ (self, Evas_Object *, e);
104
105         return INT2FIX (esmart_container_spacing_get (*e));
106 }
107
108 static VALUE c_spacing_set (VALUE self, VALUE val)
109 {
110         GET_OBJ (self, Evas_Object *, e);
111
112         Check_Type (val, T_FIXNUM);
113
114         esmart_container_spacing_set (*e, FIX2INT (val));
115
116         return Qnil;
117 }
118
119 static VALUE c_fill_policy_get (VALUE self)
120 {
121         GET_OBJ (self, Evas_Object *, e);
122
123         return INT2FIX (esmart_container_fill_policy_get (*e));
124 }
125
126 static VALUE c_fill_policy_set (VALUE self, VALUE val)
127 {
128         GET_OBJ (self, Evas_Object *, e);
129
130         Check_Type (val, T_FIXNUM);
131
132         esmart_container_fill_policy_set (*e, FIX2INT (val));
133
134         return Qnil;
135 }
136
137 static VALUE c_get_padding (VALUE self)
138 {
139         double l = 0, r = 0, t = 0, b = 0;
140
141         GET_OBJ (self, Evas_Object *, e);
142
143         esmart_container_padding_get (*e, &l, &r, &t, &b);
144
145         return rb_ary_new3 (4, rb_float_new (l), rb_float_new (r),
146                             rb_float_new (t), rb_float_new (b));
147 }
148
149 static VALUE c_set_padding (VALUE self, VALUE l, VALUE r,
150                             VALUE t, VALUE b)
151 {
152         GET_OBJ (self, Evas_Object *, e);
153
154         Check_Type (l, T_FLOAT);
155         Check_Type (r, T_FLOAT);
156         Check_Type (t, T_FLOAT);
157         Check_Type (b, T_FLOAT);
158
159         esmart_container_padding_set (*e, NUM2DBL (l), NUM2DBL (r),
160                                       NUM2DBL (t), NUM2DBL (b));
161
162         return Qnil;
163 }
164
165 static VALUE c_scroll (VALUE self, VALUE val)
166 {
167         GET_OBJ (self, Evas_Object *, e);
168
169         Check_Type (val, T_FIXNUM);
170
171         esmart_container_scroll (*e, FIX2INT (val));
172
173         return Qnil;
174 }
175
176 void Init_esmart_container (void)
177 {
178         VALUE c;
179
180         rb_require ("esmart");
181
182         c = rb_define_class_under (mEsmart, "Container", cEvasObject);
183
184         rb_define_singleton_method (c, "new", c_new, 1);
185         rb_define_method (c, "direction", c_direction_get, 0);
186         rb_define_method (c, "direction=", c_direction_set, 1);
187         rb_define_method (c, "spacing", c_spacing_get, 0);
188         rb_define_method (c, "spacing=", c_spacing_set, 1);
189         rb_define_method (c, "fill_policy", c_fill_policy_get, 0);
190         rb_define_method (c, "fill_policy=", c_fill_policy_set, 1);
191         rb_define_method (c, "get_padding", c_get_padding, 0);
192         rb_define_method (c, "set_padding", c_set_padding, 4);
193         rb_define_method (c, "element_append", c_element_append, 1);
194         rb_define_method (c, "element_prepend", c_element_prepend, 1);
195         rb_define_method (c, "element_remove", c_element_remove, 1);
196         rb_define_method (c, "scroll", c_scroll, 1);
197
198         rb_define_const (c, "HORIZONTAL",
199                          INT2FIX (CONTAINER_DIRECTION_HORIZONTAL));
200         rb_define_const (c, "VERTICAL",
201                          INT2FIX (CONTAINER_DIRECTION_VERTICAL));
202
203         rb_define_const (c, "NONE",
204                          INT2FIX (CONTAINER_FILL_POLICY_NONE));
205         rb_define_const (c, "KEEP_ASPECT",
206                          INT2FIX (CONTAINER_FILL_POLICY_KEEP_ASPECT));
207         rb_define_const (c, "FILL_X",
208                          INT2FIX (CONTAINER_FILL_POLICY_FILL_X));
209         rb_define_const (c, "FILL_Y",
210                          INT2FIX (CONTAINER_FILL_POLICY_FILL_Y));
211         rb_define_const (c, "HOMOGENOUS",
212                          INT2FIX (CONTAINER_FILL_POLICY_HOMOGENOUS));
213 }