3cfe2731b331be768e6761d9e1151ce7e3ff685e
[ruby-evas.git] / src / rb_image.c
1 /*
2  * $Id: rb_image.c 382 2006-05-25 09:20: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 <Evas.h>
24
25 #include "rb_evas_main.h"
26 #include "rb_evas.h"
27 #include "rb_evas_object.h"
28
29 /*
30  * call-seq:
31  *  Evas::Image.new(evas) => img
32  *
33  * Creates an Evas::Image object.
34  */
35 static VALUE c_init (VALUE self, VALUE evas)
36 {
37         CHECK_CLASS (evas, cEvas);
38         GET_OBJ (evas, RbEvas, e);
39         GET_OBJ (self, RbEvasObject, img);
40
41         img->real = evas_object_image_add (e->real);
42
43         rb_call_super (1, &evas);
44
45         return self;
46 }
47
48 /*
49  * call-seq:
50  *  img.get_file => array
51  *
52  * Returns an array containing the filename and the key of <i>img</i>.
53  *
54  *  img.set_file("foo", "bar") #=> nil
55  *  img.get_file               #=> ["foo", "bar"]
56  */
57 static VALUE c_get_file (VALUE self)
58 {
59         char *file = NULL, *key = NULL;
60
61         GET_OBJ (self, RbEvasObject, e);
62
63         evas_object_image_file_get (e->real, &file, &key);
64
65         return rb_ary_new3 (2, file ? rb_str_new2 (file) : Qnil,
66                             key ? rb_str_new2 (key) : Qnil);
67 }
68
69 /*
70  * call-seq:
71  *  img.set_file(file [, key]) => nil
72  *
73  * Sets the filename and optionally the key of <i>img</i>.
74  *
75  *  img.set_file("foo.png")             #=> nil
76  *  img.set_file("foo.edb", "/bar/baz") #=> nil
77  */
78 static VALUE c_set_file (int argc, VALUE *argv, VALUE self)
79 {
80         VALUE file, key;
81         char *k = NULL;
82
83         GET_OBJ (self, RbEvasObject, e);
84
85         rb_scan_args (argc, argv, "11", &file, &key);
86
87         if (!NIL_P (key))
88                 k = StringValuePtr (key);
89
90         evas_object_image_file_set (e->real, StringValuePtr (file), k);
91
92         return Qnil;
93 }
94
95 /*
96  * call-seq:
97  *  img.has_alpha? => true or false
98  *
99  * Returns true if <i>img</i> has an alpha channel, else returns false.
100  */
101 static VALUE c_has_alpha_get (VALUE self)
102 {
103         GET_OBJ (self, RbEvasObject, e);
104
105         return evas_object_image_alpha_get (e->real) ? Qtrue : Qfalse;
106 }
107
108 /*
109  * call-seq:
110  *  img.has_alpha(true or false)
111  *
112  * Sets whether <i>img</i> has an alpha channel.
113  */
114 static VALUE c_has_alpha_set (VALUE self, VALUE val)
115 {
116         GET_OBJ (self, RbEvasObject, e);
117
118         CHECK_BOOL (val);
119
120         evas_object_image_alpha_set (e->real, val == Qtrue);
121
122         return Qnil;
123 }
124
125 /*
126  * call-seq:
127  *  img.get_size => array
128  *
129  * Returns an array containing the size of <i>img</i>.
130  *
131  *  img.set_size(100, 200) #=> nil
132  *  img.get_size           #=> [100, 200]
133  */
134 static VALUE c_get_size (VALUE self)
135 {
136         int w = 0, h = 0;
137
138         GET_OBJ (self, RbEvasObject, e);
139
140         evas_object_image_size_get (e->real, &w, &h);
141
142         return rb_ary_new3 (2, INT2FIX (w), INT2FIX (h));
143 }
144
145 /*
146  * call-seq:
147  *  img.set_size(x, y) => nil
148  *
149  * Returns an array containing the size of <i>img</i>.
150  *
151  *  img.set_size(100, 200) #=> nil
152  *  img.get_size           #=> [100, 200]
153  */
154 static VALUE c_set_size (VALUE self, VALUE w, VALUE h)
155 {
156         GET_OBJ (self, RbEvasObject, e);
157
158         Check_Type (w, T_FIXNUM);
159         Check_Type (h, T_FIXNUM);
160
161         evas_object_image_size_set (e->real, FIX2INT (w), FIX2INT (h));
162
163         return Qnil;
164 }
165
166 /*
167  * call-seq:
168  *  img.get_fill => array
169  *
170  * Returns an array containing the dimensions of the rectangle
171  * on <i>img</i> that the image will be drawn to.
172  *
173  *  img.set_fill(1, 2, 3, 4) #=> nil
174  *  img.get_fill             #=> [1, 2, 3, 4]
175  */
176 static VALUE c_get_fill (VALUE self)
177 {
178         Evas_Coord x = 0, y = 0, w = 0, h = 0;
179
180         GET_OBJ (self, RbEvasObject, e);
181
182         evas_object_image_fill_get (e->real, &x, &y, &w, &h);
183
184         return rb_ary_new3 (4, INT2FIX ((int) x), INT2FIX ((int) y),
185                             INT2FIX ((int) w), INT2FIX ((int) h));
186 }
187
188 /*
189  * call-seq:
190  *  img.set_fill(x, y, w, h) => nil
191  *
192  * Sets the dimensions of the rectangle on <i>img</i> that
193  * the image will be drawn to.
194  *
195  *  img.set_fill(1, 2, 3, 4) #=> nil
196  */
197 static VALUE c_set_fill (VALUE self, VALUE x, VALUE y, VALUE w, VALUE h)
198 {
199         GET_OBJ (self, RbEvasObject, e);
200
201         Check_Type (x, T_FIXNUM);
202         Check_Type (y, T_FIXNUM);
203         Check_Type (w, T_FIXNUM);
204         Check_Type (h, T_FIXNUM);
205
206         evas_object_image_fill_set (e->real, FIX2INT (x), FIX2INT (y),
207                                     FIX2INT (w), FIX2INT (h));
208
209         return Qnil;
210 }
211
212 static VALUE c_get_border (VALUE self)
213 {
214         int x = 0, y = 0, w = 0, h = 0;
215
216         GET_OBJ (self, RbEvasObject, e);
217
218         evas_object_image_border_get (e->real, &x, &y, &w, &h);
219
220         return rb_ary_new3 (4, INT2FIX (x), INT2FIX (y),
221                             INT2FIX (w), INT2FIX (h));
222 }
223
224 static VALUE c_set_border (VALUE self, VALUE x, VALUE y, VALUE w, VALUE h)
225 {
226         GET_OBJ (self, RbEvasObject, e);
227
228         Check_Type (x, T_FIXNUM);
229         Check_Type (y, T_FIXNUM);
230         Check_Type (w, T_FIXNUM);
231         Check_Type (h, T_FIXNUM);
232
233         evas_object_image_border_set (e->real, FIX2INT (x), FIX2INT (y),
234                                       FIX2INT (w), FIX2INT (h));
235
236         return Qnil;
237 }
238
239 /*
240  * call-seq:
241  *  img.reload => nil
242  *
243  * Reloads <i>img</i>.
244  */
245 static VALUE c_reload (VALUE self)
246 {
247         GET_OBJ (self, RbEvasObject, e);
248
249         evas_object_image_reload (e->real);
250
251         return Qnil;
252 }
253
254 static VALUE c_data_get (int argc, VALUE *argv, VALUE self)
255 {
256         VALUE read_write = Qfalse;
257         void *data;
258         int w = 0, h = 0;
259
260         GET_OBJ (self, RbEvasObject, e);
261
262         rb_scan_args (argc, argv, "01", &read_write);
263
264         if (NIL_P (read_write))
265                 read_write = Qfalse;
266
267         data = evas_object_image_data_get (e->real, read_write == Qtrue);
268         evas_object_image_size_get (e->real, &w, &h);
269
270         return rb_str_new (data, h * w * 4);
271 }
272
273 static VALUE c_data_set (VALUE self, VALUE data)
274 {
275         GET_OBJ (self, RbEvasObject, e);
276
277         evas_object_image_data_set (e->real, StringValuePtr (data));
278
279         return Qnil;
280 }
281
282 static VALUE c_data_update_add (VALUE self, VALUE x, VALUE y,
283                                 VALUE w, VALUE h)
284 {
285         GET_OBJ (self, RbEvasObject, e);
286
287         evas_object_image_data_update_add (e->real,
288                                            FIX2INT (x), FIX2INT (y),
289                                            FIX2INT (w), FIX2INT (h));
290
291         return Qnil;
292 }
293
294 void Init_Image (void)
295 {
296         VALUE c = rb_define_class_under (mEvas, "Image", cEvasObject);
297
298         rb_define_method (c, "initialize", c_init, 1);
299         rb_define_method (c, "get_file", c_get_file, -1);
300         rb_define_method (c, "set_file", c_set_file, -1);
301         rb_define_method (c, "has_alpha?", c_has_alpha_get, 0);
302         rb_define_method (c, "has_alpha=", c_has_alpha_set, 1);
303         rb_define_method (c, "get_size", c_get_size, 0);
304         rb_define_method (c, "set_size", c_set_size, 2);
305         rb_define_method (c, "get_fill", c_get_fill, 0);
306         rb_define_method (c, "set_fill", c_set_fill, 4);
307         rb_define_method (c, "get_border", c_get_border, 0);
308         rb_define_method (c, "set_border", c_set_border, 4);
309         rb_define_method (c, "reload", c_reload, 0);
310         rb_define_method (c, "data", c_data_get, -1);
311         rb_define_method (c, "data=", c_data_set, 1);
312         rb_define_method (c, "data_update_add", c_data_update_add, 4);
313 }