diff options
author | David Walter Seikel | 2012-01-04 18:41:13 +1000 |
---|---|---|
committer | David Walter Seikel | 2012-01-04 18:41:13 +1000 |
commit | dd7595a3475407a7fa96a97393bae8c5220e8762 (patch) | |
tree | e341e911d7eb911a51684a7412ef7f7c7605d28e /libraries/evas/src/examples/evas-text.c | |
parent | Add the skeleton. (diff) | |
download | SledjHamr-dd7595a3475407a7fa96a97393bae8c5220e8762.zip SledjHamr-dd7595a3475407a7fa96a97393bae8c5220e8762.tar.gz SledjHamr-dd7595a3475407a7fa96a97393bae8c5220e8762.tar.bz2 SledjHamr-dd7595a3475407a7fa96a97393bae8c5220e8762.tar.xz |
Add the base Enlightenment Foundation Libraries - eina, eet, evas, ecore, embryo, and edje.
Note that embryo wont be used, but I'm not sure yet if you can build edje without it.
Diffstat (limited to '')
-rw-r--r-- | libraries/evas/src/examples/evas-text.c | 411 |
1 files changed, 411 insertions, 0 deletions
diff --git a/libraries/evas/src/examples/evas-text.c b/libraries/evas/src/examples/evas-text.c new file mode 100644 index 0000000..ba55662 --- /dev/null +++ b/libraries/evas/src/examples/evas-text.c | |||
@@ -0,0 +1,411 @@ | |||
1 | /** | ||
2 | * Simple Evas example illustrating text objects | ||
3 | * | ||
4 | * You'll need at least one engine built for it (excluding the buffer | ||
5 | * one). See stdout/stderr for output. | ||
6 | * | ||
7 | * @verbatim | ||
8 | * gcc -o evas-text evas-text.c `pkg-config --libs --cflags evas ecore ecore-evas` | ||
9 | * @endverbatim | ||
10 | */ | ||
11 | |||
12 | #ifdef HAVE_CONFIG_H | ||
13 | |||
14 | #include "config.h" | ||
15 | #else | ||
16 | |||
17 | #define PACKAGE_EXAMPLES_DIR "." | ||
18 | #define __UNUSED__ | ||
19 | |||
20 | #endif | ||
21 | |||
22 | #include <Ecore.h> | ||
23 | #include <Ecore_Evas.h> | ||
24 | #include <stdio.h> | ||
25 | #include <errno.h> | ||
26 | |||
27 | #define WIDTH (320) | ||
28 | #define HEIGHT (240) | ||
29 | |||
30 | #define GREY {190, 190, 190, 255} | ||
31 | #define BLACK {0, 0, 0, 255} | ||
32 | #define WHITE {255, 255, 255, 255} | ||
33 | #define RED {255, 0, 0, 255} | ||
34 | #define GREEN {0, 255, 0, 255} | ||
35 | #define BLUE {0, 0, 255, 255} | ||
36 | |||
37 | #define POINTER_CYCLE(_ptr, _array) \ | ||
38 | do \ | ||
39 | { \ | ||
40 | if ((unsigned)(((void *)(_ptr)) - ((void *)(_array))) >= \ | ||
41 | sizeof(_array)) \ | ||
42 | _ptr = _array; \ | ||
43 | } \ | ||
44 | while(0) | ||
45 | |||
46 | static const char *commands = \ | ||
47 | "commands are:\n" | ||
48 | "\tt - change text's current style\n" | ||
49 | "\tz - change text's font size\n" | ||
50 | "\tf - change text's font family\n" | ||
51 | "\tb - change text's base color\n" | ||
52 | "\ts - change text's \'shadow\' color\n" | ||
53 | "\to - change text's \'outline\' color\n" | ||
54 | "\tw - change text's \'glow\' color\n" | ||
55 | "\tg - change text's \'glow 2\' color\n" | ||
56 | "\th - print help\n"; | ||
57 | |||
58 | static const char *border_img_path = PACKAGE_EXAMPLES_DIR "/red.png"; | ||
59 | |||
60 | struct color_tuple | ||
61 | { | ||
62 | int r, g, b, a; | ||
63 | }; | ||
64 | |||
65 | struct text_preset_data | ||
66 | { | ||
67 | const char **font_ptr; | ||
68 | const char *font[3]; | ||
69 | |||
70 | struct color_tuple *text_ptr; | ||
71 | struct color_tuple text[6]; | ||
72 | |||
73 | struct color_tuple *shadow_ptr; | ||
74 | struct color_tuple shadow[4]; | ||
75 | |||
76 | struct color_tuple *outline_ptr; | ||
77 | struct color_tuple outline[4]; | ||
78 | |||
79 | struct color_tuple *glow_ptr; | ||
80 | struct color_tuple glow[4]; | ||
81 | |||
82 | struct color_tuple *glow2_ptr; | ||
83 | struct color_tuple glow2[4]; | ||
84 | }; | ||
85 | |||
86 | struct test_data | ||
87 | { | ||
88 | Ecore_Evas *ee; | ||
89 | Evas *evas; | ||
90 | struct text_preset_data t_data; | ||
91 | Evas_Object *text, *bg, *border; | ||
92 | }; | ||
93 | |||
94 | static struct test_data d = {0}; | ||
95 | |||
96 | static void | ||
97 | _on_destroy(Ecore_Evas *ee __UNUSED__) | ||
98 | { | ||
99 | ecore_main_loop_quit(); | ||
100 | } | ||
101 | |||
102 | /* here just to keep our example's window size and background image's | ||
103 | * size in synchrony */ | ||
104 | static void | ||
105 | _canvas_resize_cb(Ecore_Evas *ee) | ||
106 | { | ||
107 | int w, h; | ||
108 | |||
109 | ecore_evas_geometry_get(ee, NULL, NULL, &w, &h); | ||
110 | evas_object_resize(d.bg, w, h); | ||
111 | } | ||
112 | |||
113 | static const char * | ||
114 | _text_style_type_to_str(Evas_Text_Style_Type mode) | ||
115 | { | ||
116 | switch (mode) | ||
117 | { | ||
118 | case EVAS_TEXT_STYLE_PLAIN: | ||
119 | return "plain"; | ||
120 | |||
121 | case EVAS_TEXT_STYLE_SHADOW: | ||
122 | return "shadow"; | ||
123 | |||
124 | case EVAS_TEXT_STYLE_OUTLINE: | ||
125 | return "outline"; | ||
126 | |||
127 | case EVAS_TEXT_STYLE_SOFT_OUTLINE: | ||
128 | return "soft outline"; | ||
129 | |||
130 | case EVAS_TEXT_STYLE_GLOW: | ||
131 | return "glow"; | ||
132 | |||
133 | case EVAS_TEXT_STYLE_OUTLINE_SHADOW: | ||
134 | return "outline shadow"; | ||
135 | |||
136 | case EVAS_TEXT_STYLE_FAR_SHADOW: | ||
137 | return "far shadow"; | ||
138 | |||
139 | case EVAS_TEXT_STYLE_OUTLINE_SOFT_SHADOW: | ||
140 | return "outline soft shadow"; | ||
141 | |||
142 | case EVAS_TEXT_STYLE_SOFT_SHADOW: | ||
143 | return "soft shadow"; | ||
144 | |||
145 | case EVAS_TEXT_STYLE_FAR_SOFT_SHADOW: | ||
146 | return "far soft shadow"; | ||
147 | |||
148 | default: | ||
149 | return "invalid"; | ||
150 | } | ||
151 | } | ||
152 | |||
153 | static void | ||
154 | _on_keydown(void *data __UNUSED__, | ||
155 | Evas *evas __UNUSED__, | ||
156 | Evas_Object *o __UNUSED__, | ||
157 | void *einfo) | ||
158 | { | ||
159 | Evas_Event_Key_Down *ev = einfo; | ||
160 | |||
161 | if (strcmp(ev->keyname, "h") == 0) /* print help */ | ||
162 | { | ||
163 | fprintf(stdout, commands); | ||
164 | return; | ||
165 | } | ||
166 | |||
167 | if (strcmp(ev->keyname, "t") == 0) /* change text's current style */ | ||
168 | { | ||
169 | Evas_Text_Style_Type type = evas_object_text_style_get(d.text); | ||
170 | |||
171 | type = (type + 1) % 10; | ||
172 | |||
173 | evas_object_text_style_set(d.text, type); | ||
174 | |||
175 | fprintf(stdout, "Changing text's style to \'%s\'\n", | ||
176 | _text_style_type_to_str(type)); | ||
177 | |||
178 | return; | ||
179 | } | ||
180 | |||
181 | if (strcmp(ev->keyname, "f") == 0) /* change text's font */ | ||
182 | { | ||
183 | int sz; | ||
184 | |||
185 | (d.t_data.font_ptr)++; | ||
186 | |||
187 | evas_object_text_font_get(d.text, NULL, &sz); | ||
188 | |||
189 | POINTER_CYCLE(d.t_data.font_ptr, d.t_data.font); | ||
190 | |||
191 | evas_object_text_font_set(d.text, *d.t_data.font_ptr, sz); | ||
192 | |||
193 | fprintf(stdout, "Changing text's font to %s\n", *d.t_data.font_ptr); | ||
194 | |||
195 | return; | ||
196 | } | ||
197 | |||
198 | if (strcmp(ev->keyname, "b") == 0) /* change text's base color */ | ||
199 | { | ||
200 | (d.t_data.text_ptr)++; | ||
201 | |||
202 | POINTER_CYCLE(d.t_data.text_ptr, d.t_data.text); | ||
203 | |||
204 | evas_object_color_set( | ||
205 | d.text, d.t_data.text_ptr->r, d.t_data.text_ptr->g, | ||
206 | d.t_data.text_ptr->b, d.t_data.text_ptr->a); | ||
207 | |||
208 | fprintf(stdout, "Changing base color for text to (%d, %d, %d, %d)\n", | ||
209 | d.t_data.text_ptr->r, d.t_data.text_ptr->g, | ||
210 | d.t_data.text_ptr->b, d.t_data.text_ptr->a); | ||
211 | |||
212 | return; | ||
213 | } | ||
214 | |||
215 | if (strcmp(ev->keyname, "g") == 0) /* change text's glow 2 color */ | ||
216 | { | ||
217 | (d.t_data.glow2_ptr)++; | ||
218 | |||
219 | POINTER_CYCLE(d.t_data.glow2_ptr, d.t_data.glow2); | ||
220 | |||
221 | evas_object_text_glow2_color_set( | ||
222 | d.text, d.t_data.glow2_ptr->r, d.t_data.glow2_ptr->g, | ||
223 | d.t_data.glow2_ptr->b, d.t_data.glow2_ptr->a); | ||
224 | |||
225 | fprintf(stdout, "Changing glow 2 color for text to (%d, %d, %d, %d)\n", | ||
226 | d.t_data.glow2_ptr->r, d.t_data.glow2_ptr->g, | ||
227 | d.t_data.glow2_ptr->b, d.t_data.glow2_ptr->a); | ||
228 | |||
229 | return; | ||
230 | } | ||
231 | |||
232 | if (strcmp(ev->keyname, "w") == 0) /* change text's glow color */ | ||
233 | { | ||
234 | (d.t_data.glow_ptr)++; | ||
235 | |||
236 | POINTER_CYCLE(d.t_data.glow_ptr, d.t_data.glow); | ||
237 | |||
238 | evas_object_text_glow_color_set( | ||
239 | d.text, d.t_data.glow_ptr->r, d.t_data.glow_ptr->g, | ||
240 | d.t_data.glow_ptr->b, d.t_data.glow_ptr->a); | ||
241 | |||
242 | fprintf(stdout, "Changing glow color for text to (%d, %d, %d, %d)\n", | ||
243 | d.t_data.glow_ptr->r, d.t_data.glow_ptr->g, | ||
244 | d.t_data.glow_ptr->b, d.t_data.glow_ptr->a); | ||
245 | |||
246 | return; | ||
247 | } | ||
248 | |||
249 | if (strcmp(ev->keyname, "o") == 0) /* change text's outline color */ | ||
250 | { | ||
251 | (d.t_data.outline_ptr)++; | ||
252 | |||
253 | POINTER_CYCLE(d.t_data.outline_ptr, d.t_data.outline); | ||
254 | |||
255 | evas_object_text_outline_color_set( | ||
256 | d.text, d.t_data.outline_ptr->r, d.t_data.outline_ptr->g, | ||
257 | d.t_data.outline_ptr->b, d.t_data.outline_ptr->a); | ||
258 | |||
259 | fprintf(stdout, "Changing outline color for text to (%d, %d, %d, %d)\n", | ||
260 | d.t_data.outline_ptr->r, d.t_data.outline_ptr->g, | ||
261 | d.t_data.outline_ptr->b, d.t_data.outline_ptr->a); | ||
262 | |||
263 | return; | ||
264 | } | ||
265 | |||
266 | if (strcmp(ev->keyname, "s") == 0) /* change text's shadow color */ | ||
267 | { | ||
268 | (d.t_data.shadow_ptr)++; | ||
269 | |||
270 | POINTER_CYCLE(d.t_data.shadow_ptr, d.t_data.shadow); | ||
271 | |||
272 | evas_object_text_shadow_color_set( | ||
273 | d.text, d.t_data.shadow_ptr->r, d.t_data.shadow_ptr->g, | ||
274 | d.t_data.shadow_ptr->b, d.t_data.shadow_ptr->a); | ||
275 | |||
276 | fprintf(stdout, "Changing shadow color for text to (%d, %d, %d, %d)\n", | ||
277 | d.t_data.shadow_ptr->r, d.t_data.shadow_ptr->g, | ||
278 | d.t_data.shadow_ptr->b, d.t_data.shadow_ptr->a); | ||
279 | |||
280 | return; | ||
281 | } | ||
282 | |||
283 | if (strcmp(ev->keyname, "z") == 0) /* change text's font size */ | ||
284 | { | ||
285 | const char *font; | ||
286 | int size; | ||
287 | |||
288 | evas_object_text_font_get(d.text, &font, &size); | ||
289 | |||
290 | size = (size + 10) % 50; | ||
291 | if (!size) size = 10; | ||
292 | |||
293 | evas_object_text_font_set(d.text, font, size); | ||
294 | |||
295 | fprintf(stdout, "Changing text's font size to %d\n", size); | ||
296 | |||
297 | return; | ||
298 | } | ||
299 | } | ||
300 | |||
301 | int | ||
302 | main(void) | ||
303 | { | ||
304 | int size; | ||
305 | const char *font; | ||
306 | |||
307 | if (!ecore_evas_init()) | ||
308 | return EXIT_FAILURE; | ||
309 | |||
310 | /* init values one is going to cycle through while running this | ||
311 | * example */ | ||
312 | struct text_preset_data init_data = | ||
313 | { | ||
314 | .font = {"DejaVu", "Courier", "Utopia"}, | ||
315 | .text = {BLACK, WHITE, GREY, RED, GREEN, BLUE}, | ||
316 | .shadow = {WHITE, BLUE, GREEN, RED}, | ||
317 | .outline = {WHITE, RED, GREEN, BLUE}, | ||
318 | .glow = {WHITE, BLUE, GREEN, RED}, | ||
319 | .glow2 = {WHITE, RED, BLUE, GREEN} | ||
320 | }; | ||
321 | |||
322 | d.t_data = init_data; | ||
323 | d.t_data.font_ptr = d.t_data.font; | ||
324 | d.t_data.text_ptr = d.t_data.text; | ||
325 | d.t_data.glow_ptr = d.t_data.glow; | ||
326 | d.t_data.glow2_ptr = d.t_data.glow2; | ||
327 | d.t_data.outline_ptr = d.t_data.outline; | ||
328 | d.t_data.shadow_ptr = d.t_data.shadow; | ||
329 | |||
330 | /* this will give you a window with an Evas canvas under the first | ||
331 | * engine available */ | ||
332 | d.ee = ecore_evas_new(NULL, 10, 10, WIDTH, HEIGHT, NULL); | ||
333 | if (!d.ee) | ||
334 | goto error; | ||
335 | |||
336 | ecore_evas_callback_destroy_set(d.ee, _on_destroy); | ||
337 | ecore_evas_callback_resize_set(d.ee, _canvas_resize_cb); | ||
338 | ecore_evas_show(d.ee); | ||
339 | |||
340 | /* the canvas pointer, de facto */ | ||
341 | d.evas = ecore_evas_get(d.ee); | ||
342 | |||
343 | d.bg = evas_object_rectangle_add(d.evas); | ||
344 | evas_object_color_set(d.bg, 255, 255, 255, 255); /* white bg */ | ||
345 | evas_object_move(d.bg, 0, 0); /* at canvas' origin */ | ||
346 | evas_object_resize(d.bg, WIDTH, HEIGHT); /* covers full canvas */ | ||
347 | evas_object_show(d.bg); | ||
348 | |||
349 | evas_object_focus_set(d.bg, EINA_TRUE); | ||
350 | evas_object_event_callback_add( | ||
351 | d.bg, EVAS_CALLBACK_KEY_DOWN, _on_keydown, NULL); | ||
352 | |||
353 | d.text = evas_object_text_add(d.evas); | ||
354 | evas_object_text_style_set(d.text, EVAS_TEXT_STYLE_PLAIN); | ||
355 | |||
356 | /* let the pre-set thingies be enforced */ | ||
357 | evas_object_color_set( | ||
358 | d.text, d.t_data.text_ptr->r, d.t_data.text_ptr->g, | ||
359 | d.t_data.text_ptr->b, d.t_data.text_ptr->a); | ||
360 | |||
361 | evas_object_text_glow_color_set( | ||
362 | d.text, d.t_data.glow_ptr->r, d.t_data.glow_ptr->g, | ||
363 | d.t_data.glow_ptr->b, d.t_data.glow_ptr->a); | ||
364 | |||
365 | evas_object_text_glow2_color_set( | ||
366 | d.text, d.t_data.glow2_ptr->r, d.t_data.glow2_ptr->g, | ||
367 | d.t_data.glow2_ptr->b, d.t_data.glow2_ptr->a); | ||
368 | |||
369 | evas_object_text_outline_color_set( | ||
370 | d.text, d.t_data.outline_ptr->r, d.t_data.outline_ptr->g, | ||
371 | d.t_data.outline_ptr->b, d.t_data.outline_ptr->a); | ||
372 | |||
373 | evas_object_text_shadow_color_set( | ||
374 | d.text, d.t_data.shadow_ptr->r, d.t_data.shadow_ptr->g, | ||
375 | d.t_data.shadow_ptr->b, d.t_data.shadow_ptr->a); | ||
376 | |||
377 | evas_object_text_font_set(d.text, *d.t_data.font_ptr, 30); | ||
378 | evas_object_text_text_set(d.text, "sample text"); | ||
379 | |||
380 | evas_object_resize(d.text, (3 * WIDTH) / 4, HEIGHT / 4); | ||
381 | evas_object_move(d.text, WIDTH / 8, (3 * HEIGHT) / 8); | ||
382 | evas_object_show(d.text); | ||
383 | |||
384 | evas_object_text_font_get(d.text, &font, &size); | ||
385 | fprintf(stdout, "Adding text object with font %s, size %d\n", font, size); | ||
386 | |||
387 | /* this is a border around the text object above, here just to | ||
388 | * emphasize its geometry */ | ||
389 | d.border = evas_object_image_filled_add(d.evas); | ||
390 | evas_object_image_file_set(d.border, border_img_path, NULL); | ||
391 | evas_object_image_border_set(d.border, 3, 3, 3, 3); | ||
392 | evas_object_image_border_center_fill_set(d.border, EVAS_BORDER_FILL_NONE); | ||
393 | |||
394 | evas_object_resize(d.border, ((3 * WIDTH) / 4) + 3, (HEIGHT / 4) + 3); | ||
395 | evas_object_move(d.border, (WIDTH / 8) - 3, ((3 * HEIGHT) / 8) - 3); | ||
396 | evas_object_show(d.border); | ||
397 | |||
398 | fprintf(stdout, commands); | ||
399 | ecore_main_loop_begin(); | ||
400 | |||
401 | ecore_evas_free(d.ee); | ||
402 | ecore_evas_shutdown(); | ||
403 | return 0; | ||
404 | |||
405 | error: | ||
406 | fprintf(stderr, "you got to have at least one evas engine built and linked" | ||
407 | " up to ecore-evas for this example to run properly.\n"); | ||
408 | ecore_evas_shutdown(); | ||
409 | return -1; | ||
410 | } | ||
411 | |||