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