aboutsummaryrefslogtreecommitdiffstats
path: root/src/bin/e_ilist.c
blob: 94b729909fa5bec8aa64d5ca8d9a8087fb0961e3 (plain) (blame)
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
 * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
 */
#include "e.h"

#define SMART_NAME "e_ilist"
#define API_ENTRY E_Smart_Data *sd; sd = evas_object_smart_data_get(obj); if ((!obj) || (!sd) || (evas_object_type_get(obj) && strcmp(evas_object_type_get(obj), SMART_NAME)))
#define INTERNAL_ENTRY E_Smart_Data *sd; sd = evas_object_smart_data_get(obj); if (!sd) return;
typedef struct _E_Smart_Data E_Smart_Data;
typedef struct _E_Smart_Item E_Smart_Item;

struct _E_Smart_Data
{ 
   Evas_Coord   x, y, w, h;
   
   Evas_Object   *smart_obj;
   Evas_Object   *box_obj;
   Evas_List     *items;
   int            selected;
   Evas_Coord     icon_w, icon_h;
}; 

struct _E_Smart_Item
{
   E_Smart_Data  *sd;
   Evas_Object   *base_obj;
   Evas_Object   *icon_obj;
   void         (*func) (void *data);
   void          *data;
};

/* local subsystem functions */
static void _e_smart_event_mouse_down(void *data, Evas *e, Evas_Object *obj, void *event_info);
static void _e_smart_event_key_down(void *data, Evas *e, Evas_Object *obj, void *event_info);
static void _e_smart_reconfigure(E_Smart_Data *sd);
static void _e_smart_add(Evas_Object *obj);
static void _e_smart_del(Evas_Object *obj);
static void _e_smart_move(Evas_Object *obj, Evas_Coord x, Evas_Coord y);
static void _e_smart_resize(Evas_Object *obj, Evas_Coord w, Evas_Coord h);
static void _e_smart_show(Evas_Object *obj);
static void _e_smart_hide(Evas_Object *obj);
static void _e_smart_color_set(Evas_Object *obj, int r, int g, int b, int a);
static void _e_smart_clip_set(Evas_Object *obj, Evas_Object * clip);
static void _e_smart_clip_unset(Evas_Object *obj);
static void _e_smart_init(void);

/* local subsystem globals */
static Evas_Smart *_e_smart = NULL;

/* externally accessible functions */
Evas_Object *
e_ilist_add(Evas *evas)
{
   _e_smart_init();
   return evas_object_smart_add(evas, _e_smart);
}

void
e_ilist_icon_size_set(Evas_Object *obj, Evas_Coord w, Evas_Coord h)
{
   Evas_List *l;
   
   API_ENTRY return;
   if ((sd->icon_w == w) && (sd->icon_h == h)) return;
   sd->icon_w = w;
   sd->icon_h = h;
   for (l = sd->items; l; l = l->next)
     {
	E_Smart_Item *si;
	
	si = l->data;
	if (si->icon_obj)
	  {
	     Evas_Coord mw = 0, mh = 0;
	     
	     edje_extern_object_min_size_set(si->icon_obj, sd->icon_w, sd->icon_h);
	     edje_object_part_swallow(si->base_obj, "icon_swallow", si->icon_obj);
	     edje_object_size_min_calc(si->base_obj, &mw, &mh);
	     e_box_pack_options_set(si->icon_obj,
				    1, 1, /* fill */
				    1, 0, /* expand */
				    0.5, 0.5, /* align */
				    mw, mh, /* min */
				    99999, 99999 /* max */
				    );
	  }
     }
}

void
e_ilist_append(Evas_Object *obj, Evas_Object *icon, char *label,  void (*func) (void *data), void *data)
{
   E_Smart_Item *si;
   Evas_Coord mw = 0, mh = 0;
   
   API_ENTRY return;
   si = E_NEW(E_Smart_Item, 1);
   si->sd = sd;
   si->base_obj = edje_object_add(evas_object_evas_get(sd->smart_obj));
   if (evas_list_count(sd->items) & 0x1)
     e_theme_edje_object_set(si->base_obj, "base/theme/widgets",
			     "widgets/ilist_odd");
   else
     e_theme_edje_object_set(si->base_obj, "base/theme/widgets",
			     "widgets/ilist");
   edje_object_part_text_set(si->base_obj, "label", label);
   si->icon_obj = icon;
   if (si->icon_obj)
     {
	edje_extern_object_min_size_set(si->icon_obj, sd->icon_w, sd->icon_h);
	edje_object_part_swallow(si->base_obj, "icon_swallow", si->icon_obj);
	evas_object_show(si->icon_obj);
     }
   si->func = func;
   si->data = data;
   sd->items = evas_list_append(sd->items, si);
   edje_object_size_min_calc(si->base_obj, &mw, &mh);
   e_box_pack_end(sd->box_obj, si->base_obj);
   e_box_pack_options_set(si->base_obj,
			  1, 1, /* fill */
			  1, 1, /* expand */
			  0.5, 0.5, /* align */
			  mw, mh, /* min */
			  99999, 99999 /* max */
			  );
   if (sd->selected == (evas_list_count(sd->items) - 1))
     {
	edje_object_signal_emit(si->base_obj, "active", "");
	if (si->func) si->func(si->data);
     }
   evas_object_lower(si->base_obj);
   evas_object_event_callback_add(si->base_obj, EVAS_CALLBACK_MOUSE_DOWN, _e_smart_event_mouse_down, si);
   evas_object_show(si->base_obj);
}

void
e_ilist_select_set(Evas_Object *obj, int n)
{
   E_Smart_Item *si;
   
   API_ENTRY return;
   if (!sd->items) return;
   if (n >= evas_list_count(sd->items)) n = evas_list_count(sd->items) - 1;
   else if (n < 0) n = 0;
   if (sd->selected == n) return;
   si = evas_list_nth(sd->items, sd->selected);
   if (si) edje_object_signal_emit(si->base_obj, "passive", "");
   sd->selected = n;
   si = evas_list_nth(sd->items, sd->selected);
   if (si)
     {
	evas_object_raise(si->base_obj);
	edje_object_signal_emit(si->base_obj, "active", "");
	if (si->func) si->func(si->data);
     }
}

int
e_ilist_select_get(Evas_Object *obj)
{
   API_ENTRY return -1;
   if (!sd->items) return -1;
   return sd->selected;
}

void *
e_ilist_select_data_get(Evas_Object *obj)
{
   E_Smart_Item *si;
   
   API_ENTRY return NULL;
   if (!sd->items) return NULL;
   si = evas_list_nth(sd->items, sd->selected);
   if (si) return si->data;
   return NULL;
}

void
e_ilist_selected_geometry_get(Evas_Object *obj, Evas_Coord *x, Evas_Coord *y, Evas_Coord *w, Evas_Coord *h)
{
   E_Smart_Item *si;
   
   API_ENTRY return;
   si = evas_list_nth(sd->items, sd->selected);
   if (si)
     {
	evas_object_geometry_get(si->base_obj, x, y, w, h);
	*x -= sd->x;
	*y -= sd->y;
     }
}

void
e_ilist_min_size_get(Evas_Object *obj, Evas_Coord *w, Evas_Coord *h)
{
   API_ENTRY return;
   e_box_min_size_get(sd->box_obj, w, h);
}

/* local subsystem functions */
static void 
_e_smart_event_mouse_down(void *data, Evas *e, Evas_Object *obj, void *event_info)
{
   Evas_Event_Mouse_Down *ev;
   E_Smart_Item *si;
   Evas_List *l;
   int i;
   
   si = data;
   ev = event_info;
   for (i = 0, l = si->sd->items; l; l = l->next, i++)
     {
	if (l->data == si)
	  {
	     e_ilist_select_set(si->sd->smart_obj, i);
	     break;
	  }
     }
}

static void
_e_smart_event_key_down(void *data, Evas *e, Evas_Object *obj, void *event_info)
{
   Evas_Event_Key_Down *ev;
   E_Smart_Data *sd;
   
   sd = data;
   ev = event_info;
   if (!strcmp(ev->keyname, "Up"))
     {
	int n;
	
	n = e_ilist_select_get(sd->smart_obj);
	e_ilist_select_set(sd->smart_obj, n - 1);
     }
   else if (!strcmp(ev->keyname, "Down"))
     {
	int n;
	
	n = e_ilist_select_get(sd->smart_obj);
	e_ilist_select_set(sd->smart_obj, n + 1);
     }
}

static void
_e_smart_reconfigure(E_Smart_Data *sd)
{
   evas_object_move(sd->box_obj, sd->x, sd->y);
   evas_object_resize(sd->box_obj, sd->w, sd->h);
}

static void
_e_smart_add(Evas_Object *obj)
{
   E_Smart_Data *sd;
   Evas_Object *o;
   
   sd = calloc(1, sizeof(E_Smart_Data));
   if (!sd) return;
   evas_object_smart_data_set(obj, sd);
   
   sd->smart_obj = obj;
   sd->x = 0;
   sd->y = 0;
   sd->w = 0;
   sd->h = 0;
   
   sd->icon_w = 24;
   sd->icon_h = 24;
   
   sd->box_obj = e_box_add(evas_object_evas_get(obj));
   e_box_align_set(sd->box_obj, 0.0, 0.0);
   e_box_homogenous_set(sd->box_obj, 0);
   evas_object_smart_member_add(sd->box_obj, obj);
   
   evas_object_event_callback_add(obj, EVAS_CALLBACK_KEY_DOWN, _e_smart_event_key_down, sd);
   evas_object_propagate_events_set(obj, 0);
}

static void
_e_smart_del(Evas_Object *obj)
{
   INTERNAL_ENTRY;
   evas_object_del(sd->box_obj);
   free(sd);
}

static void
_e_smart_move(Evas_Object *obj, Evas_Coord x, Evas_Coord y)
{
   INTERNAL_ENTRY;
   if ((sd->x == x) && (sd->y == y)) return;
   sd->x = x;
   sd->y = y;
   _e_smart_reconfigure(sd);
}

static void
_e_smart_resize(Evas_Object *obj, Evas_Coord w, Evas_Coord h)
{
   INTERNAL_ENTRY;
   if ((sd->w == w) && (sd->h == h)) return;
   sd->w = w;
   sd->h = h;
   _e_smart_reconfigure(sd);
}

static void
_e_smart_show(Evas_Object *obj)
{
   INTERNAL_ENTRY;
   evas_object_show(sd->box_obj);
}

static void
_e_smart_hide(Evas_Object *obj)
{
   INTERNAL_ENTRY;
   evas_object_hide(sd->box_obj);
}

static void
_e_smart_color_set(Evas_Object *obj, int r, int g, int b, int a)
{
   INTERNAL_ENTRY;
   evas_object_color_set(sd->box_obj, r, g, b, a);
}

static void
_e_smart_clip_set(Evas_Object *obj, Evas_Object * clip)
{
   INTERNAL_ENTRY;
   evas_object_clip_set(sd->box_obj, clip);
}

static void
_e_smart_clip_unset(Evas_Object *obj)
{
   INTERNAL_ENTRY;
   evas_object_clip_unset(sd->box_obj);
}  

/* never need to touch this */

static void
_e_smart_init(void)
{
   if (_e_smart) return;
   _e_smart = evas_smart_new(SMART_NAME,
			     _e_smart_add,
			     _e_smart_del, 
			     NULL, NULL, NULL, NULL, NULL,
			     _e_smart_move,
			     _e_smart_resize,
			     _e_smart_show,
			     _e_smart_hide,
			     _e_smart_color_set,
			     _e_smart_clip_set,
			     _e_smart_clip_unset,
			     NULL);
}