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