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