Index widget example 2

This code places an Elementary index widget on a window, indexing grid items.

The items are placed so that their labels don't follow any order, but the index itself is ordered (through elm_index_item_sorted_insert()). This is a complement to to the first example on indexes.

Here's the list of item labels to be used on the grid (in that order):

static const char *items[] =
{
"Judith",
"Paulina",
"Cathy",
"Vendella",
"Naomi",
"Ashley",
"Stacey",
"Gail"
};

In the interesting part of the code, here, we first instantiate the grid (more on grids on their examples) and, after creating our index, for each grid item we also create an index one to reference it:

grid = elm_gengrid_add(win);
elm_gengrid_item_size_set(grid, 150, 150);
gic.item_style = "default";
gic.func.text_get = _grid_label_get;
gic.func.content_get = _grid_content_get;
gic.func.state_get = NULL;
gic.func.del = NULL;
evas_object_size_hint_weight_set(grid, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
elm_win_resize_object_add(win, grid);
evas_object_show(grid);
idx = elm_index_add(win);
evas_object_size_hint_weight_set(idx, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
elm_win_resize_object_add(win, idx);
evas_object_show(idx);
for (i = 0; i < (sizeof(items) / sizeof(items[0])); i++)
{
char buf[32];
gg_it = elm_gengrid_item_append(grid, &gic, (void *)(uintptr_t)i, NULL, NULL);
/* indexing by first letters */
snprintf(buf, sizeof(buf), "%c", items[i][0]);
elm_index_item_sorted_insert(idx, buf, NULL, gg_it, _index_icmp, NULL);
}
EVAS_HINT_EXPAND
#define EVAS_HINT_EXPAND
Use with evas_object_size_hint_weight_set(), evas_object_size_hint_weight_get(), evas_object_size_hin...
Definition: Evas_Common.h:297
elm_gengrid_add
Evas_Object * elm_gengrid_add(Evas_Object *parent)
Add a new gengrid widget to the given parent Elementary (container) object.
Definition: elm_gengrid.c:4254
elm_gengrid_item_append
Elm_Widget_Item * elm_gengrid_item_append(Elm_Gengrid *obj, const Elm_Gengrid_Item_Class *itc, const void *data, Evas_Smart_Cb func, const void *func_data)
Append a new item in a given gengrid widget.
Definition: elm_gengrid_eo.legacy.c:189
elm_gengrid_item_size_set
void elm_gengrid_item_size_set(Elm_Gengrid *obj, int w, int h)
Set the size for the items of a given gengrid widget.
Definition: elm_gengrid_eo.legacy.c:93
elm_index_item_sorted_insert
Elm_Widget_Item * elm_index_item_sorted_insert(Elm_Index *obj, const char *letter, Evas_Smart_Cb func, const void *data, Eina_Compare_Cb cmp_func, Eina_Compare_Cb cmp_data_func)
Insert a new item into the given index widget, using cmp_func function to sort items (by item handles...
Definition: elm_index_eo.legacy.c:123
elm_index_add
Evas_Object * elm_index_add(Evas_Object *parent)
Add a new index widget to the given parent Elementary (container) object.
Definition: elm_index.c:1186
elm_win_resize_object_add
void elm_win_resize_object_add(Eo *obj, Evas_Object *subobj)
Add subobj as a resize object of window obj.
Definition: efl_ui_win.c:8992
evas_object_show
EVAS_API void evas_object_show(Evas_Object *eo_obj)
Makes the given Evas object visible.
Definition: evas_object_main.c:1814
evas_object_size_hint_weight_set
EVAS_API void evas_object_size_hint_weight_set(Evas_Object *obj, double x, double y)
Sets the hints for an object's weight.
Definition: evas_object_main.c:2638
evas_object_smart_callback_add(idx, "delay,changed", _index_changed, NULL);
evas_object_smart_callback_add
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

The order in which they'll appear in the index, though, is alphabetical, becase of elm_index_item_sorted_insert() usage together with the comparing function, where we take the letters of each index item to base our ordering on. The parameters on `_index_cmp` have to be declared as void pointers because of the `Eina_Compare_Cb` prototype requisition, but in this case we know they'll be index item(Elm_Object_Item)'s:

/* ordering alphabetically */
static int
_index_icmp(const void *data1,
const void *data2)
{
const char *label1, *label2;
const Elm_Object_Item *index_it1 = data1;
const Elm_Object_Item *index_it2 = data2;
label1 = elm_index_item_letter_get(index_it1);
label2 = elm_index_item_letter_get(index_it2);
return strcasecmp(label1, label2);
}
Elm_Object_Item
Eo Elm_Object_Item
An Elementary Object item handle.
Definition: elm_object_item.h:6
elm_index_item_letter_get
const char * elm_index_item_letter_get(const Elm_Index_Item *obj)
Get the letter (string) set on a given index widget item.
Definition: elm_index_item_eo.legacy.c:15

The last interesting bit is the callback in the `"delay,changed"` smart event, which will bring the given grid item to the grid's visible area:

static void
_index_changed(void *data EINA_UNUSED,
Evas_Object *obj EINA_UNUSED,
void *event_info)
{
Elm_Object_Item *item = elm_object_item_data_get(event_info);
elm_gengrid_item_bring_in(item, ELM_GENGRID_ITEM_SCROLLTO_IN);
}
EINA_UNUSED
#define EINA_UNUSED
Used to indicate that a function parameter is purposely unused.
Definition: eina_types.h:339
elm_object_item_data_get
void * elm_object_item_data_get(const Elm_Object_Item *it)
Get the data associated with an object item.
Definition: efl_ui_widget.c:3796
elm_gengrid_item_bring_in
void elm_gengrid_item_bring_in(Elm_Gengrid_Item *obj, Elm_Gengrid_Item_Scrollto_Type type)
Animatedly bring in, to the visible area of a gengrid, a given item on it.
Definition: elm_gengrid_item_eo.legacy.c:75
ELM_GENGRID_ITEM_SCROLLTO_IN
@ ELM_GENGRID_ITEM_SCROLLTO_IN
To the nearest viewport.
Definition: elm_general.h:397
Evas_Object
Efl_Canvas_Object Evas_Object
An Evas Object handle.
Definition: Evas_Common.h:185

Note how the grid will move kind of randomly while you move your mouse pointer held over the index from top to bottom – that's because of the the random order the items have in the grid itself.

This is how the example program's window looks like:

See the full source code for this example.