2 * Simple Evas example illustrating a custom Evas box object
4 * You'll need at least one engine built for it (excluding the buffer
5 * one). See stdout/stderr for output.
8 * gcc -o evas-box evas-box.c `pkg-config --libs --cflags evas ecore ecore-evas`
16 #define PACKAGE_EXAMPLES_DIR "."
21 #include <Ecore_Evas.h>
30 static const char *border_img_path = PACKAGE_EXAMPLES_DIR "/red.png";
32 static const char *commands = \
34 "\ta - change the box's alignment values\n"
35 "\tp - change the box's padding values\n"
36 "\t1 - change the box's layout to horizontal\n"
37 "\t2 - change the box's layout to vertical\n"
38 "\t3 - change the box's layout to horizontal homogeneous\n"
39 "\t4 - change the box's layout to vertical homogeneous\n"
40 "\t5 - change the box's layout to horizontal maximum size homogeneous\n"
41 "\t6 - change the box's layout to vertical maximum size homogeneous\n"
42 "\t7 - change the box's layout to horizontal flow\n"
43 "\t8 - change the box's layout to vertical flow\n"
44 "\t9 - change the box's layout to stack\n"
45 "\t0 - change the box's layout to a custom-made one\n"
46 "\tCtrl + NUMBER - insert a new child object at that position in the box\n"
47 "\tShift + NUMBER - remove the child object at that position in the box\n"
54 Evas_Object *bg, *box, *border;
57 static struct exemple_data d;
59 static void /* custom 'diagonal' layout */
60 _custom_layout(Evas_Object *o,
61 Evas_Object_Box_Data *p,
62 void *data __UNUSED__)
68 Evas_Object_Box_Option *opt;
70 evas_object_geometry_get(o, &x, &y, &w, &h);
71 count = eina_list_count(p->children);
72 ww = w / (count ? : 1);
73 hh = h / (count ? : 1);
79 EINA_LIST_FOREACH(p->children, l, opt)
81 evas_object_move(opt->obj, xx, yy);
87 static Evas_Object * /* new rectangle to be put in the box */
88 _new_rectangle_add(Evas *e)
92 o = evas_object_rectangle_add(e);
93 evas_object_resize(o, 10, 10);
94 evas_object_color_set(o, 0, 255, 0, 255);
100 /* use the following commands to interact with this example - 'h' is
101 * the key for help */
103 _on_keydown(void *data __UNUSED__,
104 Evas *evas __UNUSED__,
105 Evas_Object *o __UNUSED__,
108 Evas_Event_Key_Down *ev = einfo;
109 const Evas_Modifier *mods = evas_key_modifier_get(evas);
111 if (strcmp(ev->keyname, "h") == 0) /* print help */
113 fprintf(stdout, commands);
117 if (evas_key_modifier_is_set(mods, "Shift"))
124 pos = atoi(ev->keyname);
125 children = evas_object_box_children_get(d.box);
127 obj = eina_list_nth(children, pos);
128 if (!obj) goto list_free;
130 ret = evas_object_box_remove_at(d.box, pos);
131 if (ret) evas_object_del(obj);
134 eina_list_free(children);
138 if (evas_key_modifier_is_set(mods, "Control"))
142 pos = atoi(ev->keyname);
143 o = _new_rectangle_add(d.evas);
144 if (!evas_object_box_insert_at(d.box, o, pos))
145 evas_object_box_append(d.box, o);
149 if (strcmp(ev->keyname, "a") == 0)
153 evas_object_box_align_get(d.box, &h, &v);
164 evas_object_box_align_set(d.box, h, v);
166 fprintf(stdout, "Applying new alignment values (%.1f, %.1f)"
167 " on the box\n", h, v);
171 if (strcmp(ev->keyname, "p") == 0)
175 evas_object_box_padding_get(d.box, &h, &v);
182 evas_object_box_padding_set(d.box, h, v);
184 fprintf(stdout, "Applying new padding values (%d, %d)"
185 " on the box\n", h, v);
189 if (strcmp(ev->keyname, "1") == 0)
191 evas_object_box_layout_set(
192 d.box, evas_object_box_layout_horizontal, NULL, NULL);
194 fprintf(stdout, "Applying '%s' layout on the box\n", "horizontal");
198 if (strcmp(ev->keyname, "2") == 0)
200 evas_object_box_layout_set(
201 d.box, evas_object_box_layout_vertical, NULL, NULL);
203 fprintf(stdout, "Applying '%s' layout on the box\n", "vertical");
207 if (strcmp(ev->keyname, "3") == 0)
209 evas_object_box_layout_set(
210 d.box, evas_object_box_layout_homogeneous_horizontal, NULL,
213 fprintf(stdout, "Applying '%s' layout on the box\n",
214 "horizontal homogeneous");
218 if (strcmp(ev->keyname, "4") == 0)
220 evas_object_box_layout_set(
221 d.box, evas_object_box_layout_homogeneous_vertical, NULL, NULL);
223 fprintf(stdout, "Applying '%s' layout on the box\n",
224 "vertical homogeneous");
228 if (strcmp(ev->keyname, "5") == 0)
230 evas_object_box_layout_set(
231 d.box, evas_object_box_layout_homogeneous_max_size_horizontal,
234 fprintf(stdout, "Applying '%s' layout on the box\n",
235 "horizontal maximum size homogeneous");
239 if (strcmp(ev->keyname, "6") == 0)
241 evas_object_box_layout_set(
242 d.box, evas_object_box_layout_homogeneous_max_size_vertical,
245 fprintf(stdout, "Applying '%s' layout on the box\n",
246 "vertical maximum size homogeneous");
250 if (strcmp(ev->keyname, "7") == 0)
252 evas_object_box_layout_set(
253 d.box, evas_object_box_layout_flow_horizontal, NULL, NULL);
255 fprintf(stdout, "Applying '%s' layout on the box\n", "horizontal flow");
259 if (strcmp(ev->keyname, "8") == 0)
261 evas_object_box_layout_set(
262 d.box, evas_object_box_layout_flow_vertical, NULL, NULL);
264 fprintf(stdout, "Applying '%s' layout on the box\n", "vertical flow");
268 if (strcmp(ev->keyname, "9") == 0)
270 evas_object_box_layout_set(
271 d.box, evas_object_box_layout_stack, NULL, NULL);
273 fprintf(stdout, "Applying '%s' layout on the box\n", "stack");
277 if (strcmp(ev->keyname, "0") == 0)
279 evas_object_box_layout_set(d.box, _custom_layout, NULL, NULL);
281 fprintf(stdout, "Applying '%s' layout on the box\n", "CUSTOM");
287 _on_delete(Ecore_Evas *ee __UNUSED__)
289 ecore_main_loop_quit();
292 static void /* adjust canvas' contents on resizes */
293 _canvas_resize_cb(Ecore_Evas *ee)
297 ecore_evas_geometry_get(ee, NULL, NULL, &w, &h);
299 evas_object_resize(d.bg, w, h);
301 evas_object_move(d.box, (w / 4), (h / 4));
302 evas_object_resize(d.box, (w / 2), (h / 2));
304 evas_object_move(d.border, (w / 4) - 2, (h / 4) - 2);
305 evas_object_resize(d.border, (w / 2) + 4, (h / 2) + 4);
311 Evas_Object *last, *o;
316 if (!ecore_evas_init())
319 /* this will give you a window with an Evas canvas under the first
320 * engine available */
321 d.ee = ecore_evas_new(NULL, 0, 0, WIDTH, HEIGHT, NULL);
325 ecore_evas_callback_delete_request_set(d.ee, _on_delete);
326 ecore_evas_callback_resize_set(d.ee, _canvas_resize_cb);
327 ecore_evas_show(d.ee);
329 d.evas = ecore_evas_get(d.ee);
331 d.bg = evas_object_rectangle_add(d.evas);
332 evas_object_color_set(d.bg, 255, 255, 255, 255); /* white bg */
333 evas_object_show(d.bg);
335 evas_object_focus_set(d.bg, EINA_TRUE);
336 evas_object_event_callback_add(
337 d.bg, EVAS_CALLBACK_KEY_DOWN, _on_keydown, NULL);
339 d.box = evas_object_box_add(d.evas);
340 evas_object_show(d.box);
342 for (i = 1; i <= 5; i++)
344 o = last = evas_object_rectangle_add(d.evas);
345 evas_object_size_hint_min_set(o, 50, 50);
346 evas_object_color_set(
347 o, rand() % 256, rand() % 256, rand() % 256, 255);
350 if (!evas_object_box_append(d.box, o))
352 fprintf(stderr, "Error appending child object on the box!\n");
357 /* this is a border around the box, container of the rectangles we
358 * are going to experiment with. this way you can see how the
359 * container relates to the children */
360 d.border = evas_object_image_filled_add(d.evas);
361 evas_object_image_file_set(d.border, border_img_path, NULL);
362 evas_object_image_border_set(d.border, 2, 2, 2, 2);
363 evas_object_image_border_center_fill_set(d.border, EVAS_BORDER_FILL_NONE);
364 evas_object_show(d.border);
366 fprintf(stdout, commands);
368 _canvas_resize_cb(d.ee);
369 ecore_main_loop_begin();
370 ecore_evas_shutdown();
374 ecore_evas_shutdown();
378 fprintf(stderr, "You got to have at least one evas engine built and linked"
379 " up to ecore-evas for this example to run properly.\n");