[evas] Examples on box object.
[framework/uifw/evas.git] / src / examples / evas-box.c
1 /**
2  * Simple Evas example illustrating a custom Evas box object
3  *
4  * You'll need at least one engine built for it (excluding the buffer
5  * one). See stdout/stderr for output.
6  *
7  * @verbatim
8  * gcc -o evas-box evas-box.c `pkg-config --libs --cflags evas ecore ecore-evas`
9  * @endverbatim
10  */
11
12 #ifdef HAVE_CONFIG_H
13
14 #include "config.h"
15 #else
16 #define PACKAGE_EXAMPLES_DIR "."
17 #define __UNUSED__
18 #endif
19
20 #include <Ecore.h>
21 #include <Ecore_Evas.h>
22
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26
27 #define WIDTH  (640)
28 #define HEIGHT (480)
29
30 static const char *border_img_path = PACKAGE_EXAMPLES_DIR "/red.png";
31
32 static const char *commands = \
33   "commands are:\n"
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"
48   "\th - print help\n";
49
50 struct exemple_data
51 {
52    Ecore_Evas  *ee;
53    Evas        *evas;
54    Evas_Object *bg, *box, *border;
55 };
56
57 static struct exemple_data d;
58
59 static void /* custom 'diagonal' layout */
60 _custom_layout(Evas_Object          *o,
61                Evas_Object_Box_Data *p,
62                void                 *data __UNUSED__)
63 {
64    int x, y, w, h;
65    int xx, yy, ww, hh;
66    int count;
67    Eina_List *l;
68    Evas_Object_Box_Option *opt;
69
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);
74    if (ww < 1) ww = 1;
75    if (hh < 1) hh = 1;
76
77    xx = x;
78    yy = y;
79    EINA_LIST_FOREACH(p->children, l, opt)
80      {
81         evas_object_move(opt->obj, xx, yy);
82         xx += ww;
83         yy += hh;
84      }
85 }
86
87 static Evas_Object * /* new rectangle to be put in the box */
88 _new_rectangle_add(Evas *e)
89 {
90    Evas_Object *o;
91
92    o = evas_object_rectangle_add(e);
93    evas_object_resize(o, 10, 10);
94    evas_object_color_set(o, 0, 255, 0, 255);
95    evas_object_show(o);
96
97    return o;
98 }
99
100 /* use the following commands to interact with this example - 'h' is
101  * the key for help */
102 static void
103 _on_keydown(void        *data __UNUSED__,
104             Evas        *evas __UNUSED__,
105             Evas_Object *o __UNUSED__,
106             void        *einfo)
107 {
108    Evas_Event_Key_Down *ev = einfo;
109    const Evas_Modifier *mods = evas_key_modifier_get(evas);
110
111    if (strcmp(ev->keyname, "h") == 0) /* print help */
112      {
113         fprintf(stdout, commands);
114         return;
115      }
116
117    if (evas_key_modifier_is_set(mods, "Shift"))
118      {
119         int pos;
120         Eina_Bool ret;
121         Evas_Object *obj;
122         Eina_List *children;
123
124         pos = atoi(ev->keyname);
125         children = evas_object_box_children_get(d.box);
126
127         obj = eina_list_nth(children, pos);
128         if (!obj) goto list_free;
129
130         ret = evas_object_box_remove_at(d.box, pos);
131         if (ret) evas_object_del(obj);
132
133 list_free:
134         eina_list_free(children);
135         return;
136      }
137
138    if (evas_key_modifier_is_set(mods, "Control"))
139      {
140         Evas_Object *o;
141         int pos;
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);
146         return;
147      }
148
149    if (strcmp(ev->keyname, "a") == 0)
150      {
151         double h, v;
152
153         evas_object_box_align_get(d.box, &h, &v);
154
155         if (h == 0.5)
156           h = v = 1.0;
157         else if (h == 1.0)
158           h = v = -1.0;
159         else if (h == -1.0)
160           h = v = 0.0;
161         else if (h == 0.0)
162           h = v = 0.5;
163
164         evas_object_box_align_set(d.box, h, v);
165
166         fprintf(stdout, "Applying new alignment values (%.1f, %.1f)"
167                         " on the box\n", h, v);
168         return;
169      }
170
171    if (strcmp(ev->keyname, "p") == 0)
172      {
173         int h, v;
174
175         evas_object_box_padding_get(d.box, &h, &v);
176
177         if (h == 0)
178           h = v = 50;
179         else
180           h = v = 0;
181
182         evas_object_box_padding_set(d.box, h, v);
183
184         fprintf(stdout, "Applying new padding values (%d, %d)"
185                         " on the box\n", h, v);
186         return;
187      }
188
189    if (strcmp(ev->keyname, "1") == 0)
190      {
191         evas_object_box_layout_set(
192           d.box, evas_object_box_layout_horizontal, NULL, NULL);
193
194         fprintf(stdout, "Applying '%s' layout on the box\n", "horizontal");
195         return;
196      }
197
198    if (strcmp(ev->keyname, "2") == 0)
199      {
200         evas_object_box_layout_set(
201           d.box, evas_object_box_layout_vertical, NULL, NULL);
202
203         fprintf(stdout, "Applying '%s' layout on the box\n", "vertical");
204         return;
205      }
206
207    if (strcmp(ev->keyname, "3") == 0)
208      {
209         evas_object_box_layout_set(
210           d.box, evas_object_box_layout_homogeneous_horizontal, NULL,
211           NULL);
212
213         fprintf(stdout, "Applying '%s' layout on the box\n",
214                 "horizontal homogeneous");
215         return;
216      }
217
218    if (strcmp(ev->keyname, "4") == 0)
219      {
220         evas_object_box_layout_set(
221           d.box, evas_object_box_layout_homogeneous_vertical, NULL, NULL);
222
223         fprintf(stdout, "Applying '%s' layout on the box\n",
224                 "vertical homogeneous");
225         return;
226      }
227
228    if (strcmp(ev->keyname, "5") == 0)
229      {
230         evas_object_box_layout_set(
231           d.box, evas_object_box_layout_homogeneous_max_size_horizontal,
232           NULL, NULL);
233
234         fprintf(stdout, "Applying '%s' layout on the box\n",
235                 "horizontal maximum size homogeneous");
236         return;
237      }
238
239    if (strcmp(ev->keyname, "6") == 0)
240      {
241         evas_object_box_layout_set(
242           d.box, evas_object_box_layout_homogeneous_max_size_vertical,
243           NULL, NULL);
244
245         fprintf(stdout, "Applying '%s' layout on the box\n",
246                 "vertical maximum size homogeneous");
247         return;
248      }
249
250    if (strcmp(ev->keyname, "7") == 0)
251      {
252         evas_object_box_layout_set(
253           d.box, evas_object_box_layout_flow_horizontal, NULL, NULL);
254
255         fprintf(stdout, "Applying '%s' layout on the box\n", "horizontal flow");
256         return;
257      }
258
259    if (strcmp(ev->keyname, "8") == 0)
260      {
261         evas_object_box_layout_set(
262           d.box, evas_object_box_layout_flow_vertical, NULL, NULL);
263
264         fprintf(stdout, "Applying '%s' layout on the box\n", "vertical flow");
265         return;
266      }
267
268    if (strcmp(ev->keyname, "9") == 0)
269      {
270         evas_object_box_layout_set(
271           d.box, evas_object_box_layout_stack, NULL, NULL);
272
273         fprintf(stdout, "Applying '%s' layout on the box\n", "stack");
274         return;
275      }
276
277    if (strcmp(ev->keyname, "0") == 0)
278      {
279         evas_object_box_layout_set(d.box, _custom_layout, NULL, NULL);
280
281         fprintf(stdout, "Applying '%s' layout on the box\n", "CUSTOM");
282         return;
283      }
284 }
285
286 static void
287 _on_delete(Ecore_Evas *ee __UNUSED__)
288 {
289    ecore_main_loop_quit();
290 }
291
292 static void /* adjust canvas' contents on resizes */
293 _canvas_resize_cb(Ecore_Evas *ee)
294 {
295    int w, h;
296
297    ecore_evas_geometry_get(ee, NULL, NULL, &w, &h);
298
299    evas_object_resize(d.bg, w, h);
300
301    evas_object_move(d.box, (w / 4), (h / 4));
302    evas_object_resize(d.box, (w / 2), (h / 2));
303
304    evas_object_move(d.border, (w / 4) - 2, (h / 4) - 2);
305    evas_object_resize(d.border, (w / 2) + 4, (h / 2) + 4);
306 }
307
308 int
309 main(void)
310 {
311    Evas_Object *last, *o;
312    int i;
313
314    srand(time(NULL));
315
316    if (!ecore_evas_init())
317      return EXIT_FAILURE;
318
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);
322    if (!d.ee)
323      goto panic;
324
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);
328
329    d.evas = ecore_evas_get(d.ee);
330
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);
334
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);
338
339    d.box = evas_object_box_add(d.evas);
340    evas_object_show(d.box);
341
342    for (i = 1; i <= 5; i++)
343      {
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);
348         evas_object_show(o);
349
350         if (!evas_object_box_append(d.box, o))
351           {
352              fprintf(stderr, "Error appending child object on the box!\n");
353              goto error;
354           }
355      }
356
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);
365
366    fprintf(stdout, commands);
367
368    _canvas_resize_cb(d.ee);
369    ecore_main_loop_begin();
370    ecore_evas_shutdown();
371    return 0;
372
373 error:
374    ecore_evas_shutdown();
375    return -1;
376
377 panic:
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");
380    return -2;
381 }
382