16994c76d413163acff9bb1cdc0a57482d66318d
[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_entry_entry_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_entry_entry_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_entry_entry_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_text_set(Evas_Object *obj, const char *text)
252 {
253    _elm_anchorview_text_set(obj, NULL, text);
254 }
255
256 EAPI const char*
257 elm_anchorview_text_get(const Evas_Object *obj)
258 {
259    return _elm_anchorview_text_get(obj, NULL);
260 }
261
262 EAPI void
263 elm_anchorview_hover_parent_set(Evas_Object *obj, Evas_Object *parent)
264 {
265    ELM_CHECK_WIDTYPE(obj, widtype);
266    Widget_Data *wd = elm_widget_data_get(obj);
267    if (!wd) return;
268    if (wd->hover_parent)
269      evas_object_event_callback_del_full(wd->hover_parent, EVAS_CALLBACK_DEL, _parent_del, obj);
270    wd->hover_parent = parent;
271    if (wd->hover_parent)
272      evas_object_event_callback_add(wd->hover_parent, EVAS_CALLBACK_DEL, _parent_del, obj);
273 }
274
275 EAPI Evas_Object *
276 elm_anchorview_hover_parent_get(const Evas_Object *obj)
277 {
278    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
279    Widget_Data *wd = elm_widget_data_get(obj);
280    if (!wd) return NULL;
281    return wd->hover_parent;
282 }
283
284 EAPI void
285 elm_anchorview_hover_style_set(Evas_Object *obj, const char *style)
286 {
287    ELM_CHECK_WIDTYPE(obj, widtype);
288    Widget_Data *wd = elm_widget_data_get(obj);
289    if (!wd) return;
290    eina_stringshare_replace(&wd->hover_style, style);
291 }
292
293 EAPI const char *
294 elm_anchorview_hover_style_get(const Evas_Object *obj)
295 {
296    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
297    Widget_Data *wd = elm_widget_data_get(obj);
298    if (!wd) return NULL;
299    return wd->hover_style;
300 }
301
302 EAPI void
303 elm_anchorview_hover_end(Evas_Object *obj)
304 {
305    ELM_CHECK_WIDTYPE(obj, widtype);
306    Widget_Data *wd = elm_widget_data_get(obj);
307    if (!wd) return;
308    if (wd->hover) evas_object_del(wd->hover);
309    if (wd->pop) evas_object_del(wd->pop);
310    wd->hover = NULL;
311    wd->pop = NULL;
312 }
313
314 EAPI void
315 elm_anchorview_bounce_set(Evas_Object *obj, Eina_Bool h_bounce, Eina_Bool v_bounce)
316 {
317    ELM_CHECK_WIDTYPE(obj, widtype);
318    Widget_Data *wd = elm_widget_data_get(obj);
319    if (!wd) return;
320    elm_scroller_bounce_set(wd->scroller, h_bounce, v_bounce);
321 }
322
323 EAPI void
324 elm_anchorview_bounce_get(const Evas_Object *obj, Eina_Bool *h_bounce, Eina_Bool *v_bounce)
325 {
326    ELM_CHECK_WIDTYPE(obj, widtype);
327    Widget_Data *wd = elm_widget_data_get(obj);
328    if (!wd) return;
329    elm_scroller_bounce_get(wd->scroller, h_bounce, v_bounce);
330 }
331
332 EAPI void
333 elm_anchorview_item_provider_append(Evas_Object *obj, Evas_Object *(*func) (void *data, Evas_Object *anchorview, const char *item), void *data)
334 {
335    ELM_CHECK_WIDTYPE(obj, widtype);
336    Widget_Data *wd = elm_widget_data_get(obj);
337    if (!wd) return;
338    EINA_SAFETY_ON_NULL_RETURN(func);
339    Elm_Anchorview_Item_Provider *ip = calloc(1, sizeof(Elm_Anchorview_Item_Provider));
340    if (!ip) return;
341    ip->func = func;
342    ip->data = data;
343    wd->item_providers = eina_list_append(wd->item_providers, ip);
344 }
345
346 EAPI void
347 elm_anchorview_item_provider_prepend(Evas_Object *obj, Evas_Object *(*func) (void *data, Evas_Object *anchorview, const char *item), void *data)
348 {
349    ELM_CHECK_WIDTYPE(obj, widtype);
350    Widget_Data *wd = elm_widget_data_get(obj);
351    if (!wd) return;
352    EINA_SAFETY_ON_NULL_RETURN(func);
353    Elm_Anchorview_Item_Provider *ip = calloc(1, sizeof(Elm_Anchorview_Item_Provider));
354    if (!ip) return;
355    ip->func = func;
356    ip->data = data;
357    wd->item_providers = eina_list_prepend(wd->item_providers, ip);
358 }
359
360 EAPI void
361 elm_anchorview_item_provider_remove(Evas_Object *obj, Evas_Object *(*func) (void *data, Evas_Object *anchorview, const char *item), void *data)
362 {
363    ELM_CHECK_WIDTYPE(obj, widtype);
364    Widget_Data *wd = elm_widget_data_get(obj);
365    Eina_List *l;
366    Elm_Anchorview_Item_Provider *ip;
367    if (!wd) return;
368    EINA_SAFETY_ON_NULL_RETURN(func);
369    EINA_LIST_FOREACH(wd->item_providers, l, ip)
370      {
371         if ((ip->func == func) && (ip->data == data))
372           {
373              wd->item_providers = eina_list_remove_list(wd->item_providers, l);
374              free(ip);
375              return;
376           }
377      }
378 }