2 //gcc -g `pkg-config --cflags --libs elementary` genlist_example_04.c -o genlist_example_04
4 #include <Elementary.h>
6 # include "elementary_config.h"
13 typedef struct _Node_Data {
20 static Elm_Genlist_Item_Class _itc;
21 static Elm_Genlist_Item_Class _itp;
22 static Elm_Genlist_Item_Class _itfav;
23 static int nitems = 0;
26 _item_text_get(void *data, Evas_Object *obj __UNUSED__, const char *part)
31 if (!strcmp(part, "elm.text"))
32 snprintf(buf, sizeof(buf), "Item # %i (level %i)", d->value, d->level);
38 _item_content_get(void *data __UNUSED__, Evas_Object *obj, const char *part)
40 Evas_Object *ic = elm_icon_add(obj);
42 if (!strcmp(part, "elm.swallow.icon"))
43 elm_icon_standard_set(ic, "file");
45 evas_object_size_hint_aspect_set(ic, EVAS_ASPECT_CONTROL_VERTICAL, 1, 1);
50 _item_sel_cb(void *data, Evas_Object *obj, void *event_info)
52 printf("sel item data [%p] on genlist obj [%p], item pointer [%p]\n",
53 data, obj, event_info);
57 _parent_text_get(void *data, Evas_Object *obj __UNUSED__, const char *part __UNUSED__)
62 snprintf(buf, sizeof(buf), "Group %d (%d items)", d->value / 7,
63 eina_list_count(d->children));
69 _parent_content_get(void *data __UNUSED__, Evas_Object *obj, const char *part __UNUSED__)
71 Evas_Object *ic = elm_icon_add(obj);
73 if (!strcmp(part, "elm.swallow.icon"))
74 elm_icon_standard_set(ic, "folder");
76 evas_object_size_hint_aspect_set(ic, EVAS_ASPECT_CONTROL_VERTICAL, 1, 1);
81 _favorite_text_get(void *data, Evas_Object *obj __UNUSED__, const char *part)
86 if (!strcmp(part, "elm.text"))
87 snprintf(buf, sizeof(buf), "Favorite # %i", d->value);
93 _favorite_content_get(void *data __UNUSED__, Evas_Object *obj, const char *part)
95 Evas_Object *ic = elm_icon_add(obj);
97 if (!strcmp(part, "elm.swallow.icon"))
98 elm_icon_standard_set(ic, "apps");
100 evas_object_size_hint_aspect_set(ic, EVAS_ASPECT_CONTROL_VERTICAL, 1, 1);
105 _append_cb(void *data, Evas_Object *o __UNUSED__, void *event_info __UNUSED__)
107 Evas_Object *list = data;
108 Elm_Object_Item *it, *parent = NULL;
109 Node_Data *pdata, *d = malloc(sizeof(*d));
113 d->favorite = EINA_FALSE;
115 it = elm_genlist_selected_item_get(list);
117 parent = elm_genlist_item_parent_get(it);
121 d->level = elm_genlist_item_expanded_depth_get(parent) + 1;
122 pdata = elm_genlist_item_data_get(parent);
123 pdata->children = eina_list_append(pdata->children, d);
128 elm_genlist_item_append(list, &_itc,
130 ELM_GENLIST_ITEM_NONE,
135 _favorite_cb(void *data, Evas_Object *o __UNUSED__, void *event_info __UNUSED__)
137 Evas_Object *list = data;
138 Elm_Object_Item *it = elm_genlist_selected_item_get(list);
143 Node_Data *d = elm_genlist_item_data_get(it);
144 d->favorite = !d->favorite;
146 elm_genlist_item_item_class_update(it, &_itfav);
150 elm_genlist_item_item_class_update(it, &_itp);
152 elm_genlist_item_item_class_update(it, &_itc);
155 elm_genlist_item_update(it);
159 _add_child_cb(void *data, Evas_Object *o __UNUSED__, void *event_info __UNUSED__)
161 Evas_Object *list = data;
162 Elm_Object_Item *it = elm_genlist_selected_item_get(list);
163 Elm_Object_Item *prev, *parent;
168 Node_Data *d = elm_genlist_item_data_get(it);
169 prev = elm_genlist_item_prev_get(it);
170 parent = elm_genlist_item_parent_get(it);
172 Eina_Bool change_item = !d->children;
174 // creating new item data
175 Node_Data *ndata = malloc(sizeof(*ndata));
176 ndata->value = nitems++;
177 ndata->children = NULL;
178 ndata->favorite = EINA_FALSE;
179 ndata->level = elm_genlist_item_expanded_depth_get(it) + 1;
180 d->children = eina_list_append(d->children, ndata);
182 // Changing leaf item to parent item
185 elm_genlist_item_del(it);
188 it = elm_genlist_item_insert_after(list, &_itp, d, parent, prev,
189 ELM_GENLIST_ITEM_SUBITEMS,
192 it = elm_genlist_item_prepend(list, &_itp, d, parent,
193 ELM_GENLIST_ITEM_SUBITEMS,
195 elm_genlist_item_expanded_set(it, EINA_FALSE);
196 elm_genlist_item_selected_set(it, EINA_TRUE);
198 else if (elm_genlist_item_expanded_get(it))
200 elm_genlist_item_append(list, &_itc, ndata, it,
201 ELM_GENLIST_ITEM_NONE, _item_sel_cb, NULL);
204 elm_genlist_item_update(it);
208 _clear_list(Node_Data *d)
212 EINA_LIST_FREE(d->children, tmp)
218 _del_item_cb(void *data, Evas_Object *o __UNUSED__, void *event_info __UNUSED__)
220 Evas_Object *list = data;
221 Elm_Object_Item *it = elm_genlist_selected_item_get(list);
222 Elm_Object_Item *parent = NULL;
227 Node_Data *pdata, *d = elm_genlist_item_data_get(it);
228 parent = elm_genlist_item_parent_get(it);
229 elm_genlist_item_subitems_clear(it);
230 elm_genlist_item_del(it);
237 pdata = elm_genlist_item_data_get(parent);
238 pdata->children = eina_list_remove(pdata->children, d);
239 elm_genlist_item_update(parent);
243 _expand_request_cb(void *data __UNUSED__, Evas_Object *o __UNUSED__, void *event_info)
245 Elm_Object_Item *it = event_info;
246 printf("expand request on item: %p\n", event_info);
247 elm_genlist_item_expanded_set(it, EINA_TRUE);
251 _contract_request_cb(void *data __UNUSED__, Evas_Object *o __UNUSED__, void *event_info)
253 Elm_Object_Item *it = event_info;
254 printf("contract request on item: %p\n", event_info);
255 elm_genlist_item_expanded_set(it, EINA_FALSE);
259 _expanded_cb(void *data __UNUSED__, Evas_Object *o __UNUSED__, void *event_info)
262 Elm_Object_Item *it = event_info;
263 Node_Data *it_data, *d = elm_genlist_item_data_get(it);
264 Evas_Object *list = elm_genlist_item_genlist_get(it);
266 Elm_Genlist_Item_Class *ic;
268 EINA_LIST_FOREACH(d->children, l, it_data)
270 Elm_Object_Item *nitem;
271 Elm_Genlist_Item_Flags flags = ELM_GENLIST_ITEM_NONE;
272 printf("expanding item: #%d from parent #%d\n", it_data->value, d->value);
273 if (it_data->favorite)
275 else if (it_data->children)
278 flags = ELM_GENLIST_ITEM_SUBITEMS;
283 nitem = elm_genlist_item_append(list, ic, it_data, it,
284 flags, _item_sel_cb, NULL);
285 elm_genlist_item_expanded_set(nitem, EINA_FALSE);
290 _contracted_cb(void *data __UNUSED__, Evas_Object *o __UNUSED__, void *event_info)
292 Elm_Object_Item *it = event_info;
293 elm_genlist_item_subitems_clear(it);
297 _button_add(Evas_Object *list, Evas_Object *box, const char *label, Evas_Smart_Cb cb)
301 bt = elm_button_add(elm_object_parent_widget_get(list));
302 elm_object_text_set(bt, label);
303 elm_box_pack_end(box, bt);
304 evas_object_show(bt);
307 evas_object_smart_callback_add(bt, "clicked", cb, list);
313 elm_main(int argc __UNUSED__, char **argv __UNUSED__)
315 Evas_Object *win, *bg, *box, *fbox;
319 win = elm_win_add(NULL, "icon", ELM_WIN_BASIC);
320 elm_win_title_set(win, "Icon");
321 elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_LAST_WINDOW_CLOSED);
322 elm_win_autodel_set(win, EINA_TRUE);
324 bg = elm_bg_add(win);
325 elm_bg_color_set(bg, 255,255 ,255);
326 evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
327 elm_win_resize_object_add(win, bg);
328 evas_object_show(bg);
330 box = elm_box_add(win);
331 evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
332 evas_object_size_hint_align_set(box, EVAS_HINT_FILL, EVAS_HINT_FILL);
333 elm_win_resize_object_add(win, box);
334 evas_object_show(box);
336 _itc.item_style = "default";
337 _itc.func.text_get = _item_text_get;
338 _itc.func.content_get = _item_content_get;
339 _itc.func.state_get = NULL;
340 _itc.func.del = NULL;
342 _itp.item_style = "default";
343 _itp.func.text_get = _parent_text_get;
344 _itp.func.content_get = _parent_content_get;
345 _itp.func.state_get = NULL;
346 _itp.func.del = NULL;
348 _itfav.item_style = "default";
349 _itfav.func.text_get = _favorite_text_get;
350 _itfav.func.content_get = _favorite_content_get;
351 _itfav.func.state_get = NULL;
352 _itfav.func.del = NULL;
354 list = elm_genlist_add(win);
356 evas_object_size_hint_weight_set(list, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
357 evas_object_size_hint_align_set(list, EVAS_HINT_FILL, EVAS_HINT_FILL);
358 elm_box_pack_end(box, list);
359 evas_object_show(list);
361 fbox = elm_box_add(win);
362 elm_box_layout_set(fbox, evas_object_box_layout_flow_horizontal,
364 evas_object_size_hint_weight_set(fbox, EVAS_HINT_EXPAND, 0);
365 evas_object_size_hint_align_set(fbox, EVAS_HINT_FILL, EVAS_HINT_FILL);
366 elm_box_pack_end(box, fbox);
367 evas_object_show(fbox);
369 _button_add(list, fbox, "append item", _append_cb);
370 _button_add(list, fbox, "favorite", _favorite_cb);
371 _button_add(list, fbox, "add child", _add_child_cb);
372 _button_add(list, fbox, "del item", _del_item_cb);
374 for (i = 0; i < N_ITEMS; i++)
376 Elm_Object_Item *gli, *glg;
377 Node_Data *data = malloc(sizeof(*data)); // data for this item
378 data->children = NULL;
380 data->favorite = EINA_FALSE;
383 Node_Data *pdata; // data for the parent of the group
385 printf("creating item: #%d\n", data->value);
388 glg = gli = elm_genlist_item_append(list, &_itp, data, NULL,
389 ELM_GENLIST_ITEM_SUBITEMS,
391 elm_genlist_item_expanded_set(glg, EINA_TRUE);
397 gli = elm_genlist_item_append(list, &_itc, data, glg,
398 ELM_GENLIST_ITEM_NONE,
400 pdata->children = eina_list_append(pdata->children, data);
405 evas_object_smart_callback_add(list, "expand,request", _expand_request_cb, list);
406 evas_object_smart_callback_add(list, "contract,request", _contract_request_cb, list);
407 evas_object_smart_callback_add(list, "expanded", _expanded_cb, list);
408 evas_object_smart_callback_add(list, "contracted", _contracted_cb, list);
410 evas_object_size_hint_min_set(bg, 160, 160);
411 evas_object_size_hint_max_set(bg, 640, 800);
412 evas_object_resize(win, 420, 600);
413 evas_object_show(win);