1 #include <Elementary.h>
5 * @defgroup Anchorblock Anchorblock
8 * Anchorblock is for displaying tet that contains markup with anchors like:
9 * \<a href=1234\>something\</\> in it. These will be drawn differently and will
10 * be able to be clicked on by the user to display a popup. This popup then
11 * is intended to contain extra options such as "call", "add to contacts",
12 * "open web page" etc.
14 * Signals that you can add callbacks for are:
16 * anchor,clicked - anchor called was clicked. event_info is anchor info -
17 * Elm_Entry_Anchorview_Info
19 typedef struct _Widget_Data Widget_Data;
20 typedef struct _Elm_Anchorblock_Item_Provider Elm_Anchorblock_Item_Provider;
25 Evas_Object *hover_parent;
26 Evas_Object *pop, *hover;
27 Eina_List *item_providers;
28 const char *hover_style;
31 struct _Elm_Anchorblock_Item_Provider
33 Evas_Object *(*func) (void *data, Evas_Object *anchorblock, const char *item);
37 static const char *widtype = NULL;
39 static const char SIG_ANCHOR_CLICKED[] = "anchor,clicked";
40 static const Evas_Smart_Cb_Description _signals[] = {
41 {SIG_ANCHOR_CLICKED, ""}, /* TODO: declare the type properly, as data is
47 static void _del_pre_hook(Evas_Object *obj);
48 static void _del_hook(Evas_Object *obj);
49 static void _sizing_eval(Evas_Object *obj);
50 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
51 static void _parent_del(void *data, Evas *e, Evas_Object *obj, void *event_info);
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 _sizing_eval(Evas_Object *obj)
81 Widget_Data *wd = elm_widget_data_get(obj);
82 Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
84 evas_object_size_hint_min_get(wd->entry, &minw, &minh);
85 evas_object_size_hint_min_set(obj, minw, minh);
86 evas_object_size_hint_max_set(obj, maxw, maxh);
90 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
96 _hover_clicked(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
98 elm_anchorblock_hover_end(data);
102 _anchor_clicked(void *data, Evas_Object *obj, void *event_info)
104 Widget_Data *wd = elm_widget_data_get(data);
105 Elm_Entry_Anchor_Info *info = event_info;
106 Evas_Object *hover_parent;
107 Elm_Entry_Anchorblock_Info ei;
108 Evas_Coord x, w, y, h, px, py;
110 wd->pop = elm_icon_add(obj);
111 evas_object_move(wd->pop, info->x, info->y);
112 evas_object_resize(wd->pop, info->w, info->h);
113 wd->hover = elm_hover_add(obj);
115 elm_object_style_set(wd->hover, wd->hover_style);
116 hover_parent = wd->hover_parent;
117 if (!hover_parent) hover_parent = obj;
118 elm_hover_parent_set(wd->hover, hover_parent);
119 elm_hover_target_set(wd->hover, wd->pop);
120 ei.name = info->name;
121 ei.button = info->button;
122 ei.hover = wd->hover;
123 ei.anchor.x = info->x;
124 ei.anchor.y = info->y;
125 ei.anchor.w = info->w;
126 ei.anchor.h = info->h;
127 evas_object_geometry_get(hover_parent, &x, &y, &w, &h);
128 ei.hover_parent.x = x;
129 ei.hover_parent.y = y;
130 ei.hover_parent.w = w;
131 ei.hover_parent.h = h;
132 px = info->x + (info->w / 2);
133 py = info->y + (info->h / 2);
135 if (px < (x + (w / 3))) ei.hover_left = 0;
137 if (px > (x + ((w * 2) / 3))) ei.hover_right = 0;
139 if (py < (y + (h / 3))) ei.hover_top = 0;
141 if (py > (y + ((h * 2) / 3))) ei.hover_bottom = 0;
142 evas_object_smart_callback_call(data, SIG_ANCHOR_CLICKED, &ei);
143 evas_object_smart_callback_add(wd->hover, "clicked", _hover_clicked, data);
144 evas_object_show(wd->hover);
148 _parent_del(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
150 Widget_Data *wd = elm_widget_data_get(data);
152 wd->hover_parent = NULL;
156 _item_provider(void *data, Evas_Object *entry __UNUSED__, const char *item)
158 Widget_Data *wd = elm_widget_data_get(data);
160 Elm_Anchorblock_Item_Provider *ip;
162 EINA_LIST_FOREACH(wd->item_providers, l, ip)
166 o = ip->func(ip->data, data, item);
173 * Add a new Anchorblock object
175 * @param parent The parent object
176 * @return The new object or NULL if it cannot be created
178 * @ingroup Anchorblock
181 elm_anchorblock_add(Evas_Object *parent)
187 wd = ELM_NEW(Widget_Data);
188 e = evas_object_evas_get(parent);
189 obj = elm_widget_add(e);
190 ELM_SET_WIDTYPE(widtype, "anchorblock");
191 elm_widget_type_set(obj, "anchorblock");
192 elm_widget_sub_object_add(parent, obj);
193 elm_widget_data_set(obj, wd);
194 elm_widget_del_pre_hook_set(obj, _del_pre_hook);
195 elm_widget_del_hook_set(obj, _del_hook);
197 wd->entry = elm_entry_add(parent);
198 elm_entry_item_provider_prepend(wd->entry, _item_provider, obj);
199 elm_widget_resize_object_set(obj, wd->entry);
200 elm_entry_editable_set(wd->entry, 0);
201 evas_object_size_hint_weight_set(wd->entry, 1.0, 1.0);
202 evas_object_size_hint_align_set(wd->entry, -1.0, -1.0);
204 evas_object_event_callback_add(wd->entry, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
205 _changed_size_hints, obj);
207 elm_entry_entry_set(wd->entry, "");
209 evas_object_smart_callback_add(wd->entry, "anchor,clicked",
210 _anchor_clicked, obj);
214 // TODO: convert Elementary to subclassing of Evas_Smart_Class
215 // TODO: and save some bytes, making descriptions per-class and not instance!
216 evas_object_smart_callbacks_descriptions_set(obj, _signals);
221 * Set the text markup of the anchorblock
223 * This sets the text of the anchorblock to be the text given as @p text. This
224 * text is in markup format with \<a href=XXX\> beginning an achor with the
225 * string link of 'XXX', and \</\> or \</a\> ending the link. Other markup can
226 * be used dependign on the style support.
228 * @param obj The anchorblock object
229 * @param text The text to set, or NULL to clear
231 * @ingroup Anchorblock
234 elm_anchorblock_text_set(Evas_Object *obj, const char *text)
236 ELM_CHECK_WIDTYPE(obj, widtype);
237 Widget_Data *wd = elm_widget_data_get(obj);
239 elm_entry_entry_set(wd->entry, text);
240 if (wd->hover) evas_object_del(wd->hover);
241 if (wd->pop) evas_object_del(wd->pop);
248 * Get the markup text set for the anchorblock
250 * This retrieves back the string set by @c elm_anchorblock_text_set().
252 * @param obj The anchorblock object
253 * @return text The markup text set or @c NULL, either if it was not set
254 * or an error ocurred
256 * @ingroup Anchorblock
259 elm_anchorblock_text_get(const Evas_Object *obj)
261 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
262 Widget_Data *wd = elm_widget_data_get(obj);
263 if (!wd) return NULL;
264 return elm_entry_entry_get(wd->entry);
268 * Set the parent of the hover popup
270 * This sets the parent of the hover that anchorblock will create. See hover
271 * objects for more information on this.
273 * @param obj The anchorblock object
274 * @param parent The parent the hover should use
276 * @ingroup Anchorblock
279 elm_anchorblock_hover_parent_set(Evas_Object *obj, Evas_Object *parent)
281 ELM_CHECK_WIDTYPE(obj, widtype);
282 Widget_Data *wd = elm_widget_data_get(obj);
284 if (wd->hover_parent)
285 evas_object_event_callback_del_full(wd->hover_parent, EVAS_CALLBACK_DEL, _parent_del, obj);
286 wd->hover_parent = parent;
287 if (wd->hover_parent)
288 evas_object_event_callback_add(wd->hover_parent, EVAS_CALLBACK_DEL, _parent_del, obj);
292 * Set the style that the hover should use
294 * This sets the style for the hover that anchorblock will create. See hover
295 * objects for more information
297 * @param obj The anchorblock object
298 * @param style The style to use
300 * @ingroup Anchorblock
303 elm_anchorblock_hover_style_set(Evas_Object *obj, const char *style)
305 ELM_CHECK_WIDTYPE(obj, widtype);
306 Widget_Data *wd = elm_widget_data_get(obj);
308 eina_stringshare_replace(&wd->hover_style, style);
312 * Stop the hover popup in the anchorblock
314 * This will stop the hover popup in the anchorblock if it is currently active.
316 * @param obj The anchorblock object
318 * @ingroup Anchorblock
321 elm_anchorblock_hover_end(Evas_Object *obj)
323 ELM_CHECK_WIDTYPE(obj, widtype);
324 Widget_Data *wd = elm_widget_data_get(obj);
326 if (wd->hover) evas_object_del(wd->hover);
327 if (wd->pop) evas_object_del(wd->pop);
333 * This appends a custom item provider to the list for that anchorblock
335 * This appends the given callback. The list is walked from beginning to end
336 * with each function called given the item href string in the text. If the
337 * function returns an object handle other than NULL (it should create an
338 * and object to do this), then this object is used to replace that item. If
339 * not the next provider is called until one provides an item object, or the
340 * default provider in anchorblock does.
342 * @param obj The anchorblock object
343 * @param func The function called to provide the item object
344 * @param data The data passed to @p func
346 * @ingroup Anchorblock
349 elm_anchorblock_item_provider_append(Evas_Object *obj, Evas_Object *(*func) (void *data, Evas_Object *anchorblock, const char *item), void *data)
351 ELM_CHECK_WIDTYPE(obj, widtype);
352 Widget_Data *wd = elm_widget_data_get(obj);
355 Elm_Anchorblock_Item_Provider *ip = calloc(1, sizeof(Elm_Anchorblock_Item_Provider));
359 wd->item_providers = eina_list_append(wd->item_providers, ip);
363 * This prepends a custom item provider to the list for that anchorblock
365 * This prepends the given callback. See elm_anchorblock_item_provider_append() for
368 * @param obj The anchorblock object
369 * @param func The function called to provide the item object
370 * @param data The data passed to @p func
372 * @ingroup Anchorblock
375 elm_anchorblock_item_provider_prepend(Evas_Object *obj, Evas_Object *(*func) (void *data, Evas_Object *anchorblock, const char *item), void *data)
377 ELM_CHECK_WIDTYPE(obj, widtype);
378 Widget_Data *wd = elm_widget_data_get(obj);
381 Elm_Anchorblock_Item_Provider *ip = calloc(1, sizeof(Elm_Anchorblock_Item_Provider));
385 wd->item_providers = eina_list_prepend(wd->item_providers, ip);
389 * This removes a custom item provider to the list for that anchorblock
391 * This removes the given callback. See elm_anchorblock_item_provider_append() for
394 * @param obj The anchorblock object
395 * @param func The function called to provide the item object
396 * @param data The data passed to @p func
398 * @ingroup Anchorblock
401 elm_anchorblock_item_provider_remove(Evas_Object *obj, Evas_Object *(*func) (void *data, Evas_Object *anchorblock, const char *item), void *data)
403 ELM_CHECK_WIDTYPE(obj, widtype);
404 Widget_Data *wd = elm_widget_data_get(obj);
406 Elm_Anchorblock_Item_Provider *ip;
409 EINA_LIST_FOREACH(wd->item_providers, l, ip)
411 if ((ip->func == func) && (ip->data == data))
413 wd->item_providers = eina_list_remove_list(wd->item_providers, l);