================================================================
[framework/uifw/elementary.git] / src / lib / elc_anchorview.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 /**
5  * @defgroup Anchorview Anchorview
6  *
7  * This is just like the Anchorblock object, but provides a scroller to hold
8  * the text automatically.
9  *
10  * Signals that you can add callbacks for are:
11  *
12  * anchor,clicked - achor called was clicked. event_info is anchor info -
13  * Elm_Entry_Anchorview_Info
14  */
15 typedef struct _Widget_Data Widget_Data;
16 typedef struct _Elm_Anchorview_Item_Provider Elm_Anchorview_Item_Provider;
17
18 struct _Widget_Data
19 {
20    Evas_Object *scroller, *entry;
21    Evas_Object *hover_parent;
22    Evas_Object *pop, *hover;
23    Eina_List *item_providers;
24    const char *hover_style;
25 };
26
27 struct _Elm_Anchorview_Item_Provider
28 {
29    Evas_Object *(*func) (void *data, Evas_Object *anchorview, const char *item);
30    void *data;
31 };
32
33 static const char *widtype = NULL;
34
35 static const char SIG_ANCHOR_CLICKED[] = "anchor,clicked";
36 static const Evas_Smart_Cb_Description _signals[] = {
37   {SIG_ANCHOR_CLICKED, ""}, /* TODO: declare the type properly, as data is
38                              * being passed
39                              */
40   {NULL, NULL}
41 };
42
43 static void _del_pre_hook(Evas_Object *obj);
44 static void _del_hook(Evas_Object *obj);
45 static void _sizing_eval(Evas_Object *obj);
46 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
47 static void _parent_del(void *data, Evas *e, Evas_Object *obj, void *event_info);
48
49 static void
50 _del_pre_hook(Evas_Object *obj)
51 {
52    elm_anchorview_hover_end(obj);
53    elm_anchorview_hover_parent_set(obj, NULL);
54 }
55
56 static void
57 _del_hook(Evas_Object *obj)
58 {
59    Widget_Data *wd = elm_widget_data_get(obj);
60    Elm_Anchorview_Item_Provider *ip;
61    if (!wd) return;
62    if (wd->hover_style) eina_stringshare_del(wd->hover_style);
63    EINA_LIST_FREE(wd->item_providers, ip)
64      {
65         free(ip);
66      }
67    free(wd);
68 }
69
70 static void
71 _sizing_eval(Evas_Object *obj)
72 {
73    Widget_Data *wd = elm_widget_data_get(obj);
74    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
75    if (!wd) return;
76    evas_object_size_hint_min_set(obj, minw, minh);
77    evas_object_size_hint_max_set(obj, maxw, maxh);
78 }
79
80 static void
81 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
82 {
83    _sizing_eval(data);
84 }
85
86 static void
87 _hover_clicked(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
88 {
89    elm_anchorview_hover_end(data);
90 }
91
92 static void
93 _anchor_clicked(void *data, Evas_Object *obj, void *event_info)
94 {
95    Widget_Data *wd = elm_widget_data_get(data);
96    Elm_Entry_Anchor_Info *info = event_info;
97    Evas_Object *hover_parent;
98    Elm_Entry_Anchorview_Info ei;
99    Evas_Coord x, w, y, h, px, py;
100    if (!wd) return;
101    wd->pop = elm_icon_add(obj);
102    evas_object_move(wd->pop, info->x, info->y);
103    evas_object_resize(wd->pop, info->w, info->h);
104    wd->hover = elm_hover_add(obj);
105    if (wd->hover_style) elm_object_style_set(wd->hover, wd->hover_style);
106    hover_parent = wd->hover_parent;
107    if (!hover_parent) hover_parent = obj;
108    elm_hover_parent_set(wd->hover, hover_parent);
109    elm_hover_target_set(wd->hover, wd->pop);
110    ei.name = info->name;
111    ei.button = info->button;
112    ei.hover = wd->hover;
113    ei.anchor.x = info->x;
114    ei.anchor.y = info->y;
115    ei.anchor.w = info->w;
116    ei.anchor.h = info->h;
117    evas_object_geometry_get(hover_parent, &x, &y, &w, &h);
118    ei.hover_parent.x = x;
119    ei.hover_parent.y = y;
120    ei.hover_parent.w = w;
121    ei.hover_parent.h = h;
122    px = info->x + (info->w / 2);
123    py = info->y + (info->h / 2);
124    ei.hover_left = 1;
125    if (px < (x + (w / 3))) ei.hover_left = 0;
126    ei.hover_right = 1;
127    if (px > (x + ((w * 2) / 3))) ei.hover_right = 0;
128    ei.hover_top = 1;
129    if (py < (y + (h / 3))) ei.hover_top = 0;
130    ei.hover_bottom = 1;
131    if (py > (y + ((h * 2) / 3))) ei.hover_bottom = 0;
132    evas_object_smart_callback_call(data, SIG_ANCHOR_CLICKED, &ei);
133    evas_object_smart_callback_add(wd->hover, "clicked", _hover_clicked, data);
134    evas_object_show(wd->hover);
135 }
136
137 static void
138 _parent_del(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
139 {
140    Widget_Data *wd = elm_widget_data_get(data);
141    if (!wd) return;
142    wd->hover_parent = NULL;
143 }
144
145 static Evas_Object *
146 _item_provider(void *data, Evas_Object *entry __UNUSED__, const char *item)
147 {
148    Widget_Data *wd = elm_widget_data_get(data);
149    Eina_List *l;
150    Elm_Anchorview_Item_Provider *ip;
151    
152    EINA_LIST_FOREACH(wd->item_providers, l, ip)
153      {
154         Evas_Object *o;
155
156         o = ip->func(ip->data, data, item);
157         if (o) return o;
158      }
159    return NULL;
160 }
161
162 /**
163  * Add a new Anchorview object
164  *
165  * @param parent The parent object
166  * @return The new object or NULL if it cannot be created
167  *
168  * @ingroup Anchorview
169  */
170 EAPI Evas_Object *
171 elm_anchorview_add(Evas_Object *parent)
172 {
173    Evas_Object *obj;
174    Evas *e;
175    Widget_Data *wd;
176
177    EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
178
179    wd = ELM_NEW(Widget_Data);
180    e = evas_object_evas_get(parent);
181    if (!e) return NULL;
182    obj = elm_widget_add(e);
183    ELM_SET_WIDTYPE(widtype, "anchorview");
184    elm_widget_type_set(obj, "anchorview");
185    elm_widget_sub_object_add(parent, obj);
186    elm_widget_data_set(obj, wd);
187    elm_widget_del_pre_hook_set(obj, _del_pre_hook);
188    elm_widget_del_hook_set(obj, _del_hook);
189    elm_widget_can_focus_set(obj, EINA_TRUE);
190
191    wd->scroller = elm_scroller_add(parent);
192    elm_widget_resize_object_set(obj, wd->scroller);
193    wd->entry = elm_entry_add(parent);
194    elm_entry_item_provider_prepend(wd->entry, _item_provider, obj);
195    elm_entry_editable_set(wd->entry, 0);
196    evas_object_size_hint_weight_set(wd->entry, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
197    evas_object_size_hint_align_set(wd->entry, EVAS_HINT_FILL, EVAS_HINT_FILL);
198    elm_scroller_content_set(wd->scroller, wd->entry);
199    evas_object_show(wd->entry);
200
201    evas_object_event_callback_add(wd->entry, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
202                                   _changed_size_hints, obj);
203
204    elm_entry_entry_set(wd->entry, "");
205
206    evas_object_smart_callback_add(wd->entry, "anchor,clicked",
207                                   _anchor_clicked, obj);
208
209    _sizing_eval(obj);
210
211    // TODO: convert Elementary to subclassing of Evas_Smart_Class
212    // TODO: and save some bytes, making descriptions per-class and not instance!
213    evas_object_smart_callbacks_descriptions_set(obj, _signals);
214    return obj;
215 }
216
217 /**
218   * Set the text markup of the anchorview
219   *
220   * This sets the text of the anchorview to be the text given as @p text. This
221   * text is in markup format with \<a href=XXX\> beginning an achor with the
222   * string link of 'XXX', and \</\> or \</a\> ending the link. Other markup can
223   * be used dependign on the style support.
224   *
225   * @param obj The anchorview object
226   * @param text The text to set, or NULL to clear
227   *
228   * @ingroup Anchorview
229   */
230 EAPI void
231 elm_anchorview_text_set(Evas_Object *obj, const char *text)
232 {
233    ELM_CHECK_WIDTYPE(obj, widtype);
234    Widget_Data *wd = elm_widget_data_get(obj);
235    if (!wd) return;
236    elm_entry_entry_set(wd->entry, text);
237    if (wd->hover) evas_object_del(wd->hover);
238    if (wd->pop) evas_object_del(wd->pop);
239    wd->hover = NULL;
240    wd->pop = NULL;
241    _sizing_eval(obj);
242 }
243
244 /**
245   * Get the markup text set for the anchorview
246   *
247   * This retrieves back the string set by @c elm_anchorview_text_set().
248   *
249   * @param obj The anchorview object
250   * @return text The markup text set or @c NULL, either if it was not set
251   * or an error occurred
252   *
253   * @ingroup Anchorview
254   */
255 EAPI const char*
256 elm_anchorview_text_get(const Evas_Object *obj)
257 {
258    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
259    Widget_Data *wd = elm_widget_data_get(obj);
260    if (!wd) return NULL;
261    return elm_entry_entry_get(wd->entry);
262 }
263
264 /**
265   * Set the parent of the hover popup
266   *
267   * This sets the parent of the hover that anchorview will create. See hover
268   * objects for more information on this.
269   *
270   * @param obj The anchorview object
271   * @param parent The parent the hover should use
272   *
273   * @ingroup Anchorview
274   */
275 EAPI void
276 elm_anchorview_hover_parent_set(Evas_Object *obj, Evas_Object *parent)
277 {
278    ELM_CHECK_WIDTYPE(obj, widtype);
279    Widget_Data *wd = elm_widget_data_get(obj);
280    if (!wd) return;
281    if (wd->hover_parent)
282      evas_object_event_callback_del_full(wd->hover_parent, EVAS_CALLBACK_DEL, _parent_del, obj);
283    wd->hover_parent = parent;
284    if (wd->hover_parent)
285      evas_object_event_callback_add(wd->hover_parent, EVAS_CALLBACK_DEL, _parent_del, obj);
286 }
287
288 /**
289   * Get the parent of the hover popup
290   *
291   * This gets the parent of the hover that anchorview will created. See hover
292   * objects for more information on this.
293   *
294   * @param obj The anchorview object
295   * @return The parent used by hover
296   *
297   * @ingroup Anchorview
298   */
299 EAPI Evas_Object *
300 elm_anchorview_hover_parent_get(const Evas_Object *obj)
301 {
302    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
303    Widget_Data *wd = elm_widget_data_get(obj);
304    if (!wd) return NULL;
305    return wd->hover_parent;
306 }
307
308 /**
309   * Set the style that the hover should use
310   *
311   * This sets the style for the hover that anchorview will create. See hover
312   * objects for more information
313   *
314   * @param obj The anchorview object
315   * @param style The style to use
316   *
317   * @ingroup Anchorview
318   */
319 EAPI void
320 elm_anchorview_hover_style_set(Evas_Object *obj, const char *style)
321 {
322    ELM_CHECK_WIDTYPE(obj, widtype);
323    Widget_Data *wd = elm_widget_data_get(obj);
324    if (!wd) return;
325    eina_stringshare_replace(&wd->hover_style, style);
326 }
327
328 /**
329  * Get the style that the hover should use
330  *
331  * This gets the style for the hover that anchorview will create. See hover
332  * objects for more information
333  *
334  * @param obj The anchorview object
335  * @return The style defined
336  *
337  * @ingroup Anchorview
338  */
339 EAPI const char *
340 elm_anchorview_hover_style_get(const Evas_Object *obj)
341 {
342    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
343    Widget_Data *wd = elm_widget_data_get(obj);
344    if (!wd) return NULL;
345    return wd->hover_style;
346 }
347
348 /**
349   * Stop the hover popup in the anchorview
350   *
351   * This will stop the hover popup in the anchorview if it is currently active.
352   *
353   * @param obj The anchorview object
354   *
355   * @ingroup Anchorview
356   */
357 EAPI void
358 elm_anchorview_hover_end(Evas_Object *obj)
359 {
360    ELM_CHECK_WIDTYPE(obj, widtype);
361    Widget_Data *wd = elm_widget_data_get(obj);
362    if (!wd) return;
363    if (wd->hover) evas_object_del(wd->hover);
364    if (wd->pop) evas_object_del(wd->pop);
365    wd->hover = NULL;
366    wd->pop = NULL;
367 }
368
369 /**
370  * Set bounce mode
371  *
372  * This will enable or disable the scroller bounce mode for the anchorview. See
373  * elm_scroller_bounce_set() for details
374  *
375  * @param obj The anchorview anchorview
376  * @param h_bounce Allow bounce horizontally
377  * @param v_bounce Allow bounce vertically
378  *
379  * @ingroup Anchorview
380  */
381 EAPI void
382 elm_anchorview_bounce_set(Evas_Object *obj, Eina_Bool h_bounce, Eina_Bool v_bounce)
383 {
384    ELM_CHECK_WIDTYPE(obj, widtype);
385    Widget_Data *wd = elm_widget_data_get(obj);
386    if (!wd) return;
387    elm_scroller_bounce_set(wd->scroller, h_bounce, v_bounce);
388 }
389
390 /**
391  * Get the bounce mode
392  *
393  * @param obj The Anchorview object
394  * @param h_bounce Allow bounce horizontally
395  * @param v_bounce Allow bounce vertically
396  *
397  * @ingroup Anchorview
398  */
399 EAPI void
400 elm_anchorview_bounce_get(const Evas_Object *obj, Eina_Bool *h_bounce, Eina_Bool *v_bounce)
401 {
402    ELM_CHECK_WIDTYPE(obj, widtype);
403    Widget_Data *wd = elm_widget_data_get(obj);
404    if (!wd) return;
405    elm_scroller_bounce_get(wd->scroller, h_bounce, v_bounce);
406 }
407
408 /**
409  * This appends a custom item provider to the list for that anchorview
410  *
411  * This appends the given callback. The list is walked from beginning to end
412  * with each function called given the item href string in the text. If the
413  * function returns an object handle other than NULL (it should create an
414  * and object to do this), then this object is used to replace that item. If
415  * not the next provider is called until one provides an item object, or the
416  * default provider in anchorview does.
417  * 
418  * @param obj The anchorview object
419  * @param func The function called to provide the item object
420  * @param data The data passed to @p func
421  *
422  * @ingroup Anchorview
423  */
424 EAPI void
425 elm_anchorview_item_provider_append(Evas_Object *obj, Evas_Object *(*func) (void *data, Evas_Object *anchorview, const char *item), void *data)
426 {
427    ELM_CHECK_WIDTYPE(obj, widtype);
428    Widget_Data *wd = elm_widget_data_get(obj);
429    if (!wd) return;
430    EINA_SAFETY_ON_NULL_RETURN(func);
431    Elm_Anchorview_Item_Provider *ip = calloc(1, sizeof(Elm_Anchorview_Item_Provider));
432    if (!ip) return;
433    ip->func = func;
434    ip->data = data;
435    wd->item_providers = eina_list_append(wd->item_providers, ip);
436 }
437
438 /**
439  * This prepends a custom item provider to the list for that anchorview
440  *
441  * This prepends the given callback. See elm_anchorview_item_provider_append() for
442  * more information
443  * 
444  * @param obj The anchorview object
445  * @param func The function called to provide the item object
446  * @param data The data passed to @p func
447  *
448  * @ingroup Anchorview
449  */
450 EAPI void
451 elm_anchorview_item_provider_prepend(Evas_Object *obj, Evas_Object *(*func) (void *data, Evas_Object *anchorview, const char *item), void *data)
452 {
453    ELM_CHECK_WIDTYPE(obj, widtype);
454    Widget_Data *wd = elm_widget_data_get(obj);
455    if (!wd) return;
456    EINA_SAFETY_ON_NULL_RETURN(func);
457    Elm_Anchorview_Item_Provider *ip = calloc(1, sizeof(Elm_Anchorview_Item_Provider));
458    if (!ip) return;
459    ip->func = func;
460    ip->data = data;
461    wd->item_providers = eina_list_prepend(wd->item_providers, ip);
462 }
463
464 /**
465  * This removes a custom item provider to the list for that anchorview
466  *
467  * This removes the given callback. See elm_anchorview_item_provider_append() for
468  * more information
469  * 
470  * @param obj The anchorview object
471  * @param func The function called to provide the item object
472  * @param data The data passed to @p func
473  *
474  * @ingroup Anchorview
475  */
476 EAPI void
477 elm_anchorview_item_provider_remove(Evas_Object *obj, Evas_Object *(*func) (void *data, Evas_Object *anchorview, const char *item), void *data)
478 {
479    ELM_CHECK_WIDTYPE(obj, widtype);
480    Widget_Data *wd = elm_widget_data_get(obj);
481    Eina_List *l;
482    Elm_Anchorview_Item_Provider *ip;
483    if (!wd) return;
484    EINA_SAFETY_ON_NULL_RETURN(func);
485    EINA_LIST_FOREACH(wd->item_providers, l, ip)
486      {
487         if ((ip->func == func) && (ip->data == data))
488           {
489              wd->item_providers = eina_list_remove_list(wd->item_providers, l);
490              free(ip);
491              return;
492           }
493      }
494 }