1 #include <Elementary.h>
4 typedef struct _Widget_Data Widget_Data;
5 typedef struct _Elm_Anchorblock_Item_Provider Elm_Anchorblock_Item_Provider;
10 Evas_Object *hover_parent;
11 Evas_Object *pop, *hover;
12 Eina_List *item_providers;
13 const char *hover_style;
16 struct _Elm_Anchorblock_Item_Provider
18 Evas_Object *(*func) (void *data, Evas_Object *anchorblock, const char *item);
22 static const char *widtype = NULL;
24 static const char SIG_ANCHOR_CLICKED[] = "anchor,clicked";
25 static const Evas_Smart_Cb_Description _signals[] = {
26 {SIG_ANCHOR_CLICKED, ""}, /* TODO: declare the type properly, as data is
32 static void _del_pre_hook(Evas_Object *obj);
33 static void _del_hook(Evas_Object *obj);
34 static void _sizing_eval(Evas_Object *obj);
35 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
36 static void _parent_del(void *data, Evas *e, Evas_Object *obj, void *event_info);
37 static void _mirrored_set(Evas_Object *obj, Eina_Bool rtl);
40 _del_pre_hook(Evas_Object *obj)
42 Widget_Data *wd = elm_widget_data_get(obj);
44 evas_object_event_callback_del_full(wd->entry, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
45 _changed_size_hints, obj);
46 elm_anchorblock_hover_end(obj);
47 elm_anchorblock_hover_parent_set(obj, NULL);
51 _del_hook(Evas_Object *obj)
53 Widget_Data *wd = elm_widget_data_get(obj);
54 Elm_Anchorblock_Item_Provider *ip;
56 if (wd->hover_style) eina_stringshare_del(wd->hover_style);
57 EINA_LIST_FREE(wd->item_providers, ip)
65 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
67 Widget_Data *wd = elm_widget_data_get(obj);
69 elm_widget_mirrored_set(wd->hover, rtl);
73 _theme_hook(Evas_Object *obj)
75 Widget_Data *wd = elm_widget_data_get(obj);
77 _elm_widget_mirrored_reload(obj);
78 _mirrored_set(obj, elm_widget_mirrored_get(obj));
82 _sizing_eval(Evas_Object *obj)
84 Widget_Data *wd = elm_widget_data_get(obj);
85 Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
87 evas_object_size_hint_min_get(wd->entry, &minw, &minh);
88 evas_object_size_hint_min_set(obj, minw, minh);
89 evas_object_size_hint_max_set(obj, maxw, maxh);
93 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
99 _hover_clicked(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
101 elm_anchorblock_hover_end(data);
105 _anchor_clicked(void *data, Evas_Object *obj, void *event_info)
107 Widget_Data *wd = elm_widget_data_get(data);
108 Elm_Entry_Anchor_Info *info = event_info;
109 Evas_Object *hover_parent;
110 Elm_Entry_Anchorblock_Info ei;
111 Evas_Coord x, w, y, h, px, py;
113 wd->pop = elm_icon_add(obj);
114 evas_object_move(wd->pop, info->x, info->y);
115 evas_object_resize(wd->pop, info->w, info->h);
116 wd->hover = elm_hover_add(obj);
117 elm_widget_mirrored_set(wd->hover, elm_widget_mirrored_get((Evas_Object *) data));
119 elm_object_style_set(wd->hover, wd->hover_style);
120 hover_parent = wd->hover_parent;
121 if (!hover_parent) hover_parent = obj;
122 elm_hover_parent_set(wd->hover, hover_parent);
123 elm_hover_target_set(wd->hover, wd->pop);
124 ei.name = info->name;
125 ei.button = info->button;
126 ei.hover = wd->hover;
127 ei.anchor.x = info->x;
128 ei.anchor.y = info->y;
129 ei.anchor.w = info->w;
130 ei.anchor.h = info->h;
131 evas_object_geometry_get(hover_parent, &x, &y, &w, &h);
132 ei.hover_parent.x = x;
133 ei.hover_parent.y = y;
134 ei.hover_parent.w = w;
135 ei.hover_parent.h = h;
136 px = info->x + (info->w / 2);
137 py = info->y + (info->h / 2);
139 if (px < (x + (w / 3))) ei.hover_left = 0;
141 if (px > (x + ((w * 2) / 3))) ei.hover_right = 0;
143 if (py < (y + (h / 3))) ei.hover_top = 0;
145 if (py > (y + ((h * 2) / 3))) ei.hover_bottom = 0;
147 if (elm_widget_mirrored_get(wd->hover))
148 { /* Swap right and left because they switch sides in RTL */
149 Eina_Bool tmp = ei.hover_left;
150 ei.hover_left = ei.hover_right;
151 ei.hover_right = tmp;
154 evas_object_smart_callback_call(data, SIG_ANCHOR_CLICKED, &ei);
155 evas_object_smart_callback_add(wd->hover, "clicked", _hover_clicked, data);
156 evas_object_show(wd->hover);
160 _parent_del(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
162 Widget_Data *wd = elm_widget_data_get(data);
164 wd->hover_parent = NULL;
168 _item_provider(void *data, Evas_Object *entry __UNUSED__, const char *item)
170 Widget_Data *wd = elm_widget_data_get(data);
172 Elm_Anchorblock_Item_Provider *ip;
174 EINA_LIST_FOREACH(wd->item_providers, l, ip)
178 o = ip->func(ip->data, data, item);
185 _elm_anchorblock_text_set(Evas_Object *obj, const char *item, const char *text)
187 ELM_CHECK_WIDTYPE(obj, widtype);
188 Widget_Data *wd = elm_widget_data_get(obj);
189 if (item && strcmp(item, "default")) return;
191 elm_object_text_set(wd->entry, text);
192 if (wd->hover) evas_object_del(wd->hover);
193 if (wd->pop) evas_object_del(wd->pop);
200 _elm_anchorblock_text_get(const Evas_Object *obj, const char *item)
202 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
203 Widget_Data *wd = elm_widget_data_get(obj);
204 if (item && strcmp(item, "default")) return NULL;
205 if (!wd) return NULL;
206 return elm_object_text_get(wd->entry);
210 elm_anchorblock_add(Evas_Object *parent)
216 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
218 ELM_SET_WIDTYPE(widtype, "anchorblock");
219 elm_widget_type_set(obj, "anchorblock");
220 elm_widget_sub_object_add(parent, obj);
221 elm_widget_data_set(obj, wd);
222 elm_widget_del_pre_hook_set(obj, _del_pre_hook);
223 elm_widget_del_hook_set(obj, _del_hook);
224 elm_widget_theme_hook_set(obj, _theme_hook);
225 elm_widget_can_focus_set(obj, EINA_TRUE);
226 elm_widget_text_set_hook_set(obj, _elm_anchorblock_text_set);
227 elm_widget_text_get_hook_set(obj, _elm_anchorblock_text_get);
229 wd->entry = elm_entry_add(parent);
230 elm_entry_item_provider_prepend(wd->entry, _item_provider, obj);
231 elm_widget_resize_object_set(obj, wd->entry);
232 elm_entry_editable_set(wd->entry, 0);
233 evas_object_size_hint_weight_set(wd->entry, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
234 evas_object_size_hint_align_set(wd->entry, EVAS_HINT_FILL, EVAS_HINT_FILL);
236 evas_object_event_callback_add(wd->entry, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
237 _changed_size_hints, obj);
239 elm_object_text_set(wd->entry, "");
241 evas_object_smart_callback_add(wd->entry, "anchor,clicked",
242 _anchor_clicked, obj);
244 _mirrored_set(obj, elm_widget_mirrored_get(obj));
247 // TODO: convert Elementary to subclassing of Evas_Smart_Class
248 // TODO: and save some bytes, making descriptions per-class and not instance!
249 evas_object_smart_callbacks_descriptions_set(obj, _signals);
254 elm_anchorblock_text_set(Evas_Object *obj, const char *text)
256 _elm_anchorblock_text_set(obj, NULL, text);
260 elm_anchorblock_text_get(const Evas_Object *obj)
262 return _elm_anchorblock_text_get(obj, NULL);
266 elm_anchorblock_hover_parent_set(Evas_Object *obj, Evas_Object *parent)
268 ELM_CHECK_WIDTYPE(obj, widtype);
269 Widget_Data *wd = elm_widget_data_get(obj);
271 if (wd->hover_parent)
272 evas_object_event_callback_del_full(wd->hover_parent, EVAS_CALLBACK_DEL, _parent_del, obj);
273 wd->hover_parent = parent;
274 if (wd->hover_parent)
275 evas_object_event_callback_add(wd->hover_parent, EVAS_CALLBACK_DEL, _parent_del, obj);
279 elm_anchorblock_hover_parent_get(const Evas_Object *obj)
281 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
282 Widget_Data *wd = elm_widget_data_get(obj);
283 if (!wd) return NULL;
284 return wd->hover_parent;
288 elm_anchorblock_hover_style_set(Evas_Object *obj, const char *style)
290 ELM_CHECK_WIDTYPE(obj, widtype);
291 Widget_Data *wd = elm_widget_data_get(obj);
293 eina_stringshare_replace(&wd->hover_style, style);
297 elm_anchorblock_hover_style_get(const Evas_Object *obj)
299 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
300 Widget_Data *wd = elm_widget_data_get(obj);
301 if (!wd) return NULL;
302 return wd->hover_style;
306 elm_anchorblock_hover_end(Evas_Object *obj)
308 ELM_CHECK_WIDTYPE(obj, widtype);
309 Widget_Data *wd = elm_widget_data_get(obj);
311 if (wd->hover) evas_object_del(wd->hover);
312 if (wd->pop) evas_object_del(wd->pop);
318 elm_anchorblock_item_provider_append(Evas_Object *obj, Evas_Object *(*func) (void *data, Evas_Object *anchorblock, const char *item), void *data)
320 ELM_CHECK_WIDTYPE(obj, widtype);
321 Widget_Data *wd = elm_widget_data_get(obj);
323 EINA_SAFETY_ON_NULL_RETURN(func);
324 Elm_Anchorblock_Item_Provider *ip = calloc(1, sizeof(Elm_Anchorblock_Item_Provider));
328 wd->item_providers = eina_list_append(wd->item_providers, ip);
332 elm_anchorblock_item_provider_prepend(Evas_Object *obj, Evas_Object *(*func) (void *data, Evas_Object *anchorblock, const char *item), void *data)
334 ELM_CHECK_WIDTYPE(obj, widtype);
335 Widget_Data *wd = elm_widget_data_get(obj);
337 EINA_SAFETY_ON_NULL_RETURN(func);
338 Elm_Anchorblock_Item_Provider *ip = calloc(1, sizeof(Elm_Anchorblock_Item_Provider));
342 wd->item_providers = eina_list_prepend(wd->item_providers, ip);
346 elm_anchorblock_item_provider_remove(Evas_Object *obj, Evas_Object *(*func) (void *data, Evas_Object *anchorblock, const char *item), void *data)
348 ELM_CHECK_WIDTYPE(obj, widtype);
349 Widget_Data *wd = elm_widget_data_get(obj);
351 Elm_Anchorblock_Item_Provider *ip;
353 EINA_SAFETY_ON_NULL_RETURN(func);
354 EINA_LIST_FOREACH(wd->item_providers, l, ip)
356 if ((ip->func == func) && (ip->data == data))
358 wd->item_providers = eina_list_remove_list(wd->item_providers, l);