Merge "[Password]: New design based changes, a new style removed password mode contro...
[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 static void _mirrored_set(Evas_Object *obj, Eina_Bool rtl);
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 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
80 {
81    Widget_Data *wd = elm_widget_data_get(obj);
82    if (!wd) return;
83    elm_widget_mirrored_set(wd->hover, rtl);
84 }
85
86 static void
87 _theme_hook(Evas_Object *obj)
88 {
89    Widget_Data *wd = elm_widget_data_get(obj);
90    if (!wd) return;
91    _elm_widget_mirrored_reload(obj);
92    _mirrored_set(obj, elm_widget_mirrored_get(obj));
93 }
94
95 static void
96 _sizing_eval(Evas_Object *obj)
97 {
98    Widget_Data *wd = elm_widget_data_get(obj);
99    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
100    if (!wd) return;
101    evas_object_size_hint_min_get(wd->entry, &minw, &minh);
102    evas_object_size_hint_min_set(obj, minw, minh);
103    evas_object_size_hint_max_set(obj, maxw, maxh);
104 }
105
106 static void
107 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
108 {
109    _sizing_eval(data);
110 }
111
112 static void
113 _hover_clicked(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
114 {
115    elm_anchorblock_hover_end(data);
116 }
117
118 static void
119 _anchor_clicked(void *data, Evas_Object *obj, void *event_info)
120 {
121    Widget_Data *wd = elm_widget_data_get(data);
122    Elm_Entry_Anchor_Info *info = event_info;
123    Evas_Object *hover_parent;
124    Elm_Entry_Anchorblock_Info ei;
125    Evas_Coord x, w, y, h, px, py;
126    if (!wd) return;
127    wd->pop = elm_icon_add(obj);
128    evas_object_move(wd->pop, info->x, info->y);
129    evas_object_resize(wd->pop, info->w, info->h);
130    wd->hover = elm_hover_add(obj);
131    elm_widget_mirrored_set(wd->hover, elm_widget_mirrored_get((Evas_Object *) data));
132    if (wd->hover_style)
133      elm_object_style_set(wd->hover, wd->hover_style);
134    hover_parent = wd->hover_parent;
135    if (!hover_parent) hover_parent = obj;
136    elm_hover_parent_set(wd->hover, hover_parent);
137    elm_hover_target_set(wd->hover, wd->pop);
138    ei.name = info->name;
139    ei.button = info->button;
140    ei.hover = wd->hover;
141    ei.anchor.x = info->x;
142    ei.anchor.y = info->y;
143    ei.anchor.w = info->w;
144    ei.anchor.h = info->h;
145    evas_object_geometry_get(hover_parent, &x, &y, &w, &h);
146    ei.hover_parent.x = x;
147    ei.hover_parent.y = y;
148    ei.hover_parent.w = w;
149    ei.hover_parent.h = h;
150    px = info->x + (info->w / 2);
151    py = info->y + (info->h / 2);
152    ei.hover_left = 1;
153    if (px < (x + (w / 3))) ei.hover_left = 0;
154    ei.hover_right = 1;
155    if (px > (x + ((w * 2) / 3))) ei.hover_right = 0;
156    ei.hover_top = 1;
157    if (py < (y + (h / 3))) ei.hover_top = 0;
158    ei.hover_bottom = 1;
159    if (py > (y + ((h * 2) / 3))) ei.hover_bottom = 0;
160
161    if (elm_widget_mirrored_get(wd->hover))
162      {  /* Swap right and left because they switch sides in RTL */
163         Eina_Bool tmp = ei.hover_left;
164         ei.hover_left = ei.hover_right;
165         ei.hover_right = tmp;
166      }
167
168    evas_object_smart_callback_call(data, SIG_ANCHOR_CLICKED, &ei);
169    evas_object_smart_callback_add(wd->hover, "clicked", _hover_clicked, data);
170    evas_object_show(wd->hover);
171 }
172
173 static void
174 _parent_del(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
175 {
176    Widget_Data *wd = elm_widget_data_get(data);
177    if (!wd) return;
178    wd->hover_parent = NULL;
179 }
180
181 static Evas_Object *
182 _item_provider(void *data, Evas_Object *entry __UNUSED__, const char *item)
183 {
184    Widget_Data *wd = elm_widget_data_get(data);
185    Eina_List *l;
186    Elm_Anchorblock_Item_Provider *ip;
187
188    EINA_LIST_FOREACH(wd->item_providers, l, ip)
189      {
190         Evas_Object *o;
191
192         o = ip->func(ip->data, data, item);
193         if (o) return o;
194      }
195    return NULL;
196 }
197
198 /**
199  * Add a new Anchorblock object
200  *
201  * @param parent The parent object
202  * @return The new object or NULL if it cannot be created
203  *
204  * @ingroup Anchorblock
205  */
206 EAPI Evas_Object *
207 elm_anchorblock_add(Evas_Object *parent)
208 {
209    Evas_Object *obj;
210    Evas *e;
211    Widget_Data *wd;
212
213    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
214
215    ELM_SET_WIDTYPE(widtype, "anchorblock");
216    elm_widget_type_set(obj, "anchorblock");
217    elm_widget_sub_object_add(parent, obj);
218    elm_widget_data_set(obj, wd);
219    elm_widget_del_pre_hook_set(obj, _del_pre_hook);
220    elm_widget_del_hook_set(obj, _del_hook);
221    elm_widget_theme_hook_set(obj, _theme_hook);
222    elm_widget_can_focus_set(obj, EINA_TRUE);
223
224    wd->entry = elm_entry_add(parent);
225    elm_entry_item_provider_prepend(wd->entry, _item_provider, obj);
226    elm_widget_resize_object_set(obj, wd->entry);
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
231    evas_object_event_callback_add(wd->entry, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
232                                   _changed_size_hints, obj);
233
234    elm_entry_entry_set(wd->entry, "");
235
236    evas_object_smart_callback_add(wd->entry, "anchor,clicked",
237                                   _anchor_clicked, obj);
238
239    _mirrored_set(obj, elm_widget_mirrored_get(obj));
240    _sizing_eval(obj);
241
242    // TODO: convert Elementary to subclassing of Evas_Smart_Class
243    // TODO: and save some bytes, making descriptions per-class and not instance!
244    evas_object_smart_callbacks_descriptions_set(obj, _signals);
245    return obj;
246 }
247
248 /**
249  * Set the text markup of the anchorblock
250  *
251  * This sets the text of the anchorblock to be the text given as @p text. This
252  * text is in markup format with \<a href=XXX\> beginning an achor with the
253  * string link of 'XXX', and \</\> or \</a\> ending the link. Other markup can
254  * be used dependign on the style support.
255  *
256  * @param obj The anchorblock object
257  * @param text The text to set, or NULL to clear
258  *
259  * @ingroup Anchorblock
260  */
261 EAPI void
262 elm_anchorblock_text_set(Evas_Object *obj, const char *text)
263 {
264    ELM_CHECK_WIDTYPE(obj, widtype);
265    Widget_Data *wd = elm_widget_data_get(obj);
266    if (!wd) return;
267    elm_entry_entry_set(wd->entry, text);
268    if (wd->hover) evas_object_del(wd->hover);
269    if (wd->pop) evas_object_del(wd->pop);
270    wd->hover = NULL;
271    wd->pop = NULL;
272    _sizing_eval(obj);
273 }
274
275 /**
276  * Get the markup text set for the anchorblock
277  *
278  * This retrieves back the string set by @c elm_anchorblock_text_set().
279  *
280  * @param obj The anchorblock object
281  * @return text The markup text set or @c NULL, either if it was not set
282  * or an error occurred
283  *
284  * @ingroup Anchorblock
285  */
286 EAPI const char*
287 elm_anchorblock_text_get(const Evas_Object *obj)
288 {
289    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
290    Widget_Data *wd = elm_widget_data_get(obj);
291    if (!wd) return NULL;
292    return elm_entry_entry_get(wd->entry);
293 }
294
295 /**
296  * Set the parent of the hover popup
297  *
298  * This sets the parent of the hover that anchorblock will create. See hover
299  * objects for more information on this.
300  *
301  * @param obj The anchorblock object
302  * @param parent The parent the hover should use
303  *
304  * @ingroup Anchorblock
305  */
306 EAPI void
307 elm_anchorblock_hover_parent_set(Evas_Object *obj, Evas_Object *parent)
308 {
309    ELM_CHECK_WIDTYPE(obj, widtype);
310    Widget_Data *wd = elm_widget_data_get(obj);
311    if (!wd) return;
312    if (wd->hover_parent)
313      evas_object_event_callback_del_full(wd->hover_parent, EVAS_CALLBACK_DEL, _parent_del, obj);
314    wd->hover_parent = parent;
315    if (wd->hover_parent)
316      evas_object_event_callback_add(wd->hover_parent, EVAS_CALLBACK_DEL, _parent_del, obj);
317 }
318
319 /**
320  * Get the parent of the hover popup
321  *
322  * This sgets the parent of the hover that anchorblock will create. See hover
323  * objects for more information on this.
324  *
325  * @param obj The anchorblock object
326  * @return The parent used by the hover
327  *
328  * @ingroup Anchorblock
329  */
330 EAPI Evas_Object *
331 elm_anchorblock_hover_parent_get(const Evas_Object *obj)
332 {
333    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
334    Widget_Data *wd = elm_widget_data_get(obj);
335    if (!wd) return NULL;
336    return wd->hover_parent;
337 }
338
339 /**
340  * Set the style that the hover should use
341  *
342  * This sets the style for the hover that anchorblock will create. See hover
343  * objects for more information
344  *
345  * @param obj The anchorblock object
346  * @param style The style to use
347  *
348  * @ingroup Anchorblock
349  */
350 EAPI void
351 elm_anchorblock_hover_style_set(Evas_Object *obj, const char *style)
352 {
353    ELM_CHECK_WIDTYPE(obj, widtype);
354    Widget_Data *wd = elm_widget_data_get(obj);
355    if (!wd) return;
356    eina_stringshare_replace(&wd->hover_style, style);
357 }
358
359 /**
360  * Get the style that the hover should use
361  *
362  * This gets the style for the hover that anchorblock will create. See hover
363  * objects for more information
364  *
365  * @param obj The anchorblock object
366  * @return The style defined
367  *
368  * @ingroup Anchorblock
369  */
370 EAPI const char *
371 elm_anchorblock_hover_style_get(const Evas_Object *obj)
372 {
373    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
374    Widget_Data *wd = elm_widget_data_get(obj);
375    if (!wd) return NULL;
376    return wd->hover_style;
377 }
378
379 /**
380  * Stop the hover popup in the anchorblock
381  *
382  * This will stop the hover popup in the anchorblock if it is currently active.
383  *
384  * @param obj The anchorblock object
385  *
386  * @ingroup Anchorblock
387  */
388 EAPI void
389 elm_anchorblock_hover_end(Evas_Object *obj)
390 {
391    ELM_CHECK_WIDTYPE(obj, widtype);
392    Widget_Data *wd = elm_widget_data_get(obj);
393    if (!wd) return;
394    if (wd->hover) evas_object_del(wd->hover);
395    if (wd->pop) evas_object_del(wd->pop);
396    wd->hover = NULL;
397    wd->pop = NULL;
398 }
399
400 /**
401  * This appends a custom item provider to the list for that anchorblock
402  *
403  * This appends the given callback. The list is walked from beginning to end
404  * with each function called given the item href string in the text. If the
405  * function returns an object handle other than NULL (it should create an
406  * and object to do this), then this object is used to replace that item. If
407  * not the next provider is called until one provides an item object, or the
408  * default provider in anchorblock does.
409  *
410  * @param obj The anchorblock object
411  * @param func The function called to provide the item object
412  * @param data The data passed to @p func
413  *
414  * @ingroup Anchorblock
415  */
416 EAPI void
417 elm_anchorblock_item_provider_append(Evas_Object *obj, Evas_Object *(*func) (void *data, Evas_Object *anchorblock, const char *item), void *data)
418 {
419    ELM_CHECK_WIDTYPE(obj, widtype);
420    Widget_Data *wd = elm_widget_data_get(obj);
421    if (!wd) return;
422    EINA_SAFETY_ON_NULL_RETURN(func);
423    Elm_Anchorblock_Item_Provider *ip = calloc(1, sizeof(Elm_Anchorblock_Item_Provider));
424    if (!ip) return;
425    ip->func = func;
426    ip->data = data;
427    wd->item_providers = eina_list_append(wd->item_providers, ip);
428 }
429
430 /**
431  * This prepends a custom item provider to the list for that anchorblock
432  *
433  * This prepends the given callback. See elm_anchorblock_item_provider_append() for
434  * more information
435  *
436  * @param obj The anchorblock object
437  * @param func The function called to provide the item object
438  * @param data The data passed to @p func
439  *
440  * @ingroup Anchorblock
441  */
442 EAPI void
443 elm_anchorblock_item_provider_prepend(Evas_Object *obj, Evas_Object *(*func) (void *data, Evas_Object *anchorblock, const char *item), void *data)
444 {
445    ELM_CHECK_WIDTYPE(obj, widtype);
446    Widget_Data *wd = elm_widget_data_get(obj);
447    if (!wd) return;
448    EINA_SAFETY_ON_NULL_RETURN(func);
449    Elm_Anchorblock_Item_Provider *ip = calloc(1, sizeof(Elm_Anchorblock_Item_Provider));
450    if (!ip) return;
451    ip->func = func;
452    ip->data = data;
453    wd->item_providers = eina_list_prepend(wd->item_providers, ip);
454 }
455
456 /**
457  * This removes a custom item provider to the list for that anchorblock
458  *
459  * This removes the given callback. See elm_anchorblock_item_provider_append() for
460  * more information
461  *
462  * @param obj The anchorblock object
463  * @param func The function called to provide the item object
464  * @param data The data passed to @p func
465  *
466  * @ingroup Anchorblock
467  */
468 EAPI void
469 elm_anchorblock_item_provider_remove(Evas_Object *obj, Evas_Object *(*func) (void *data, Evas_Object *anchorblock, const char *item), void *data)
470 {
471    ELM_CHECK_WIDTYPE(obj, widtype);
472    Widget_Data *wd = elm_widget_data_get(obj);
473    Eina_List *l;
474    Elm_Anchorblock_Item_Provider *ip;
475    if (!wd) return;
476    EINA_SAFETY_ON_NULL_RETURN(func);
477    EINA_LIST_FOREACH(wd->item_providers, l, ip)
478      {
479         if ((ip->func == func) && (ip->data == data))
480           {
481              wd->item_providers = eina_list_remove_list(wd->item_providers, l);
482              free(ip);
483              return;
484           }
485      }
486 }