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
16 * "selected" - an item in the hoversel list is selected. event_info is the item
17 * "dismissed" - the hover is dismissed
19 typedef struct _Widget_Data Widget_Data;
23 Evas_Object *btn, *hover;
24 Evas_Object *hover_parent;
26 Eina_Bool horizontal : 1;
27 Eina_Bool expanded : 1;
30 struct _Elm_Hoversel_Item
34 const char *icon_file;
35 const char *icon_group;
36 Elm_Icon_Type icon_type;
40 static const char *widtype = NULL;
41 static void _del_pre_hook(Evas_Object *obj);
42 static void _del_hook(Evas_Object *obj);
43 static void _activate(Evas_Object *obj);
44 static void _activate_hook(Evas_Object *obj);
45 static void _disable_hook(Evas_Object *obj);
46 static void _sizing_eval(Evas_Object *obj);
47 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
48 static void _parent_del(void *data, Evas *e, Evas_Object *obj, void *event_info);
50 static const char SIG_CLICKED[] = "clicked";
51 static const char SIG_SELECTED[] = "selected";
52 static const char SIG_DISMISSED[] = "dismissed";
54 static const Evas_Smart_Cb_Description _signals[] = {
62 _del_pre_hook(Evas_Object *obj)
64 Elm_Hoversel_Item *item;
65 Widget_Data *wd = elm_widget_data_get(obj);
67 elm_hoversel_hover_end(obj);
68 elm_hoversel_hover_parent_set(obj, NULL);
69 EINA_LIST_FREE(wd->items, item)
71 elm_widget_item_pre_notify_del(item);
72 eina_stringshare_del(item->label);
73 eina_stringshare_del(item->icon_file);
74 eina_stringshare_del(item->icon_group);
75 elm_widget_item_del(item);
80 _del_hook(Evas_Object *obj)
82 Widget_Data *wd = elm_widget_data_get(obj);
88 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
90 Widget_Data *wd = elm_widget_data_get(obj);
92 elm_widget_mirrored_set(wd->btn, rtl);
93 elm_widget_mirrored_set(wd->hover, rtl);
97 _theme_hook(Evas_Object *obj)
99 Widget_Data *wd = elm_widget_data_get(obj);
102 _elm_widget_mirrored_reload(obj);
104 elm_hoversel_hover_end(obj);
106 snprintf(buf, sizeof(buf), "hoversel_horizontal/%s", elm_widget_style_get(obj));
108 snprintf(buf, sizeof(buf), "hoversel_vertical/%s", elm_widget_style_get(obj));
109 elm_object_style_set(wd->btn, buf);
110 elm_object_disabled_set(wd->btn, elm_widget_disabled_get(obj));
111 _mirrored_set(obj, elm_widget_mirrored_get(obj));
115 _disable_hook(Evas_Object *obj)
117 Widget_Data *wd = elm_widget_data_get(obj);
119 elm_object_disabled_set(wd->btn, elm_widget_disabled_get(obj));
123 _sizing_eval(Evas_Object *obj)
125 Widget_Data *wd = elm_widget_data_get(obj);
126 Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
128 evas_object_size_hint_min_get(wd->btn, &minw, &minh);
129 evas_object_size_hint_max_get(wd->btn, &maxw, &maxh);
130 evas_object_size_hint_min_set(obj, minw, minh);
131 evas_object_size_hint_max_set(obj, maxw, maxh);
135 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
137 Widget_Data *wd = elm_widget_data_get(obj);
139 if (elm_widget_focus_get(obj))
140 elm_widget_focus_steal(wd->btn);
144 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
150 _hover_clicked(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
152 elm_hoversel_hover_end(data);
156 _item_clicked(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
158 Elm_Hoversel_Item *item = data;
159 Evas_Object *obj2 = item->base.widget;
161 elm_hoversel_hover_end(obj2);
162 if (item->func) item->func((void *)item->base.data, obj2, item);
163 evas_object_smart_callback_call(obj2, SIG_SELECTED, item);
167 _activate(Evas_Object *obj)
169 Widget_Data *wd = elm_widget_data_get(obj);
170 Evas_Object *bt, *bx, *ic;
172 const Elm_Hoversel_Item *item;
178 elm_hoversel_hover_end(obj);
181 wd->expanded = EINA_TRUE;
183 if (elm_widget_disabled_get(obj)) return;
184 wd->hover = elm_hover_add(obj);
185 elm_widget_mirrored_automatic_set(wd->hover, EINA_FALSE);
187 snprintf(buf, sizeof(buf), "hoversel_horizontal/%s", elm_widget_style_get(obj));
189 snprintf(buf, sizeof(buf), "hoversel_vertical/%s", elm_widget_style_get(obj));
190 elm_object_style_set(wd->hover, buf);
191 evas_object_smart_callback_add(wd->hover, "clicked", _hover_clicked, obj);
192 elm_hover_parent_set(wd->hover, wd->hover_parent);
193 elm_hover_target_set(wd->hover, wd->btn);
195 bx = elm_box_add(wd->hover);
196 elm_widget_mirrored_automatic_set(bx, EINA_FALSE);
197 elm_box_homogeneous_set(bx, 1);
199 elm_box_horizontal_set(bx, wd->horizontal);
202 snprintf(buf, sizeof(buf), "hoversel_horizontal_entry/%s",
203 elm_widget_style_get(obj));
205 snprintf(buf, sizeof(buf), "hoversel_vertical_entry/%s",
206 elm_widget_style_get(obj));
207 EINA_LIST_FOREACH(wd->items, l, item)
209 bt = elm_button_add(wd->hover);
210 elm_widget_mirrored_automatic_set(bt, EINA_FALSE);
211 elm_widget_mirrored_set(bt, elm_widget_mirrored_get(obj));
212 elm_object_style_set(bt, buf);
213 elm_object_text_set(bt, item->label);
216 ic = elm_icon_add(obj);
217 elm_icon_scale_set(ic, 0, 1);
218 if (item->icon_type == ELM_ICON_FILE)
219 elm_icon_file_set(ic, item->icon_file, item->icon_group);
220 else if (item->icon_type == ELM_ICON_STANDARD)
221 elm_icon_standard_set(ic, item->icon_file);
222 elm_button_icon_set(bt, ic);
223 evas_object_show(ic);
225 evas_object_size_hint_weight_set(bt, EVAS_HINT_EXPAND, 0.0);
226 evas_object_size_hint_align_set(bt, EVAS_HINT_FILL, EVAS_HINT_FILL);
227 elm_box_pack_end(bx, bt);
228 evas_object_smart_callback_add(bt, "clicked", _item_clicked, item);
229 evas_object_show(bt);
233 elm_hover_content_set(wd->hover,
234 elm_hover_best_content_location_get(wd->hover,
235 ELM_HOVER_AXIS_HORIZONTAL),
238 elm_hover_content_set(wd->hover,
239 elm_hover_best_content_location_get(wd->hover,
240 ELM_HOVER_AXIS_VERTICAL),
242 evas_object_show(bx);
244 evas_object_show(wd->hover);
245 evas_object_smart_callback_call(obj, SIG_CLICKED, NULL);
247 // if (wd->horizontal) evas_object_hide(wd->btn);
251 _activate_hook(Evas_Object *obj)
257 _button_clicked(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
263 _parent_del(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
265 Widget_Data *wd = elm_widget_data_get(data);
267 wd->hover_parent = NULL;
271 _elm_hoversel_label_set(Evas_Object *obj, const char *item, const char *label)
273 ELM_CHECK_WIDTYPE(obj, widtype);
274 Widget_Data *wd = elm_widget_data_get(obj);
277 elm_object_text_set(wd->btn, label);
281 _elm_hoversel_label_get(const Evas_Object *obj, const char *item)
283 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
284 Widget_Data *wd = elm_widget_data_get(obj);
285 if (item) return NULL;
286 if ((!wd) || (!wd->btn)) return NULL;
287 return elm_object_text_get(wd->btn);
291 * Add a new Hoversel object
293 * @param parent The parent object
294 * @return The new object or NULL if it cannot be created
299 elm_hoversel_add(Evas_Object *parent)
305 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
307 ELM_SET_WIDTYPE(widtype, "hoversel");
308 elm_widget_type_set(obj, "hoversel");
309 elm_widget_sub_object_add(parent, obj);
310 elm_widget_data_set(obj, wd);
311 elm_widget_del_pre_hook_set(obj, _del_pre_hook);
312 elm_widget_del_hook_set(obj, _del_hook);
313 elm_widget_theme_hook_set(obj, _theme_hook);
314 elm_widget_disable_hook_set(obj, _disable_hook);
315 elm_widget_activate_hook_set(obj, _activate_hook);
316 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
317 elm_widget_can_focus_set(obj, EINA_TRUE);
318 elm_widget_label_set_hook_set(obj, _elm_hoversel_label_set);
319 elm_widget_label_get_hook_set(obj, _elm_hoversel_label_get);
321 wd->btn = elm_button_add(parent);
322 elm_widget_mirrored_automatic_set(wd->btn, EINA_FALSE);
323 wd->expanded = EINA_FALSE;
324 elm_widget_resize_object_set(obj, wd->btn);
325 evas_object_event_callback_add(wd->btn, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
326 _changed_size_hints, obj);
327 evas_object_smart_callback_add(wd->btn, "clicked", _button_clicked, obj);
328 evas_object_smart_callbacks_descriptions_set(obj, _signals);
330 elm_widget_sub_object_add(obj, wd->btn);
332 elm_hoversel_hover_parent_set(obj, parent);
339 * Set the Hover parent
341 * Sets the hover parent object. Should probably be the window that the hoversel
342 * is in. See Hover objects for more information.
344 * @param obj The hoversel object
345 * @param parent The parent to use
350 elm_hoversel_hover_parent_set(Evas_Object *obj, Evas_Object *parent)
352 ELM_CHECK_WIDTYPE(obj, widtype);
353 Widget_Data *wd = elm_widget_data_get(obj);
355 if (wd->hover_parent)
356 evas_object_event_callback_del_full(wd->hover_parent, EVAS_CALLBACK_DEL,
358 wd->hover_parent = parent;
359 if (wd->hover_parent)
360 evas_object_event_callback_add(wd->hover_parent, EVAS_CALLBACK_DEL,
365 * Get the Hover parent
367 * Gets the hover parent object. Should probably be the window that the hoversel
368 * is in. See Hover objects for more information.
370 * @param obj The hoversel object
371 * @return The used parent
376 elm_hoversel_hover_parent_get(const Evas_Object *obj)
378 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
379 Widget_Data *wd = elm_widget_data_get(obj);
380 if (!wd) return NULL;
381 return wd->hover_parent;
385 * Set the hoversel button label
387 * This sets the label of the button that is always visible (before it is
388 * clicked and expanded). Also see elm_object_text_set().
390 * @param obj The hoversel object
391 * @param label The label text.
396 elm_hoversel_label_set(Evas_Object *obj, const char *label)
398 _elm_hoversel_label_set(obj, NULL, label);
402 * Get the hoversel button label
404 * @param obj The hoversel object
405 * @return The label text.
410 elm_hoversel_label_get(const Evas_Object *obj)
412 return _elm_hoversel_label_get(obj, NULL);
416 * This sets the hoversel to expand horizontally. The initial button
417 * will display horizontally regardless of this setting.
419 * @param obj The hoversel object
420 * @param horizontal If true, the hover will expand horizontally to the right.
425 elm_hoversel_horizontal_set(Evas_Object *obj, Eina_Bool horizontal)
427 ELM_CHECK_WIDTYPE(obj, widtype);
428 Widget_Data *wd = elm_widget_data_get(obj);
430 wd->horizontal = !!horizontal;
435 * This returns whether the hoversel is set to expand horizontally.
437 * @param obj The hoversel object
438 * @return If true, the hover will expand horizontally to the right.
443 elm_hoversel_horizontal_get(const Evas_Object *obj)
445 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
446 Widget_Data *wd = elm_widget_data_get(obj);
447 if (!wd) return EINA_FALSE;
448 return wd->horizontal;
452 * Set the icon of the hoversel button
454 * Sets the icon of the button that is always visible (before it is clicked
455 * and expanded). Also see elm_button_icon_set().
456 * Once the icon object is set, a previously set one will be deleted
457 * If you want to keep that old content object, use the
458 * elm_hoversel_icon_unset() function.
460 * @param obj The hoversel object
461 * @param icon The icon object
466 elm_hoversel_icon_set(Evas_Object *obj, Evas_Object *icon)
468 ELM_CHECK_WIDTYPE(obj, widtype);
469 Widget_Data *wd = elm_widget_data_get(obj);
471 elm_button_icon_set(wd->btn, icon);
475 * Get the icon of the hoversel button
477 * Get the icon of the button that is always visible (before it is clicked
478 * and expanded). Also see elm_button_icon_get().
480 * @param obj The hoversel object
481 * @return The icon object
486 elm_hoversel_icon_get(const Evas_Object *obj)
488 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
489 Widget_Data *wd = elm_widget_data_get(obj);
490 if ((!wd) || (!wd->btn)) return NULL;
491 return elm_button_icon_get(wd->btn);
495 * Get the icon of the hoversel button
497 * Unparent and return the icon of the button that is always visible
498 * (before it is clicked and expanded). Also see elm_button_icon_unset().
500 * @param obj The hoversel object
501 * @return The icon object that was being used
506 elm_hoversel_icon_unset(Evas_Object *obj)
508 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
509 Widget_Data *wd = elm_widget_data_get(obj);
510 if ((!wd) || (!wd->btn)) return NULL;
511 return elm_button_icon_unset(wd->btn);
515 * This triggers the hoversel popup from code, the same as though the
516 * user clicked the button.
518 * @param obj The hoversel object
523 elm_hoversel_hover_begin(Evas_Object *obj)
525 ELM_CHECK_WIDTYPE(obj, widtype);
526 Widget_Data *wd = elm_widget_data_get(obj);
528 if (wd->hover) return;
533 * This ends the hoversel popup as though the user clicked outside the hover.
535 * @param obj The hoversel object
540 elm_hoversel_hover_end(Evas_Object *obj)
542 ELM_CHECK_WIDTYPE(obj, widtype);
543 Widget_Data *wd = elm_widget_data_get(obj);
545 if (!wd->hover) return;
546 wd->expanded = EINA_FALSE;
547 evas_object_del(wd->hover);
549 evas_object_smart_callback_call(obj, SIG_DISMISSED, NULL);
553 * Returns whether the hoversel is expanded.
555 * @param obj The hoversel object
556 * @return This will return EINA_TRUE if the hoversel
557 * is expanded or EINA_FALSE if it is not expanded.
562 elm_hoversel_expanded_get(const Evas_Object *obj)
564 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
565 Widget_Data *wd = elm_widget_data_get(obj);
566 if (!wd) return EINA_FALSE;
567 return (wd->hover) ? EINA_TRUE : EINA_FALSE;
571 * This will remove all the children items from the hoversel. (should not be
572 * called while the hoversel is active; use elm_hoversel_expanded_get()
575 * @param obj The hoversel object
580 elm_hoversel_clear(Evas_Object *obj)
582 Elm_Hoversel_Item *item;
584 ELM_CHECK_WIDTYPE(obj, widtype);
585 Widget_Data *wd = elm_widget_data_get(obj);
587 EINA_LIST_FOREACH_SAFE(wd->items, l, ll, item) elm_hoversel_item_del(item);
591 * Get the list of items within the given hoversel.
593 * @param obj The hoversel object
594 * @return Returns a list of Elm_Hoversel_Item*
598 EAPI const Eina_List *
599 elm_hoversel_items_get(const Evas_Object *obj)
601 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
602 Widget_Data *wd = elm_widget_data_get(obj);
603 if (!wd) return NULL;
608 * Add an item to the hoversel button
610 * This adds an item to the hoversel to show when it is clicked. Note: if you
611 * need to use an icon from an edje file then use elm_hoversel_item_icon_set()
612 * right after the this function, and set icon_file to NULL here.
614 * @param obj The hoversel object
615 * @param label The text label to use for the item (NULL if not desired)
616 * @param icon_file An image file path on disk to use for the icon or standard
617 * icon name (NULL if not desired)
618 * @param icon_type The icon type if relevant
619 * @param func Convenience function to call when this item is selected
620 * @param data Data to pass to item-related functions
621 * @return A handle to the item added.
625 EAPI Elm_Hoversel_Item *
626 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)
628 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
629 Widget_Data *wd = elm_widget_data_get(obj);
630 if (!wd) return NULL;
631 Elm_Hoversel_Item *item = elm_widget_item_new(obj, Elm_Hoversel_Item);
632 if (!item) return NULL;
633 wd->items = eina_list_append(wd->items, item);
634 item->label = eina_stringshare_add(label);
635 item->icon_file = eina_stringshare_add(icon_file);
636 item->icon_type = icon_type;
638 item->base.data = data;
643 * Delete an item from the hoversel
645 * This deletes the item from the hoversel (should not be called while the
646 * hoversel is active; use elm_hoversel_expanded_get()
649 * @param item The item to delete
654 elm_hoversel_item_del(Elm_Hoversel_Item *item)
656 ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item);
657 Widget_Data *wd = elm_widget_data_get(item->base.widget);
659 elm_hoversel_hover_end(item->base.widget);
660 wd->items = eina_list_remove(wd->items, item);
661 elm_widget_item_pre_notify_del(item);
662 eina_stringshare_del(item->label);
663 eina_stringshare_del(item->icon_file);
664 eina_stringshare_del(item->icon_group);
665 elm_widget_item_del(item);
669 * Set the function called when an item within the hoversel
670 * is freed. That function will receive these parameters:
673 * Evas_Object *the_item_object
674 * Elm_Hoversel_Item *the_object_struct
676 * @param item The item to set the callback on
677 * @param func The function called
682 elm_hoversel_item_del_cb_set(Elm_Hoversel_Item *item, Evas_Smart_Cb func)
684 ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item);
685 elm_widget_item_del_cb_set(item, func);
689 * This returns the data pointer supplied with elm_hoversel_item_add() that
690 * will be passed to associated function callbacks.
692 * @param item The item to get the data from
693 * @return The data pointer set with elm_hoversel_item_add()
698 elm_hoversel_item_data_get(const Elm_Hoversel_Item *item)
700 ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item, NULL);
701 return elm_widget_item_data_get(item);
705 * This returns the label text of the given hoversel item.
707 * @param item The item to get the label
708 * @return The label text of the hoversel item
713 elm_hoversel_item_label_get(const Elm_Hoversel_Item *item)
715 ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item, NULL);
720 * This sets the icon for the given hoversel item. The icon can be loaded from
721 * the standard set, from an image file, or from an edje file.
723 * @param item The item to set the icon
724 * @param icon_file An image file path on disk to use for the icon or standard
726 * @param icon_group The edje group to use if @p icon_file is an edje file. Set this
727 * to NULL if the icon is not an edje file
728 * @param icon_type The icon type
733 elm_hoversel_item_icon_set(Elm_Hoversel_Item *item, const char *icon_file, const char *icon_group, Elm_Icon_Type icon_type)
735 ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item);
736 eina_stringshare_replace(&item->icon_file, icon_file);
737 eina_stringshare_replace(&item->icon_group, icon_group);
738 item->icon_type = icon_type;
742 * Get the icon object of the hoversel item
744 * @param item The item to get the icon from
745 * @param icon_file The image file path on disk used for the icon or standard
747 * @param icon_group The edje group used if @p icon_file is an edje file. NULL
748 * if the icon is not an edje file
749 * @param icon_type The icon type
754 elm_hoversel_item_icon_get(const Elm_Hoversel_Item *item, const char **icon_file, const char **icon_group, Elm_Icon_Type *icon_type)
756 ELM_WIDGET_ITEM_WIDTYPE_CHECK_OR_RETURN(item);
757 if (icon_file) *icon_file = item->icon_file;
758 if (icon_group) *icon_group = item->icon_group;
759 if (icon_type) *icon_type = item->icon_type;