1 #include <Elementary.h>
4 typedef struct _Widget_Data Widget_Data;
5 typedef struct _Elm_Hoversel_Item Elm_Hoversel_Item;
9 Evas_Object *btn, *hover;
10 Evas_Object *hover_parent;
12 Eina_Bool horizontal : 1;
13 Eina_Bool expanded : 1;
16 struct _Elm_Hoversel_Item
20 const char *icon_file;
21 const char *icon_group;
22 Elm_Icon_Type icon_type;
26 static const char *widtype = NULL;
27 static void _del_pre_hook(Evas_Object *obj);
28 static void _del_hook(Evas_Object *obj);
29 static void _activate(Evas_Object *obj);
30 static void _activate_hook(Evas_Object *obj);
31 static void _disable_hook(Evas_Object *obj);
32 static void _sizing_eval(Evas_Object *obj);
33 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
34 static void _parent_del(void *data, Evas *e, Evas_Object *obj, void *event_info);
36 static const char SIG_CLICKED[] = "clicked";
37 static const char SIG_SELECTED[] = "selected";
38 static const char SIG_DISMISSED[] = "dismissed";
40 static const Evas_Smart_Cb_Description _signals[] = {
48 _del_pre_hook(Evas_Object *obj)
50 Elm_Hoversel_Item *item;
51 Widget_Data *wd = elm_widget_data_get(obj);
53 evas_object_event_callback_del_full(wd->btn, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
54 _changed_size_hints, obj);
55 elm_hoversel_hover_end(obj);
56 elm_hoversel_hover_parent_set(obj, NULL);
57 EINA_LIST_FREE(wd->items, item)
59 eina_stringshare_del(item->label);
60 eina_stringshare_del(item->icon_file);
61 eina_stringshare_del(item->icon_group);
62 elm_widget_item_free(item);
67 _del_hook(Evas_Object *obj)
69 Widget_Data *wd = elm_widget_data_get(obj);
75 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
77 Widget_Data *wd = elm_widget_data_get(obj);
79 elm_widget_mirrored_set(wd->btn, rtl);
80 elm_widget_mirrored_set(wd->hover, rtl);
84 _theme_hook(Evas_Object *obj)
86 Widget_Data *wd = elm_widget_data_get(obj);
89 _elm_widget_mirrored_reload(obj);
91 elm_hoversel_hover_end(obj);
93 snprintf(buf, sizeof(buf), "hoversel_horizontal/%s", elm_widget_style_get(obj));
95 snprintf(buf, sizeof(buf), "hoversel_vertical/%s", elm_widget_style_get(obj));
96 elm_object_style_set(wd->btn, buf);
97 elm_object_disabled_set(wd->btn, elm_widget_disabled_get(obj));
98 _mirrored_set(obj, elm_widget_mirrored_get(obj));
102 _disable_hook(Evas_Object *obj)
104 Widget_Data *wd = elm_widget_data_get(obj);
106 elm_object_disabled_set(wd->btn, elm_widget_disabled_get(obj));
110 _sizing_eval(Evas_Object *obj)
112 Widget_Data *wd = elm_widget_data_get(obj);
113 Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
115 evas_object_size_hint_min_get(wd->btn, &minw, &minh);
116 evas_object_size_hint_max_get(wd->btn, &maxw, &maxh);
117 evas_object_size_hint_min_set(obj, minw, minh);
118 evas_object_size_hint_max_set(obj, maxw, maxh);
122 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
124 Widget_Data *wd = elm_widget_data_get(obj);
126 if (elm_widget_focus_get(obj))
127 elm_widget_focus_steal(wd->btn);
131 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
137 _hover_clicked(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
139 elm_hoversel_hover_end(data);
143 _item_clicked(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
145 Elm_Hoversel_Item *item = data;
146 Evas_Object *obj2 = WIDGET(item);
148 elm_hoversel_hover_end(obj2);
149 if (item->func) item->func((void *)item->base.data, obj2, item);
150 evas_object_smart_callback_call(obj2, SIG_SELECTED, item);
154 _activate(Evas_Object *obj)
156 Widget_Data *wd = elm_widget_data_get(obj);
157 Evas_Object *bt, *bx, *ic;
159 const Elm_Hoversel_Item *item;
165 elm_hoversel_hover_end(obj);
168 wd->expanded = EINA_TRUE;
170 if (elm_widget_disabled_get(obj)) return;
171 wd->hover = elm_hover_add(obj);
172 elm_widget_mirrored_automatic_set(wd->hover, EINA_FALSE);
174 snprintf(buf, sizeof(buf), "hoversel_horizontal/%s", elm_widget_style_get(obj));
176 snprintf(buf, sizeof(buf), "hoversel_vertical/%s", elm_widget_style_get(obj));
177 elm_object_style_set(wd->hover, buf);
178 evas_object_smart_callback_add(wd->hover, "clicked", _hover_clicked, obj);
179 elm_hover_parent_set(wd->hover, wd->hover_parent);
180 elm_hover_target_set(wd->hover, wd->btn);
182 bx = elm_box_add(wd->hover);
183 elm_widget_mirrored_automatic_set(bx, EINA_FALSE);
184 elm_box_homogeneous_set(bx, 1);
186 elm_box_horizontal_set(bx, wd->horizontal);
189 snprintf(buf, sizeof(buf), "hoversel_horizontal_entry/%s",
190 elm_widget_style_get(obj));
192 snprintf(buf, sizeof(buf), "hoversel_vertical_entry/%s",
193 elm_widget_style_get(obj));
194 EINA_LIST_FOREACH(wd->items, l, item)
196 bt = elm_button_add(wd->hover);
197 elm_widget_mirrored_automatic_set(bt, EINA_FALSE);
198 elm_widget_mirrored_set(bt, elm_widget_mirrored_get(obj));
199 elm_object_style_set(bt, buf);
200 elm_object_text_set(bt, item->label);
203 ic = elm_icon_add(obj);
204 elm_icon_scale_set(ic, 0, 1);
205 if (item->icon_type == ELM_ICON_FILE)
206 elm_icon_file_set(ic, item->icon_file, item->icon_group);
207 else if (item->icon_type == ELM_ICON_STANDARD)
208 elm_icon_standard_set(ic, item->icon_file);
209 elm_object_part_content_set(bt, "icon", ic);
210 evas_object_show(ic);
212 evas_object_size_hint_weight_set(bt, EVAS_HINT_EXPAND, 0.0);
213 evas_object_size_hint_align_set(bt, EVAS_HINT_FILL, EVAS_HINT_FILL);
214 elm_box_pack_end(bx, bt);
215 evas_object_smart_callback_add(bt, "clicked", _item_clicked, item);
216 evas_object_show(bt);
220 elm_object_part_content_set(wd->hover,
221 elm_hover_best_content_location_get(wd->hover,
222 ELM_HOVER_AXIS_HORIZONTAL),
225 elm_object_part_content_set(wd->hover,
226 elm_hover_best_content_location_get(wd->hover,
227 ELM_HOVER_AXIS_VERTICAL),
229 evas_object_show(bx);
231 evas_object_show(wd->hover);
232 evas_object_smart_callback_call(obj, SIG_CLICKED, NULL);
234 // if (wd->horizontal) evas_object_hide(wd->btn);
238 _activate_hook(Evas_Object *obj)
244 _button_clicked(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
250 _parent_del(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
252 Widget_Data *wd = elm_widget_data_get(data);
254 wd->hover_parent = NULL;
258 _elm_hoversel_label_set(Evas_Object *obj, const char *item, const char *label)
260 ELM_CHECK_WIDTYPE(obj, widtype);
261 Widget_Data *wd = elm_widget_data_get(obj);
262 if (item && strcmp(item, "default")) return;
264 elm_object_text_set(wd->btn, label);
268 _elm_hoversel_label_get(const Evas_Object *obj, const char *item)
270 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
271 Widget_Data *wd = elm_widget_data_get(obj);
272 if (item && strcmp(item, "default")) return NULL;
273 if ((!wd) || (!wd->btn)) return NULL;
274 return elm_object_text_get(wd->btn);
278 _content_set_hook(Evas_Object *obj, const char *part, Evas_Object *content)
280 ELM_CHECK_WIDTYPE(obj, widtype);
281 Widget_Data *wd = elm_widget_data_get(obj);
283 elm_object_part_content_set(wd->btn, part, content);
287 _content_get_hook(const Evas_Object *obj, const char *part)
289 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
290 Widget_Data *wd = elm_widget_data_get(obj);
291 if ((!wd) || (!wd->btn)) return NULL;
292 return elm_object_part_content_get(wd->btn, part);
296 _content_unset_hook(Evas_Object *obj, const char *part)
298 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
299 Widget_Data *wd = elm_widget_data_get(obj);
300 if ((!wd) || (!wd->btn)) return NULL;
301 return elm_object_part_content_unset(wd->btn, part);
305 _item_text_get_hook(const Elm_Object_Item *it, const char *part)
307 ELM_OBJ_ITEM_CHECK_OR_RETURN(it, NULL);
308 if (part && strcmp(part, "default")) return NULL;
309 return ((Elm_Hoversel_Item *) it)->label;
313 _item_del_pre_hook(Elm_Object_Item *it)
315 ELM_OBJ_ITEM_CHECK_OR_RETURN(it, EINA_FALSE);
317 Elm_Hoversel_Item *item = (Elm_Hoversel_Item *) it;
318 wd = elm_widget_data_get(WIDGET(item));
319 if (!wd) return EINA_FALSE;
320 elm_hoversel_hover_end(WIDGET(item));
321 wd->items = eina_list_remove(wd->items, item);
322 eina_stringshare_del(item->label);
323 eina_stringshare_del(item->icon_file);
324 eina_stringshare_del(item->icon_group);
330 elm_hoversel_add(Evas_Object *parent)
336 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
338 ELM_SET_WIDTYPE(widtype, "hoversel");
339 elm_widget_type_set(obj, "hoversel");
340 elm_widget_sub_object_add(parent, obj);
341 elm_widget_data_set(obj, wd);
342 elm_widget_del_pre_hook_set(obj, _del_pre_hook);
343 elm_widget_del_hook_set(obj, _del_hook);
344 elm_widget_theme_hook_set(obj, _theme_hook);
345 elm_widget_disable_hook_set(obj, _disable_hook);
346 elm_widget_activate_hook_set(obj, _activate_hook);
347 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
348 elm_widget_can_focus_set(obj, EINA_TRUE);
349 elm_widget_text_set_hook_set(obj, _elm_hoversel_label_set);
350 elm_widget_text_get_hook_set(obj, _elm_hoversel_label_get);
351 elm_widget_content_set_hook_set(obj, _content_set_hook);
352 elm_widget_content_get_hook_set(obj, _content_get_hook);
353 elm_widget_content_unset_hook_set(obj, _content_unset_hook);
355 wd->btn = elm_button_add(parent);
356 elm_widget_mirrored_automatic_set(wd->btn, EINA_FALSE);
357 wd->expanded = EINA_FALSE;
358 elm_widget_resize_object_set(obj, wd->btn);
359 evas_object_event_callback_add(wd->btn, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
360 _changed_size_hints, obj);
361 evas_object_smart_callback_add(wd->btn, "clicked", _button_clicked, obj);
362 evas_object_smart_callbacks_descriptions_set(obj, _signals);
364 elm_widget_sub_object_add(obj, wd->btn);
366 elm_hoversel_hover_parent_set(obj, parent);
373 elm_hoversel_hover_parent_set(Evas_Object *obj, Evas_Object *parent)
375 ELM_CHECK_WIDTYPE(obj, widtype);
376 Widget_Data *wd = elm_widget_data_get(obj);
378 if (wd->hover_parent)
379 evas_object_event_callback_del_full(wd->hover_parent, EVAS_CALLBACK_DEL,
381 wd->hover_parent = parent;
382 if (wd->hover_parent)
383 evas_object_event_callback_add(wd->hover_parent, EVAS_CALLBACK_DEL,
388 elm_hoversel_hover_parent_get(const Evas_Object *obj)
390 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
391 Widget_Data *wd = elm_widget_data_get(obj);
392 if (!wd) return NULL;
393 return wd->hover_parent;
397 elm_hoversel_label_set(Evas_Object *obj, const char *label)
399 _elm_hoversel_label_set(obj, NULL, label);
403 elm_hoversel_label_get(const Evas_Object *obj)
405 return _elm_hoversel_label_get(obj, NULL);
409 elm_hoversel_horizontal_set(Evas_Object *obj, Eina_Bool horizontal)
411 ELM_CHECK_WIDTYPE(obj, widtype);
412 Widget_Data *wd = elm_widget_data_get(obj);
414 wd->horizontal = !!horizontal;
418 elm_hoversel_horizontal_get(const Evas_Object *obj)
420 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
421 Widget_Data *wd = elm_widget_data_get(obj);
422 if (!wd) return EINA_FALSE;
423 return wd->horizontal;
427 elm_hoversel_icon_set(Evas_Object *obj, Evas_Object *icon)
429 _content_set_hook(obj, "icon", icon);
433 elm_hoversel_icon_get(const Evas_Object *obj)
435 return _content_get_hook(obj, "icon");
439 elm_hoversel_icon_unset(Evas_Object *obj)
441 return _content_unset_hook(obj, "icon");
445 elm_hoversel_hover_begin(Evas_Object *obj)
447 ELM_CHECK_WIDTYPE(obj, widtype);
448 Widget_Data *wd = elm_widget_data_get(obj);
450 if (wd->hover) return;
455 elm_hoversel_hover_end(Evas_Object *obj)
457 ELM_CHECK_WIDTYPE(obj, widtype);
458 Widget_Data *wd = elm_widget_data_get(obj);
460 if (!wd->hover) return;
461 wd->expanded = EINA_FALSE;
462 evas_object_del(wd->hover);
464 evas_object_smart_callback_call(obj, SIG_DISMISSED, NULL);
468 elm_hoversel_expanded_get(const Evas_Object *obj)
470 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
471 Widget_Data *wd = elm_widget_data_get(obj);
472 if (!wd) return EINA_FALSE;
473 return (wd->hover) ? EINA_TRUE : EINA_FALSE;
477 elm_hoversel_clear(Evas_Object *obj)
481 ELM_CHECK_WIDTYPE(obj, widtype);
482 Widget_Data *wd = elm_widget_data_get(obj);
484 EINA_LIST_FOREACH_SAFE(wd->items, l, ll, it)
486 _item_del_pre_hook(it);
487 elm_widget_item_free(it);
491 EAPI const Eina_List *
492 elm_hoversel_items_get(const Evas_Object *obj)
494 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
495 Widget_Data *wd = elm_widget_data_get(obj);
496 if (!wd) return NULL;
500 EAPI Elm_Object_Item *
501 elm_hoversel_item_add(Evas_Object *obj, const char *label, const char *icon_file, Elm_Icon_Type icon_type, Evas_Smart_Cb func, const void *data)
503 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
504 Widget_Data *wd = elm_widget_data_get(obj);
505 if (!wd) return NULL;
506 Elm_Hoversel_Item *item = elm_widget_item_new(obj, Elm_Hoversel_Item);
507 if (!item) return NULL;
508 elm_widget_item_del_pre_hook_set(item, _item_del_pre_hook);
509 elm_widget_item_text_get_hook_set(item, _item_text_get_hook);
510 wd->items = eina_list_append(wd->items, item);
511 item->label = eina_stringshare_add(label);
512 item->icon_file = eina_stringshare_add(icon_file);
513 item->icon_type = icon_type;
515 item->base.data = data;
516 return (Elm_Object_Item *) item;
520 elm_hoversel_item_del(Elm_Object_Item *it)
522 elm_object_item_del(it);
526 elm_hoversel_item_del_cb_set(Elm_Object_Item *it, Evas_Smart_Cb func)
528 elm_object_item_del_cb_set(it, func);
532 elm_hoversel_item_data_get(const Elm_Object_Item *it)
534 return elm_object_item_data_get(it);
538 elm_hoversel_item_label_get(const Elm_Object_Item *it)
540 return _item_text_get_hook(it, NULL);
544 elm_hoversel_item_icon_set(Elm_Object_Item *it, const char *icon_file, const char *icon_group, Elm_Icon_Type icon_type)
546 ELM_OBJ_ITEM_CHECK_OR_RETURN(it);
547 Elm_Hoversel_Item *item = (Elm_Hoversel_Item *) it;
548 eina_stringshare_replace(&item->icon_file, icon_file);
549 eina_stringshare_replace(&item->icon_group, icon_group);
550 item->icon_type = icon_type;
554 elm_hoversel_item_icon_get(const Elm_Object_Item *it, const char **icon_file, const char **icon_group, Elm_Icon_Type *icon_type)
556 ELM_OBJ_ITEM_CHECK_OR_RETURN(it);
557 Elm_Hoversel_Item *item = (Elm_Hoversel_Item *) it;
558 if (icon_file) *icon_file = item->icon_file;
559 if (icon_group) *icon_group = item->icon_group;
560 if (icon_type) *icon_type = item->icon_type;