Rename mbe shrink_mode to expanded. Consistent with genlist naming.
[framework/uifw/elementary.git] / src / lib / elc_anchorview.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 typedef struct _Widget_Data Widget_Data;
5 typedef struct _Elm_Anchorview_Item_Provider Elm_Anchorview_Item_Provider;
6
7 struct _Widget_Data
8 {
9    Evas_Object *scroller, *entry;
10    Evas_Object *hover_parent;
11    Evas_Object *pop, *hover;
12    Eina_List *item_providers;
13    const char *hover_style;
14 };
15
16 struct _Elm_Anchorview_Item_Provider
17 {
18    Evas_Object *(*func) (void *data, Evas_Object *anchorview, const char *item);
19    void *data;
20 };
21
22 static const char *widtype = NULL;
23
24 static const char SIG_ANCHOR_CLICKED[] = "anchor,clicked";
25 static const Evas_Smart_Cb_Description _signals[] = {
26        {SIG_ANCHOR_CLICKED, ""}, /* TODO: declare the type properly, as data is
27                                   * being passed
28                                   */
29        {NULL, NULL}
30 };
31
32 static void _del_pre_hook(Evas_Object *obj);
33 static void _del_hook(Evas_Object *obj);
34 static void _sizing_eval(Evas_Object *obj);
35 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
36 static void _parent_del(void *data, Evas *e, Evas_Object *obj, void *event_info);
37 static void _mirrored_set(Evas_Object *obj, Eina_Bool rtl);
38
39 static void
40 _del_pre_hook(Evas_Object *obj)
41 {
42    elm_anchorview_hover_end(obj);
43    elm_anchorview_hover_parent_set(obj, NULL);
44 }
45
46 static void
47 _del_hook(Evas_Object *obj)
48 {
49    Widget_Data *wd = elm_widget_data_get(obj);
50    Elm_Anchorview_Item_Provider *ip;
51    if (!wd) return;
52    if (wd->hover_style) eina_stringshare_del(wd->hover_style);
53    EINA_LIST_FREE(wd->item_providers, ip)
54      {
55         free(ip);
56      }
57    free(wd);
58 }
59
60 static void
61 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
62 {
63    Widget_Data *wd = elm_widget_data_get(obj);
64    if (!wd) return;
65    elm_widget_mirrored_set(wd->hover, rtl);
66 }
67
68 static void
69 _theme_hook(Evas_Object *obj)
70 {
71    Widget_Data *wd = elm_widget_data_get(obj);
72    if (!wd) return;
73    _elm_widget_mirrored_reload(obj);
74    _mirrored_set(obj, elm_widget_mirrored_get(obj));
75 }
76
77 static void
78 _sizing_eval(Evas_Object *obj)
79 {
80    Widget_Data *wd = elm_widget_data_get(obj);
81    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
82    if (!wd) return;
83    evas_object_size_hint_min_set(obj, minw, minh);
84    evas_object_size_hint_max_set(obj, maxw, maxh);
85 }
86
87 static void
88 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
89 {
90    _sizing_eval(data);
91 }
92
93 static void
94 _hover_clicked(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
95 {
96    elm_anchorview_hover_end(data);
97 }
98
99 static void
100 _anchor_clicked(void *data, Evas_Object *obj, void *event_info)
101 {
102    Widget_Data *wd = elm_widget_data_get(data);
103    Elm_Entry_Anchor_Info *info = event_info;
104    Evas_Object *hover_parent;
105    Elm_Entry_Anchorview_Info ei;
106    Evas_Coord x, w, y, h, px, py;
107    if (!wd) return;
108    wd->pop = elm_icon_add(obj);
109    evas_object_move(wd->pop, info->x, info->y);
110    evas_object_resize(wd->pop, info->w, info->h);
111    wd->hover = elm_hover_add(obj);
112    elm_widget_mirrored_set(wd->hover, elm_widget_mirrored_get((Evas_Object *) data));
113    if (wd->hover_style) elm_object_style_set(wd->hover, wd->hover_style);
114    hover_parent = wd->hover_parent;
115    if (!hover_parent) hover_parent = obj;
116    elm_hover_parent_set(wd->hover, hover_parent);
117    elm_hover_target_set(wd->hover, wd->pop);
118    ei.name = info->name;
119    ei.button = info->button;
120    ei.hover = wd->hover;
121    ei.anchor.x = info->x;
122    ei.anchor.y = info->y;
123    ei.anchor.w = info->w;
124    ei.anchor.h = info->h;
125    evas_object_geometry_get(hover_parent, &x, &y, &w, &h);
126    ei.hover_parent.x = x;
127    ei.hover_parent.y = y;
128    ei.hover_parent.w = w;
129    ei.hover_parent.h = h;
130    px = info->x + (info->w / 2);
131    py = info->y + (info->h / 2);
132    ei.hover_left = 1;
133    if (px < (x + (w / 3))) ei.hover_left = 0;
134    ei.hover_right = 1;
135    if (px > (x + ((w * 2) / 3))) ei.hover_right = 0;
136    ei.hover_top = 1;
137    if (py < (y + (h / 3))) ei.hover_top = 0;
138    ei.hover_bottom = 1;
139    if (py > (y + ((h * 2) / 3))) ei.hover_bottom = 0;
140
141    if (elm_widget_mirrored_get(wd->hover))
142      {  /* Swap right and left because they switch sides in RTL */
143         Eina_Bool tmp = ei.hover_left;
144         ei.hover_left = ei.hover_right;
145         ei.hover_right = tmp;
146      }
147
148    evas_object_smart_callback_call(data, SIG_ANCHOR_CLICKED, &ei);
149    evas_object_smart_callback_add(wd->hover, "clicked", _hover_clicked, data);
150    evas_object_show(wd->hover);
151 }
152
153 static void
154 _parent_del(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
155 {
156    Widget_Data *wd = elm_widget_data_get(data);
157    if (!wd) return;
158    wd->hover_parent = NULL;
159 }
160
161 static Evas_Object *
162 _item_provider(void *data, Evas_Object *entry __UNUSED__, const char *item)
163 {
164    Widget_Data *wd = elm_widget_data_get(data);
165    Eina_List *l;
166    Elm_Anchorview_Item_Provider *ip;
167
168    EINA_LIST_FOREACH(wd->item_providers, l, ip)
169      {
170         Evas_Object *o;
171
172         o = ip->func(ip->data, data, item);
173         if (o) return o;
174      }
175    return NULL;
176 }
177
178 static void
179 _elm_anchorview_text_set(Evas_Object *obj, const char *item, const char *text)
180 {
181    ELM_CHECK_WIDTYPE(obj, widtype);
182    Widget_Data *wd = elm_widget_data_get(obj);
183    if (item && strcmp(item, "default")) return;
184    if (!wd) return;
185    elm_object_text_set(wd->entry, text);
186    if (wd->hover) evas_object_del(wd->hover);
187    if (wd->pop) evas_object_del(wd->pop);
188    wd->hover = NULL;
189    wd->pop = NULL;
190    _sizing_eval(obj);
191 }
192
193 static const char*
194 _elm_anchorview_text_get(const Evas_Object *obj, const char *item)
195 {
196    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
197    Widget_Data *wd = elm_widget_data_get(obj);
198    if (item && strcmp(item, "default")) return NULL;
199    if (!wd) return NULL;
200    return elm_object_text_get(wd->entry);
201 }
202
203 EAPI Evas_Object *
204 elm_anchorview_add(Evas_Object *parent)
205 {
206    Evas_Object *obj;
207    Evas *e;
208    Widget_Data *wd;
209
210    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
211
212    ELM_SET_WIDTYPE(widtype, "anchorview");
213    elm_widget_type_set(obj, "anchorview");
214    elm_widget_sub_object_add(parent, obj);
215    elm_widget_data_set(obj, wd);
216    elm_widget_del_pre_hook_set(obj, _del_pre_hook);
217    elm_widget_del_hook_set(obj, _del_hook);
218    elm_widget_theme_hook_set(obj, _theme_hook);
219    elm_widget_can_focus_set(obj, EINA_TRUE);
220    elm_widget_text_set_hook_set(obj, _elm_anchorview_text_set);
221    elm_widget_text_get_hook_set(obj, _elm_anchorview_text_get);
222
223    wd->scroller = elm_scroller_add(parent);
224    elm_widget_resize_object_set(obj, wd->scroller);
225    wd->entry = elm_entry_add(parent);
226    elm_entry_item_provider_prepend(wd->entry, _item_provider, obj);
227    elm_entry_editable_set(wd->entry, 0);
228    evas_object_size_hint_weight_set(wd->entry, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
229    evas_object_size_hint_align_set(wd->entry, EVAS_HINT_FILL, EVAS_HINT_FILL);
230    elm_object_content_set(wd->scroller, wd->entry);
231    evas_object_show(wd->entry);
232
233    evas_object_event_callback_add(wd->entry, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
234                                   _changed_size_hints, obj);
235
236    elm_object_text_set(wd->entry, "");
237
238    evas_object_smart_callback_add(wd->entry, "anchor,clicked",
239                                   _anchor_clicked, obj);
240
241    _mirrored_set(obj, elm_widget_mirrored_get(obj));
242    _sizing_eval(obj);
243
244    // TODO: convert Elementary to subclassing of Evas_Smart_Class
245    // TODO: and save some bytes, making descriptions per-class and not instance!
246    evas_object_smart_callbacks_descriptions_set(obj, _signals);
247    return obj;
248 }
249
250 EAPI void
251 elm_anchorview_hover_parent_set(Evas_Object *obj, Evas_Object *parent)
252 {
253    ELM_CHECK_WIDTYPE(obj, widtype);
254    Widget_Data *wd = elm_widget_data_get(obj);
255    if (!wd) return;
256    if (wd->hover_parent)
257      evas_object_event_callback_del_full(wd->hover_parent, EVAS_CALLBACK_DEL, _parent_del, obj);
258    wd->hover_parent = parent;
259    if (wd->hover_parent)
260      evas_object_event_callback_add(wd->hover_parent, EVAS_CALLBACK_DEL, _parent_del, obj);
261 }
262
263 EAPI Evas_Object *
264 elm_anchorview_hover_parent_get(const Evas_Object *obj)
265 {
266    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
267    Widget_Data *wd = elm_widget_data_get(obj);
268    if (!wd) return NULL;
269    return wd->hover_parent;
270 }
271
272 EAPI void
273 elm_anchorview_hover_style_set(Evas_Object *obj, const char *style)
274 {
275    ELM_CHECK_WIDTYPE(obj, widtype);
276    Widget_Data *wd = elm_widget_data_get(obj);
277    if (!wd) return;
278    eina_stringshare_replace(&wd->hover_style, style);
279 }
280
281 EAPI const char *
282 elm_anchorview_hover_style_get(const Evas_Object *obj)
283 {
284    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
285    Widget_Data *wd = elm_widget_data_get(obj);
286    if (!wd) return NULL;
287    return wd->hover_style;
288 }
289
290 EAPI void
291 elm_anchorview_hover_end(Evas_Object *obj)
292 {
293    ELM_CHECK_WIDTYPE(obj, widtype);
294    Widget_Data *wd = elm_widget_data_get(obj);
295    if (!wd) return;
296    if (wd->hover) evas_object_del(wd->hover);
297    if (wd->pop) evas_object_del(wd->pop);
298    wd->hover = NULL;
299    wd->pop = NULL;
300 }
301
302 EAPI void
303 elm_anchorview_bounce_set(Evas_Object *obj, Eina_Bool h_bounce, Eina_Bool v_bounce)
304 {
305    ELM_CHECK_WIDTYPE(obj, widtype);
306    Widget_Data *wd = elm_widget_data_get(obj);
307    if (!wd) return;
308    elm_scroller_bounce_set(wd->scroller, h_bounce, v_bounce);
309 }
310
311 EAPI void
312 elm_anchorview_bounce_get(const Evas_Object *obj, Eina_Bool *h_bounce, Eina_Bool *v_bounce)
313 {
314    ELM_CHECK_WIDTYPE(obj, widtype);
315    Widget_Data *wd = elm_widget_data_get(obj);
316    if (!wd) return;
317    elm_scroller_bounce_get(wd->scroller, h_bounce, v_bounce);
318 }
319
320 EAPI void
321 elm_anchorview_item_provider_append(Evas_Object *obj, Evas_Object *(*func) (void *data, Evas_Object *anchorview, const char *item), void *data)
322 {
323    ELM_CHECK_WIDTYPE(obj, widtype);
324    Widget_Data *wd = elm_widget_data_get(obj);
325    if (!wd) return;
326    EINA_SAFETY_ON_NULL_RETURN(func);
327    Elm_Anchorview_Item_Provider *ip = calloc(1, sizeof(Elm_Anchorview_Item_Provider));
328    if (!ip) return;
329    ip->func = func;
330    ip->data = data;
331    wd->item_providers = eina_list_append(wd->item_providers, ip);
332 }
333
334 EAPI void
335 elm_anchorview_item_provider_prepend(Evas_Object *obj, Evas_Object *(*func) (void *data, Evas_Object *anchorview, const char *item), void *data)
336 {
337    ELM_CHECK_WIDTYPE(obj, widtype);
338    Widget_Data *wd = elm_widget_data_get(obj);
339    if (!wd) return;
340    EINA_SAFETY_ON_NULL_RETURN(func);
341    Elm_Anchorview_Item_Provider *ip = calloc(1, sizeof(Elm_Anchorview_Item_Provider));
342    if (!ip) return;
343    ip->func = func;
344    ip->data = data;
345    wd->item_providers = eina_list_prepend(wd->item_providers, ip);
346 }
347
348 EAPI void
349 elm_anchorview_item_provider_remove(Evas_Object *obj, Evas_Object *(*func) (void *data, Evas_Object *anchorview, const char *item), void *data)
350 {
351    ELM_CHECK_WIDTYPE(obj, widtype);
352    Widget_Data *wd = elm_widget_data_get(obj);
353    Eina_List *l;
354    Elm_Anchorview_Item_Provider *ip;
355    if (!wd) return;
356    EINA_SAFETY_ON_NULL_RETURN(func);
357    EINA_LIST_FOREACH(wd->item_providers, l, ip)
358      {
359         if ((ip->func == func) && (ip->data == data))
360           {
361              wd->item_providers = eina_list_remove_list(wd->item_providers, l);
362              free(ip);
363              return;
364           }
365      }
366 }