Added style support to Evas::Text.
[ruby-evas.git] / src / rb_text.c
1 /*
2  * $Id: rb_text.c 368 2006-02-15 18:07:49Z 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 #define DEF_CONST(mod, prefix, name) \
30         rb_define_const ((mod), #name, INT2FIX (prefix##name));
31
32 #define GET_COLOR_METHOD(name) \
33         int r = 0, g = 0, b = 0, a = 0; \
34 \
35         GET_OBJ (self, RbEvasObject, e); \
36 \
37         evas_object_text_##name##_color_get (e->real, &r, &g, &b, &a); \
38 \
39         return rb_ary_new3 (4, \
40                             INT2FIX (r), INT2FIX (g), \
41                             INT2FIX (b), INT2FIX (a));
42
43 #define SET_COLOR_METHOD(name) \
44         GET_OBJ (self, RbEvasObject, e); \
45 \
46         Check_Type (r, T_FIXNUM); \
47         Check_Type (g, T_FIXNUM); \
48         Check_Type (b, T_FIXNUM); \
49         Check_Type (a, T_FIXNUM); \
50 \
51         evas_object_text_##name##_color_set (e->real, \
52                                              FIX2INT (r), FIX2INT (g), \
53                                              FIX2INT (b), FIX2INT (a)); \
54 \
55         return Qnil;
56
57 /*
58  * call-seq:
59  *  Evas::Text.new(evas) => text
60  *
61  * Creates an Evas::Text object.
62  */
63 static VALUE c_init (VALUE self, VALUE evas)
64 {
65         CHECK_CLASS (evas, cEvas);
66         GET_OBJ (evas, RbEvas, e);
67         GET_OBJ (self, RbEvasObject, text);
68
69         text->real = evas_object_text_add (e->real);
70
71         rb_call_super (1, &evas);
72
73         return self;
74 }
75
76 /*
77  * call-seq:
78  *  text.font_source => string or nil
79  *
80  * Returns the font source of <i>text</i>.
81  */
82 static VALUE c_font_source_get (VALUE self)
83 {
84         const char *tmp;
85
86         GET_OBJ (self, RbEvasObject, e);
87
88         if (!(tmp = evas_object_text_font_source_get (e->real)))
89                 return Qnil;
90         else
91                 return rb_str_new2 (tmp);
92 }
93
94 /*
95  * call-seq:
96  *  text.font_source(source)
97  *
98  * Sets the font source of <i>text</i>.
99  */
100 static VALUE c_font_source_set (VALUE self, VALUE val)
101 {
102         GET_OBJ (self, RbEvasObject, e);
103
104         Check_Type (val, T_STRING);
105
106         evas_object_text_font_source_set (e->real, StringValuePtr (val));
107
108         return Qnil;
109 }
110
111 /*
112  * call-seq:
113  *  text.get_font => array
114  *
115  * Returns the font name and font size of <i>text</i>.
116  *
117  *  text.set_font("vera", 10) #=> nil
118  *  text_get_font             #=> ["vera", 10]
119  */
120 static VALUE c_get_font (VALUE self)
121 {
122         char *font = NULL;
123         Evas_Font_Size size = 0;
124
125         GET_OBJ (self, RbEvasObject, e);
126
127         evas_object_text_font_get (e->real, &font, &size);
128
129         return rb_ary_new3 (2, font ? rb_str_new2 (font) : Qnil,
130                             INT2FIX (size));
131 }
132
133 /*
134  * call-seq:
135  *  text.set_font(font, size) => nil
136  *
137  * Sets the font name and font size of <i>text</i>.
138  *
139  *  text.set_font("vera", 10) #=> nil
140  */
141 static VALUE c_set_font (VALUE self, VALUE font, VALUE size)
142 {
143         GET_OBJ (self, RbEvasObject, e);
144
145         Check_Type (font, T_STRING);
146         Check_Type (size, T_FIXNUM);
147
148         evas_object_text_font_set (e->real, StringValuePtr (font),
149                                    FIX2INT (size));
150
151         return Qnil;
152 }
153
154 /*
155  * call-seq:
156  *  text.text => string
157  *
158  * Returns the text of <i>text</i>.
159  */
160 static VALUE c_text_get (VALUE self)
161 {
162         const char *tmp;
163
164         GET_OBJ (self, RbEvasObject, e);
165
166         if (!(tmp = evas_object_text_text_get (e->real)))
167                 return Qnil;
168         else
169                 return rb_str_new2 (tmp);
170 }
171
172 /*
173  * call-seq:
174  *  text.text(string)
175  *
176  * Sets the text of <i>text</i>.
177  */
178 static VALUE c_text_set (VALUE self, VALUE val)
179 {
180         GET_OBJ (self, RbEvasObject, e);
181
182         Check_Type (val, T_STRING);
183
184         evas_object_text_text_set (e->real, StringValuePtr (val));
185
186         return Qnil;
187 }
188
189 /*
190  * call-seq:
191  *  text.style => integer
192  *
193  * Returns the style of <i>text</i>.
194  */
195 static VALUE c_style_get (VALUE self)
196 {
197         GET_OBJ (self, RbEvasObject, e);
198
199         return INT2FIX (evas_object_text_style_get (e->real));
200 }
201
202 /*
203  * call-seq:
204  *  text.style = integer
205  *
206  * Sets the style of <i>text</i>.
207  */
208 static VALUE c_style_set (VALUE self, VALUE val)
209 {
210         GET_OBJ (self, RbEvasObject, e);
211
212         Check_Type (val, T_FIXNUM);
213
214         evas_object_text_style_set (e->real, FIX2INT (val));
215
216         return Qnil;
217 }
218
219 static VALUE c_style_pad_get (VALUE self)
220 {
221         int l = 0, r = 0, t = 0, b = 0;
222
223         GET_OBJ (self, RbEvasObject, e);
224
225         evas_object_text_style_pad_get (e->real, &l, &r, &t, &b);
226
227         return rb_ary_new3 (4,
228                             INT2FIX (l), INT2FIX (r),
229                             INT2FIX (r), INT2FIX (b));
230 }
231
232 /*
233  * call-seq:
234  *  e.get_shadow_color => array
235  *
236  * Returns the shadow color of <i>e</i>.
237  *
238  *  e.set_shadow_color(128, 128, 128, 0) #=> nil
239  *  e.get_shadow_color                   #=> [128, 128, 128, 0]
240  */
241 static VALUE c_get_shadow_color (VALUE self)
242 {
243         GET_COLOR_METHOD (shadow);
244 }
245
246 /*
247  * call-seq:
248  *  e.set_shadow_color(r, g, b, a) => nil
249  *
250  * Sets the shadow color of <i>e</i>.
251  *
252  *  e.set_shadow_color(128, 128, 128, 0) #=> nil
253  */
254 static VALUE c_set_shadow_color (VALUE self,
255                                  VALUE r, VALUE g, VALUE b, VALUE a)
256 {
257         SET_COLOR_METHOD (shadow);
258 }
259
260 /*
261  * call-seq:
262  *  e.get_glow_color => array
263  *
264  * Returns the glow color of <i>e</i>.
265  *
266  *  e.set_glow_color(128, 128, 128, 0) #=> nil
267  *  e.get_glow_color                   #=> [128, 128, 128, 0]
268  */
269 static VALUE c_get_glow_color (VALUE self)
270 {
271         GET_COLOR_METHOD (glow);
272 }
273
274 /*
275  * call-seq:
276  *  e.set_glow_color(r, g, b, a) => nil
277  *
278  * Sets the glow color of <i>e</i>.
279  *
280  *  e.set_glow_color(128, 128, 128, 0) #=> nil
281  */
282 static VALUE c_set_glow_color (VALUE self,
283                                  VALUE r, VALUE g, VALUE b, VALUE a)
284 {
285         SET_COLOR_METHOD (glow);
286 }
287
288 /*
289  * call-seq:
290  *  e.get_glow2_color => array
291  *
292  * Returns the glow2 color of <i>e</i>.
293  *
294  *  e.set_glow2_color(128, 128, 128, 0) #=> nil
295  *  e.get_glow2_color                   #=> [128, 128, 128, 0]
296  */
297 static VALUE c_get_glow2_color (VALUE self)
298 {
299         GET_COLOR_METHOD (glow2);
300 }
301
302 /*
303  * call-seq:
304  *  e.set_glow2_color(r, g, b, a) => nil
305  *
306  * Sets the glow2 color of <i>e</i>.
307  *
308  *  e.set_glow2_color(128, 128, 128, 0) #=> nil
309  */
310 static VALUE c_set_glow2_color (VALUE self,
311                                VALUE r, VALUE g, VALUE b, VALUE a)
312 {
313         SET_COLOR_METHOD (glow2);
314 }
315
316 /*
317  * call-seq:
318  *  e.get_outline_color => array
319  *
320  * Returns the outline color of <i>e</i>.
321  *
322  *  e.set_outline_color(128, 128, 128, 0) #=> nil
323  *  e.get_outline_color                   #=> [128, 128, 128, 0]
324  */
325 static VALUE c_get_outline_color (VALUE self)
326 {
327         GET_COLOR_METHOD (outline);
328 }
329
330 /*
331  * call-seq:
332  *  e.set_outline_color(r, g, b, a) => nil
333  *
334  * Sets the outline color of <i>e</i>.
335  *
336  *  e.set_outline_color(128, 128, 128, 0) #=> nil
337  */
338 static VALUE c_set_outline_color (VALUE self,
339                                   VALUE r, VALUE g, VALUE b, VALUE a)
340 {
341         SET_COLOR_METHOD (outline);
342 }
343
344 void Init_Text (void)
345 {
346         VALUE c, c2;
347
348         c = rb_define_class_under (mEvas, "Text", cEvasObject);
349
350         rb_define_method (c, "initialize", c_init, 1);
351         rb_define_method (c, "font_source", c_font_source_get, 0);
352         rb_define_method (c, "font_source=", c_font_source_set, 1);
353         rb_define_method (c, "get_font", c_get_font, 0);
354         rb_define_method (c, "set_font", c_set_font, 2);
355         rb_define_method (c, "text", c_text_get, 0);
356         rb_define_method (c, "text=", c_text_set, 1);
357         rb_define_method (c, "style", c_style_get, 0);
358         rb_define_method (c, "style=", c_style_set, 1);
359         rb_define_method (c, "style_pad", c_style_pad_get, 0);
360         rb_define_method (c, "get_shadow_color", c_get_shadow_color, 0);
361         rb_define_method (c, "set_shadow_color", c_set_shadow_color, 4);
362         rb_define_method (c, "get_glow_color", c_get_glow_color, 0);
363         rb_define_method (c, "set_glow_color", c_set_glow_color, 4);
364         rb_define_method (c, "get_glow2_color", c_get_glow2_color, 0);
365         rb_define_method (c, "set_glow2_color", c_set_glow2_color, 4);
366         rb_define_method (c, "get_outline_color", c_get_outline_color, 0);
367         rb_define_method (c, "set_outline_color", c_set_outline_color, 4);
368
369         c2 = rb_define_class_under (mEvas, "Style", c);
370
371         DEF_CONST (c2, EVAS_TEXT_STYLE_, PLAIN);
372         DEF_CONST (c2, EVAS_TEXT_STYLE_, SHADOW);
373         DEF_CONST (c2, EVAS_TEXT_STYLE_, OUTLINE);
374         DEF_CONST (c2, EVAS_TEXT_STYLE_, SOFT_OUTLINE);
375         DEF_CONST (c2, EVAS_TEXT_STYLE_, GLOW);
376         DEF_CONST (c2, EVAS_TEXT_STYLE_, OUTLINE_SHADOW);
377         DEF_CONST (c2, EVAS_TEXT_STYLE_, FAR_SHADOW);
378         DEF_CONST (c2, EVAS_TEXT_STYLE_, OUTLINE_SOFT_SHADOW);
379         DEF_CONST (c2, EVAS_TEXT_STYLE_, SOFT_SHADOW);
380         DEF_CONST (c2, EVAS_TEXT_STYLE_, FAR_SOFT_SHADOW);
381 }