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