Genlist - list setup functions

- Main Page
- Related Pages
- Modules
- Data Structures
- Files
- Examples

Genlist - list setup functions

This example is very similar to the Genlist - basic usage, but it fetch most of the properties of the genlist and displays them on startup (thus getting the default value for them) and then set them to some other values, to show how to use that API.

The full source code is at genlist_example_02.c.

Considering that the base code for instantiating a genlist was already described in the previous example, we are going to focus on the new code.

Just a small difference for the `_item_label_get` function, we are going to store the time that this function was called. This is the "realized" time, the time when the visual representation of this item was created. This is the code for the `label_get` function:

static char *
_item_label_get(void *data, Evas_Object *obj EINA_UNUSED, const char *part EINA_UNUSED)
{
time_t t = (time_t)ecore_time_unix_get();
char buf[256];
int i = (int)(uintptr_t)data;
if (i % 2)
{
int n;
snprintf(buf, sizeof(buf), "Very Long Item # %i - realized at %s", i, ctime(&t));
n = strlen(buf);
buf[n - 1] = '\0';
}
else
snprintf(buf, sizeof(buf), "short item # %i", i);
return strdup(buf);
ecore_time_unix_get
double ecore_time_unix_get(void)
Retrieves the current UNIX time as a floating point value in seconds.
Definition: ecore_time.c:61
EINA_UNUSED
#define EINA_UNUSED
Used to indicate that a function parameter is purposely unused.
Definition: eina_types.h:339
Evas_Object
Efl_Canvas_Object Evas_Object
An Evas Object handle.
Definition: Evas_Common.h:185

Now let's go to the list creation and setup. First, just after creating the list, we get most of the default properties from it, and print them on the console:

list = elm_genlist_add(win);
if (!_itc)
{
_itc = elm_genlist_item_class_new();
_itc->item_style = "default";
_itc->func.text_get = _item_label_get;
_itc->func.content_get = _item_content_get;
_itc->func.state_get = NULL;
_itc->func.del = NULL;
}
Eina_Bool hbounce, vbounce, always, no_sel;
Elm_Scroller_Policy hp, vp;
Elm_Object_Select_Mode sel_mode;
always = no_sel = EINA_FALSE;
sel_mode = elm_genlist_select_mode_get(list);
if (sel_mode == ELM_OBJECT_SELECT_MODE_ALWAYS)
always = EINA_TRUE;
else if (sel_mode == ELM_OBJECT_SELECT_MODE_NONE)
no_sel = EINA_TRUE;
printf("default values:\n");
printf("always select: %d\n", always);
elm_scroller_bounce_get(list, &hbounce, &vbounce);
printf("bounce - horizontal: %d, vertical: %d\n", hbounce, vbounce);
printf("homogeneous: %d\n", elm_genlist_homogeneous_get(list));
printf("horizontal mode: %d\n", elm_genlist_mode_get(list));
printf("longpress timeout: %0.3f\n",
elm_genlist_longpress_timeout_get(list));
printf("multi selection: %d\n", elm_genlist_multi_select_get(list));
printf("no selection mode: %d\n", no_sel);
elm_scroller_policy_get(list, &hp, &vp);
printf("scroller policy - horizontal: %d, vertical: %d\n", hp, vp);
printf("block count: %d\n", elm_genlist_block_count_get(list));
printf("\n");
EINA_TRUE
#define EINA_TRUE
boolean value TRUE (numerical value 1)
Definition: eina_types.h:539
EINA_FALSE
#define EINA_FALSE
boolean value FALSE (numerical value 0)
Definition: eina_types.h:533
Eina_Bool
unsigned char Eina_Bool
Type to mimic a boolean.
Definition: eina_types.h:527
Elm_Object_Select_Mode
Elm_Object_Select_Mode
Possible values for the selection policy of some widgets.
Definition: elm_general.h:33
ELM_OBJECT_SELECT_MODE_NONE
@ ELM_OBJECT_SELECT_MODE_NONE
no select mode.
Definition: elm_general.h:43
ELM_OBJECT_SELECT_MODE_ALWAYS
@ ELM_OBJECT_SELECT_MODE_ALWAYS
always select mode.
Definition: elm_general.h:39
elm_genlist_select_mode_get
Elm_Object_Select_Mode elm_genlist_select_mode_get(const Elm_Genlist *obj)
Get the genlist select mode.
Definition: elm_genlist_eo.legacy.c:21
elm_genlist_add
Evas_Object * elm_genlist_add(Evas_Object *parent)
Add a new genlist widget to the given parent Elementary (container) object.
Definition: elm_genlist.c:5998
elm_genlist_block_count_get
int elm_genlist_block_count_get(const Elm_Genlist *obj)
Get the maximum number of items within an item block.
Definition: elm_genlist_eo.legacy.c:105
elm_genlist_homogeneous_get
Eina_Bool elm_genlist_homogeneous_get(const Elm_Genlist *obj)
Get whether the homogeneous mode is enabled.
Definition: elm_genlist_eo.legacy.c:9
elm_genlist_multi_select_get
Eina_Bool elm_genlist_multi_select_get(const Elm_Genlist *obj)
Get if multi-selection in genlist is enabled or disabled.
Definition: elm_genlist_eo.legacy.c:57
elm_genlist_mode_get
Elm_List_Mode elm_genlist_mode_get(const Elm_Genlist *obj)
Get the horizontal stretching mode.
Definition: elm_genlist_eo.legacy.c:141
elm_genlist_longpress_timeout_get
double elm_genlist_longpress_timeout_get(const Elm_Genlist *obj)
Get the timeout in seconds for the longpress event.
Definition: elm_genlist_eo.legacy.c:45
elm_genlist_item_class_new
Elm_Genlist_Item_Class * elm_genlist_item_class_new(void)
Create a new genlist item class in a given genlist widget.
Definition: elm_genlist.c:8392
Elm_Scroller_Policy
Elm_Scroller_Policy
Type that controls when scrollbars should appear.
Definition: elm_scroller_legacy.h:14
elm_scroller_policy_get
void elm_scroller_policy_get(const Evas_Object *obj, Elm_Scroller_Policy *policy_h, Elm_Scroller_Policy *policy_v)
Get scrollbar visibility policy.
Definition: elm_scroller.c:964
elm_scroller_bounce_get
void elm_scroller_bounce_get(const Evas_Object *obj, Eina_Bool *h_bounce, Eina_Bool *v_bounce)
Get the bounce behaviour.
Definition: elm_scroller.c:1060

We are going to change some of the properties of our list.

There's no need to call the selected callback at every click, just when the selected item changes, thus we call elm_genlist_select_mode_set() with ELM_OBJECT_SELECT_MODE_ALWAYS.

For this list we don't want bounce animations at all, so we set both the horizontal bounce and the vertical bounce to false with elm_genlist_bounce_set().

We also want our list to compress items if they are wider than the list width (thus we call elm_genlist_mode_set(obj, ELM_LIST_COMPRESS).

The items have different width, so they are not homogeneous: elm_genlist_homogeneous_set() is set to false.

Since the compress mode is active, the call to elm_genlist_mode_set() doesn't make difference, but the current option would make the list to have at least the width of the largest item.

This list will support multiple selection, so we call elm_genlist_multi_select_set() on it.

The option elm_genlist_mode_set() would allow text block to wrap lines if the Edje part is configured with "text.min: 0 1", for example. But since we are compressing the elements to the width of the list, this option wouldn't take any effect.

We want the vertical scrollbar to be always displayed, and the orizontal one to never be displayed, and set this with elm_genlist_scroller_policy_set().

The timeout to consider a longpress is set to half of a second with elm_genlist_longpress_timeout_set().

We also change the block count to a smaller value, but that should have not impact on performance since the number of visible items is too small. We just increase the granularity of the block count (setting it to have at most 4 items).

elm_scroller_bounce_set(list, EINA_FALSE, EINA_FALSE);
elm_genlist_homogeneous_set(list, EINA_FALSE);
elm_genlist_mode_set(list, ELM_LIST_LIMIT);
elm_genlist_multi_select_set(list, EINA_TRUE);
elm_genlist_select_mode_set(list, ELM_OBJECT_SELECT_MODE_DEFAULT);
elm_scroller_policy_set(list, ELM_SCROLLER_POLICY_OFF, ELM_SCROLLER_POLICY_ON);
elm_genlist_longpress_timeout_set(list, 0.5);
elm_genlist_block_count_set(list, 16);
ELM_OBJECT_SELECT_MODE_DEFAULT
@ ELM_OBJECT_SELECT_MODE_DEFAULT
default select mode.
Definition: elm_general.h:34
ELM_LIST_LIMIT
@ ELM_LIST_LIMIT
Sets a minimum size hint on the list object, so that containers may respect it (and resize itself to ...
Definition: elm_general.h:443
elm_genlist_mode_set
void elm_genlist_mode_set(Elm_Genlist *obj, Elm_List_Mode mode)
This sets the horizontal stretching mode.
Definition: elm_genlist_eo.legacy.c:135
elm_genlist_longpress_timeout_set
void elm_genlist_longpress_timeout_set(Elm_Genlist *obj, double timeout)
Set the timeout in seconds for the longpress event.
Definition: elm_genlist_eo.legacy.c:39
elm_genlist_multi_select_set
void elm_genlist_multi_select_set(Elm_Genlist *obj, Eina_Bool multi)
Enable or disable multi-selection in the genlist.
Definition: elm_genlist_eo.legacy.c:51
elm_genlist_block_count_set
void elm_genlist_block_count_set(Elm_Genlist *obj, int count)
Set the maximum number of items within an item block.
Definition: elm_genlist_eo.legacy.c:99
elm_genlist_homogeneous_set
void elm_genlist_homogeneous_set(Elm_Genlist *obj, Eina_Bool homogeneous)
Enable/disable homogeneous mode.
Definition: elm_genlist_eo.legacy.c:3
elm_genlist_select_mode_set
void elm_genlist_select_mode_set(Elm_Genlist *obj, Elm_Object_Select_Mode mode)
Set the genlist select mode.
Definition: elm_genlist_eo.legacy.c:15
elm_scroller_policy_set
void elm_scroller_policy_set(Evas_Object *obj, Elm_Scroller_Policy policy_h, Elm_Scroller_Policy policy_v)
Set the scrollbar visibility policy.
Definition: elm_scroller.c:945
ELM_SCROLLER_POLICY_OFF
@ ELM_SCROLLER_POLICY_OFF
Never show scrollbars.
Definition: elm_scroller_legacy.h:17
ELM_SCROLLER_POLICY_ON
@ ELM_SCROLLER_POLICY_ON
Always show scrollbars.
Definition: elm_scroller_legacy.h:16
elm_scroller_bounce_set
void elm_scroller_bounce_set(Evas_Object *obj, Eina_Bool h_bounce, Eina_Bool v_bounce)
Set bouncing behavior.
Definition: elm_scroller.c:1050

Now let's add elements to the list:

for (i = 0; i < N_ITEMS; i++)
{
elm_genlist_item_append(list, _itc,
elm_genlist_item_append
Elm_Widget_Item * elm_genlist_item_append(Elm_Genlist *obj, const Elm_Genlist_Item_Class *itc, const void *data, Elm_Widget_Item *parent, Elm_Genlist_Item_Type type, Evas_Smart_Cb func, const void *func_data)
Append a new item in a given genlist widget.
Definition: elm_genlist_eo.legacy.c:243
(void *)(uintptr_t)i, NULL,
ELM_GENLIST_ITEM_NONE,
_item_sel_cb, NULL);
}
ELM_GENLIST_ITEM_NONE
@ ELM_GENLIST_ITEM_NONE
Simple item.
Definition: elm_general.h:349

It's exactly the same as the previous example. The difference is on the behavior of the list, if you try to scroll, select items and so.

In this example we also need two buttons. One of them, when clicked, will display several status info about the current selection, the "realized" items, the item in the middle of the screen, and the current mode and active item of that mode for the genlist.

The other button will ask the genlist to "realize" again the items already "realized", so their respective label_get and icon_get functions will be called again.

These are the callbacks for both of these buttons:

static void
_show_status_cb(void *data, Evas_Object *o EINA_UNUSED, void *event_info EINA_UNUSED)
{
Evas_Object *list = data;
Evas_Coord x, y, w, h, mx, my;
Elm_Object_Item *glit = elm_genlist_selected_item_get(list);
const Eina_List *selected, *l;
Eina_List *realized;
printf("\nfirst selected item: %p\n", glit);
selected = elm_genlist_selected_items_get(list);
printf("all selected items (%d): ", eina_list_count(selected));
EINA_LIST_FOREACH(selected, l, glit)
printf("%p ", glit);
printf("\n");
realized = elm_genlist_realized_items_get(list);
printf("realized items (%d): ", eina_list_count(realized));
// The realized items list should be freed by either eina_list_free() or EINA_LIST_FREE when it is no longer needed
EINA_LIST_FREE(realized, glit)
printf("%p ", glit);
printf("\n");
printf("genlist mode: %d\n", elm_genlist_decorate_mode_get(list));
printf("mode item: %p\n", elm_genlist_decorated_item_get(list));
evas_object_geometry_get(list, &x, &y, &w, &h);
mx = w / 2 + x;
my = h / 2 + y;
glit = elm_genlist_at_xy_item_get(list, mx, my, NULL);
printf("item in the middle of the screen: %p\n", glit);
}
Evas_Coord
int Evas_Coord
Type used for coordinates (in pixels, int).
Definition: Evas_Common.h:116
EINA_LIST_FOREACH
#define EINA_LIST_FOREACH(list, l, _data)
Definition for the macro to iterate over a list.
Definition: eina_list.h:1415
EINA_LIST_FREE
#define EINA_LIST_FREE(list, data)
Definition for the macro to remove each list node while having access to each node's data.
Definition: eina_list.h:1629
Elm_Object_Item
Eo Elm_Object_Item
An Elementary Object item handle.
Definition: elm_object_item.h:6
elm_genlist_selected_item_get
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_genlist_decorated_item_get
Elm_Widget_Item * elm_genlist_decorated_item_get(const Elm_Genlist *obj)
Get active genlist mode item.
Definition: elm_genlist_eo.legacy.c:147
elm_genlist_realized_items_get
Eina_List * elm_genlist_realized_items_get(const Elm_Genlist *obj)
Get a list of realized items in genlist.
Definition: elm_genlist_eo.legacy.c:165
elm_genlist_at_xy_item_get
Elm_Widget_Item * elm_genlist_at_xy_item_get(const Elm_Genlist *obj, int x, int y, int *posret)
Get the item that is at the x, y canvas coords.
Definition: elm_genlist_eo.legacy.c:201
elm_genlist_selected_items_get
const Eina_List * elm_genlist_selected_items_get(const Elm_Genlist *obj)
Get a list of selected items in the genlist.
Definition: elm_genlist_eo.legacy.c:171
elm_genlist_decorate_mode_get
Eina_Bool elm_genlist_decorate_mode_get(const Elm_Genlist *obj)
Get Genlist decorate mode.
Definition: elm_genlist_eo.legacy.c:81
evas_object_geometry_get
EVAS_API void evas_object_geometry_get(const Evas_Object *eo_obj, Evas_Coord *x, Evas_Coord *y, Evas_Coord *w, Evas_Coord *h)
Retrieves the position and (rectangular) size of the given Evas object.
Definition: evas_object_main.c:1335
_Eina_List
Type for a generic double linked list.
Definition: eina_list.h:318
static void
_realize_cb(void *data, Evas_Object *o EINA_UNUSED, void *event_info EINA_UNUSED)
{
Evas_Object *list = data;
elm_genlist_realized_items_update(list);
}
elm_genlist_realized_items_update
void elm_genlist_realized_items_update(Elm_Genlist *obj)
Update the contents of all realized items.
Definition: elm_genlist_eo.legacy.c:189

Try to scroll, select some items and click on the "Show status" button. You'll notice that not all items of the list are "realized", thus consuming just a small amount of memory. The selected items are listed in the order that they were selected, and the current selected item printed using elm_genlist_selected_item_get() is the first selected item of the multiple selection.

Now resize the window so that you can see the "realized time" of some items. This is the time of when the label_get function was called. If you click on the "Realize" button, all the already realized items will be rebuilt, so the time will be updated for all of them.

The current example will look like this when running: