[evas] Documantation and examples on this group of
[framework/uifw/evas.git] / src / examples / evas-load-error-str.c
1 /**
2  * Simple Evas example illustrating some image objects functions and  evas_load_error_str()'s usage.
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-load-error-str evas-load-error-str.c `pkg-config --libs \
10  * --cflags ecore-evas`
11  * @endverbatim
12  *
13  */
14
15 #ifdef HAVE_CONFIG_H
16
17 #include "config.h"
18 #endif
19
20 #include <Ecore.h>
21 #include <Ecore_Evas.h>
22 #include <stdio.h>
23 #include <errno.h>
24
25 #define WIDTH  (320)
26 #define HEIGHT (240)
27
28 static const char *border_img_path = PACKAGE_EXAMPLES_DIR "/red.png";
29 static const char *valid_path = PACKAGE_EXAMPLES_DIR "/enlightenment.png";
30 static const char *bogus_path = "/tmp/non-existent-220986.png";
31
32 static void
33 _on_keydown(void        *data,
34             Evas        *evas __UNUSED__,
35             Evas_Object *o __UNUSED__,
36             void        *einfo)
37 {
38    Evas_Object *img = data;
39    Evas_Event_Key_Down *ev = einfo;
40
41    if (strcmp(ev->keyname, "h") == 0) /* print help */
42      {
43         fprintf(stdout, "commands are:\n"
44                         "\tx - change image's x fill coordinate\n"
45                         "\ty - change image's y fill coordinate\n"
46                         "\tw - change image's w fill size\n"
47                         "\te - change image's h fill size\n"
48                         "\tf - toggle image filled property (overrides fill)\n"
49                         "\ts - print image's fill property status\n"
50                         "\th - print help\n");
51         return;
52      }
53
54    if (strcmp(ev->keyname, "f") == 0) /* toggle filled property */
55      {
56         Eina_Bool filled = evas_object_image_filled_get(img);
57
58         evas_object_image_filled_set(img, !filled);
59
60         fprintf(stdout, "Image's x filled property is now %s\n",
61                 filled ? "off" : "on");
62
63         return;
64      }
65
66    if (strcmp(ev->keyname, "x") == 0) /* change x fill coordinate */
67      {
68         Evas_Coord x, y, w, h;
69
70         evas_object_image_fill_get(img, &x, &y, &w, &h);
71         x = (x + 20) % (WIDTH / 2);
72         evas_object_image_fill_set(img, x, y, w, h);
73
74         fprintf(stdout, "Image's x fill coordinate changed to %d\n", x);
75
76         return;
77      }
78
79    if (strcmp(ev->keyname, "y") == 0) /* change y fill coordinate */
80      {
81         Evas_Coord x, y, w, h;
82
83         evas_object_image_fill_get(img, &x, &y, &w, &h);
84         y = (y + 20) % (HEIGHT / 2);
85         evas_object_image_fill_set(img, x, y, w, h);
86
87         fprintf(stdout, "Image's y fill coordinate changed to %d\n", y);
88
89         return;
90      }
91
92    if (strcmp(ev->keyname, "w") == 0) /* change w fill size */
93      {
94         Evas_Coord x, y, w, h;
95
96         evas_object_image_fill_get(img, &x, &y, &w, &h);
97         if (w > (WIDTH / 2)) w = (WIDTH / 2);
98         else w = WIDTH;
99         evas_object_image_fill_set(img, x, y, w, h);
100
101         fprintf(stdout, "Image's w fill size changed to %d\n", w);
102
103         return;
104      }
105
106    if (strcmp(ev->keyname, "e") == 0) /* change h fill size */
107      {
108         Evas_Coord x, y, w, h;
109
110         evas_object_image_fill_get(img, &x, &y, &w, &h);
111         if (h > (HEIGHT / 2)) h = (HEIGHT / 2);
112         else h = HEIGHT;
113         evas_object_image_fill_set(img, x, y, w, h);
114
115         fprintf(stdout, "Image's h fill size changed to %d\n", h);
116
117         return;
118      }
119
120    if (strcmp(ev->keyname, "s") == 0) /* status */
121      {
122         Evas_Coord x, y, w, h;
123
124         evas_object_image_fill_get(img, &x, &y, &w, &h);
125
126         fprintf(stdout, "Image has fill properties set to: %d, %d, %d, %d\n",
127                 x, y, w, h);
128
129         return;
130      }
131 }
132
133 int
134 main(void)
135 {
136    Evas *evas;
137    Ecore_Evas *ee;
138    Evas_Object *img1, *img2, *bg, *border;
139    int err;
140
141    if (!ecore_evas_init())
142      return EXIT_FAILURE;
143
144    /* this will give you a window with an Evas canvas under the first
145     * engine available */
146    ee = ecore_evas_new(NULL, 10, 10, WIDTH, HEIGHT, NULL);
147    if (!ee)
148      goto error;
149
150    ecore_evas_show(ee);
151
152    /* the canvas pointer, de facto */
153    evas = ecore_evas_get(ee);
154
155    bg = evas_object_rectangle_add(evas);
156    evas_object_color_set(bg, 255, 255, 255, 255); /* white bg */
157    evas_object_move(bg, 0, 0); /* at canvas' origin */
158    evas_object_resize(bg, WIDTH, HEIGHT); /* covers full canvas */
159    evas_object_show(bg);
160
161    img1 = evas_object_image_add(evas);
162    evas_object_image_file_set(img1, valid_path, NULL);
163    err = evas_object_image_load_error_get(img1);
164    if (err != EVAS_LOAD_ERROR_NONE)
165      {
166         fprintf(stderr, "could not load image '%s'. error string is \"%s\"\n",
167                 valid_path, evas_load_error_str(err));
168      }
169    else
170      {
171         fprintf(stdout,
172                 "loaded image '%s' with succes! error string is \"%s\"\n",
173                 valid_path, evas_load_error_str(err));
174
175         evas_object_move(img1, 0, 0);
176         evas_object_image_fill_set(img1, 0, 0, WIDTH / 2, HEIGHT / 2);
177         evas_object_resize(img1, WIDTH / 2, HEIGHT / 2);
178         evas_object_show(img1);
179
180         evas_object_focus_set(bg, EINA_TRUE);
181         evas_object_event_callback_add(
182           bg, EVAS_CALLBACK_KEY_DOWN, _on_keydown, img1);
183      }
184
185    /* this is a border around the image above, here just to emphasize
186     * its geometry */
187    border = evas_object_image_filled_add(evas);
188    evas_object_image_file_set(border, border_img_path, NULL);
189    evas_object_image_border_set(border, 3, 3, 3, 3);
190    evas_object_image_border_center_fill_set(border, EVAS_BORDER_FILL_NONE);
191
192    evas_object_move(border, 0, 0);
193    evas_object_resize(border, (WIDTH / 2) + 3, (HEIGHT / 2) + 3);
194    evas_object_show(border);
195
196    /* image loading will fail for this one -- unless one cheats and
197     * puts a valid image on that path */
198    img2 = evas_object_image_add(evas);
199    evas_object_image_file_set(img2, bogus_path, NULL);
200    err = evas_object_image_load_error_get(img2);
201    if (err != EVAS_LOAD_ERROR_NONE)
202      {
203         fprintf(stderr, "could not load image '%s': error string is \"%s\"\n",
204                 bogus_path, evas_load_error_str(err));
205      }
206    else
207      {
208         evas_object_move(img2, WIDTH / 2, HEIGHT / 2);
209         evas_object_image_fill_set(img2, 0, 0, WIDTH / 2, HEIGHT / 2);
210         evas_object_resize(img2, WIDTH / 2, HEIGHT / 2);
211         evas_object_show(img2);
212      }
213
214    ecore_main_loop_begin();
215
216    ecore_evas_free(ee);
217    ecore_evas_shutdown();
218    return 0;
219
220 error:
221    fprintf(stderr, "you got to have at least one evas engine built and linked"
222                    " up to ecore-evas for this example to run properly.\n");
223    ecore_evas_shutdown();
224    return -1;
225 }