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);
53 _del_pre_hook(Evas_Object *obj)
55 Widget_Data *wd = elm_widget_data_get(obj);
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);
64 _del_hook(Evas_Object *obj)
66 Widget_Data *wd = elm_widget_data_get(obj);
67 Elm_Anchorblock_Item_Provider *ip;
69 if (wd->hover_style) eina_stringshare_del(wd->hover_style);
70 EINA_LIST_FREE(wd->item_providers, ip)
78 _sizing_eval(Evas_Object *obj)
80 Widget_Data *wd = elm_widget_data_get(obj);
81 Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
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);
89 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
95 _hover_clicked(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
97 elm_anchorblock_hover_end(data);
101 _anchor_clicked(void *data, Evas_Object *obj, void *event_info)
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;
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);
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);
134 if (px < (x + (w / 3))) ei.hover_left = 0;
136 if (px > (x + ((w * 2) / 3))) ei.hover_right = 0;
138 if (py < (y + (h / 3))) ei.hover_top = 0;
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);
147 _parent_del(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
149 Widget_Data *wd = elm_widget_data_get(data);
151 wd->hover_parent = NULL;
155 _item_provider(void *data, Evas_Object *entry __UNUSED__, const char *item)
157 Widget_Data *wd = elm_widget_data_get(data);
159 Elm_Anchorblock_Item_Provider *ip;
161 EINA_LIST_FOREACH(wd->item_providers, l, ip)
165 o = ip->func(ip->data, data, item);
172 * Add a new Anchorblock object
174 * @param parent The parent object
175 * @return The new object or NULL if it cannot be created
177 * @ingroup Anchorblock
180 elm_anchorblock_add(Evas_Object *parent)
186 EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
188 wd = ELM_NEW(Widget_Data);
189 e = evas_object_evas_get(parent);
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);
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);
207 evas_object_event_callback_add(wd->entry, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
208 _changed_size_hints, obj);
210 elm_entry_entry_set(wd->entry, "");
212 evas_object_smart_callback_add(wd->entry, "anchor,clicked",
213 _anchor_clicked, obj);
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);
224 * Set the text markup of the anchorblock
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.
231 * @param obj The anchorblock object
232 * @param text The text to set, or NULL to clear
234 * @ingroup Anchorblock
237 elm_anchorblock_text_set(Evas_Object *obj, const char *text)
239 ELM_CHECK_WIDTYPE(obj, widtype);
240 Widget_Data *wd = elm_widget_data_get(obj);
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);
251 * Get the markup text set for the anchorblock
253 * This retrieves back the string set by @c elm_anchorblock_text_set().
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
259 * @ingroup Anchorblock
262 elm_anchorblock_text_get(const Evas_Object *obj)
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);
271 * Set the parent of the hover popup
273 * This sets the parent of the hover that anchorblock will create. See hover
274 * objects for more information on this.
276 * @param obj The anchorblock object
277 * @param parent The parent the hover should use
279 * @ingroup Anchorblock
282 elm_anchorblock_hover_parent_set(Evas_Object *obj, Evas_Object *parent)
284 ELM_CHECK_WIDTYPE(obj, widtype);
285 Widget_Data *wd = elm_widget_data_get(obj);
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);
295 * Get the parent of the hover popup
297 * This sgets the parent of the hover that anchorblock will create. See hover
298 * objects for more information on this.
300 * @param obj The anchorblock object
301 * @return The parent used by the hover
303 * @ingroup Anchorblock
306 elm_anchorblock_hover_parent_get(const Evas_Object *obj)
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;
315 * Set the style that the hover should use
317 * This sets the style for the hover that anchorblock will create. See hover
318 * objects for more information
320 * @param obj The anchorblock object
321 * @param style The style to use
323 * @ingroup Anchorblock
326 elm_anchorblock_hover_style_set(Evas_Object *obj, const char *style)
328 ELM_CHECK_WIDTYPE(obj, widtype);
329 Widget_Data *wd = elm_widget_data_get(obj);
331 eina_stringshare_replace(&wd->hover_style, style);
335 * Get the style that the hover should use
337 * This gets the style for the hover that anchorblock will create. See hover
338 * objects for more information
340 * @param obj The anchorblock object
341 * @return The style defined
343 * @ingroup Anchorblock
346 elm_anchorblock_hover_style_get(const Evas_Object *obj)
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;
355 * Stop the hover popup in the anchorblock
357 * This will stop the hover popup in the anchorblock if it is currently active.
359 * @param obj The anchorblock object
361 * @ingroup Anchorblock
364 elm_anchorblock_hover_end(Evas_Object *obj)
366 ELM_CHECK_WIDTYPE(obj, widtype);
367 Widget_Data *wd = elm_widget_data_get(obj);
369 if (wd->hover) evas_object_del(wd->hover);
370 if (wd->pop) evas_object_del(wd->pop);
376 * This appends a custom item provider to the list for that anchorblock
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.
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
389 * @ingroup Anchorblock
392 elm_anchorblock_item_provider_append(Evas_Object *obj, Evas_Object *(*func) (void *data, Evas_Object *anchorblock, const char *item), void *data)
394 ELM_CHECK_WIDTYPE(obj, widtype);
395 Widget_Data *wd = elm_widget_data_get(obj);
397 EINA_SAFETY_ON_NULL_RETURN(func);
398 Elm_Anchorblock_Item_Provider *ip = calloc(1, sizeof(Elm_Anchorblock_Item_Provider));
402 wd->item_providers = eina_list_append(wd->item_providers, ip);
406 * This prepends a custom item provider to the list for that anchorblock
408 * This prepends the given callback. See elm_anchorblock_item_provider_append() for
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
415 * @ingroup Anchorblock
418 elm_anchorblock_item_provider_prepend(Evas_Object *obj, Evas_Object *(*func) (void *data, Evas_Object *anchorblock, const char *item), void *data)
420 ELM_CHECK_WIDTYPE(obj, widtype);
421 Widget_Data *wd = elm_widget_data_get(obj);
423 EINA_SAFETY_ON_NULL_RETURN(func);
424 Elm_Anchorblock_Item_Provider *ip = calloc(1, sizeof(Elm_Anchorblock_Item_Provider));
428 wd->item_providers = eina_list_prepend(wd->item_providers, ip);
432 * This removes a custom item provider to the list for that anchorblock
434 * This removes the given callback. See elm_anchorblock_item_provider_append() for
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
441 * @ingroup Anchorblock
444 elm_anchorblock_item_provider_remove(Evas_Object *obj, Evas_Object *(*func) (void *data, Evas_Object *anchorblock, const char *item), void *data)
446 ELM_CHECK_WIDTYPE(obj, widtype);
447 Widget_Data *wd = elm_widget_data_get(obj);
449 Elm_Anchorblock_Item_Provider *ip;
451 EINA_SAFETY_ON_NULL_RETURN(func);
452 EINA_LIST_FOREACH(wd->item_providers, l, ip)
454 if ((ip->func == func) && (ip->data == data))
456 wd->item_providers = eina_list_remove_list(wd->item_providers, l);