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