summaryrefslogtreecommitdiff
path: root/main/src/util/ivug-listpopup.c
blob: 6ea395f523120c12ddae73510d2df6439fc65d09 (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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
 * Copyright 2012  Samsung Electronics Co., Ltd
 *
 * Licensed under the Flora License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *	  http://www.tizenopensource.org/license
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


#include "ivug-common.h"
#include "ivug-listpopup.h"


#define POPUP_RESPONSE_NOT_SELECTED (-99)


typedef struct {
	Evas_Object *popup;

	Evas_Smart_Cb response;
	void* user_data;

} Selectpopup;


static char *
_on_label_set(void *data, Evas_Object *obj, const char *part)
{
	IV_ASSERT( data != NULL);

	ivug_listpopup_item *item = (ivug_listpopup_item *)data;

	return strdup(item->caption); //dump
}


static void
_on_genlist_selected(void *data, Evas_Object *obj, void *event_info)
{
	Selectpopup *pData = data;
	Evas_Object *genlist = obj;
	Elm_Object_Item *genitem = (Elm_Object_Item *)event_info;

	IV_ASSERT( genlist != NULL);

	if (genitem == NULL)
	{
		MSG_MAIN_ERROR("genlist item is NULL");
		return;
	}

	ivug_listpopup_item *item = (ivug_listpopup_item *)elm_object_item_data_get(genitem);

// Call response
	pData->response(pData->user_data, pData->popup, item);

}


static ivug_listpopup_item *_dup_item(ivug_listpopup_item *item)
{
	ivug_listpopup_item *newitem = NULL;

	newitem = calloc(1, sizeof(ivug_listpopup_item));

	newitem->index = item->index;

	if ( item->iconpath )
		newitem->iconpath = strdup(item->iconpath);

	if ( item->caption )
		newitem->caption = strdup(item->caption);		// Should be freed

	newitem->data = item->data;

	return newitem;
}


static void _on_genlist_item_del(void *data, Evas_Object *obj)
{
	IV_ASSERT( data != NULL);

	ivug_listpopup_item *item = data;

	MSG_IMAGEVIEW_HIGH("Remove genlist item");

	if ( item->caption )
		free(item->caption);

	if ( item->iconpath )
		free(item->iconpath);

	free(item);
}


static void
_on_popup_close(void *data, Evas_Object *obj, void *event_info)
{
	Selectpopup *pData = data;

	pData->response(pData->user_data, pData->popup, NULL);		// Call user callback
}

static void _on_popup_deleted(void * data, Evas * e, Evas_Object * obj, void * event_info)
{
	MSG_IMAGEVIEW_HIGH("Remove popup");

	Selectpopup *pData = data;

	free(pData);
}



typedef struct {
	Eina_List *list;
} _ivug_listpopup_itemlist;


ivug_listpopup_itemlist ivug_listpopup_itemlist_new()
{
	_ivug_listpopup_itemlist *pList = malloc(sizeof(_ivug_listpopup_itemlist));
	IV_ASSERT(pList != NULL);

	pList->list = NULL;

	return (ivug_listpopup_itemlist)pList;
}

unsigned int ivug_listpopup_itemlist_get_count(ivug_listpopup_itemlist items)
{
	_ivug_listpopup_itemlist *pList = items;
	IV_ASSERT(pList != NULL);

	return eina_list_count(pList->list);
}

ivug_listpopup_item *ivug_listpopup_itemlist_add(ivug_listpopup_itemlist items, int index, const char *iconpath, const char *caption, void *data, bool bDisabled)
{
	_ivug_listpopup_itemlist *pList = items;
	IV_ASSERT(pList != NULL);

	ivug_listpopup_item *item = NULL;

	{
		item = calloc(1, sizeof(ivug_listpopup_item));

		item->index = index;

		if ( iconpath )
			item->iconpath = strdup(iconpath);

		if ( caption )
			item->caption = strdup(caption);		// Should be freed

		item->data = data;
		item->bDisabled = bDisabled;
	}

	pList->list = eina_list_append(pList->list, item);

	return item;


}

void ivug_listpopup_itemlist_free(ivug_listpopup_itemlist items)
{
	_ivug_listpopup_itemlist *pList = items;
	IV_ASSERT(pList != NULL);

	ivug_listpopup_item *item = NULL;

	EINA_LIST_FREE(pList->list, item )
	{
		if ( item->caption )
			free(item->caption);

		if ( item->iconpath )
			free(item->iconpath);

		free(item);
	}

	free(pList);
}


Evas_Object *ivug_listpopup_show(Evas_Object *parent, const char* title, ivug_listpopup_itemlist items, Evas_Smart_Cb response, void* user_data)
{
	Evas_Object *popup;

// create popup
	popup = elm_popup_add(parent);
	if (!popup)
	{
		MSG_IMAGEVIEW_ERROR("Error : popup create failed.");
		return NULL;
	}

	elm_object_style_set(popup, "menustyle");
   	elm_object_part_text_set(popup, "title,text", title);

	evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
	evas_object_size_hint_weight_set(popup, EVAS_HINT_FILL, EVAS_HINT_FILL);

	Evas_Object *btn_close = elm_button_add(popup);
	elm_object_text_set(btn_close, IDS_CLOSE);
	elm_object_part_content_set(popup, "button1", btn_close);

// create genlist
	Evas_Object *genlist;
	static Elm_Genlist_Item_Class itc = {0,};

	genlist = elm_genlist_add(popup);

	itc.version = ELM_GENLIST_ITEM_CLASS_VERSION;
	itc.item_style = "1text";
	itc.func.text_get = _on_label_set;
   	itc.func.content_get = NULL;
   	itc.func.state_get = NULL;
   	itc.func.del = _on_genlist_item_del;

	evas_object_size_hint_weight_set(genlist, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
	evas_object_size_hint_align_set(genlist, EVAS_HINT_FILL, EVAS_HINT_FILL);

	Selectpopup *pData = malloc(sizeof(Selectpopup));

	pData->popup = popup;
	pData->response = response;
	pData->user_data = user_data;

	_ivug_listpopup_itemlist *pList = items;

	ivug_listpopup_item *pItem = NULL;
	Eina_List *tmp;
	Elm_Object_Item *gItem;


	EINA_LIST_FOREACH(pList->list, tmp, pItem)
	{
		gItem = elm_genlist_item_append(genlist, &itc, _dup_item(pItem), NULL /* parent */, ELM_GENLIST_ITEM_NONE, _on_genlist_selected, pData);

		elm_object_item_disabled_set(gItem, pItem->bDisabled);
	}

// Put together
	Evas_Object *box;
	box = elm_box_add(popup);
	evas_object_show(genlist);
	elm_box_pack_end(box, genlist);
	elm_object_content_set(popup, box);

	evas_object_smart_callback_add(btn_close, "clicked", _on_popup_close, pData);

	evas_object_event_callback_add(popup, EVAS_CALLBACK_DEL, _on_popup_deleted, pData);

	evas_object_show(popup);

	return popup;

}