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