fd460fb3ac3f7f7530192d206ac474f9070f286e
[framework/uifw/evas.git] / src / examples / evas-load-error-str.c
1 /**
2  * Simple Evas example illustrating 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 *valid_path = PACKAGE_EXAMPLES_DIR "/enlightenment.png";
29 static const char *bogus_path = "/tmp/non-existent-220986.png";
30
31 int
32 main(void)
33 {
34    Evas *evas;
35    Ecore_Evas *ee;
36    Evas_Object *img1, *img2, *bg;
37    int err;
38
39    if (!ecore_evas_init())
40      return EXIT_FAILURE;
41
42    /* this will give you a window with an Evas canvas under the first
43     * engine available */
44    ee = ecore_evas_new(NULL, 10, 10, WIDTH, HEIGHT, NULL);
45    if (!ee)
46      goto error;
47
48    ecore_evas_show(ee);
49
50    /* the canvas pointer, de facto */
51    evas = ecore_evas_get(ee);
52
53    bg = evas_object_rectangle_add(evas);
54    evas_object_color_set(bg, 255, 255, 255, 255); /* white bg */
55    evas_object_move(bg, 0, 0); /* at canvas' origin */
56    evas_object_resize(bg, WIDTH, HEIGHT); /* covers full canvas */
57    evas_object_show(bg);
58
59    img1 = evas_object_image_filled_add(evas);
60    evas_object_image_file_set(img1, valid_path, NULL);
61    err = evas_object_image_load_error_get(img1);
62    if (err != EVAS_LOAD_ERROR_NONE)
63      {
64         fprintf(stderr, "could not load image '%s'. error string is \"%s\"\n",
65                 valid_path, evas_load_error_str(err));
66      }
67    else
68      {
69         fprintf(stdout,
70                 "loaded image '%s' with succes! error string is \"%s\"\n",
71                 valid_path, evas_load_error_str(err));
72
73         evas_object_move(img1, 0, 0);
74         evas_object_resize(img1, WIDTH / 2, HEIGHT / 2);
75         evas_object_show(img1);
76      }
77
78    /* image loading will fail for this one -- unless one cheats and
79     * puts a valid image on that path */
80    img2 = evas_object_image_filled_add(evas);
81    evas_object_image_file_set(img2, bogus_path, NULL);
82    err = evas_object_image_load_error_get(img2);
83    if (err != EVAS_LOAD_ERROR_NONE)
84      {
85         fprintf(stderr, "could not load image '%s': error string is \"%s\"\n",
86                 bogus_path, evas_load_error_str(err));
87      }
88    else
89      {
90         evas_object_move(img2, WIDTH / 2, HEIGHT / 2);
91         evas_object_resize(img2, WIDTH / 2, HEIGHT / 2);
92         evas_object_show(img2);
93      }
94
95    ecore_main_loop_begin();
96
97    ecore_evas_free(ee);
98    ecore_evas_shutdown();
99    return 0;
100
101   error:
102    fprintf(stderr, "you got to have at least one evas engine built and linked"
103            " up to ecore-evas for this example to run properly.\n");
104    ecore_evas_shutdown();
105    return -1;
106 }
107