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