When the separators exist in toolbar and the toolbar shrinks by resizing window,
[framework/uifw/elementary.git] / src / examples / gengrid_example.c
1 /**
2  * Simple Elementary's <b>gengrid widget</b> example, illustrating its
3  * usage and API.
4  *
5  * See stdout/stderr for output. Compile with:
6  *
7  * @verbatim
8  * gcc -g gengrid_example.c -o gengrid_example `pkg-config --cflags --libs elementary`
9  * @endverbatim
10  */
11
12 #include <Elementary.h>
13
14 typedef struct _Example_Item
15 {
16    const char *path;
17 } Example_Item;
18
19 static const char *imgs[9] =
20 {
21    "panel_01.jpg",
22    "plant_01.jpg",
23    "rock_01.jpg",
24    "rock_02.jpg",
25    "sky_01.jpg",
26    "sky_02.jpg",
27    "sky_03.jpg",
28    "sky_04.jpg",
29    "wood_01.jpg",
30 };
31
32 static Elm_Gengrid_Item_Class *gic = NULL;
33 static Evas_Object *before_bt, *after_bt;
34
35 static void
36 _on_done(void        *data,
37          Evas_Object *obj,
38          void        *event_info)
39 {
40    elm_exit();
41 }
42
43 /* change layouting mode */
44 static void
45 _horizontal_grid(void        *data,
46                  Evas_Object *obj,
47                  void        *event_info)
48 {
49    Evas_Object *grid = data;
50
51    elm_gengrid_horizontal_set(grid, elm_check_state_get(obj));
52 }
53
54 /* "always select" callback */
55 static void
56 _always_select_change(void        *data,
57                       Evas_Object *obj,
58                       void        *event_info)
59 {
60    Evas_Object *grid = data;
61    Eina_Bool always = elm_check_state_get(obj);
62
63    if (always)
64      elm_gengrid_select_mode_set(grid, ELM_OBJECT_SELECT_MODE_ALWAYS);
65    else
66      elm_gengrid_select_mode_set(grid, ELM_OBJECT_SELECT_MODE_DEFAULT);
67
68    fprintf(stdout, "\"Always select\" mode for gengrid items is now %s\n",
69            always ? "on" : "off");
70 }
71
72 /* "bouncing mode" callback */
73 static void
74 _bouncing_change(void        *data,
75                  Evas_Object *obj,
76                  void        *event_info)
77 {
78    Evas_Object *grid = data;
79    Eina_Bool bounce = elm_check_state_get(obj);
80
81    elm_gengrid_bounce_set(grid, bounce, bounce);
82
83    fprintf(stdout, "Bouncing effect for gengrid is now %s\n",
84            bounce ? "on" : "off");
85 }
86
87 /* multi-selection callback */
88 static void
89 _multi_change(void        *data,
90               Evas_Object *obj,
91               void        *event_info)
92 {
93    Evas_Object *grid = data;
94    Eina_Bool multi = elm_check_state_get(obj);
95
96    elm_gengrid_multi_select_set(grid, multi);
97
98    fprintf(stdout, "Multi-selection for gengrid is now %s\n",
99            multi ? "on" : "off");
100
101    elm_object_disabled_set(before_bt, multi);
102    elm_object_disabled_set(after_bt, multi);
103
104    if (!multi)
105      {
106         Elm_Object_Item *gg_it;
107         const Eina_List *selected = elm_gengrid_selected_items_get(grid), *l;
108         EINA_LIST_FOREACH(selected, l, gg_it)
109           elm_gengrid_item_selected_set(gg_it, EINA_FALSE);
110      }
111 }
112
113 /* no selection callback */
114 static void
115 _no_sel_change(void        *data,
116                Evas_Object *obj,
117                void        *event_info)
118 {
119    Evas_Object *grid = data;
120    Eina_Bool no_sel = elm_check_state_get(obj);
121
122    if (no_sel)
123      elm_gengrid_select_mode_set(grid, ELM_OBJECT_SELECT_MODE_NONE);
124    else
125      elm_gengrid_select_mode_set(grid, ELM_OBJECT_SELECT_MODE_DEFAULT);
126
127    fprintf(stdout, "Selection for gengrid items is now %s\n",
128            no_sel ? "disabled" : "enabled");
129 }
130
131 /* item selection callback */
132 static void
133 _grid_sel(void        *data,
134           Evas_Object *obj,
135           void        *event_info)
136 {
137    unsigned int x, y;
138    Example_Item *it = elm_object_item_data_get(event_info);
139
140    elm_gengrid_item_pos_get(event_info, &x, &y);
141
142    fprintf(stdout, "Item [%p], with data [%p], path %s, at position (%d, %d),"
143                    " has been selected\n", event_info, data, it->path, x, y);
144 }
145
146 /* new item with random path */
147 static Example_Item *
148 _item_new(void)
149 {
150    Example_Item *it;
151
152    it = malloc(sizeof(*it));
153    it->path = eina_stringshare_add(imgs[rand() % (sizeof(imgs) /
154                                                   sizeof(imgs[0]))]);
155    return it;
156 }
157
158 /* "insert before" callback */
159 static void
160 _before_bt_clicked(void        *data,
161                    Evas_Object *obj,
162                    void        *event_info)
163 {
164    Example_Item *it;
165    Evas_Object *grid = data;
166    Elm_Object_Item *sel;
167
168    sel = elm_gengrid_selected_item_get(grid);
169    if (!sel)
170      return;
171
172    it = _item_new();
173    elm_gengrid_item_insert_before(grid, gic, it, sel, _grid_sel, NULL);
174 }
175
176 /* "insert after" callback */
177 static void
178 _after_bt_clicked(void        *data,
179                   Evas_Object *obj,
180                   void        *event_info)
181 {
182    Example_Item *it;
183    Evas_Object *grid = data;
184    Elm_Object_Item *sel;
185
186    sel = elm_gengrid_selected_item_get(grid);
187    if (!sel)
188      return;
189
190    it = _item_new();
191    elm_gengrid_item_insert_after(grid, gic, it, sel, _grid_sel, NULL);
192 }
193
194 /* prepend an item */
195 static void
196 _prepend_bt_clicked(void        *data,
197                     Evas_Object *obj,
198                     void        *event_info)
199 {
200    Example_Item *it;
201    Evas_Object *grid = data;
202
203    it = _item_new();
204    elm_gengrid_item_prepend(grid, gic, it, _grid_sel, NULL);
205 }
206
207 /* append an item */
208 static void
209 _append_bt_clicked(void        *data,
210                    Evas_Object *obj,
211                    void        *event_info)
212 {
213    Evas_Object *grid = data;
214    Example_Item *it = _item_new();
215
216    elm_gengrid_item_append(grid, gic, it, _grid_sel, NULL);
217 }
218
219 /* delete items */
220 static void
221 _clear_cb(void        *data,
222           Evas_Object *obj,
223           void        *event_info)
224 {
225    elm_gengrid_clear(data);
226
227    fprintf(stdout, "Clearing the grid!\n");
228 }
229
230 /* bring in 1st item */
231 static void
232 _bring_1st_clicked(void        *data,
233                    Evas_Object *obj,
234                    void        *event_info)
235 {
236    Elm_Object_Item *gg_it = elm_gengrid_first_item_get(data);
237
238    if (!gg_it) return;
239
240    elm_gengrid_item_bring_in(gg_it, ELM_GENGRID_ITEM_SCROLLTO_IN);
241 }
242
243 /* show last item */
244 static void
245 _show_last_clicked(void        *data,
246                    Evas_Object *obj,
247                    void        *event_info)
248 {
249    Elm_Object_Item *gg_it = elm_gengrid_last_item_get(data);
250
251    if (!gg_it) return;
252
253    elm_gengrid_item_show(gg_it, ELM_GENGRID_ITEM_SCROLLTO_IN);
254 }
255
256 /* disable selected item */
257 static void
258 _toggle_disabled_cb(void        *data,
259                     Evas_Object *obj,
260                     void        *event_info)
261 {
262    Elm_Object_Item *gg_it = elm_gengrid_selected_item_get(data);
263
264    if (!gg_it) return;
265
266    elm_gengrid_item_selected_set(gg_it, EINA_FALSE);
267    elm_object_item_disabled_set(gg_it, EINA_TRUE);
268 }
269
270 /* change items' size */
271 static void
272 _size_changed(void        *data,
273               Evas_Object *obj,
274               void        *event_info)
275 {
276    Evas_Object *grid = data;
277    int size = elm_spinner_value_get(obj);
278
279    elm_gengrid_item_size_set(grid, size, size);
280 }
281
282 /* item double click callback */
283 static void
284 _double_click(void        *data,
285               Evas_Object *obj,
286               void        *event_info)
287 {
288    fprintf(stdout, "Double click on item with handle %p\n", event_info);
289 }
290
291 /* item long press callback */
292 static void
293 _long_pressed(void        *data,
294               Evas_Object *obj,
295               void        *event_info)
296 {
297    fprintf(stdout, "Long press on item with handle %p\n", event_info);
298 }
299
300
301 /* label fetching callback */
302 static char *
303 _grid_label_get(void        *data,
304                 Evas_Object *obj,
305                 const char  *part)
306 {
307    const Example_Item *it = data;
308    char buf[256];
309
310    snprintf(buf, sizeof(buf), "Photo %s", it->path);
311    return strdup(buf);
312 }
313
314 /* icon fetching callback */
315 static Evas_Object *
316 _grid_content_get(void        *data,
317                Evas_Object *obj,
318                const char  *part)
319 {
320    const Example_Item *it = data;
321
322    if (!strcmp(part, "elm.swallow.icon"))
323      {
324         Evas_Object *icon = elm_bg_add(obj);
325         char buf[PATH_MAX];
326
327         snprintf(buf, sizeof(buf), "%s/images/%s", elm_app_data_dir_get(),
328                  it->path);
329
330         elm_bg_file_set(icon, buf, NULL);
331         evas_object_size_hint_aspect_set(icon, EVAS_ASPECT_CONTROL_VERTICAL, 1,
332                                          1);
333         evas_object_show(icon);
334         return icon;
335      }
336    else if (!strcmp(part, "elm.swallow.end"))
337      {
338         Evas_Object *ck;
339         ck = elm_check_add(obj);
340         evas_object_propagate_events_set(ck, EINA_FALSE);
341         evas_object_show(ck);
342         return ck;
343      }
344
345    return NULL;
346 }
347
348 /* state fetching callback */
349 static Eina_Bool
350 _grid_state_get(void        *data,
351                 Evas_Object *obj,
352                 const char  *part)
353 {
354    return EINA_FALSE;
355 }
356
357 /* deletion callback */
358 static void
359 _grid_del(void        *data,
360           Evas_Object *obj)
361 {
362    Example_Item *it = data;
363
364    eina_stringshare_del(it->path);
365    free(it);
366 }
367
368 /* scrolling animation stopped callback */
369 static void
370 _grid_scroll_stopped_cb(void        *data,
371                         Evas_Object *obj,
372                         void        *event_info)
373 {
374   int h_pagenumber = 0, v_pagenumber = 0;
375   elm_gengrid_current_page_get(obj, &h_pagenumber, &v_pagenumber);
376   fprintf(stdout, "Grid current horiz page is %d, vert page is %d\n",
377           h_pagenumber, v_pagenumber);
378 }
379
380 /* items grid horizontal alignment change */
381 static void
382 _h_align_change_cb(void        *data,
383                    Evas_Object *obj,
384                    void        *event_info)
385 {
386    double v_align;
387    double val = elm_slider_value_get(obj);
388
389    elm_gengrid_align_get(data, NULL, &v_align);
390
391    fprintf(stdout, "Setting horizontal alignment to %f\n", val);
392    elm_gengrid_align_set(data, val, v_align);
393 }
394
395 static void
396 _v_align_change_cb(void        *data,
397                    Evas_Object *obj,
398                    void        *event_info)
399 {
400    double h_align;
401    double val = elm_slider_value_get(obj);
402
403    elm_gengrid_align_get(data, &h_align, NULL);
404
405    fprintf(stdout, "Setting vertical alignment to %f\n", val);
406    elm_gengrid_align_set(data, h_align, val);
407 }
408
409 /* page relative size change */
410 static void
411 _page_change_cb(void        *data,
412                 Evas_Object *obj,
413                 void        *event_info)
414 {
415    double val = elm_slider_value_get(obj);
416
417    elm_gengrid_page_relative_set(data, val, val);
418
419    fprintf(stdout, "Setting grid page's relative size to %f\n", val);
420 }
421
422 EAPI_MAIN int
423 elm_main(int    argc,
424          char **argv)
425 {
426    Evas_Object *win, *bg, *grid, *bx, *hbx_1, *hbx_2, *hbx_3, *bt, *ck, *sl,
427    *sp;
428    Eina_Bool bounce;
429    double h, v;
430
431    srand(time(NULL));
432
433    elm_app_info_set(elm_main, "elementary", "images");
434    win = elm_win_add(NULL, "gengrid", ELM_WIN_BASIC);
435    elm_win_title_set(win, "Generic Grid Example");
436    evas_object_smart_callback_add(win, "delete,request", _on_done, NULL);
437
438    bg = elm_bg_add(win);
439    evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
440    elm_win_resize_object_add(win, bg);
441    evas_object_show(bg);
442
443    bx = elm_box_add(win);
444    evas_object_size_hint_weight_set(bx, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
445    elm_win_resize_object_add(win, bx);
446    evas_object_show(bx);
447
448    grid = elm_gengrid_add(win);
449    elm_gengrid_item_size_set(grid, 150, 150);
450    evas_object_size_hint_weight_set(grid, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
451    evas_object_size_hint_min_set(grid, 600, 500);
452    elm_box_pack_end(bx, grid);
453    evas_object_smart_callback_add(grid, "clicked,double", _double_click, NULL);
454    evas_object_smart_callback_add(grid, "longpressed", _long_pressed, NULL);
455    evas_object_show(grid);
456
457    hbx_1 = elm_box_add(win);
458    evas_object_size_hint_weight_set(hbx_1, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
459    elm_box_horizontal_set(hbx_1, EINA_TRUE);
460    elm_box_pack_end(bx, hbx_1);
461    evas_object_show(hbx_1);
462
463    bt = elm_button_add(win);
464    elm_object_text_set(bt, "Append");
465    evas_object_smart_callback_add(bt, "clicked", _append_bt_clicked, grid);
466    elm_box_pack_end(hbx_1, bt);
467    evas_object_show(bt);
468
469    bt = elm_button_add(win);
470    elm_object_text_set(bt, "Prepend");
471    evas_object_smart_callback_add(bt, "clicked", _prepend_bt_clicked, grid);
472    elm_box_pack_end(hbx_1, bt);
473    evas_object_show(bt);
474
475    before_bt = elm_button_add(win);
476    elm_object_text_set(before_bt, "Insert before");
477    evas_object_smart_callback_add(before_bt, "clicked", _before_bt_clicked,
478                                   grid);
479    elm_box_pack_end(hbx_1, before_bt);
480    evas_object_show(before_bt);
481
482    after_bt = elm_button_add(win);
483    elm_object_text_set(after_bt, "Insert after");
484    evas_object_smart_callback_add(after_bt, "clicked", _after_bt_clicked, grid);
485    elm_box_pack_end(hbx_1, after_bt);
486    evas_object_show(after_bt);
487
488    bt = elm_button_add(win);
489    elm_object_text_set(bt, "Clear");
490    evas_object_smart_callback_add(bt, "clicked", _clear_cb, grid);
491    elm_box_pack_end(hbx_1, bt);
492    evas_object_show(bt);
493
494    bt = elm_button_add(win);
495    elm_object_text_set(bt, "Bring in 1st");
496    evas_object_smart_callback_add(bt, "clicked", _bring_1st_clicked, grid);
497    elm_box_pack_end(hbx_1, bt);
498    evas_object_show(bt);
499
500    bt = elm_button_add(win);
501    elm_object_text_set(bt, "Show last");
502    evas_object_smart_callback_add(bt, "clicked", _show_last_clicked, grid);
503    elm_box_pack_end(hbx_1, bt);
504    evas_object_show(bt);
505
506    sp = elm_spinner_add(win);
507    elm_spinner_min_max_set(sp, 10, 1024);
508    elm_spinner_value_set(sp, 150);
509    elm_spinner_label_format_set(sp, "Item size: %.0f");
510    evas_object_smart_callback_add(sp, "changed", _size_changed, grid);
511    evas_object_size_hint_weight_set(sp, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
512    elm_box_pack_end(hbx_1, sp);
513    evas_object_show(sp);
514
515    hbx_2 = elm_box_add(win);
516    evas_object_size_hint_weight_set(hbx_2, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
517    elm_box_horizontal_set(hbx_2, EINA_TRUE);
518    elm_box_pack_end(bx, hbx_2);
519    evas_object_show(hbx_2);
520
521    bt = elm_button_add(win);
522    elm_object_text_set(bt, "Disable item");
523    evas_object_smart_callback_add(bt, "clicked", _toggle_disabled_cb, grid);
524    elm_box_pack_end(hbx_2, bt);
525    evas_object_show(bt);
526
527    ck = elm_check_add(win);
528    elm_object_text_set(ck, "Horizontal mode");
529    evas_object_smart_callback_add(ck, "changed", _horizontal_grid, grid);
530    elm_box_pack_end(hbx_2, ck);
531    evas_object_show(ck);
532
533    ck = elm_check_add(win);
534    elm_object_text_set(ck, "Always select");
535    evas_object_smart_callback_add(ck, "changed", _always_select_change, grid);
536    elm_box_pack_end(hbx_2, ck);
537    evas_object_show(ck);
538
539    ck = elm_check_add(win);
540    elm_gengrid_bounce_get(grid, &bounce, NULL);
541    elm_object_text_set(ck, "Bouncing");
542    elm_check_state_set(ck, bounce);
543    evas_object_smart_callback_add(ck, "changed", _bouncing_change, grid);
544    elm_box_pack_end(hbx_2, ck);
545    evas_object_show(ck);
546
547    ck = elm_check_add(win);
548    elm_object_text_set(ck, "Multi-selection");
549    elm_check_state_set(ck, elm_gengrid_multi_select_get(grid));
550    evas_object_smart_callback_add(ck, "changed", _multi_change, grid);
551    elm_box_pack_end(hbx_2, ck);
552    evas_object_show(ck);
553
554    ck = elm_check_add(win);
555    elm_object_text_set(ck, "No selection");
556    evas_object_smart_callback_add(ck, "changed", _no_sel_change, grid);
557    elm_box_pack_end(hbx_2, ck);
558    evas_object_show(ck);
559
560    hbx_3 = elm_box_add(win);
561    evas_object_size_hint_weight_set(hbx_3, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
562    elm_box_horizontal_set(hbx_3, EINA_TRUE);
563    elm_box_pack_end(bx, hbx_3);
564    evas_object_show(hbx_3);
565
566    elm_gengrid_align_get(grid, &h, &v);
567    evas_object_smart_callback_add(grid, "scroll,anim,stop", _grid_scroll_stopped_cb, NULL);
568
569    sl = elm_slider_add(win);
570    elm_object_text_set(sl, "Horiz. alignment");
571    elm_slider_span_size_set(sl, 100);
572    evas_object_size_hint_align_set(sl, 0.5, EVAS_HINT_FILL);
573    evas_object_size_hint_weight_set(sl, 0.0, EVAS_HINT_EXPAND);
574    elm_slider_indicator_format_set(sl, "%1.1f");
575    elm_slider_value_set(sl, h);
576    elm_box_pack_end(hbx_3, sl);
577    evas_object_show(sl);
578
579    evas_object_smart_callback_add(sl, "changed", _h_align_change_cb, grid);
580
581    sl = elm_slider_add(win);
582    elm_object_text_set(sl, "Vert. alignment");
583    elm_slider_span_size_set(sl, 100);
584    evas_object_size_hint_align_set(sl, 0.5, EVAS_HINT_FILL);
585    evas_object_size_hint_weight_set(sl, 0.0, EVAS_HINT_EXPAND);
586    elm_slider_indicator_format_set(sl, "%1.1f");
587    elm_slider_value_set(sl, v);
588    elm_box_pack_end(hbx_3, sl);
589    evas_object_show(sl);
590
591    evas_object_smart_callback_add(sl, "changed", _v_align_change_cb, grid);
592
593    elm_gengrid_align_get(grid, &h, &v);
594
595    sl = elm_slider_add(win);
596    elm_object_text_set(sl, "Page rel. size");
597    elm_slider_span_size_set(sl, 100);
598    evas_object_size_hint_align_set(sl, 0.5, EVAS_HINT_FILL);
599    evas_object_size_hint_weight_set(sl, 0.0, EVAS_HINT_EXPAND);
600    elm_slider_indicator_format_set(sl, "%1.1f");
601    elm_slider_value_set(sl, h);
602    elm_box_pack_end(hbx_3, sl);
603    evas_object_show(sl);
604
605    _page_change_cb(grid, sl, NULL);
606    evas_object_smart_callback_add(sl, "changed", _page_change_cb, grid);
607
608    if (!gic)
609      {
610         gic = elm_gengrid_item_class_new();
611         gic->item_style = "default";
612         gic->func.text_get = _grid_label_get;
613         gic->func.content_get = _grid_content_get;
614         gic->func.state_get = _grid_state_get;
615         gic->func.del = _grid_del;
616      } // we only create the first time its needed. we dont unref/free
617
618    _append_bt_clicked(grid, NULL, NULL);
619    _append_bt_clicked(grid, NULL, NULL);
620    _append_bt_clicked(grid, NULL, NULL);
621
622    evas_object_resize(win, 600, 600);
623    evas_object_show(win);
624
625    elm_run();
626    elm_shutdown();
627
628    return 0;
629 }
630 ELM_MAIN()