2 * $Id: rb_image.c 382 2006-05-25 09:20:31Z tilman $
4 * Copyright (C) 2004 Tilman Sauerbeck (tilman at code-monkey de)
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.
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.
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
25 #include "rb_evas_main.h"
27 #include "rb_evas_object.h"
31 * Evas::Image.new(evas) => img
33 * Creates an Evas::Image object.
35 static VALUE c_init (VALUE self, VALUE evas)
37 CHECK_CLASS (evas, cEvas);
38 GET_OBJ (evas, RbEvas, e);
39 GET_OBJ (self, RbEvasObject, img);
41 img->real = evas_object_image_add (e->real);
43 rb_call_super (1, &evas);
50 * img.get_file => array
52 * Returns an array containing the filename and the key of <i>img</i>.
54 * img.set_file("foo", "bar") #=> nil
55 * img.get_file #=> ["foo", "bar"]
57 static VALUE c_get_file (VALUE self)
59 char *file = NULL, *key = NULL;
61 GET_OBJ (self, RbEvasObject, e);
63 evas_object_image_file_get (e->real, &file, &key);
65 return rb_ary_new3 (2, file ? rb_str_new2 (file) : Qnil,
66 key ? rb_str_new2 (key) : Qnil);
71 * img.set_file(file [, key]) => nil
73 * Sets the filename and optionally the key of <i>img</i>.
75 * img.set_file("foo.png") #=> nil
76 * img.set_file("foo.edb", "/bar/baz") #=> nil
78 static VALUE c_set_file (int argc, VALUE *argv, VALUE self)
83 GET_OBJ (self, RbEvasObject, e);
85 rb_scan_args (argc, argv, "11", &file, &key);
88 k = StringValuePtr (key);
90 evas_object_image_file_set (e->real, StringValuePtr (file), k);
97 * img.has_alpha? => true or false
99 * Returns true if <i>img</i> has an alpha channel, else returns false.
101 static VALUE c_has_alpha_get (VALUE self)
103 GET_OBJ (self, RbEvasObject, e);
105 return evas_object_image_alpha_get (e->real) ? Qtrue : Qfalse;
110 * img.has_alpha(true or false)
112 * Sets whether <i>img</i> has an alpha channel.
114 static VALUE c_has_alpha_set (VALUE self, VALUE val)
116 GET_OBJ (self, RbEvasObject, e);
120 evas_object_image_alpha_set (e->real, val == Qtrue);
127 * img.get_size => array
129 * Returns an array containing the size of <i>img</i>.
131 * img.set_size(100, 200) #=> nil
132 * img.get_size #=> [100, 200]
134 static VALUE c_get_size (VALUE self)
138 GET_OBJ (self, RbEvasObject, e);
140 evas_object_image_size_get (e->real, &w, &h);
142 return rb_ary_new3 (2, INT2FIX (w), INT2FIX (h));
147 * img.set_size(x, y) => nil
149 * Returns an array containing the size of <i>img</i>.
151 * img.set_size(100, 200) #=> nil
152 * img.get_size #=> [100, 200]
154 static VALUE c_set_size (VALUE self, VALUE w, VALUE h)
156 GET_OBJ (self, RbEvasObject, e);
158 Check_Type (w, T_FIXNUM);
159 Check_Type (h, T_FIXNUM);
161 evas_object_image_size_set (e->real, FIX2INT (w), FIX2INT (h));
168 * img.get_fill => array
170 * Returns an array containing the dimensions of the rectangle
171 * on <i>img</i> that the image will be drawn to.
173 * img.set_fill(1, 2, 3, 4) #=> nil
174 * img.get_fill #=> [1, 2, 3, 4]
176 static VALUE c_get_fill (VALUE self)
178 Evas_Coord x = 0, y = 0, w = 0, h = 0;
180 GET_OBJ (self, RbEvasObject, e);
182 evas_object_image_fill_get (e->real, &x, &y, &w, &h);
184 return rb_ary_new3 (4, INT2FIX ((int) x), INT2FIX ((int) y),
185 INT2FIX ((int) w), INT2FIX ((int) h));
190 * img.set_fill(x, y, w, h) => nil
192 * Sets the dimensions of the rectangle on <i>img</i> that
193 * the image will be drawn to.
195 * img.set_fill(1, 2, 3, 4) #=> nil
197 static VALUE c_set_fill (VALUE self, VALUE x, VALUE y, VALUE w, VALUE h)
199 GET_OBJ (self, RbEvasObject, e);
201 Check_Type (x, T_FIXNUM);
202 Check_Type (y, T_FIXNUM);
203 Check_Type (w, T_FIXNUM);
204 Check_Type (h, T_FIXNUM);
206 evas_object_image_fill_set (e->real, FIX2INT (x), FIX2INT (y),
207 FIX2INT (w), FIX2INT (h));
212 static VALUE c_get_border (VALUE self)
214 int x = 0, y = 0, w = 0, h = 0;
216 GET_OBJ (self, RbEvasObject, e);
218 evas_object_image_border_get (e->real, &x, &y, &w, &h);
220 return rb_ary_new3 (4, INT2FIX (x), INT2FIX (y),
221 INT2FIX (w), INT2FIX (h));
224 static VALUE c_set_border (VALUE self, VALUE x, VALUE y, VALUE w, VALUE h)
226 GET_OBJ (self, RbEvasObject, e);
228 Check_Type (x, T_FIXNUM);
229 Check_Type (y, T_FIXNUM);
230 Check_Type (w, T_FIXNUM);
231 Check_Type (h, T_FIXNUM);
233 evas_object_image_border_set (e->real, FIX2INT (x), FIX2INT (y),
234 FIX2INT (w), FIX2INT (h));
243 * Reloads <i>img</i>.
245 static VALUE c_reload (VALUE self)
247 GET_OBJ (self, RbEvasObject, e);
249 evas_object_image_reload (e->real);
254 static VALUE c_data_get (int argc, VALUE *argv, VALUE self)
256 VALUE read_write = Qfalse;
260 GET_OBJ (self, RbEvasObject, e);
262 rb_scan_args (argc, argv, "01", &read_write);
264 if (NIL_P (read_write))
267 data = evas_object_image_data_get (e->real, read_write == Qtrue);
268 evas_object_image_size_get (e->real, &w, &h);
270 return rb_str_new (data, h * w * 4);
273 static VALUE c_data_set (VALUE self, VALUE data)
275 GET_OBJ (self, RbEvasObject, e);
277 evas_object_image_data_set (e->real, StringValuePtr (data));
282 static VALUE c_data_update_add (VALUE self, VALUE x, VALUE y,
285 GET_OBJ (self, RbEvasObject, e);
287 evas_object_image_data_update_add (e->real,
288 FIX2INT (x), FIX2INT (y),
289 FIX2INT (w), FIX2INT (h));
294 void Init_Image (void)
296 VALUE c = rb_define_class_under (mEvas, "Image", cEvasObject);
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);