Imported Upstream version 1.7.1
[platform/upstream/edje.git] / src / examples / edje-swallow.c
1 /**
2  * Simple Edje example illustrating swallow functions.
3  *
4  * You'll need at least one Evas engine built for it (excluding the
5  * buffer one). See stdout/stderr for output.
6  *
7  * @verbatim
8  * edje_cc swallow.edc && gcc -o edje-swallow edje-swallow.c `pkg-config --libs --cflags evas ecore ecore-evas edje`
9  * @endverbatim
10  */
11
12 #ifdef HAVE_CONFIG_H
13 # include "config.h"
14 #else
15 # define __UNUSED__
16 #endif
17
18 #include <Ecore.h>
19 #include <Ecore_Evas.h>
20 #include <Edje.h>
21
22 #define WIDTH  (300)
23 #define HEIGHT (300)
24
25 static void
26 _on_delete(Ecore_Evas *ee __UNUSED__)
27 {
28    ecore_main_loop_quit();
29 }
30
31 /* here just to keep our example's window size and background image's
32  * size in synchrony */
33 static void
34 _on_canvas_resize(Ecore_Evas *ee)
35 {
36    Evas_Object *bg;
37    int          w;
38    int          h;
39
40    bg = ecore_evas_data_get(ee, "background");
41    ecore_evas_geometry_get(ee, NULL, NULL, &w, &h);
42    evas_object_resize(bg, w, h);
43 }
44
45 int
46 main(int argc __UNUSED__, char *argv[])
47 {
48    char         edje_file_path[PATH_MAX];
49    const char  *edje_file = "swallow.edj";
50    Ecore_Evas  *ee;
51    Evas        *evas;
52    Evas_Object *bg;
53    Evas_Object *rect;
54    Evas_Object *obj;
55    Evas_Object *edje_obj;
56    Eina_Prefix *pfx;
57
58    if (!ecore_evas_init())
59      return EXIT_FAILURE;
60
61    if (!edje_init())
62      goto shutdown_ecore_evas;
63
64    pfx = eina_prefix_new(argv[0], main,
65                          "EDJE_EXAMPLES",
66                          "edje/examples",
67                          edje_file,
68                          PACKAGE_BIN_DIR,
69                          PACKAGE_LIB_DIR,
70                          PACKAGE_DATA_DIR,
71                          PACKAGE_DATA_DIR);
72    if (!pfx)
73      goto shutdown_edje;
74
75    /* this will give you a window with an Evas canvas under the first
76     * engine available */
77    ee = ecore_evas_new(NULL, 0, 0, WIDTH, HEIGHT, NULL);
78    if (!ee)
79      goto free_prefix;
80
81    ecore_evas_callback_delete_request_set(ee, _on_delete);
82    ecore_evas_callback_resize_set(ee, _on_canvas_resize);
83    ecore_evas_title_set(ee, "Edje Swallow Example");
84
85    evas = ecore_evas_get(ee);
86
87    bg = evas_object_rectangle_add(evas);
88    evas_object_color_set(bg, 255, 255, 255, 255); /* white bg */
89    evas_object_move(bg, 0, 0); /* at canvas' origin */
90    evas_object_resize(bg, WIDTH, HEIGHT); /* covers full canvas */
91    evas_object_show(bg);
92    ecore_evas_data_set(ee, "background", bg);
93
94    edje_obj = edje_object_add(evas);
95
96    snprintf(edje_file_path, sizeof(edje_file_path),
97             "%s/examples/%s", eina_prefix_data_get(pfx), edje_file);
98    edje_object_file_set(edje_obj, edje_file_path, "example_group");
99    evas_object_move(edje_obj, 20, 20);
100    evas_object_resize(edje_obj, WIDTH - 40, HEIGHT - 40);
101    evas_object_show(edje_obj);
102
103    rect = evas_object_rectangle_add(evas);
104    evas_object_color_set(rect, 255, 0, 0, 255);
105    edje_object_part_swallow(edje_obj, "part_one", rect);
106
107    obj = edje_object_part_swallow_get(edje_obj, "part_one");
108    if(obj == rect)
109       printf("Swallowing worked!\n");
110
111    ecore_evas_show(ee);
112
113    ecore_main_loop_begin();
114
115    eina_prefix_free(pfx);
116    ecore_evas_free(ee);
117    ecore_evas_shutdown();
118    edje_shutdown();
119
120    return EXIT_SUCCESS;
121
122  free_prefix:
123    eina_prefix_free(pfx);
124  shutdown_edje:
125    edje_shutdown();
126  shutdown_ecore_evas:
127    ecore_evas_shutdown();
128
129    return EXIT_FAILURE;
130 }