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, const char *item)
157 Widget_Data *wd = elm_widget_data_get(data);
160 Elm_Anchorblock_Item_Provider *ip;
162 EINA_LIST_FOREACH(wd->item_providers, l, ip)
164 o = ip->func(ip->data, data, item);
171 * Add a new Anchorblock object
173 * @param parent The parent object
174 * @return The new object or NULL if it cannot be created
176 * @ingroup Anchorblock
179 elm_anchorblock_add(Evas_Object *parent)
185 wd = ELM_NEW(Widget_Data);
186 e = evas_object_evas_get(parent);
187 obj = elm_widget_add(e);
188 ELM_SET_WIDTYPE(widtype, "anchorblock");
189 elm_widget_type_set(obj, "anchorblock");
190 elm_widget_sub_object_add(parent, obj);
191 elm_widget_data_set(obj, wd);
192 elm_widget_del_pre_hook_set(obj, _del_pre_hook);
193 elm_widget_del_hook_set(obj, _del_hook);
195 wd->entry = elm_entry_add(parent);
196 elm_entry_item_provider_prepend(wd->entry, _item_provider, obj);
197 elm_widget_resize_object_set(obj, wd->entry);
198 elm_entry_editable_set(wd->entry, 0);
199 evas_object_size_hint_weight_set(wd->entry, 1.0, 1.0);
200 evas_object_size_hint_align_set(wd->entry, -1.0, -1.0);
202 evas_object_event_callback_add(wd->entry, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
203 _changed_size_hints, obj);
205 elm_entry_entry_set(wd->entry, "");
207 evas_object_smart_callback_add(wd->entry, "anchor,clicked",
208 _anchor_clicked, obj);
212 // TODO: convert Elementary to subclassing of Evas_Smart_Class
213 // TODO: and save some bytes, making descriptions per-class and not instance!
214 evas_object_smart_callbacks_descriptions_set(obj, _signals);
219 * Set the text markup of the anchorblock
221 * This sets the text of the anchorblock to be the text given as @p text. This
222 * text is in markup format with \<a href=XXX\> beginning an achor with the
223 * string link of 'XXX', and \</\> or \</a\> ending the link. Other markup can
224 * be used dependign on the style support.
226 * @param obj The anchorblock object
227 * @param text The text to set, or NULL to clear
229 * @ingroup Anchorblock
232 elm_anchorblock_text_set(Evas_Object *obj, const char *text)
234 ELM_CHECK_WIDTYPE(obj, widtype);
235 Widget_Data *wd = elm_widget_data_get(obj);
237 elm_entry_entry_set(wd->entry, text);
238 if (wd->hover) evas_object_del(wd->hover);
239 if (wd->pop) evas_object_del(wd->pop);
246 * Get the markup text set for the anchorblock
248 * This retrieves back the string set by @c elm_anchorblock_text_set().
250 * @param obj The anchorblock object
251 * @return text The markup text set or @c NULL, either if it was not set
252 * or an error ocurred
254 * @ingroup Anchorblock
257 elm_anchorblock_text_get(const Evas_Object *obj)
259 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
260 Widget_Data *wd = elm_widget_data_get(obj);
261 if (!wd) return NULL;
262 return elm_entry_entry_get(wd->entry);
266 * Set the parent of the hover popup
268 * This sets the parent of the hover that anchorblock will create. See hover
269 * objects for more information on this.
271 * @param obj The anchorblock object
272 * @param parent The parent the hover should use
274 * @ingroup Anchorblock
277 elm_anchorblock_hover_parent_set(Evas_Object *obj, Evas_Object *parent)
279 ELM_CHECK_WIDTYPE(obj, widtype);
280 Widget_Data *wd = elm_widget_data_get(obj);
282 if (wd->hover_parent)
283 evas_object_event_callback_del_full(wd->hover_parent, EVAS_CALLBACK_DEL, _parent_del, obj);
284 wd->hover_parent = parent;
285 if (wd->hover_parent)
286 evas_object_event_callback_add(wd->hover_parent, EVAS_CALLBACK_DEL, _parent_del, obj);
290 * Set the style that the hover should use
292 * This sets the style for the hover that anchorblock will create. See hover
293 * objects for more information
295 * @param obj The anchorblock object
296 * @param style The style to use
298 * @ingroup Anchorblock
301 elm_anchorblock_hover_style_set(Evas_Object *obj, const char *style)
303 ELM_CHECK_WIDTYPE(obj, widtype);
304 Widget_Data *wd = elm_widget_data_get(obj);
306 eina_stringshare_replace(&wd->hover_style, style);
310 * Stop the hover popup in the anchorblock
312 * This will stop the hover popup in the anchorblock if it is currently active.
314 * @param obj The anchorblock object
316 * @ingroup Anchorblock
319 elm_anchorblock_hover_end(Evas_Object *obj)
321 ELM_CHECK_WIDTYPE(obj, widtype);
322 Widget_Data *wd = elm_widget_data_get(obj);
324 if (wd->hover) evas_object_del(wd->hover);
325 if (wd->pop) evas_object_del(wd->pop);
331 * This appends a custom item provider to the list for that anchorblock
333 * This appends the given callback. The list is walked from beginning to end
334 * with each function called given the item href string in the text. If the
335 * function returns an object handle other than NULL (it should create an
336 * and object to do this), then this object is used to replace that item. If
337 * not the next provider is called until one provides an item object, or the
338 * default provider in anchorblock does.
340 * @param obj The anchorblock object
341 * @param func The function called to provide the item object
342 * @param data The data passed to @p func
344 * @ingroup Anchorblock
347 elm_anchorblock_item_provider_append(Evas_Object *obj, Evas_Object *(*func) (void *data, Evas_Object *anchorblock, const char *item), void *data)
349 ELM_CHECK_WIDTYPE(obj, widtype);
350 Widget_Data *wd = elm_widget_data_get(obj);
353 Elm_Anchorblock_Item_Provider *ip = calloc(1, sizeof(Elm_Anchorblock_Item_Provider));
357 wd->item_providers = eina_list_append(wd->item_providers, ip);
361 * This prepends a custom item provider to the list for that anchorblock
363 * This prepends the given callback. See elm_anchorblock_item_provider_append() for
366 * @param obj The anchorblock object
367 * @param func The function called to provide the item object
368 * @param data The data passed to @p func
370 * @ingroup Anchorblock
373 elm_anchorblock_item_provider_prepend(Evas_Object *obj, Evas_Object *(*func) (void *data, Evas_Object *anchorblock, const char *item), void *data)
375 ELM_CHECK_WIDTYPE(obj, widtype);
376 Widget_Data *wd = elm_widget_data_get(obj);
379 Elm_Anchorblock_Item_Provider *ip = calloc(1, sizeof(Elm_Anchorblock_Item_Provider));
383 wd->item_providers = eina_list_prepend(wd->item_providers, ip);
387 * This removes a custom item provider to the list for that anchorblock
389 * This removes the given callback. See elm_anchorblock_item_provider_append() for
392 * @param obj The anchorblock object
393 * @param func The function called to provide the item object
394 * @param data The data passed to @p func
396 * @ingroup Anchorblock
399 elm_anchorblock_item_provider_remove(Evas_Object *obj, Evas_Object *(*func) (void *data, Evas_Object *anchorblock, const char *item), void *data)
401 ELM_CHECK_WIDTYPE(obj, widtype);
402 Widget_Data *wd = elm_widget_data_get(obj);
404 Elm_Anchorblock_Item_Provider *ip;
407 EINA_LIST_FOREACH(wd->item_providers, l, ip)
409 if ((ip->func == func) && (ip->data == data))
411 wd->item_providers = eina_list_remove_list(wd->item_providers, l);