2 * Simple Elementary's <b>gengrid widget</b> example, illustrating its
5 * See stdout/stderr for output. Compile with:
8 * gcc -g gengrid_example.c -o gengrid_example `pkg-config --cflags --libs elementary`
12 #include <Elementary.h>
14 typedef struct _Example_Item
19 static const char *imgs[9] =
32 static Elm_Gengrid_Item_Class *gic = NULL;
33 static Evas_Object *before_bt, *after_bt;
43 /* change layouting mode */
45 _horizontal_grid(void *data,
49 Evas_Object *grid = data;
51 elm_gengrid_horizontal_set(grid, elm_check_state_get(obj));
54 /* "always select" callback */
56 _always_select_change(void *data,
60 Evas_Object *grid = data;
61 Eina_Bool always = elm_check_state_get(obj);
64 elm_gengrid_select_mode_set(grid, ELM_OBJECT_SELECT_MODE_ALWAYS);
66 elm_gengrid_select_mode_set(grid, ELM_OBJECT_SELECT_MODE_DEFAULT);
68 fprintf(stdout, "\"Always select\" mode for gengrid items is now %s\n",
69 always ? "on" : "off");
72 /* "bouncing mode" callback */
74 _bouncing_change(void *data,
78 Evas_Object *grid = data;
79 Eina_Bool bounce = elm_check_state_get(obj);
81 elm_gengrid_bounce_set(grid, bounce, bounce);
83 fprintf(stdout, "Bouncing effect for gengrid is now %s\n",
84 bounce ? "on" : "off");
87 /* multi-selection callback */
89 _multi_change(void *data,
93 Evas_Object *grid = data;
94 Eina_Bool multi = elm_check_state_get(obj);
96 elm_gengrid_multi_select_set(grid, multi);
98 fprintf(stdout, "Multi-selection for gengrid is now %s\n",
99 multi ? "on" : "off");
101 elm_object_disabled_set(before_bt, multi);
102 elm_object_disabled_set(after_bt, multi);
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);
113 /* no selection callback */
115 _no_sel_change(void *data,
119 Evas_Object *grid = data;
120 Eina_Bool no_sel = elm_check_state_get(obj);
123 elm_gengrid_select_mode_set(grid, ELM_OBJECT_SELECT_MODE_NONE);
125 elm_gengrid_select_mode_set(grid, ELM_OBJECT_SELECT_MODE_DEFAULT);
127 fprintf(stdout, "Selection for gengrid items is now %s\n",
128 no_sel ? "disabled" : "enabled");
131 /* item selection callback */
133 _grid_sel(void *data,
138 Example_Item *it = elm_object_item_data_get(event_info);
140 elm_gengrid_item_pos_get(event_info, &x, &y);
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);
146 /* new item with random path */
147 static Example_Item *
152 it = malloc(sizeof(*it));
153 it->path = eina_stringshare_add(imgs[rand() % (sizeof(imgs) /
158 /* "insert before" callback */
160 _before_bt_clicked(void *data,
165 Evas_Object *grid = data;
166 Elm_Object_Item *sel;
168 sel = elm_gengrid_selected_item_get(grid);
173 elm_gengrid_item_insert_before(grid, gic, it, sel, _grid_sel, NULL);
176 /* "insert after" callback */
178 _after_bt_clicked(void *data,
183 Evas_Object *grid = data;
184 Elm_Object_Item *sel;
186 sel = elm_gengrid_selected_item_get(grid);
191 elm_gengrid_item_insert_after(grid, gic, it, sel, _grid_sel, NULL);
194 /* prepend an item */
196 _prepend_bt_clicked(void *data,
201 Evas_Object *grid = data;
204 elm_gengrid_item_prepend(grid, gic, it, _grid_sel, NULL);
209 _append_bt_clicked(void *data,
213 Evas_Object *grid = data;
214 Example_Item *it = _item_new();
216 elm_gengrid_item_append(grid, gic, it, _grid_sel, NULL);
221 _clear_cb(void *data,
225 elm_gengrid_clear(data);
227 fprintf(stdout, "Clearing the grid!\n");
230 /* bring in 1st item */
232 _bring_1st_clicked(void *data,
236 Elm_Object_Item *gg_it = elm_gengrid_first_item_get(data);
240 elm_gengrid_item_bring_in(gg_it, ELM_GENGRID_ITEM_SCROLLTO_IN);
245 _show_last_clicked(void *data,
249 Elm_Object_Item *gg_it = elm_gengrid_last_item_get(data);
253 elm_gengrid_item_show(gg_it, ELM_GENGRID_ITEM_SCROLLTO_IN);
256 /* disable selected item */
258 _toggle_disabled_cb(void *data,
262 Elm_Object_Item *gg_it = elm_gengrid_selected_item_get(data);
266 elm_gengrid_item_selected_set(gg_it, EINA_FALSE);
267 elm_object_item_disabled_set(gg_it, EINA_TRUE);
270 /* change items' size */
272 _size_changed(void *data,
276 Evas_Object *grid = data;
277 int size = elm_spinner_value_get(obj);
279 elm_gengrid_item_size_set(grid, size, size);
282 /* item double click callback */
284 _double_click(void *data,
288 fprintf(stdout, "Double click on item with handle %p\n", event_info);
291 /* item long press callback */
293 _long_pressed(void *data,
297 fprintf(stdout, "Long press on item with handle %p\n", event_info);
301 /* label fetching callback */
303 _grid_label_get(void *data,
307 const Example_Item *it = data;
310 snprintf(buf, sizeof(buf), "Photo %s", it->path);
314 /* icon fetching callback */
316 _grid_content_get(void *data,
320 const Example_Item *it = data;
322 if (!strcmp(part, "elm.swallow.icon"))
324 Evas_Object *icon = elm_bg_add(obj);
327 snprintf(buf, sizeof(buf), "%s/images/%s", elm_app_data_dir_get(),
330 elm_bg_file_set(icon, buf, NULL);
331 evas_object_size_hint_aspect_set(icon, EVAS_ASPECT_CONTROL_VERTICAL, 1,
333 evas_object_show(icon);
336 else if (!strcmp(part, "elm.swallow.end"))
339 ck = elm_check_add(obj);
340 evas_object_propagate_events_set(ck, EINA_FALSE);
341 evas_object_show(ck);
348 /* state fetching callback */
350 _grid_state_get(void *data,
357 /* deletion callback */
359 _grid_del(void *data,
362 Example_Item *it = data;
364 eina_stringshare_del(it->path);
368 /* scrolling animation stopped callback */
370 _grid_scroll_stopped_cb(void *data,
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);
380 /* items grid horizontal alignment change */
382 _h_align_change_cb(void *data,
387 double val = elm_slider_value_get(obj);
389 elm_gengrid_align_get(data, NULL, &v_align);
391 fprintf(stdout, "Setting horizontal alignment to %f\n", val);
392 elm_gengrid_align_set(data, val, v_align);
396 _v_align_change_cb(void *data,
401 double val = elm_slider_value_get(obj);
403 elm_gengrid_align_get(data, &h_align, NULL);
405 fprintf(stdout, "Setting vertical alignment to %f\n", val);
406 elm_gengrid_align_set(data, h_align, val);
409 /* page relative size change */
411 _page_change_cb(void *data,
415 double val = elm_slider_value_get(obj);
417 elm_gengrid_page_relative_set(data, val, val);
419 fprintf(stdout, "Setting grid page's relative size to %f\n", val);
426 Evas_Object *win, *bg, *grid, *bx, *hbx_1, *hbx_2, *hbx_3, *bt, *ck, *sl,
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);
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);
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);
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);
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);
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);
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);
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,
479 elm_box_pack_end(hbx_1, before_bt);
480 evas_object_show(before_bt);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
566 elm_gengrid_align_get(grid, &h, &v);
567 evas_object_smart_callback_add(grid, "scroll,anim,stop", _grid_scroll_stopped_cb, NULL);
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);
579 evas_object_smart_callback_add(sl, "changed", _h_align_change_cb, grid);
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);
591 evas_object_smart_callback_add(sl, "changed", _v_align_change_cb, grid);
593 elm_gengrid_align_get(grid, &h, &v);
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);
605 _page_change_cb(grid, sl, NULL);
606 evas_object_smart_callback_add(sl, "changed", _page_change_cb, grid);
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
618 _append_bt_clicked(grid, NULL, NULL);
619 _append_bt_clicked(grid, NULL, NULL);
620 _append_bt_clicked(grid, NULL, NULL);
622 evas_object_resize(win, 600, 600);
623 evas_object_show(win);