List select operations

This code snippet shows working with selections in elementary list widget: select an item, get single selected item, iterate through a list of all of the selected items.
const char * labels[] = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };

static void button_delete_cb(void *data, Evas_Object *obj, void *event_info) {
	const Evas_Object *list = (Evas_Object *) data;

	//get list of selected items
	const Eina_List *item = elm_list_selected_items_get(list);

	//remove them
	while (item != NULL) {
		Eina_List *next = item->next;
		Elm_Object_Item *data = item->data;
		elm_object_item_del(data);
		item = next;
	}
}

static void button_add_cb(void *data, Evas_Object *obj, void *event_info) {
	Evas_Object *list = (Evas_Object *) data;

	//get selected item (if multiple items are selected then this function returns the first one)
	Elm_Object_Item *item = elm_list_selected_item_get(list);

	elm_list_item_insert_after(list, item, "new item", NULL, NULL, NULL, NULL);
}

static void list_test(Evas_Object *parent) {
	Evas_Object *box = elm_box_add(parent);
	evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
	elm_win_resize_object_add(parent, box);
	evas_object_show(box);

	//create the list object
	Evas_Object *list = elm_list_add(parent);
	evas_object_size_hint_weight_set(list, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
	evas_object_size_hint_align_set(list, EVAS_HINT_FILL, EVAS_HINT_FILL);

	int labelsCnt = sizeof(labels) / sizeof(labels[0]);
	for (int i = 0; i < labelsCnt; i++)
		elm_list_item_append(list, labels[i], NULL, NULL, NULL, NULL);

	//enable selecting multiple items
	elm_list_multi_select_set(list, EINA_TRUE);

	//select the first item
	Elm_Object_Item *first_item = elm_list_first_item_get(list);
	elm_list_item_selected_set(first_item, EINA_TRUE);

	elm_box_pack_end(box, list);
	evas_object_show(list);
	elm_list_go(list);

	Evas_Object *button_delete = elm_button_add(parent);
	elm_object_text_set(button_delete, "Delete selected items");
	evas_object_smart_callback_add(button_delete, "clicked", button_delete_cb, list);
	elm_box_pack_end(box, button_delete);
	evas_object_show(button_delete);

	Evas_Object *button_add = elm_button_add(parent);
	elm_object_text_set(button_add, "Insert after selected item");
	evas_object_smart_callback_add(button_add, "clicked", button_add_cb, list);
	elm_box_pack_end(box, button_add);
	evas_object_show(button_add);
}

Responses

0 Replies