diff options
Diffstat (limited to '')
-rw-r--r-- | libraries/evas/src/examples/evas-images.c | 353 |
1 files changed, 353 insertions, 0 deletions
diff --git a/libraries/evas/src/examples/evas-images.c b/libraries/evas/src/examples/evas-images.c new file mode 100644 index 0000000..a8cf3c4 --- /dev/null +++ b/libraries/evas/src/examples/evas-images.c | |||
@@ -0,0 +1,353 @@ | |||
1 | /** | ||
2 | * Simple Evas example illustrating some image objects functions | ||
3 | * | ||
4 | * You'll need at least one engine built for it (excluding the buffer | ||
5 | * one) and the png image loader also built. See stdout/stderr for | ||
6 | * output. | ||
7 | * | ||
8 | * @verbatim | ||
9 | * gcc -o evas-images evas-images.c `pkg-config --libs --cflags evas ecore ecore-evas` | ||
10 | * @endverbatim | ||
11 | */ | ||
12 | |||
13 | #ifdef HAVE_CONFIG_H | ||
14 | |||
15 | #include "config.h" | ||
16 | #else | ||
17 | |||
18 | #define PACKAGE_EXAMPLES_DIR "." | ||
19 | #define __UNUSED__ | ||
20 | |||
21 | #endif | ||
22 | |||
23 | #include <Ecore.h> | ||
24 | #include <Ecore_Evas.h> | ||
25 | #include <stdio.h> | ||
26 | #include <errno.h> | ||
27 | |||
28 | #define WIDTH (320) | ||
29 | #define HEIGHT (240) | ||
30 | |||
31 | static const char *border_img_path = PACKAGE_EXAMPLES_DIR "/red.png"; | ||
32 | static const char *valid_path = PACKAGE_EXAMPLES_DIR "/enlightenment.png"; | ||
33 | static const char *bogus_path = "/tmp/non-existent-220986.png"; | ||
34 | static const char *commands = \ | ||
35 | "commands are:\n" | ||
36 | "\tx - change image's x fill coordinate\n" | ||
37 | "\ty - change image's y fill coordinate\n" | ||
38 | "\tw - change image's w fill size\n" | ||
39 | "\te - change image's h fill size\n" | ||
40 | "\tf - toggle image filled property (overrides fill)\n" | ||
41 | "\ta - toggle image's alpha channel usage\n" | ||
42 | "\tm - toggle border's smooth scaling\n" | ||
43 | "\tt - change border's thickness\n" | ||
44 | "\tb - change border's center region aspect\n" | ||
45 | "\tc - change border's scaling factor\n" | ||
46 | "\ts - print image's fill property status\n" | ||
47 | "\th - print help\n"; | ||
48 | |||
49 | struct test_data | ||
50 | { | ||
51 | Ecore_Evas *ee; | ||
52 | Evas *evas; | ||
53 | Evas_Object *img1, *img2, *bg, *border; | ||
54 | }; | ||
55 | |||
56 | static struct test_data d = {0}; | ||
57 | |||
58 | static void | ||
59 | _on_destroy(Ecore_Evas *ee __UNUSED__) | ||
60 | { | ||
61 | ecore_main_loop_quit(); | ||
62 | } | ||
63 | |||
64 | /* here just to keep our example's window size and background image's | ||
65 | * size in synchrony */ | ||
66 | static void | ||
67 | _canvas_resize_cb(Ecore_Evas *ee) | ||
68 | { | ||
69 | int w, h; | ||
70 | |||
71 | ecore_evas_geometry_get(ee, NULL, NULL, &w, &h); | ||
72 | evas_object_resize(d.bg, w, h); | ||
73 | } | ||
74 | |||
75 | static const char * | ||
76 | _border_fill_mode_to_str(Evas_Border_Fill_Mode mode) | ||
77 | { | ||
78 | switch (mode) | ||
79 | { | ||
80 | case EVAS_BORDER_FILL_NONE: | ||
81 | return "none"; | ||
82 | |||
83 | case EVAS_BORDER_FILL_DEFAULT: | ||
84 | return "default"; | ||
85 | |||
86 | case EVAS_BORDER_FILL_SOLID: | ||
87 | return "solid"; | ||
88 | |||
89 | default: | ||
90 | return "invalid"; | ||
91 | } | ||
92 | } | ||
93 | |||
94 | static void | ||
95 | _on_keydown(void *data __UNUSED__, | ||
96 | Evas *evas __UNUSED__, | ||
97 | Evas_Object *o __UNUSED__, | ||
98 | void *einfo) | ||
99 | { | ||
100 | Evas_Event_Key_Down *ev = einfo; | ||
101 | |||
102 | if (strcmp(ev->keyname, "h") == 0) /* print help */ | ||
103 | { | ||
104 | fprintf(stdout, commands); | ||
105 | return; | ||
106 | } | ||
107 | |||
108 | if (strcmp(ev->keyname, "m") == 0) /* toggle border image's smooth scaling */ | ||
109 | { | ||
110 | Eina_Bool smooth_scale = evas_object_image_smooth_scale_get(d.border); | ||
111 | |||
112 | evas_object_image_smooth_scale_set(d.border, !smooth_scale); | ||
113 | |||
114 | fprintf(stdout, "Image's border is now %s smooth scaling\n", | ||
115 | smooth_scale ? "without" : "with"); | ||
116 | |||
117 | return; | ||
118 | } | ||
119 | |||
120 | if (strcmp(ev->keyname, "t") == 0) /* change border's thickness */ | ||
121 | { | ||
122 | int l, r, t, b; | ||
123 | |||
124 | evas_object_image_border_get(d.border, &l, &r, &t, &b); | ||
125 | |||
126 | l = (l + 3) % 9; | ||
127 | r = (r + 3) % 9; | ||
128 | t = (t + 3) % 9; | ||
129 | b = (b + 3) % 9; | ||
130 | |||
131 | evas_object_image_border_set(d.border, l, r, t, b); | ||
132 | |||
133 | fprintf(stdout, "Image's border thickness is now %d\n", l); | ||
134 | |||
135 | return; | ||
136 | } | ||
137 | |||
138 | if (strcmp(ev->keyname, "c") == 0) /* change border's scaling factor */ | ||
139 | { | ||
140 | double scale = evas_object_image_border_scale_get(d.border); | ||
141 | |||
142 | scale *= 2; | ||
143 | if (scale > 4.0) scale = 1.0; | ||
144 | |||
145 | evas_object_image_border_scale_set(d.border, scale); | ||
146 | |||
147 | fprintf(stdout, "Image's border scaling factor is now %f\n", scale); | ||
148 | |||
149 | return; | ||
150 | } | ||
151 | |||
152 | if (strcmp(ev->keyname, "b") == 0) /* change border's center | ||
153 | * region's aspect */ | ||
154 | { | ||
155 | Eina_Bool fill = \ | ||
156 | evas_object_image_border_center_fill_get(d.border); | ||
157 | |||
158 | fill = (fill + 1) % 3; | ||
159 | |||
160 | evas_object_image_border_center_fill_set(d.border, fill); | ||
161 | |||
162 | fprintf(stdout, "Image's border center region aspect is now \"%s\"\n", | ||
163 | _border_fill_mode_to_str(fill)); | ||
164 | |||
165 | return; | ||
166 | } | ||
167 | |||
168 | if (strcmp(ev->keyname, "a") == 0) /* toggle alpha channel usage */ | ||
169 | { | ||
170 | Eina_Bool alpha = evas_object_image_alpha_get(d.img1); | ||
171 | |||
172 | evas_object_image_alpha_set(d.img1, !alpha); | ||
173 | |||
174 | fprintf(stdout, "Image's alpha channel is now %s\n", | ||
175 | alpha ? "off" : "on"); | ||
176 | |||
177 | return; | ||
178 | } | ||
179 | |||
180 | if (strcmp(ev->keyname, "f") == 0) /* toggle filled property */ | ||
181 | { | ||
182 | Eina_Bool filled = evas_object_image_filled_get(d.img1); | ||
183 | |||
184 | evas_object_image_filled_set(d.img1, !filled); | ||
185 | |||
186 | fprintf(stdout, "Image's x filled property is now %s\n", | ||
187 | filled ? "off" : "on"); | ||
188 | |||
189 | return; | ||
190 | } | ||
191 | |||
192 | if (strcmp(ev->keyname, "x") == 0) /* change x fill coordinate */ | ||
193 | { | ||
194 | Evas_Coord x, y, w, h; | ||
195 | |||
196 | evas_object_image_fill_get(d.img1, &x, &y, &w, &h); | ||
197 | x = (x + 20) % (WIDTH / 2); | ||
198 | evas_object_image_fill_set(d.img1, x, y, w, h); | ||
199 | |||
200 | fprintf(stdout, "Image's x fill coordinate changed to %d\n", x); | ||
201 | |||
202 | return; | ||
203 | } | ||
204 | |||
205 | if (strcmp(ev->keyname, "y") == 0) /* change y fill coordinate */ | ||
206 | { | ||
207 | Evas_Coord x, y, w, h; | ||
208 | |||
209 | evas_object_image_fill_get(d.img1, &x, &y, &w, &h); | ||
210 | y = (y + 20) % (HEIGHT / 2); | ||
211 | evas_object_image_fill_set(d.img1, x, y, w, h); | ||
212 | |||
213 | fprintf(stdout, "Image's y fill coordinate changed to %d\n", y); | ||
214 | |||
215 | return; | ||
216 | } | ||
217 | |||
218 | if (strcmp(ev->keyname, "w") == 0) /* change w fill size */ | ||
219 | { | ||
220 | Evas_Coord x, y, w, h; | ||
221 | |||
222 | evas_object_image_fill_get(d.img1, &x, &y, &w, &h); | ||
223 | if (w == (WIDTH / 4)) w = (WIDTH / 2); | ||
224 | else if (w == WIDTH / 2) w = WIDTH; | ||
225 | else w = (WIDTH / 4); | ||
226 | evas_object_image_fill_set(d.img1, x, y, w, h); | ||
227 | |||
228 | fprintf(stdout, "Image's w fill size changed to %d\n", w); | ||
229 | |||
230 | return; | ||
231 | } | ||
232 | |||
233 | if (strcmp(ev->keyname, "e") == 0) /* change h fill size */ | ||
234 | { | ||
235 | Evas_Coord x, y, w, h; | ||
236 | |||
237 | evas_object_image_fill_get(d.img1, &x, &y, &w, &h); | ||
238 | if (h == (HEIGHT / 4)) h = (HEIGHT / 2); | ||
239 | else if (h == HEIGHT / 2) h = HEIGHT; | ||
240 | else h = (HEIGHT / 4); | ||
241 | evas_object_image_fill_set(d.img1, x, y, w, h); | ||
242 | |||
243 | fprintf(stdout, "Image's h fill size changed to %d\n", h); | ||
244 | |||
245 | return; | ||
246 | } | ||
247 | |||
248 | if (strcmp(ev->keyname, "s") == 0) /* status */ | ||
249 | { | ||
250 | Evas_Coord x, y, w, h; | ||
251 | |||
252 | evas_object_image_fill_get(d.img1, &x, &y, &w, &h); | ||
253 | |||
254 | fprintf(stdout, "Image has fill properties set to: %d, %d, %d, %d\n", | ||
255 | x, y, w, h); | ||
256 | |||
257 | return; | ||
258 | } | ||
259 | } | ||
260 | |||
261 | int | ||
262 | main(void) | ||
263 | { | ||
264 | int err; | ||
265 | |||
266 | if (!ecore_evas_init()) | ||
267 | return EXIT_FAILURE; | ||
268 | |||
269 | /* this will give you a window with an Evas canvas under the first | ||
270 | * engine available */ | ||
271 | d.ee = ecore_evas_new(NULL, 10, 10, WIDTH, HEIGHT, NULL); | ||
272 | if (!d.ee) | ||
273 | goto error; | ||
274 | |||
275 | ecore_evas_callback_destroy_set(d.ee, _on_destroy); | ||
276 | ecore_evas_callback_resize_set(d.ee, _canvas_resize_cb); | ||
277 | ecore_evas_show(d.ee); | ||
278 | |||
279 | /* the canvas pointer, de facto */ | ||
280 | d.evas = ecore_evas_get(d.ee); | ||
281 | |||
282 | d.bg = evas_object_rectangle_add(d.evas); | ||
283 | evas_object_color_set(d.bg, 255, 255, 255, 255); /* white bg */ | ||
284 | evas_object_move(d.bg, 0, 0); /* at canvas' origin */ | ||
285 | evas_object_resize(d.bg, WIDTH, HEIGHT); /* covers full canvas */ | ||
286 | evas_object_show(d.bg); | ||
287 | |||
288 | d.img1 = evas_object_image_add(d.evas); | ||
289 | evas_object_image_file_set(d.img1, valid_path, NULL); | ||
290 | err = evas_object_image_load_error_get(d.img1); | ||
291 | if (err != EVAS_LOAD_ERROR_NONE) | ||
292 | { | ||
293 | fprintf(stderr, "could not load image '%s'. error string is \"%s\"\n", | ||
294 | valid_path, evas_load_error_str(err)); | ||
295 | } | ||
296 | else | ||
297 | { | ||
298 | fprintf(stdout, | ||
299 | "loaded image '%s' with succes! error string is \"%s\"\n", | ||
300 | valid_path, evas_load_error_str(err)); | ||
301 | |||
302 | evas_object_move(d.img1, 3, 3); | ||
303 | evas_object_image_fill_set(d.img1, 0, 0, WIDTH / 2, HEIGHT / 2); | ||
304 | evas_object_resize(d.img1, WIDTH / 2, HEIGHT / 2); | ||
305 | evas_object_show(d.img1); | ||
306 | |||
307 | evas_object_focus_set(d.bg, EINA_TRUE); | ||
308 | evas_object_event_callback_add( | ||
309 | d.bg, EVAS_CALLBACK_KEY_DOWN, _on_keydown, NULL); | ||
310 | } | ||
311 | |||
312 | /* this is a border around the image above, here just to emphasize | ||
313 | * its geometry */ | ||
314 | d.border = evas_object_image_filled_add(d.evas); | ||
315 | evas_object_image_file_set(d.border, border_img_path, NULL); | ||
316 | evas_object_image_border_set(d.border, 3, 3, 3, 3); | ||
317 | evas_object_image_border_center_fill_set(d.border, EVAS_BORDER_FILL_NONE); | ||
318 | |||
319 | evas_object_move(d.border, 0, 0); | ||
320 | evas_object_resize(d.border, (WIDTH / 2) + 6, (HEIGHT / 2) + 6); | ||
321 | evas_object_show(d.border); | ||
322 | |||
323 | /* image loading will fail for this one -- unless one cheats and | ||
324 | * puts a valid image on that path */ | ||
325 | d.img2 = evas_object_image_add(d.evas); | ||
326 | evas_object_image_file_set(d.img2, bogus_path, NULL); | ||
327 | err = evas_object_image_load_error_get(d.img2); | ||
328 | if (err != EVAS_LOAD_ERROR_NONE) | ||
329 | { | ||
330 | fprintf(stderr, "could not load image '%s': error string is \"%s\"\n", | ||
331 | bogus_path, evas_load_error_str(err)); | ||
332 | } | ||
333 | else | ||
334 | { | ||
335 | evas_object_move(d.img2, WIDTH / 2, HEIGHT / 2); | ||
336 | evas_object_image_fill_set(d.img2, 0, 0, WIDTH / 2, HEIGHT / 2); | ||
337 | evas_object_resize(d.img2, WIDTH / 2, HEIGHT / 2); | ||
338 | evas_object_show(d.img2); | ||
339 | } | ||
340 | |||
341 | fprintf(stdout, commands); | ||
342 | ecore_main_loop_begin(); | ||
343 | |||
344 | ecore_evas_free(d.ee); | ||
345 | ecore_evas_shutdown(); | ||
346 | return 0; | ||
347 | |||
348 | error: | ||
349 | fprintf(stderr, "you got to have at least one evas engine built and linked" | ||
350 | " up to ecore-evas for this example to run properly.\n"); | ||
351 | ecore_evas_shutdown(); | ||
352 | return -1; | ||
353 | } | ||