================================================================
[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 __UNUSED__, const char *item)
156 {
157    Widget_Data *wd = elm_widget_data_get(data);
158    Eina_List *l;
159    Elm_Anchorblock_Item_Provider *ip;
160
161    EINA_LIST_FOREACH(wd->item_providers, l, ip)
162      {
163         Evas_Object *o;
164
165         o = ip->func(ip->data, data, item);
166         if (o) return o;
167      }
168    return NULL;
169 }
170
171 /**
172  * Add a new Anchorblock object
173  *
174  * @param parent The parent object
175  * @return The new object or NULL if it cannot be created
176  *
177  * @ingroup Anchorblock
178  */
179 EAPI Evas_Object *
180 elm_anchorblock_add(Evas_Object *parent)
181 {
182    Evas_Object *obj;
183    Evas *e;
184    Widget_Data *wd;
185
186    EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
187
188    wd = ELM_NEW(Widget_Data);
189    e = evas_object_evas_get(parent);
190    if (!e) return NULL;
191    obj = elm_widget_add(e);
192    ELM_SET_WIDTYPE(widtype, "anchorblock");
193    elm_widget_type_set(obj, "anchorblock");
194    elm_widget_sub_object_add(parent, obj);
195    elm_widget_data_set(obj, wd);
196    elm_widget_del_pre_hook_set(obj, _del_pre_hook);
197    elm_widget_del_hook_set(obj, _del_hook);
198    elm_widget_can_focus_set(obj, EINA_TRUE);
199
200    wd->entry = elm_entry_add(parent);
201    elm_entry_item_provider_prepend(wd->entry, _item_provider, obj);
202    elm_widget_resize_object_set(obj, wd->entry);
203    elm_entry_editable_set(wd->entry, 0);
204    evas_object_size_hint_weight_set(wd->entry, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
205    evas_object_size_hint_align_set(wd->entry, EVAS_HINT_FILL, EVAS_HINT_FILL);
206
207    evas_object_event_callback_add(wd->entry, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
208                                   _changed_size_hints, obj);
209
210    elm_entry_entry_set(wd->entry, "");
211
212    evas_object_smart_callback_add(wd->entry, "anchor,clicked",
213                                   _anchor_clicked, obj);
214
215    _sizing_eval(obj);
216
217    // TODO: convert Elementary to subclassing of Evas_Smart_Class
218    // TODO: and save some bytes, making descriptions per-class and not instance!
219    evas_object_smart_callbacks_descriptions_set(obj, _signals);
220    return obj;
221 }
222
223 /**
224  * Set the text markup of the anchorblock
225  *
226  * This sets the text of the anchorblock to be the text given as @p text. This
227  * text is in markup format with \<a href=XXX\> beginning an achor with the
228  * string link of 'XXX', and \</\> or \</a\> ending the link. Other markup can
229  * be used dependign on the style support.
230  *
231  * @param obj The anchorblock object
232  * @param text The text to set, or NULL to clear
233  *
234  * @ingroup Anchorblock
235  */
236 EAPI void
237 elm_anchorblock_text_set(Evas_Object *obj, const char *text)
238 {
239    ELM_CHECK_WIDTYPE(obj, widtype);
240    Widget_Data *wd = elm_widget_data_get(obj);
241    if (!wd) return;
242    elm_entry_entry_set(wd->entry, text);
243    if (wd->hover) evas_object_del(wd->hover);
244    if (wd->pop) evas_object_del(wd->pop);
245    wd->hover = NULL;
246    wd->pop = NULL;
247    _sizing_eval(obj);
248 }
249
250 /**
251   * Get the markup text set for the anchorblock
252   *
253   * This retrieves back the string set by @c elm_anchorblock_text_set().
254   *
255   * @param obj The anchorblock object
256   * @return text The markup text set or @c NULL, either if it was not set
257   * or an error occurred
258   *
259   * @ingroup Anchorblock
260   */
261 EAPI const char*
262 elm_anchorblock_text_get(const Evas_Object *obj)
263 {
264    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
265    Widget_Data *wd = elm_widget_data_get(obj);
266    if (!wd) return NULL;
267    return elm_entry_entry_get(wd->entry);
268 }
269
270 /**
271  * Set the parent of the hover popup
272  *
273  * This sets the parent of the hover that anchorblock will create. See hover
274  * objects for more information on this.
275  *
276  * @param obj The anchorblock object
277  * @param parent The parent the hover should use
278  *
279  * @ingroup Anchorblock
280  */
281 EAPI void
282 elm_anchorblock_hover_parent_set(Evas_Object *obj, Evas_Object *parent)
283 {
284    ELM_CHECK_WIDTYPE(obj, widtype);
285    Widget_Data *wd = elm_widget_data_get(obj);
286    if (!wd) return;
287    if (wd->hover_parent)
288      evas_object_event_callback_del_full(wd->hover_parent, EVAS_CALLBACK_DEL, _parent_del, obj);
289    wd->hover_parent = parent;
290    if (wd->hover_parent)
291      evas_object_event_callback_add(wd->hover_parent, EVAS_CALLBACK_DEL, _parent_del, obj);
292 }
293
294 /**
295  * Get the parent of the hover popup
296  *
297  * This sgets the parent of the hover that anchorblock will create. See hover
298  * objects for more information on this.
299  *
300  * @param obj The anchorblock object
301  * @return The parent used by the hover
302  *
303  * @ingroup Anchorblock
304  */
305 EAPI Evas_Object *
306 elm_anchorblock_hover_parent_get(const Evas_Object *obj)
307 {
308    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
309    Widget_Data *wd = elm_widget_data_get(obj);
310    if (!wd) return NULL;
311    return wd->hover_parent;
312 }
313
314 /**
315  * Set the style that the hover should use
316  *
317  * This sets the style for the hover that anchorblock will create. See hover
318  * objects for more information
319  *
320  * @param obj The anchorblock object
321  * @param style The style to use
322  *
323  * @ingroup Anchorblock
324  */
325 EAPI void
326 elm_anchorblock_hover_style_set(Evas_Object *obj, const char *style)
327 {
328    ELM_CHECK_WIDTYPE(obj, widtype);
329    Widget_Data *wd = elm_widget_data_get(obj);
330    if (!wd) return;
331    eina_stringshare_replace(&wd->hover_style, style);
332 }
333
334 /**
335  * Get the style that the hover should use
336  *
337  * This gets the style for the hover that anchorblock will create. See hover
338  * objects for more information
339  *
340  * @param obj The anchorblock object
341  * @return The style defined
342  *
343  * @ingroup Anchorblock
344  */
345 EAPI const char *
346 elm_anchorblock_hover_style_get(const Evas_Object *obj)
347 {
348    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
349    Widget_Data *wd = elm_widget_data_get(obj);
350    if (!wd) return NULL;
351    return wd->hover_style;
352 }
353
354 /**
355  * Stop the hover popup in the anchorblock
356  *
357  * This will stop the hover popup in the anchorblock if it is currently active.
358  *
359  * @param obj The anchorblock object
360  *
361  * @ingroup Anchorblock
362  */
363 EAPI void
364 elm_anchorblock_hover_end(Evas_Object *obj)
365 {
366    ELM_CHECK_WIDTYPE(obj, widtype);
367    Widget_Data *wd = elm_widget_data_get(obj);
368    if (!wd) return;
369    if (wd->hover) evas_object_del(wd->hover);
370    if (wd->pop) evas_object_del(wd->pop);
371    wd->hover = NULL;
372    wd->pop = NULL;
373 }
374
375 /**
376  * This appends a custom item provider to the list for that anchorblock
377  *
378  * This appends the given callback. The list is walked from beginning to end
379  * with each function called given the item href string in the text. If the
380  * function returns an object handle other than NULL (it should create an
381  * and object to do this), then this object is used to replace that item. If
382  * not the next provider is called until one provides an item object, or the
383  * default provider in anchorblock does.
384  * 
385  * @param obj The anchorblock object
386  * @param func The function called to provide the item object
387  * @param data The data passed to @p func
388  *
389  * @ingroup Anchorblock
390  */
391 EAPI void
392 elm_anchorblock_item_provider_append(Evas_Object *obj, Evas_Object *(*func) (void *data, Evas_Object *anchorblock, const char *item), void *data)
393 {
394    ELM_CHECK_WIDTYPE(obj, widtype);
395    Widget_Data *wd = elm_widget_data_get(obj);
396    if (!wd) return;
397    EINA_SAFETY_ON_NULL_RETURN(func);
398    Elm_Anchorblock_Item_Provider *ip = calloc(1, sizeof(Elm_Anchorblock_Item_Provider));
399    if (!ip) return;
400    ip->func = func;
401    ip->data = data;
402    wd->item_providers = eina_list_append(wd->item_providers, ip);
403 }
404
405 /**
406  * This prepends a custom item provider to the list for that anchorblock
407  *
408  * This prepends the given callback. See elm_anchorblock_item_provider_append() for
409  * more information
410  * 
411  * @param obj The anchorblock object
412  * @param func The function called to provide the item object
413  * @param data The data passed to @p func
414  *
415  * @ingroup Anchorblock
416  */
417 EAPI void
418 elm_anchorblock_item_provider_prepend(Evas_Object *obj, Evas_Object *(*func) (void *data, Evas_Object *anchorblock, const char *item), void *data)
419 {
420    ELM_CHECK_WIDTYPE(obj, widtype);
421    Widget_Data *wd = elm_widget_data_get(obj);
422    if (!wd) return;
423    EINA_SAFETY_ON_NULL_RETURN(func);
424    Elm_Anchorblock_Item_Provider *ip = calloc(1, sizeof(Elm_Anchorblock_Item_Provider));
425    if (!ip) return;
426    ip->func = func;
427    ip->data = data;
428    wd->item_providers = eina_list_prepend(wd->item_providers, ip);
429 }
430
431 /**
432  * This removes a custom item provider to the list for that anchorblock
433  *
434  * This removes the given callback. See elm_anchorblock_item_provider_append() for
435  * more information
436  * 
437  * @param obj The anchorblock object
438  * @param func The function called to provide the item object
439  * @param data The data passed to @p func
440  *
441  * @ingroup Anchorblock
442  */
443 EAPI void
444 elm_anchorblock_item_provider_remove(Evas_Object *obj, Evas_Object *(*func) (void *data, Evas_Object *anchorblock, const char *item), void *data)
445 {
446    ELM_CHECK_WIDTYPE(obj, widtype);
447    Widget_Data *wd = elm_widget_data_get(obj);
448    Eina_List *l;
449    Elm_Anchorblock_Item_Provider *ip;
450    if (!wd) return;
451    EINA_SAFETY_ON_NULL_RETURN(func);
452    EINA_LIST_FOREACH(wd->item_providers, l, ip)
453      {
454         if ((ip->func == func) && (ip->data == data))
455           {
456              wd->item_providers = eina_list_remove_list(wd->item_providers, l);
457              free(ip);
458              return;
459           }
460      }
461 }