1 #include <Elementary.h>
5 * @defgroup Anchorblock Anchorblock
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.
13 * Signals that you can add callbacks for are:
15 * anchor,clicked - anchor called was clicked. event_info is anchor info -
16 * Elm_Entry_Anchorview_Info
18 typedef struct _Widget_Data Widget_Data;
19 typedef struct _Elm_Anchorblock_Item_Provider Elm_Anchorblock_Item_Provider;
24 Evas_Object *hover_parent;
25 Evas_Object *pop, *hover;
26 Eina_List *item_providers;
27 const char *hover_style;
30 struct _Elm_Anchorblock_Item_Provider
32 Evas_Object *(*func) (void *data, Evas_Object *anchorblock, const char *item);
36 static const char *widtype = NULL;
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
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);
54 _del_pre_hook(Evas_Object *obj)
56 Widget_Data *wd = elm_widget_data_get(obj);
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);
65 _del_hook(Evas_Object *obj)
67 Widget_Data *wd = elm_widget_data_get(obj);
68 Elm_Anchorblock_Item_Provider *ip;
70 if (wd->hover_style) eina_stringshare_del(wd->hover_style);
71 EINA_LIST_FREE(wd->item_providers, ip)
79 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
81 Widget_Data *wd = elm_widget_data_get(obj);
83 elm_widget_mirrored_set(wd->hover, rtl);
87 _theme_hook(Evas_Object *obj)
89 Widget_Data *wd = elm_widget_data_get(obj);
91 _elm_widget_mirrored_reload(obj);
92 _mirrored_set(obj, elm_widget_mirrored_get(obj));
96 _sizing_eval(Evas_Object *obj)
98 Widget_Data *wd = elm_widget_data_get(obj);
99 Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
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);
107 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
113 _hover_clicked(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
115 elm_anchorblock_hover_end(data);
119 _anchor_clicked(void *data, Evas_Object *obj, void *event_info)
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;
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));
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);
153 if (px < (x + (w / 3))) ei.hover_left = 0;
155 if (px > (x + ((w * 2) / 3))) ei.hover_right = 0;
157 if (py < (y + (h / 3))) ei.hover_top = 0;
159 if (py > (y + ((h * 2) / 3))) ei.hover_bottom = 0;
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;
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);
174 _parent_del(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
176 Widget_Data *wd = elm_widget_data_get(data);
178 wd->hover_parent = NULL;
182 _item_provider(void *data, Evas_Object *entry __UNUSED__, const char *item)
184 Widget_Data *wd = elm_widget_data_get(data);
186 Elm_Anchorblock_Item_Provider *ip;
188 EINA_LIST_FOREACH(wd->item_providers, l, ip)
192 o = ip->func(ip->data, data, item);
199 * Add a new Anchorblock object
201 * @param parent The parent object
202 * @return The new object or NULL if it cannot be created
204 * @ingroup Anchorblock
207 elm_anchorblock_add(Evas_Object *parent)
213 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
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);
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);
231 evas_object_event_callback_add(wd->entry, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
232 _changed_size_hints, obj);
234 elm_entry_entry_set(wd->entry, "");
236 evas_object_smart_callback_add(wd->entry, "anchor,clicked",
237 _anchor_clicked, obj);
239 _mirrored_set(obj, elm_widget_mirrored_get(obj));
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);
249 * Set the text markup of the anchorblock
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.
256 * @param obj The anchorblock object
257 * @param text The text to set, or NULL to clear
259 * @ingroup Anchorblock
262 elm_anchorblock_text_set(Evas_Object *obj, const char *text)
264 ELM_CHECK_WIDTYPE(obj, widtype);
265 Widget_Data *wd = elm_widget_data_get(obj);
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);
276 * Get the markup text set for the anchorblock
278 * This retrieves back the string set by @c elm_anchorblock_text_set().
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
284 * @ingroup Anchorblock
287 elm_anchorblock_text_get(const Evas_Object *obj)
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);
296 * Set the parent of the hover popup
298 * This sets the parent of the hover that anchorblock will create. See hover
299 * objects for more information on this.
301 * @param obj The anchorblock object
302 * @param parent The parent the hover should use
304 * @ingroup Anchorblock
307 elm_anchorblock_hover_parent_set(Evas_Object *obj, Evas_Object *parent)
309 ELM_CHECK_WIDTYPE(obj, widtype);
310 Widget_Data *wd = elm_widget_data_get(obj);
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);
320 * Get the parent of the hover popup
322 * This sgets the parent of the hover that anchorblock will create. See hover
323 * objects for more information on this.
325 * @param obj The anchorblock object
326 * @return The parent used by the hover
328 * @ingroup Anchorblock
331 elm_anchorblock_hover_parent_get(const Evas_Object *obj)
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;
340 * Set the style that the hover should use
342 * This sets the style for the hover that anchorblock will create. See hover
343 * objects for more information
345 * @param obj The anchorblock object
346 * @param style The style to use
348 * @ingroup Anchorblock
351 elm_anchorblock_hover_style_set(Evas_Object *obj, const char *style)
353 ELM_CHECK_WIDTYPE(obj, widtype);
354 Widget_Data *wd = elm_widget_data_get(obj);
356 eina_stringshare_replace(&wd->hover_style, style);
360 * Get the style that the hover should use
362 * This gets the style for the hover that anchorblock will create. See hover
363 * objects for more information
365 * @param obj The anchorblock object
366 * @return The style defined
368 * @ingroup Anchorblock
371 elm_anchorblock_hover_style_get(const Evas_Object *obj)
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;
380 * Stop the hover popup in the anchorblock
382 * This will stop the hover popup in the anchorblock if it is currently active.
384 * @param obj The anchorblock object
386 * @ingroup Anchorblock
389 elm_anchorblock_hover_end(Evas_Object *obj)
391 ELM_CHECK_WIDTYPE(obj, widtype);
392 Widget_Data *wd = elm_widget_data_get(obj);
394 if (wd->hover) evas_object_del(wd->hover);
395 if (wd->pop) evas_object_del(wd->pop);
401 * This appends a custom item provider to the list for that anchorblock
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.
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
414 * @ingroup Anchorblock
417 elm_anchorblock_item_provider_append(Evas_Object *obj, Evas_Object *(*func) (void *data, Evas_Object *anchorblock, const char *item), void *data)
419 ELM_CHECK_WIDTYPE(obj, widtype);
420 Widget_Data *wd = elm_widget_data_get(obj);
422 EINA_SAFETY_ON_NULL_RETURN(func);
423 Elm_Anchorblock_Item_Provider *ip = calloc(1, sizeof(Elm_Anchorblock_Item_Provider));
427 wd->item_providers = eina_list_append(wd->item_providers, ip);
431 * This prepends a custom item provider to the list for that anchorblock
433 * This prepends the given callback. See elm_anchorblock_item_provider_append() for
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
440 * @ingroup Anchorblock
443 elm_anchorblock_item_provider_prepend(Evas_Object *obj, Evas_Object *(*func) (void *data, Evas_Object *anchorblock, const char *item), void *data)
445 ELM_CHECK_WIDTYPE(obj, widtype);
446 Widget_Data *wd = elm_widget_data_get(obj);
448 EINA_SAFETY_ON_NULL_RETURN(func);
449 Elm_Anchorblock_Item_Provider *ip = calloc(1, sizeof(Elm_Anchorblock_Item_Provider));
453 wd->item_providers = eina_list_prepend(wd->item_providers, ip);
457 * This removes a custom item provider to the list for that anchorblock
459 * This removes the given callback. See elm_anchorblock_item_provider_append() for
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
466 * @ingroup Anchorblock
469 elm_anchorblock_item_provider_remove(Evas_Object *obj, Evas_Object *(*func) (void *data, Evas_Object *anchorblock, const char *item), void *data)
471 ELM_CHECK_WIDTYPE(obj, widtype);
472 Widget_Data *wd = elm_widget_data_get(obj);
474 Elm_Anchorblock_Item_Provider *ip;
476 EINA_SAFETY_ON_NULL_RETURN(func);
477 EINA_LIST_FOREACH(wd->item_providers, l, ip)
479 if ((ip->func == func) && (ip->data == data))
481 wd->item_providers = eina_list_remove_list(wd->item_providers, l);