fcc70c403c2542f75340249ad92293233caee6ae
[ruby-evas.git] / src / rb_text.c
1 /*
2  * $Id: rb_text.c 388 2006-10-07 14:39:24Z 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         evas_object_text_font_source_set (e->real, StringValuePtr (val));
105
106         return Qnil;
107 }
108
109 /*
110  * call-seq:
111  *  text.get_font => array
112  *
113  * Returns the font name and font size of <i>text</i>.
114  *
115  *  text.set_font("vera", 10) #=> nil
116  *  text_get_font             #=> ["vera", 10]
117  */
118 static VALUE c_get_font (VALUE self)
119 {
120         char *font = NULL;
121         Evas_Font_Size size = 0;
122
123         GET_OBJ (self, RbEvasObject, e);
124
125         evas_object_text_font_get (e->real, &font, &size);
126
127         return rb_ary_new3 (2, font ? rb_str_new2 (font) : Qnil,
128                             INT2FIX (size));
129 }
130
131 /*
132  * call-seq:
133  *  text.set_font(font, size) => nil
134  *
135  * Sets the font name and font size of <i>text</i>.
136  *
137  *  text.set_font("vera", 10) #=> nil
138  */
139 static VALUE c_set_font (VALUE self, VALUE font, VALUE size)
140 {
141         GET_OBJ (self, RbEvasObject, e);
142
143         Check_Type (size, T_FIXNUM);
144
145         evas_object_text_font_set (e->real, StringValuePtr (font),
146                                    FIX2INT (size));
147
148         return Qnil;
149 }
150
151 /*
152  * call-seq:
153  *  text.text => string
154  *
155  * Returns the text of <i>text</i>.
156  */
157 static VALUE c_text_get (VALUE self)
158 {
159         const char *tmp;
160
161         GET_OBJ (self, RbEvasObject, e);
162
163         if (!(tmp = evas_object_text_text_get (e->real)))
164                 return Qnil;
165         else
166                 return rb_str_new2 (tmp);
167 }
168
169 /*
170  * call-seq:
171  *  text.text(string)
172  *
173  * Sets the text of <i>text</i>.
174  */
175 static VALUE c_text_set (VALUE self, VALUE val)
176 {
177         GET_OBJ (self, RbEvasObject, e);
178
179         evas_object_text_text_set (e->real, StringValuePtr (val));
180
181         return Qnil;
182 }
183
184 /*
185  * call-seq:
186  *  text.style => integer
187  *
188  * Returns the style of <i>text</i>.
189  */
190 static VALUE c_style_get (VALUE self)
191 {
192         GET_OBJ (self, RbEvasObject, e);
193
194         return INT2FIX (evas_object_text_style_get (e->real));
195 }
196
197 /*
198  * call-seq:
199  *  text.style = integer
200  *
201  * Sets the style of <i>text</i>.
202  */
203 static VALUE c_style_set (VALUE self, VALUE val)
204 {
205         GET_OBJ (self, RbEvasObject, e);
206
207         Check_Type (val, T_FIXNUM);
208
209         evas_object_text_style_set (e->real, FIX2INT (val));
210
211         return Qnil;
212 }
213
214 static VALUE c_style_pad_get (VALUE self)
215 {
216         int l = 0, r = 0, t = 0, b = 0;
217
218         GET_OBJ (self, RbEvasObject, e);
219
220         evas_object_text_style_pad_get (e->real, &l, &r, &t, &b);
221
222         return rb_ary_new3 (4,
223                             INT2FIX (l), INT2FIX (r),
224                             INT2FIX (r), INT2FIX (b));
225 }
226
227 /*
228  * call-seq:
229  *  e.get_shadow_color => array
230  *
231  * Returns the shadow color of <i>e</i>.
232  *
233  *  e.set_shadow_color(128, 128, 128, 0) #=> nil
234  *  e.get_shadow_color                   #=> [128, 128, 128, 0]
235  */
236 static VALUE c_get_shadow_color (VALUE self)
237 {
238         GET_COLOR_METHOD (shadow);
239 }
240
241 /*
242  * call-seq:
243  *  e.set_shadow_color(r, g, b, a) => nil
244  *
245  * Sets the shadow color of <i>e</i>.
246  *
247  *  e.set_shadow_color(128, 128, 128, 0) #=> nil
248  */
249 static VALUE c_set_shadow_color (VALUE self,
250                                  VALUE r, VALUE g, VALUE b, VALUE a)
251 {
252         SET_COLOR_METHOD (shadow);
253 }
254
255 /*
256  * call-seq:
257  *  e.get_glow_color => array
258  *
259  * Returns the glow color of <i>e</i>.
260  *
261  *  e.set_glow_color(128, 128, 128, 0) #=> nil
262  *  e.get_glow_color                   #=> [128, 128, 128, 0]
263  */
264 static VALUE c_get_glow_color (VALUE self)
265 {
266         GET_COLOR_METHOD (glow);
267 }
268
269 /*
270  * call-seq:
271  *  e.set_glow_color(r, g, b, a) => nil
272  *
273  * Sets the glow color of <i>e</i>.
274  *
275  *  e.set_glow_color(128, 128, 128, 0) #=> nil
276  */
277 static VALUE c_set_glow_color (VALUE self,
278                                  VALUE r, VALUE g, VALUE b, VALUE a)
279 {
280         SET_COLOR_METHOD (glow);
281 }
282
283 /*
284  * call-seq:
285  *  e.get_glow2_color => array
286  *
287  * Returns the glow2 color of <i>e</i>.
288  *
289  *  e.set_glow2_color(128, 128, 128, 0) #=> nil
290  *  e.get_glow2_color                   #=> [128, 128, 128, 0]
291  */
292 static VALUE c_get_glow2_color (VALUE self)
293 {
294         GET_COLOR_METHOD (glow2);
295 }
296
297 /*
298  * call-seq:
299  *  e.set_glow2_color(r, g, b, a) => nil
300  *
301  * Sets the glow2 color of <i>e</i>.
302  *
303  *  e.set_glow2_color(128, 128, 128, 0) #=> nil
304  */
305 static VALUE c_set_glow2_color (VALUE self,
306                                VALUE r, VALUE g, VALUE b, VALUE a)
307 {
308         SET_COLOR_METHOD (glow2);
309 }
310
311 /*
312  * call-seq:
313  *  e.get_outline_color => array
314  *
315  * Returns the outline color of <i>e</i>.
316  *
317  *  e.set_outline_color(128, 128, 128, 0) #=> nil
318  *  e.get_outline_color                   #=> [128, 128, 128, 0]
319  */
320 static VALUE c_get_outline_color (VALUE self)
321 {
322         GET_COLOR_METHOD (outline);
323 }
324
325 /*
326  * call-seq:
327  *  e.set_outline_color(r, g, b, a) => nil
328  *
329  * Sets the outline color of <i>e</i>.
330  *
331  *  e.set_outline_color(128, 128, 128, 0) #=> nil
332  */
333 static VALUE c_set_outline_color (VALUE self,
334                                   VALUE r, VALUE g, VALUE b, VALUE a)
335 {
336         SET_COLOR_METHOD (outline);
337 }
338
339 static VALUE c_ascent_get (VALUE self)
340 {
341         GET_OBJ (self, RbEvasObject, e);
342
343         return INT2FIX ((int) evas_object_text_ascent_get (e->real));
344 }
345
346 static VALUE c_descent_get (VALUE self)
347 {
348         GET_OBJ (self, RbEvasObject, e);
349
350         return INT2FIX ((int) evas_object_text_descent_get (e->real));
351 }
352
353 static VALUE c_max_ascent_get (VALUE self)
354 {
355         GET_OBJ (self, RbEvasObject, e);
356
357         return INT2FIX ((int) evas_object_text_max_ascent_get (e->real));
358 }
359
360 static VALUE c_max_descent_get (VALUE self)
361 {
362         GET_OBJ (self, RbEvasObject, e);
363
364         return INT2FIX ((int) evas_object_text_max_descent_get (e->real));
365 }
366
367 static VALUE c_advance_get (VALUE self)
368 {
369         int h = 0, v = 0;
370
371         GET_OBJ (self, RbEvasObject, e);
372
373         h = (int) evas_object_text_horiz_advance_get (e->real);
374         v = (int) evas_object_text_vert_advance_get (e->real);
375
376         return rb_ary_new3 (2, INT2FIX (h), INT2FIX (v));
377 }
378
379 static VALUE c_inset_get (VALUE self)
380 {
381         GET_OBJ (self, RbEvasObject, e);
382
383         return INT2FIX ((int) evas_object_text_inset_get (e->real));
384 }
385
386 void Init_Text (void)
387 {
388         VALUE c, c2;
389
390         c = rb_define_class_under (mEvas, "Text", cEvasObject);
391
392         rb_define_method (c, "initialize", c_init, 1);
393         rb_define_method (c, "font_source", c_font_source_get, 0);
394         rb_define_method (c, "font_source=", c_font_source_set, 1);
395         rb_define_method (c, "get_font", c_get_font, 0);
396         rb_define_method (c, "set_font", c_set_font, 2);
397         rb_define_method (c, "text", c_text_get, 0);
398         rb_define_method (c, "text=", c_text_set, 1);
399         rb_define_method (c, "style", c_style_get, 0);
400         rb_define_method (c, "style=", c_style_set, 1);
401         rb_define_method (c, "style_pad", c_style_pad_get, 0);
402         rb_define_method (c, "get_shadow_color", c_get_shadow_color, 0);
403         rb_define_method (c, "set_shadow_color", c_set_shadow_color, 4);
404         rb_define_method (c, "get_glow_color", c_get_glow_color, 0);
405         rb_define_method (c, "set_glow_color", c_set_glow_color, 4);
406         rb_define_method (c, "get_glow2_color", c_get_glow2_color, 0);
407         rb_define_method (c, "set_glow2_color", c_set_glow2_color, 4);
408         rb_define_method (c, "get_outline_color", c_get_outline_color, 0);
409         rb_define_method (c, "set_outline_color", c_set_outline_color, 4);
410
411         rb_define_method (c, "ascent", c_ascent_get, 0);
412         rb_define_method (c, "descent", c_descent_get, 0);
413         rb_define_method (c, "max_ascent", c_max_ascent_get, 0);
414         rb_define_method (c, "max_descent", c_max_descent_get, 0);
415         rb_define_method (c, "advance", c_advance_get, 0);
416         rb_define_method (c, "inset", c_inset_get, 0);
417
418         c2 = rb_define_class_under (mEvas, "Style", c);
419
420         DEF_CONST (c2, EVAS_TEXT_STYLE_, PLAIN);
421         DEF_CONST (c2, EVAS_TEXT_STYLE_, SHADOW);
422         DEF_CONST (c2, EVAS_TEXT_STYLE_, OUTLINE);
423         DEF_CONST (c2, EVAS_TEXT_STYLE_, SOFT_OUTLINE);
424         DEF_CONST (c2, EVAS_TEXT_STYLE_, GLOW);
425         DEF_CONST (c2, EVAS_TEXT_STYLE_, OUTLINE_SHADOW);
426         DEF_CONST (c2, EVAS_TEXT_STYLE_, FAR_SHADOW);
427         DEF_CONST (c2, EVAS_TEXT_STYLE_, OUTLINE_SOFT_SHADOW);
428         DEF_CONST (c2, EVAS_TEXT_STYLE_, SOFT_SHADOW);
429         DEF_CONST (c2, EVAS_TEXT_STYLE_, FAR_SOFT_SHADOW);
430 }