summaryrefslogtreecommitdiff
path: root/src/examples/gengrid_example.c
diff options
context:
space:
mode:
authorGustavo Lima Chaves <glima@profusion.mobi>2011-07-25 19:51:57 +0000
committerGustavo Lima Chaves <glima@profusion.mobi>2011-07-25 19:51:57 +0000
commit054f54a4dda35e08db946abf4f3c18be517aa3eb (patch)
tree99e89e463cd1d1b621ca6ae71fd8eed9bc7192d9 /src/examples/gengrid_example.c
parent7e02a4c44ad09941bea2aaa82d51ce156a47e38a (diff)
[elm] Documenting/exemplifying the following:
- elm_gengrid_add - elm_gengrid_align_get - elm_gengrid_align_set - elm_gengrid_always_select_mode_get - elm_gengrid_always_select_mode_set - elm_gengrid_bounce_get - elm_gengrid_bounce_set - elm_gengrid_clear - elm_gengrid_horizontal_get - elm_gengrid_horizontal_set - elm_gengrid_multi_select_get - elm_gengrid_multi_select_set - elm_gengrid_no_select_mode_get - elm_gengrid_no_select_mode_set - elm_gengrid_page_relative_get - elm_gengrid_page_relative_set - elm_gengrid_page_size_set SVN revision: 61704
Diffstat (limited to 'src/examples/gengrid_example.c')
-rw-r--r--src/examples/gengrid_example.c536
1 files changed, 536 insertions, 0 deletions
diff --git a/src/examples/gengrid_example.c b/src/examples/gengrid_example.c
new file mode 100644
index 000000000..7e22e9515
--- /dev/null
+++ b/src/examples/gengrid_example.c
@@ -0,0 +1,536 @@
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 `pkg-config --cflags --libs elementary` gengrid_example.c -o gengrid_example
9 * @endverbatim
10 */
11
12#include <Elementary.h>
13#ifdef HAVE_CONFIG_H
14# include "elementary_config.h"
15#else
16# define __UNUSED__
17# define PACKAGE_DATA_DIR "../../data"
18#endif
19
20typedef struct _Example_Item
21{
22 const char *path;
23} Example_Item;
24
25static const char *imgs[9] =
26{
27 "panel_01.jpg",
28 "plant_01.jpg",
29 "rock_01.jpg",
30 "rock_02.jpg",
31 "sky_01.jpg",
32 "sky_02.jpg",
33 "sky_03.jpg",
34 "sky_04.jpg",
35 "wood_01.jpg",
36};
37
38static Elm_Gengrid_Item_Class gic;
39Evas_Object *before_bt, *after_bt;
40
41static void
42_on_done(void *data __UNUSED__,
43 Evas_Object *obj __UNUSED__,
44 void *event_info __UNUSED__)
45{
46 elm_exit();
47}
48
49/* change layouting mode */
50static void
51_horizontal_grid(void *data,
52 Evas_Object *obj,
53 void *event_info __UNUSED__)
54{
55 Evas_Object *grid = data;
56
57 elm_gengrid_horizontal_set(grid, elm_check_state_get(obj));
58}
59
60/* "always select" callback */
61static void
62_always_select_change(void *data,
63 Evas_Object *obj,
64 void *event_info __UNUSED__)
65{
66 Evas_Object *grid = data;
67 Eina_Bool always = elm_check_state_get(obj);
68
69 elm_gengrid_always_select_mode_set(grid, always);
70
71 fprintf(stdout, "\"Always select\" mode for gengrid items is now %s\n",
72 always ? "on" : "off");
73}
74
75/* "bouncing mode" callback */
76static void
77_bouncing_change(void *data,
78 Evas_Object *obj,
79 void *event_info __UNUSED__)
80{
81 Evas_Object *grid = data;
82 Eina_Bool bounce = elm_check_state_get(obj);
83
84 elm_gengrid_bounce_set(grid, bounce, bounce);
85
86 fprintf(stdout, "Bouncing effect for gengrid is now %s\n",
87 bounce ? "on" : "off");
88}
89
90/* multi-selection callback */
91static void
92_multi_change(void *data,
93 Evas_Object *obj,
94 void *event_info __UNUSED__)
95{
96 Evas_Object *grid = data;
97 Eina_Bool multi = elm_check_state_get(obj);
98
99 elm_gengrid_multi_select_set(grid, multi);
100
101 fprintf(stdout, "Multi-selection for gengrid is now %s\n",
102 multi ? "on" : "off");
103
104 elm_object_disabled_set(before_bt, multi);
105 elm_object_disabled_set(after_bt, multi);
106
107 if (!multi)
108 {
109 Elm_Gengrid_Item *it;
110 const Eina_List *selected = elm_gengrid_selected_items_get(grid), *l;
111 EINA_LIST_FOREACH(selected, l, it)
112 elm_gengrid_item_selected_set(it, EINA_FALSE);
113 }
114}
115
116/* no selection callback */
117static void
118_no_sel_change(void *data,
119 Evas_Object *obj,
120 void *event_info __UNUSED__)
121{
122 Evas_Object *grid = data;
123 Eina_Bool no_sel = elm_check_state_get(obj);
124
125 elm_gengrid_no_select_mode_set(grid, no_sel);
126
127 fprintf(stdout, "Selection for gengrid items is now %s\n",
128 no_sel ? "disabled" : "enabled");
129}
130
131/* item selection callback */
132static void
133_grid_sel(void *data,
134 Evas_Object *obj __UNUSED__,
135 void *event_info)
136{
137 fprintf(stdout, "Item [%p], with data [%p], has been selected\n",
138 event_info, data);
139}
140
141/* new item with random path */
142static Example_Item *
143_item_new(void)
144{
145 Example_Item *it;
146
147 it = malloc(sizeof(*it));
148 it->path = eina_stringshare_add(imgs[rand() % (sizeof(imgs) /
149 sizeof(imgs[0]))]);
150 return it;
151}
152
153/* "insert before" callback */
154static void
155_before_bt_clicked(void *data,
156 Evas_Object *obj __UNUSED__,
157 void *event_info __UNUSED__)
158{
159 Example_Item *it;
160 Evas_Object *grid = data;
161 Elm_Gengrid_Item *sel;
162
163 sel = elm_gengrid_selected_item_get(grid);
164 if (!sel)
165 return;
166
167 it = _item_new();
168 elm_gengrid_item_insert_before(grid, &gic, it, sel, _grid_sel, NULL);
169}
170
171/* "insert after" callback */
172static void
173_after_bt_clicked(void *data,
174 Evas_Object *obj __UNUSED__,
175 void *event_info __UNUSED__)
176{
177 Example_Item *it;
178 Evas_Object *grid = data;
179 Elm_Gengrid_Item *sel;
180
181 sel = elm_gengrid_selected_item_get(grid);
182 if (!sel)
183 return;
184
185 it = _item_new();
186 elm_gengrid_item_insert_after(grid, &gic, it, sel, _grid_sel, NULL);
187}
188
189/* prepend an item */
190static void
191_prepend_bt_clicked(void *data,
192 Evas_Object *obj __UNUSED__,
193 void *event_info __UNUSED__)
194{
195 Example_Item *it;
196 Evas_Object *grid = data;
197
198 it = _item_new();
199 elm_gengrid_item_prepend(grid, &gic, it, _grid_sel, NULL);
200}
201
202/* append an item */
203static void
204_append_bt_clicked(void *data,
205 Evas_Object *obj __UNUSED__,
206 void *event_info __UNUSED__)
207{
208 Evas_Object *grid = data;
209 Example_Item *it = _item_new();
210
211 elm_gengrid_item_append(grid, &gic, it, _grid_sel, NULL);
212}
213
214/* delete items */
215static void
216_clear_cb(void *data,
217 Evas_Object *obj __UNUSED__,
218 void *event_info __UNUSED__)
219{
220 elm_gengrid_clear(data);
221
222 fprintf(stdout, "Clearing the grid!\n");
223}
224
225/* change items' size */
226static void
227_size_changed(void *data,
228 Evas_Object *obj,
229 void *event_info __UNUSED__)
230{
231 Evas_Object *grid = data;
232 int size = elm_spinner_value_get(obj);
233
234 elm_gengrid_item_size_set(grid, size, size);
235}
236
237/* item double click callback */
238static void
239_double_click(void *data __UNUSED__,
240 Evas_Object *obj __UNUSED__,
241 void *event_info)
242{
243 fprintf(stdout, "Double click on item with handle %p\n", event_info);
244}
245
246/* label fetching callback */
247static char *
248_grid_label_get(void *data,
249 Evas_Object *obj __UNUSED__,
250 const char *part __UNUSED__)
251{
252 const Example_Item *it = data;
253 char buf[256];
254
255 snprintf(buf, sizeof(buf), "Photo %s", it->path);
256 return strdup(buf);
257}
258
259/* icon fetching callback */
260static Evas_Object *
261_grid_icon_get(void *data,
262 Evas_Object *obj,
263 const char *part)
264{
265 const Example_Item *it = data;
266
267 if (!strcmp(part, "elm.swallow.icon"))
268 {
269 Evas_Object *icon = elm_bg_add(obj);
270 char buf[PATH_MAX];
271
272 snprintf(buf, sizeof(buf), "%s/images/%s", PACKAGE_DATA_DIR,
273 it->path);
274
275 elm_bg_file_set(icon, buf, NULL);
276 evas_object_size_hint_aspect_set(icon, EVAS_ASPECT_CONTROL_VERTICAL, 1,
277 1);
278 evas_object_show(icon);
279 return icon;
280 }
281 else if (!strcmp(part, "elm.swallow.end"))
282 {
283 Evas_Object *ck;
284 ck = elm_check_add(obj);
285 evas_object_propagate_events_set(ck, EINA_FALSE);
286 evas_object_show(ck);
287 return ck;
288 }
289
290 return NULL;
291}
292
293/* state fetching callback */
294static Eina_Bool
295_grid_state_get(void *data __UNUSED__,
296 Evas_Object *obj __UNUSED__,
297 const char *part __UNUSED__)
298{
299 return EINA_FALSE;
300}
301
302/* deletion callback */
303static void
304_grid_del(void *data,
305 Evas_Object *obj __UNUSED__)
306{
307 Example_Item *it = data;
308
309 eina_stringshare_del(it->path);
310 free(it);
311}
312
313/* items grid horizontal alignment change */
314static void
315_h_align_change_cb(void *data,
316 Evas_Object *obj,
317 void *event_info __UNUSED__)
318{
319 double v_align;
320 double val = elm_slider_value_get(obj);
321
322 elm_gengrid_align_get(data, NULL, &v_align);
323
324 fprintf(stdout, "Setting horizontal alignment to %f\n", val);
325 elm_gengrid_align_set(data, val, v_align);
326}
327
328static void
329_v_align_change_cb(void *data,
330 Evas_Object *obj,
331 void *event_info __UNUSED__)
332{
333 double h_align;
334 double val = elm_slider_value_get(obj);
335
336 elm_gengrid_align_get(data, &h_align, NULL);
337
338 fprintf(stdout, "Setting vertical alignment to %f\n", val);
339 elm_gengrid_align_set(data, h_align, val);
340}
341
342/* page relative size change */
343static void
344_page_change_cb(void *data,
345 Evas_Object *obj,
346 void *event_info __UNUSED__)
347{
348 double val = elm_slider_value_get(obj);
349
350 elm_gengrid_page_relative_set(data, val, val);
351
352 fprintf(stdout, "Setting grid page's relative size to %f\n", val);
353}
354
355int
356elm_main(int argc __UNUSED__,
357 char **argv __UNUSED__)
358{
359 Evas_Object *win, *bg, *grid, *bx, *hbx_1, *hbx_2, *hbx_3, *bt, *ck, *sl,
360 *sp;
361 Eina_Bool bounce;
362 double h, v;
363
364 srand(time(NULL));
365
366 win = elm_win_add(NULL, "gengrid", ELM_WIN_BASIC);
367 elm_win_title_set(win, "Generic Grid Example");
368 evas_object_smart_callback_add(win, "delete,request", _on_done, NULL);
369
370 bg = elm_bg_add(win);
371 evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
372 elm_win_resize_object_add(win, bg);
373 evas_object_show(bg);
374
375 bx = elm_box_add(win);
376 evas_object_size_hint_weight_set(bx, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
377 elm_win_resize_object_add(win, bx);
378 evas_object_show(bx);
379
380 grid = elm_gengrid_add(win);
381 elm_gengrid_item_size_set(grid, 150, 150);
382 evas_object_size_hint_weight_set(grid, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
383 evas_object_size_hint_min_set(grid, 600, 500);
384 elm_box_pack_end(bx, grid);
385 evas_object_smart_callback_add(grid, "clicked,double", _double_click, NULL);
386 evas_object_show(grid);
387
388 hbx_1 = elm_box_add(win);
389 evas_object_size_hint_weight_set(hbx_1, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
390 elm_box_horizontal_set(hbx_1, EINA_TRUE);
391 elm_box_pack_end(bx, hbx_1);
392 evas_object_show(hbx_1);
393
394 bt = elm_button_add(win);
395 elm_object_text_set(bt, "Append");
396 evas_object_smart_callback_add(bt, "clicked", _append_bt_clicked, grid);
397 elm_box_pack_end(hbx_1, bt);
398 evas_object_show(bt);
399
400 bt = elm_button_add(win);
401 elm_object_text_set(bt, "Prepend");
402 evas_object_smart_callback_add(bt, "clicked", _prepend_bt_clicked, grid);
403 elm_box_pack_end(hbx_1, bt);
404 evas_object_show(bt);
405
406 before_bt = elm_button_add(win);
407 elm_object_text_set(before_bt, "Insert before");
408 evas_object_smart_callback_add(before_bt, "clicked", _before_bt_clicked,
409 grid);
410 elm_box_pack_end(hbx_1, before_bt);
411 evas_object_show(before_bt);
412
413 after_bt = elm_button_add(win);
414 elm_object_text_set(after_bt, "Insert after");
415 evas_object_smart_callback_add(after_bt, "clicked", _after_bt_clicked, grid);
416 elm_box_pack_end(hbx_1, after_bt);
417 evas_object_show(after_bt);
418
419 bt = elm_button_add(win);
420 elm_object_text_set(bt, "Clear");
421 evas_object_smart_callback_add(bt, "clicked", _clear_cb, grid);
422 elm_box_pack_end(hbx_1, bt);
423 evas_object_show(bt);
424
425 sp = elm_spinner_add(win);
426 elm_spinner_min_max_set(sp, 10, 1024);
427 elm_spinner_value_set(sp, 150);
428 elm_spinner_label_format_set(sp, "Item size: %.0f");
429 evas_object_smart_callback_add(sp, "changed", _size_changed, grid);
430 evas_object_size_hint_weight_set(sp, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
431 elm_box_pack_end(hbx_1, sp);
432 evas_object_show(sp);
433
434 hbx_2 = elm_box_add(win);
435 evas_object_size_hint_weight_set(hbx_2, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
436 elm_box_horizontal_set(hbx_2, EINA_TRUE);
437 elm_box_pack_end(bx, hbx_2);
438 evas_object_show(hbx_2);
439
440 ck = elm_check_add(win);
441 elm_object_text_set(ck, "Horizontal mode");
442 evas_object_smart_callback_add(ck, "changed", _horizontal_grid, grid);
443 elm_box_pack_end(hbx_2, ck);
444 evas_object_show(ck);
445
446 ck = elm_check_add(win);
447 elm_object_text_set(ck, "Always select");
448 evas_object_smart_callback_add(ck, "changed", _always_select_change, grid);
449 elm_box_pack_end(hbx_2, ck);
450 evas_object_show(ck);
451
452 ck = elm_check_add(win);
453 elm_gengrid_bounce_get(grid, &bounce, NULL);
454 elm_object_text_set(ck, "Bouncing");
455 elm_check_state_set(ck, bounce);
456 evas_object_smart_callback_add(ck, "changed", _bouncing_change, grid);
457 elm_box_pack_end(hbx_2, ck);
458 evas_object_show(ck);
459
460 ck = elm_check_add(win);
461 elm_object_text_set(ck, "Multi-selection");
462 elm_check_state_set(ck, elm_gengrid_multi_select_get(grid));
463 evas_object_smart_callback_add(ck, "changed", _multi_change, grid);
464 elm_box_pack_end(hbx_2, ck);
465 evas_object_show(ck);
466
467 ck = elm_check_add(win);
468 elm_object_text_set(ck, "No selection");
469 evas_object_smart_callback_add(ck, "changed", _no_sel_change, grid);
470 elm_box_pack_end(hbx_2, ck);
471 evas_object_show(ck);
472
473 hbx_3 = elm_box_add(win);
474 evas_object_size_hint_weight_set(hbx_3, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
475 elm_box_horizontal_set(hbx_3, EINA_TRUE);
476 elm_box_pack_end(bx, hbx_3);
477 evas_object_show(hbx_3);
478
479 elm_gengrid_align_get(grid, &h, &v);
480
481 sl = elm_slider_add(win);
482 elm_object_text_set(sl, "Horiz. alignment");
483 elm_slider_span_size_set(sl, 100);
484 evas_object_size_hint_align_set(sl, 0.5, EVAS_HINT_FILL);
485 evas_object_size_hint_weight_set(sl, 0.0, EVAS_HINT_EXPAND);
486 elm_slider_indicator_format_set(sl, "%1.1f");
487 elm_slider_value_set(sl, h);
488 elm_box_pack_end(hbx_3, sl);
489 evas_object_show(sl);
490
491 evas_object_smart_callback_add(sl, "changed", _h_align_change_cb, grid);
492
493 sl = elm_slider_add(win);
494 elm_object_text_set(sl, "Vert. alignment");
495 elm_slider_span_size_set(sl, 100);
496 evas_object_size_hint_align_set(sl, 0.5, EVAS_HINT_FILL);
497 evas_object_size_hint_weight_set(sl, 0.0, EVAS_HINT_EXPAND);
498 elm_slider_indicator_format_set(sl, "%1.1f");
499 elm_slider_value_set(sl, v);
500 elm_box_pack_end(hbx_3, sl);
501 evas_object_show(sl);
502
503 evas_object_smart_callback_add(sl, "changed", _v_align_change_cb, grid);
504
505 elm_gengrid_align_get(grid, &h, &v);
506
507 sl = elm_slider_add(win);
508 elm_object_text_set(sl, "Page rel. size");
509 elm_slider_span_size_set(sl, 100);
510 evas_object_size_hint_align_set(sl, 0.5, EVAS_HINT_FILL);
511 evas_object_size_hint_weight_set(sl, 0.0, EVAS_HINT_EXPAND);
512 elm_slider_indicator_format_set(sl, "%1.1f");
513 elm_slider_value_set(sl, h);
514 elm_box_pack_end(hbx_3, sl);
515 evas_object_show(sl);
516
517 evas_object_smart_callback_add(sl, "changed", _page_change_cb, grid);
518
519 gic.item_style = "default";
520 gic.func.label_get = _grid_label_get;
521 gic.func.icon_get = _grid_icon_get;
522 gic.func.state_get = _grid_state_get;
523 gic.func.del = _grid_del;
524
525 _append_bt_clicked(grid, NULL, NULL);
526 _append_bt_clicked(grid, NULL, NULL);
527 _append_bt_clicked(grid, NULL, NULL);
528
529 evas_object_resize(win, 600, 600);
530 evas_object_show(win);
531
532 elm_run();
533 return 0;
534}
535
536ELM_MAIN()