summaryrefslogtreecommitdiffstats
path: root/src/eulogium_item_list.c
blob: 2cf62a143282be0a1b25423aec733bb3521ab405 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
 * Eulogium_item_list, inspired elm_list.
 *
 * Copyright (c) 2015 Ultimaker B.V.
 * Author: Olliver Schinagl <o.schinagl@ultimaker.com>
 *
 * SPDX-License-Identifier:	AGPL-3.0+
 */

#include <Elementary.h>
#include <libintl.h>

#include "eulogium_item_list.h"
#include "eulogium_private.h"
#include "gettext.h"


Evas_Object *eulogium_item_list_add(Evas_Object *parent)
{
	Evas_Object *list, *box;
	Eina_List *l = NULL;

	list = elm_scroller_add(parent);
	evas_object_data_set(list, "eulogium_item_list", l);
	box = elm_box_add(list);
	evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND, 0);
	evas_object_data_set(list, "content", box);
	elm_object_content_set(list, box);
	evas_object_show(box);

	return list;
}

static void _item_destroy_cb(void *data, Evas *e EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
{
	Evas_Object **_widget = data;

	*_widget = NULL;
}

static Evas_Object *_item_new(Evas_Object *parent, Evas_Object *icon, char const *label,
			      struct list_item *item, Evas_Smart_Cb func, const void *data)
{
	Evas_Object *it;
	const char *cb = "changed";
	struct list_item dummy = {.type = LIST_ITEM_BUTTON, ._widget = NULL};

	if (!item) /* If no button type is passed (file-list for ex.) use the default 'button' entry */
		item = &dummy;

	switch (item->type) {
	case LIST_ITEM_RADIO:
		it = elm_radio_add(parent);
		/* elm_radio_pointer_set(it, &item->state); */
		/* elm_object_style_set(it, "list"); */
	case LIST_ITEM_CHECK:
		it = elm_check_add(parent);
		elm_check_state_pointer_set(it, &item->state);
		/* elm_object_style_set(it, "list"); */
		/* XXX FIXME: this is a hack. PRINT -> Return -> Segfault.
		 * When we return to the from the file list in the PRINT menu, we segfault.
		 * Technically we don't want to store the pointer to the widget for anything
		 * but checkboxes and radio buttons, but a generic callback at the bottom for
		 * all cases was nicer. Since we don't have radio buttons yet we can leave
		 * this here for now.
		 */
		evas_object_event_callback_add(it, EVAS_CALLBACK_DEL, _item_destroy_cb, &item->_widget);
		if (evas_alloc_error() != EVAS_ALLOC_ERROR_NONE)
			EINA_LOG_CRIT("Callback registering failed! Problems ensured!\n");
		break;
	case LIST_ITEM_BUTTON:
		cb = "clicked";
		it = elm_button_add(parent);
		elm_object_style_set(it, "list");
		break;
	default:
		return NULL;
	}
	if (label)
		elm_object_text_set(it, _(label));
	if (icon)
		elm_object_part_content_set(it, "icon", icon);
	if (data)
		evas_object_data_set(it, "sort_data", data);
	if (func)
		evas_object_smart_callback_add(it, cb, func, data);
	evas_object_size_hint_weight_set(it, EVAS_HINT_EXPAND, 0);
	evas_object_size_hint_align_set(it, EVAS_HINT_FILL, 0);
	evas_object_show(it);
	item->_widget = it;

	return it;
}

Evas_Object *eulogium_item_list_prepend(Evas_Object *list, Evas_Object *icon,
					char const *label, struct list_item *item,
					Evas_Smart_Cb func, const void *data)
{
	Evas_Object *box = evas_object_data_get(list, "content");
	Eina_List *l = evas_object_data_get(list, "eulogium_item_list");
	Evas_Object *it;

	if ((!box) && (!list))
		return NULL;

	it = _item_new(box, icon, label, item, func, data);
	if (!it)
		return NULL;

	if (l)
		l = eina_list_prepend(l, it);
	else
		l = eina_list_prepend(NULL, it);
	evas_object_data_set(list, "eulogium_item_list", l);

	return it;
}

Evas_Object *eulogium_item_list_append(Evas_Object *list, Evas_Object *icon,
					char const *label, struct list_item *item,
					Evas_Smart_Cb func, const void *data)
{
	Evas_Object *box = evas_object_data_get(list, "content");
	Eina_List *l = evas_object_data_get(list, "eulogium_item_list");
	Evas_Object *it;

	if ((!box) && (!list))
		return NULL;

	it = _item_new(box, icon, label, item, func, data);
	if (!it)
		return NULL;

	if (l)
		l = eina_list_append(l, it);
	else
		l = eina_list_append(NULL, it);
	evas_object_data_set(list, "eulogium_item_list", l);

	return it;
}

Evas_Object *eulogium_item_list_sorted_insert(Evas_Object *list, Evas_Object *icon,
					      char const *label, struct list_item *item,
					      Evas_Smart_Cb func, const void *data,
					      Eina_Compare_Cb cmp_func)
{
	Evas_Object *box = evas_object_data_get(list, "content");
	Eina_List *l = evas_object_data_get(list, "eulogium_item_list");
	Evas_Object *it;

	if ((!box) && (!list))
		return NULL;

	it = _item_new(box, icon, label, item, func, data);
	if (!it)
		return NULL;

	if (l)
		l = eina_list_sorted_insert(l, cmp_func, it);
	else
		l = eina_list_sorted_insert(NULL, cmp_func, it);
	evas_object_data_set(list, "eulogium_item_list", l);

	return it;
}

void eulogium_item_list_go(Evas_Object *list)
{
	Evas_Object *box = evas_object_data_get(list, "content");
	Eina_List *l, *item_list = evas_object_data_get(list, "eulogium_item_list");
	Evas_Object *it;

	if ((!box) && (!list))
		return;

	EINA_LIST_FOREACH(item_list, l, it)
		elm_box_pack_end(box, it);
}