Imported Upstream version 1.7.1
[platform/upstream/edje.git] / src / examples / edje-box2.c
1 /**
2  * Simple Edje example illustrating a custom box layout.
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 box.edc && gcc -o edje-box2 edje-box2.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 <stdlib.h>
19 #include <stdio.h>
20 #include <string.h>
21
22 #include <Ecore.h>
23 #include <Evas.h>
24 #include <Ecore_Evas.h>
25 #include <Edje.h>
26
27 static const char commands[] = \
28   "commands are:\n"
29   "\tShift - remove box\n"
30   "\tCtrl - insert box\n"
31   "\tEsc - exit\n"
32   "\th - print help\n";
33
34 static void
35 custom_layout(Evas_Object *o, Evas_Object_Box_Data *p, void *data __UNUSED__)
36 {
37    Evas_Object_Box_Option *opt;
38    Eina_List *l;
39    int x, y, w, h;
40    int xx, yy, ww, hh;
41    int count;
42
43    evas_object_geometry_get(o, &x, &y, &w, &h);
44    count = eina_list_count(p->children);
45    ww = w / (count?:1);
46    hh = h / (count?:1);
47    if (ww < 1) ww = 1;
48    if (hh < 1) hh = 1;
49
50    xx = x;
51    yy = y;
52    EINA_LIST_FOREACH(p->children, l, opt)
53      {
54         evas_object_move(opt->obj, xx, yy);
55         xx += ww;
56         yy += hh;
57      }
58 }
59
60 static Evas_Object *
61 new_greenie_block(Evas *e)
62 {
63    Evas_Object *o;
64
65    o = evas_object_rectangle_add(e);
66    evas_object_resize(o, 10, 10);
67    evas_object_color_set(o, 0, 255, 0, 255);
68    evas_object_show(o);
69
70    return o;
71 }
72
73 static void
74 on_keydown(void *data, Evas *evas, Evas_Object *o __UNUSED__, void *einfo)
75 {
76    Evas_Event_Key_Down *ev;
77    Evas_Object         *edje_obj;
78    const Evas_Modifier *mods;
79
80    ev = (Evas_Event_Key_Down *)einfo;
81    edje_obj = (Evas_Object *)data;
82
83    mods = evas_key_modifier_get(evas);
84    if (!strcmp(ev->keyname, "h"))
85      {
86         fprintf(stdout, commands);
87         return;
88      }
89    if (evas_key_modifier_is_set(mods, "Shift"))
90      {
91         int pos;
92         Evas_Object *obj = NULL;
93         pos = atoi(ev->keyname);
94         obj = edje_object_part_box_remove_at(edje_obj, "example/box", pos);
95         if (obj)
96           evas_object_del(obj);
97         return;
98      }
99    if (evas_key_modifier_is_set(mods, "Control"))
100      {
101         Evas_Object *obj;
102         int pos;
103         pos = atoi(ev->keyname);
104         obj = new_greenie_block(evas);
105         if (!edje_object_part_box_insert_at(edje_obj, "example/box", obj, pos))
106           edje_object_part_box_append(edje_obj, "example/box", obj);
107         return;
108      }
109    if (!strcmp(ev->keyname, "Escape"))
110      ecore_main_loop_quit();
111 }
112
113 static Evas_Object *
114 box_new(Ecore_Evas *ee, const char *edje_file_path, const char *name, int x, int y, int w, int h)
115 {
116    Evas_Object *edje_obj;
117
118    edje_obj = edje_object_add(ecore_evas_get(ee));
119    evas_object_move(edje_obj, x, y);
120    evas_object_resize(edje_obj, w, h);
121    if (!edje_object_file_set(edje_obj, edje_file_path, "example/group2"))
122      {
123         printf("error: could not load file object.\n");
124      }
125    evas_object_show(edje_obj);
126    evas_object_name_set(edje_obj, name);
127    ecore_evas_data_set(ee, "edje_obj", edje_obj);
128
129    return edje_obj;
130 }
131
132 static void
133 on_resize(Ecore_Evas *ee)
134 {
135    Evas_Object *bg;
136    Evas_Object *edje_obj;
137    int          w;
138    int          h;
139
140    bg = ecore_evas_data_get(ee, "background");
141    edje_obj = ecore_evas_data_get(ee, "edje_obj");
142    ecore_evas_geometry_get(ee, NULL, NULL, &w, &h);
143    evas_object_resize(bg, w, h);
144    evas_object_resize(edje_obj, w, h);
145 }
146
147 static void
148 on_destroy(Ecore_Evas *ee __UNUSED__)
149 {
150    ecore_main_loop_quit();
151 }
152
153 int
154 main(int argc __UNUSED__, char *argv[])
155 {
156    char         edje_file_path[PATH_MAX];
157    const char  *edje_file = "box.edj";
158    Ecore_Evas  *ee;
159    Evas        *evas;
160    Evas_Object *bg;
161    Evas_Object *edje_obj;
162    Evas_Object *last;
163    Evas_Object *o;
164    Eina_Prefix *pfx;
165    int          w;
166    int          h;
167    int          i;
168
169    if (!ecore_evas_init())
170      return EXIT_FAILURE;
171
172    if (!edje_init())
173      goto shutdown_ecore_evas;
174
175    pfx = eina_prefix_new(argv[0], main,
176                          "EDJE_EXAMPLES",
177                          "edje/examples",
178                          edje_file,
179                          PACKAGE_BIN_DIR,
180                          PACKAGE_LIB_DIR,
181                          PACKAGE_DATA_DIR,
182                          PACKAGE_DATA_DIR);
183    if (!pfx)
184      goto shutdown_edje;
185
186    /* this will give you a window with an Evas canvas under the first
187     * engine available */
188    ee = ecore_evas_new(NULL, 0, 0, 640, 480, NULL);
189    if (!ee)
190      goto free_prefix;
191
192    ecore_evas_callback_resize_set(ee, on_resize);
193    ecore_evas_callback_destroy_set(ee, on_destroy);
194    ecore_evas_geometry_get(ee, NULL, NULL, &w, &h);
195
196    evas = ecore_evas_get(ee);
197
198    bg = evas_object_rectangle_add(evas);
199    evas_object_resize(bg, w, h);
200    evas_object_show(bg);
201    evas_object_focus_set(bg, 1);
202    ecore_evas_data_set(ee, "background", bg);
203
204    edje_box_layout_register("custom_layout", custom_layout, NULL, NULL, NULL, NULL);
205
206    snprintf(edje_file_path, sizeof(edje_file_path),
207             "%s/examples/%s", eina_prefix_data_get(pfx), edje_file);
208    edje_obj = box_new(ee, edje_file_path, "box", 0, 0, w, h);
209    evas_object_event_callback_add(bg, EVAS_CALLBACK_KEY_DOWN, on_keydown, edje_obj);
210
211    for (i = 1; i <= 5; i++)
212      {
213         o = last = evas_object_rectangle_add(evas);
214         evas_object_size_hint_min_set(o, 50, 50);
215         evas_object_resize(o, 50, 50);
216         evas_object_color_set(o, 255, 0, 0, 128);
217         evas_object_show(o);
218
219         if (!edje_object_part_box_append(edje_obj, "example/box", o))
220           {
221              fprintf(stderr, "error appending child object!\n");
222              return 1;
223           }
224      }
225
226    fprintf(stdout, commands);
227
228    ecore_evas_show(ee);
229
230    ecore_main_loop_begin();
231
232    eina_prefix_free(pfx);
233    ecore_evas_free(ee);
234    ecore_evas_shutdown();
235    edje_shutdown();
236
237    return EXIT_SUCCESS;
238
239  free_prefix:
240    eina_prefix_free(pfx);
241  shutdown_edje:
242    edje_shutdown();
243  shutdown_ecore_evas:
244    ecore_evas_shutdown();
245
246    return EXIT_FAILURE;
247 }