1 #include <Elementary.h>
7 * A hoversel is a button that pops up a list of items (automatically
8 * choosing the direction to display) that have a lable and/or an icon to
9 * select from. It is a convenience widget to avoid the need to do all the
10 * piecing together yourself. It is intended for a small number of items in
11 * the hoversel menu (no more than 8), though is capable of many more.
13 * Signals that you can add callbacks for are:
15 * clicked - the user clicked the hoversel button and popped up the sel
17 * selected - an item in the hoversel list is selected. event_info is the item
18 * selected - Elm_Hoversel_Item
20 * dismissed - the hover is dismissed
22 typedef struct _Widget_Data Widget_Data;
26 Evas_Object *btn, *hover;
27 Evas_Object *hover_parent;
29 Eina_Bool horizontal : 1;
30 Eina_Bool expanded : 1;
33 struct _Elm_Hoversel_Item
37 const char *icon_file;
38 const char *icon_group;
39 Elm_Icon_Type icon_type;
43 static const char *widtype = NULL;
44 static void _del_pre_hook(Evas_Object *obj);
45 static void _del_hook(Evas_Object *obj);
46 static void _activate(Evas_Object *obj);
47 static void _activate_hook(Evas_Object *obj);
48 static void _disable_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 Elm_Hoversel_Item *item;
57 Widget_Data *wd = elm_widget_data_get(obj);
59 elm_hoversel_hover_end(obj);
60 elm_hoversel_hover_parent_set(obj, NULL);
61 EINA_LIST_FREE(wd->items, item)
63 elm_widget_item_pre_notify_del(item);
64 eina_stringshare_del(item->label);
65 eina_stringshare_del(item->icon_file);
66 eina_stringshare_del(item->icon_group);
67 elm_widget_item_del(item);
72 _del_hook(Evas_Object *obj)
74 Widget_Data *wd = elm_widget_data_get(obj);
80 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
82 Widget_Data *wd = elm_widget_data_get(obj);
84 elm_widget_mirrored_set(wd->btn, rtl);
85 elm_widget_mirrored_set(wd->hover, rtl);
89 _theme_hook(Evas_Object *obj)
91 Widget_Data *wd = elm_widget_data_get(obj);
94 _elm_widget_mirrored_reload(obj);
96 elm_hoversel_hover_end(obj);
98 snprintf(buf, sizeof(buf), "hoversel_horizontal/%s", elm_widget_style_get(obj));
100 snprintf(buf, sizeof(buf), "hoversel_vertical/%s", elm_widget_style_get(obj));
101 elm_object_style_set(wd->btn, buf);
102 elm_object_disabled_set(wd->btn, elm_widget_disabled_get(obj));
103 _mirrored_set(obj, elm_widget_mirrored_get(obj));
107 _disable_hook(Evas_Object *obj)
109 Widget_Data *wd = elm_widget_data_get(obj);
111 elm_object_disabled_set(wd->btn, elm_widget_disabled_get(obj));
115 _sizing_eval(Evas_Object *obj)
117 Widget_Data *wd = elm_widget_data_get(obj);
118 Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
120 evas_object_size_hint_min_get(wd->btn, &minw, &minh);
121 evas_object_size_hint_max_get(wd->btn, &maxw, &maxh);
122 evas_object_size_hint_min_set(obj, minw, minh);
123 evas_object_size_hint_max_set(obj, maxw, maxh);
127 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
129 Widget_Data *wd = elm_widget_data_get(obj);
131 if (elm_widget_focus_get(obj))
132 elm_widget_focus_steal(wd->btn);
136 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
142 _hover_clicked(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
144 elm_hoversel_hover_end(data);
148 _item_clicked(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
150 Elm_Hoversel_Item *item = data;
151 Evas_Object *obj2 = item->base.widget;
153 elm_hoversel_hover_end(obj2);
154 if (item->func) item->func((void *)item->base.data, obj2, item);
155 evas_object_smart_callback_call(obj2, "selected", item);
159 _activate(Evas_Object *obj)
161 Widget_Data *wd = elm_widget_data_get(obj);
162 Evas_Object *bt, *bx, *ic;
164 const Elm_Hoversel_Item *item;
170 elm_hoversel_hover_end(obj);
173 wd->expanded = EINA_TRUE;
175 if (elm_widget_disabled_get(obj)) return;
176 wd->hover = elm_hover_add(obj);
177 elm_widget_mirrored_automatic_set(wd->hover, EINA_FALSE);
179 snprintf(buf, sizeof(buf), "hoversel_horizontal/%s", elm_widget_style_get(obj));
181 snprintf(buf, sizeof(buf), "hoversel_vertical/%s", elm_widget_style_get(obj));
182 elm_object_style_set(wd->hover, buf);
183 evas_object_smart_callback_add(wd->hover, "clicked", _hover_clicked, obj);
184 elm_hover_parent_set(wd->hover, wd->hover_parent);
185 elm_hover_target_set(wd->hover, wd->btn);
187 bx = elm_box_add(wd->hover);
188 elm_widget_mirrored_automatic_set(bx, EINA_FALSE);
189 elm_box_homogenous_set(bx, 1);
191 elm_box_horizontal_set(bx, wd->horizontal);
194 snprintf(buf, sizeof(buf), "hoversel_horizontal_entry/%s",
195 elm_widget_style_get(obj));
197 snprintf(buf, sizeof(buf), "hoversel_vertical_entry/%s",
198 elm_widget_style_get(obj));
199 EINA_LIST_FOREACH(wd->items, l, item)
201 bt = elm_button_add(wd->hover);
202 elm_widget_mirrored_automatic_set(bt, EINA_FALSE);
203 elm_widget_mirrored_set(bt, elm_widget_mirrored_get(obj));
204 elm_object_style_set(bt, buf);
205 elm_button_label_set(bt, item->label);
208 ic = elm_icon_add(obj);
209 elm_icon_scale_set(ic, 0, 1);
210 if (item->icon_type == ELM_ICON_FILE)
211 elm_icon_file_set(ic, item->icon_file, item->icon_group);
212 else if (item->icon_type == ELM_ICON_STANDARD)
213 elm_icon_standard_set(ic, item->icon_file);
214 elm_button_icon_set(bt, ic);
215 evas_object_show(ic);
217 evas_object_size_hint_weight_set(bt, EVAS_HINT_EXPAND, 0.0);
218 evas_object_size_hint_align_set(bt, EVAS_HINT_FILL, EVAS_HINT_FILL);
219 elm_box_pack_end(bx, bt);
220 evas_object_smart_callback_add(bt, "clicked", _item_clicked, item);
221 evas_object_show(bt);
225 elm_hover_content_set(wd->hover,
226 elm_hover_best_content_location_get(wd->hover,
227 ELM_HOVER_AXIS_HORIZONTAL),
230 elm_hover_content_set(wd->hover,
231 elm_hover_best_content_location_get(wd->hover,
232 ELM_HOVER_AXIS_VERTICAL),
234 evas_object_show(bx);
236 evas_object_show(wd->hover);
237 evas_object_smart_callback_call(obj, "clicked", NULL);
239 // if (wd->horizontal) evas_object_hide(wd->btn);
243 _activate_hook(Evas_Object *obj)
249 _button_clicked(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
255 _parent_del(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
257 Widget_Data *wd = elm_widget_data_get(data);
259 wd->hover_parent = NULL;
263 * Add a new Hoversel object
265 * @param parent The parent object
266 * @return The new object or NULL if it cannot be created
271 elm_hoversel_add(Evas_Object *parent)
277 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
279 ELM_SET_WIDTYPE(widtype, "hoversel");
280 elm_widget_type_set(obj, "hoversel");
281 elm_widget_sub_object_add(parent, obj);
282 elm_widget_data_set(obj, wd);
283 elm_widget_del_pre_hook_set(obj, _del_pre_hook);
284 elm_widget_del_hook_set(obj, _del_hook);
285 elm_widget_theme_hook_set(obj, _theme_hook);
286 elm_widget_disable_hook_set(obj, _disable_hook);
287 elm_widget_activate_hook_set(obj, _activate_hook);
288 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
289 elm_widget_can_focus_set(obj, EINA_TRUE);
291 wd->btn = elm_button_add(parent);
292 elm_widget_mirrored_automatic_set(wd->btn, EINA_FALSE);
293 wd->expanded = EINA_FALSE;
294 elm_widget_resize_object_set(obj, wd->btn);
295 evas_object_event_callback_add(wd->btn, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
296 _changed_size_hints, obj);
297 evas_object_smart_callback_add(wd->btn, "clicked", _button_clicked, obj);
298 elm_widget_sub_object_add(obj, wd->btn);
300 elm_hoversel_hover_parent_set(obj, parent);
306 * Set the Hover parent
308 * Sets the hover parent object. Should probably be the window that the hoversel
309 * is in. See Hover objects for more information.
311 * @param obj The hoversel object
312 * @param parent The parent to use
317 elm_hoversel_hover_parent_set(Evas_Object *obj, Evas_Object *parent)
319 ELM_CHECK_WIDTYPE(obj, widtype);
320 Widget_Data *wd = elm_widget_data_get(obj);
322 if (wd->hover_parent)
323 evas_object_event_callback_del_full(wd->hover_parent, EVAS_CALLBACK_DEL,
325 wd->hover_parent = parent;
326 if (wd->hover_parent)
327 evas_object_event_callback_add(wd->hover_parent, EVAS_CALLBACK_DEL,
332 * Get the Hover parent
334 * Gets the hover parent object. Should probably be the window that the hoversel
335 * is in. See Hover objects for more information.
337 * @param obj The hoversel object
338 * @return The used parent
343 elm_hoversel_hover_parent_get(const Evas_Object *obj)
345 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
346 Widget_Data *wd = elm_widget_data_get(obj);
347 if (!wd) return NULL;
348 return wd->hover_parent;
352 * Set the hoversel button label
354 * This sets the label of the button that is always visible (before it is
355 * clicked and expanded). Also see elm_button_label_set().
357 * @param obj The hoversel object
358 * @param label The label text.
363 elm_hoversel_label_set(Evas_Object *obj, const char *label)
365 ELM_CHECK_WIDTYPE(obj, widtype);
366 Widget_Data *wd = elm_widget_data_get(obj);
368 elm_button_label_set(wd->btn, label);
372 * Get the hoversel button label
374 * @param obj The hoversel object
375 * @return The label text.
380 elm_hoversel_label_get(const Evas_Object *obj)
382 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
383 Widget_Data *wd = elm_widget_data_get(obj);
384 if ((!wd) || (!wd->btn)) return NULL;
385 return elm_button_label_get(wd->btn);
389 * This sets the hoversel to expand horizontally. The initial button
390 * will display horizontally regardless of this setting.
392 * @param obj The hoversel object
393 * @param horizontal If true, the hover will expand horizontally to the right.
398 elm_hoversel_horizontal_set(Evas_Object *obj, Eina_Bool horizontal)
400 ELM_CHECK_WIDTYPE(obj, widtype);
401 Widget_Data *wd = elm_widget_data_get(obj);
403 wd->horizontal = !!horizontal;
408 * This returns whether the hoversel is set to expand horizontally.
410 * @param obj The hoversel object
411 * @return If true, the hover will expand horizontally to the right.
416 elm_hoversel_horizontal_get(const Evas_Object *obj)
418 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
419 Widget_Data *wd = elm_widget_data_get(obj);
420 if (!wd) return EINA_FALSE;
421 return wd->horizontal;
425 * Set the icon of the hoversel button
427 * Sets the icon of the button that is always visible (before it is clicked
428 * and expanded). Also see elm_button_icon_set().
429 * Once the icon object is set, a previously set one will be deleted
430 * If you want to keep that old content object, use the
431 * elm_hoversel_icon_unset() function.
433 * @param obj The hoversel object
434 * @param icon The icon object
439 elm_hoversel_icon_set(Evas_Object *obj, Evas_Object *icon)
441 ELM_CHECK_WIDTYPE(obj, widtype);
442 Widget_Data *wd = elm_widget_data_get(obj);
444 elm_button_icon_set(wd->btn, icon);
448 * Get the icon of the hoversel button
450 * Get the icon of the button that is always visible (before it is clicked
451 * and expanded). Also see elm_button_icon_get().
453 * @param obj The hoversel object
454 * @return The icon object
459 elm_hoversel_icon_get(const Evas_Object *obj)
461 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
462 Widget_Data *wd = elm_widget_data_get(obj);
463 if ((!wd) || (!wd->btn)) return NULL;
464 return elm_button_icon_get(wd->btn);
468 * Get the icon of the hoversel button
470 * Unparent and return the icon of the button that is always visible
471 * (before it is clicked and expanded). Also see elm_button_icon_unset().
473 * @param obj The hoversel object
474 * @return The icon object that was being used
479 elm_hoversel_icon_unset(Evas_Object *obj)
481 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
482 Widget_Data *wd = elm_widget_data_get(obj);
483 if ((!wd) || (!wd->btn)) return NULL;
484 return elm_button_icon_unset(wd->btn);
488 * This triggers the hoversel popup from code, the same as though the
489 * user clicked the button.
491 * @param obj The hoversel object
496 elm_hoversel_hover_begin(Evas_Object *obj)
498 ELM_CHECK_WIDTYPE(obj, widtype);
499 Widget_Data *wd = elm_widget_data_get(obj);
501 if (wd->hover) return;
506 * This ends the hoversel popup as though the user clicked outside the hover.
508 * @param obj The hoversel object
513 elm_hoversel_hover_end(Evas_Object *obj)
515 ELM_CHECK_WIDTYPE(obj, widtype);
516 Widget_Data *wd = elm_widget_data_get(obj);
518 if (!wd->hover) return;
519 wd->expanded = EINA_FALSE;
520 evas_object_del(wd->hover);
522 evas_object_smart_callback_call(obj, "dismissed", NULL);
526 * Returns whether the hoversel is expanded.
528 * @param obj The hoversel object
529 * @return This will return EINA_TRUE if the hoversel
530 * is expanded or EINA_FALSE if it is not expanded.
535 elm_hoversel_expanded_get(const Evas_Object *obj)
537 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
538 Widget_Data *wd = elm_widget_data_get(obj);
539 if (!wd) return EINA_FALSE;
540 return (wd->hover) ? EINA_TRUE : EINA_FALSE;
544 * This will remove all the children items from the hoversel. (should not be
545 * called while the hoversel is active; use elm_hoversel_expanded_get()
548 * @param obj The hoversel object
553 elm_hoversel_clear(Evas_Object *obj)
555 Elm_Hoversel_Item *item;
557 ELM_CHECK_WIDTYPE(obj, widtype);
558 Widget_Data *wd = elm_widget_data_get(obj);
560 EINA_LIST_FOREACH_SAFE(wd->items, l, ll, item) elm_hoversel_item_del(item);
564 * Get the list of items within the given hoversel.
566 * @param obj The hoversel object
567 * @return Returns a list of Elm_Hoversel_Item*
571 EAPI const Eina_List *
572 elm_hoversel_items_get(const Evas_Object *obj)
574 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
575 Widget_Data *wd = elm_widget_data_get(obj);
576 if (!wd) return NULL;
581 * Add an item to the hoversel button
583 * This adds an item to the hoversel to show when it is clicked. Note: if you
584 * need to use an icon from an edje file then use elm_hoversel_item_icon_set()
585 * right after the this function, and set icon_file to NULL here.
587 * @param obj The hoversel object
588 * @param label The text label to use for the item (NULL if not desired)
589 * @param icon_file An image file path on disk to use for the icon or standard
590 * icon name (NULL if not desired)
591 * @param icon_type The icon type if relevant
592 * @param func Convenience function to call when this item is selected
593 * @param data Data to pass to item-related functions
594 * @return A handle to the item added.
598 EAPI Elm_Hoversel_Item *
599 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)
601 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
602 Widget_Data *wd = elm_widget_data_get(obj);
603 if (!wd) return NULL;
604 Elm_Hoversel_Item *item = elm_widget_item_new(obj, Elm_Hoversel_Item);
605 if (!item) return NULL;
606 wd->items = eina_list_append(wd->items, item);
607 item->label = eina_stringshare_add(label);
608 item->icon_file = eina_stringshare_add(icon_file);
609 item->icon_type = icon_type;
611 item->base.data = data;
616 * Delete an item from the hoversel
618 * This deletes the item from the hoversel (should not be called while the
619 * hoversel is active; use elm_hoversel_expanded_get()
622 * @param item The item to delete
627 elm_hoversel_item_del(Elm_Hoversel_Item *item)
629 ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item);
630 Widget_Data *wd = elm_widget_data_get(item->base.widget);
632 elm_hoversel_hover_end(item->base.widget);
633 wd->items = eina_list_remove(wd->items, item);
634 elm_widget_item_pre_notify_del(item);
635 eina_stringshare_del(item->label);
636 eina_stringshare_del(item->icon_file);
637 eina_stringshare_del(item->icon_group);
638 elm_widget_item_del(item);
642 * Set the function called when an item within the hoversel
643 * is freed. That function will receive these parameters:
646 * Evas_Object *the_item_object
647 * Elm_Hoversel_Item *the_object_struct
649 * @param item The item to set the callback on
650 * @param func The function called
655 elm_hoversel_item_del_cb_set(Elm_Hoversel_Item *item, Evas_Smart_Cb func)
657 ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item);
658 elm_widget_item_del_cb_set(item, func);
662 * This returns the data pointer supplied with elm_hoversel_item_add() that
663 * will be passed to associated function callbacks.
665 * @param item The item to get the data from
666 * @return The data pointer set with elm_hoversel_item_add()
671 elm_hoversel_item_data_get(const Elm_Hoversel_Item *item)
673 ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item, NULL);
674 return elm_widget_item_data_get(item);
678 * This returns the label text of the given hoversel item.
680 * @param item The item to get the label
681 * @return The label text of the hoversel item
686 elm_hoversel_item_label_get(const Elm_Hoversel_Item *item)
688 ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item, NULL);
693 * This sets the icon for the given hoversel item. The icon can be loaded from
694 * the standard set, from an image file, or from an edje file.
696 * @param item The item to set the icon
697 * @param icon_file An image file path on disk to use for the icon or standard
699 * @param icon_group The edje group to use if @p icon_file is an edje file. Set this
700 * to NULL if the icon is not an edje file
701 * @param icon_type The icon type
706 elm_hoversel_item_icon_set(Elm_Hoversel_Item *item, const char *icon_file, const char *icon_group, Elm_Icon_Type icon_type)
708 ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item);
709 eina_stringshare_replace(&item->icon_file, icon_file);
710 eina_stringshare_replace(&item->icon_group, icon_group);
711 item->icon_type = icon_type;
715 * Get the icon object of the hoversel item
717 * @param item The item to get the icon from
718 * @param icon_file The image file path on disk used for the icon or standard
720 * @param icon_group The edje group used if @p icon_file is an edje file. NULL
721 * if the icon is not an edje file
722 * @param icon_type The icon type
727 elm_hoversel_item_icon_get(const Elm_Hoversel_Item *item, const char **icon_file, const char **icon_group, Elm_Icon_Type *icon_type)
729 ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item);
730 if (icon_file) *icon_file = item->icon_file;
731 if (icon_group) *icon_group = item->icon_group;
732 if (icon_type) *icon_type = item->icon_type;