Genlist - items manipulation

This example is also similar ot the Genlist - basic usage, but it demonstrates most of the item manipulation functions.

See the full source code at genlist_example_04.c.

In this example, we also will use the concept of creating groups of items in the genlist. Each group of items is composed by a parent item (which will be the index of the group) and several children of this item. Thus, for the children, we declare a normal item class. But we also are going to declare a different item class for the group index (which in practice is another type of item in the genlist):

static char *
_group_label_get(void *data, Evas_Object *obj EINA_UNUSED, const char *part EINA_UNUSED)
{
char buf[256];
int i = (int)(uintptr_t)data;
snprintf(buf, sizeof(buf), "Group %d (item #%d)", i / 7, i);
return strdup(buf);
}
#define EINA_UNUSED
Used to indicate that a function parameter is purposely unused.
Definition: eina_types.h:339
Efl_Canvas_Object Evas_Object
An Evas Object handle.
Definition: Evas_Common.h:185
static Evas_Object *
_group_content_get(void *data EINA_UNUSED, Evas_Object *obj, const char *part)
{
if (!strcmp(part, "elm.swallow.icon"))
elm_icon_standard_set(ic, "home");
return ic;
}
@ EVAS_ASPECT_CONTROL_VERTICAL
Use all vertical container space to place an object, using the given aspect.
Definition: Evas_Common.h:377
Eina_Bool elm_icon_standard_set(Evas_Object *obj, const char *name)
Set the icon by icon standards names.
Definition: elm_icon.c:885
Evas_Object * elm_icon_add(Evas_Object *parent)
Add a new icon object to the parent.
Definition: elm_icon.c:613
EVAS_API void evas_object_size_hint_aspect_set(Evas_Object *obj, Evas_Aspect_Control aspect, Evas_Coord w, Evas_Coord h)
Sets the hints for an object's aspect ratio.
Definition: evas_object_main.c:2581

We will add buttons to the window, where each button provides one functionality of the genlist item API. Each button will have a callback attached, that will really execute this functionality. An example of these callbacks is the next one, for the elm_genlist_item_insert_after() function:

static void
_insert_after_cb(void *data, Evas_Object *o EINA_UNUSED, void *event_info EINA_UNUSED)
{
Evas_Object *list = data;
if (!glit) return;
(void *)(uintptr_t)nitems++, NULL,
_item_sel_cb, NULL);
}
Eo Elm_Object_Item
An Elementary Object item handle.
Definition: elm_object_item.h:6
Elm_Widget_Item * elm_genlist_selected_item_get(const Elm_Genlist *obj)
Get the selected item in the genlist.
Definition: elm_genlist_eo.legacy.c:153
Elm_Widget_Item * elm_genlist_item_insert_after(Elm_Genlist *obj, const Elm_Genlist_Item_Class *itc, const void *data, Elm_Widget_Item *parent, Elm_Widget_Item *after_it, Elm_Genlist_Item_Type type, Evas_Smart_Cb func, const void *func_data)
Insert an item after another in a genlist widget.
Definition: elm_genlist_eo.legacy.c:195
@ ELM_GENLIST_ITEM_NONE
Simple item.
Definition: elm_general.h:349

If you want ot see the other button functions, look at the full source code link above.

Each button will be created with a function that already creates the button, add it to an elementary box, and attach the specified callback. This is the function that does it:

static Evas_Object *
_button_add(Evas_Object *list, Evas_Object *box, const char *label, Evas_Smart_Cb cb)
{
elm_object_text_set(bt, label);
elm_box_pack_end(box, bt);
if (cb)
evas_object_smart_callback_add(bt, "clicked", cb, list);
return bt;
}
void(* Evas_Smart_Cb)(void *data, Evas_Object *obj, void *event_info)
Evas smart objects' "smart callback" function signature.
Definition: Evas_Common.h:463
void elm_box_pack_end(Elm_Box *obj, Efl_Canvas_Object *subobj)
Add an object at the end of the pack list.
Definition: elm_box_eo.legacy.c:57
Evas_Object * elm_button_add(Evas_Object *parent)
Add a new button to the parent's canvas.
Definition: efl_ui_button.c:459
Evas_Object * elm_object_parent_widget_get(const Evas_Object *obj)
Get the first parent of the given object that is an Elementary widget.
Definition: elm_main.c:1833
EVAS_API void evas_object_show(Evas_Object *eo_obj)
Makes the given Evas object visible.
Definition: evas_object_main.c:1814
EVAS_API void evas_object_smart_callback_add(Evas_Object *eo_obj, const char *event, Evas_Smart_Cb func, const void *data)
Add (register) a callback function to the smart event specified by event on the smart object obj.
Definition: evas_object_smart.c:1040

In our elm_main function, besides the code for setting up the window, box and background, we also initialize our two item classes:

This example uses a different style for the items, the double_label, which provides a text field for the item text, and another text field for a subtext.

For the group index we use the group_index style, which provides a different appearance, helping to identify the end of a group and beginning of another one.

Now, after the code for creating the list, setting up the box and other stuff, let's add the buttons with their respective callbacks:

The main code for adding items to the list is a bit more complex than the one from the previous examples. We check if each item is multiple of 7, and if so, they are group indexes (thus each group has 6 elements by default, in this example):

Then we also check for specific items, and add callbacks to them on the respective buttons, so we can show, bring in, etc.:

Once you understand the code from the Genlist - basic usage, it should be easy to understand this one too. Look at the full code, and also try to play a bit with the buttons, adding items, bringing them to the viewport, and so.

The example will look like this when running: