1 #include <Elementary.h>
5 * @defgroup Hoversel Hoversel
8 * A hoversel is a button that pops up a list of items (automatically
9 * choosing the direction to display) that have a lable and/or an icon to
10 * select from. It is a convenience widget to avoid the need to do all the
11 * piecing together yourself. It is intended for a small number of items in
12 * the hoversel menu (no more than 8), though is capable of many more.
14 * Signals that you can add callbacks for are:
16 * clicked - the user clicked the hoversel button and popped up the sel
18 * selected - an item in the hoversel list is selected. event_info is the item
19 * selected - Elm_Hoversel_Item
21 * dismissed - the hover is dismissed
23 typedef struct _Widget_Data Widget_Data;
27 Evas_Object *btn, *hover;
28 Evas_Object *hover_parent;
30 Eina_Bool horizontal : 1;
33 struct _Elm_Hoversel_Item
37 const char *icon_file;
38 const char *icon_group;
39 Elm_Icon_Type icon_type;
45 static const char *widtype = NULL;
46 static void _del_pre_hook(Evas_Object *obj);
47 static void _del_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 *it;
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, it)
63 if (it->del_cb) it->del_cb((void *)it->data, it->obj, it);
64 eina_stringshare_del(it->label);
65 eina_stringshare_del(it->icon_file);
66 eina_stringshare_del(it->icon_group);
72 _del_hook(Evas_Object *obj)
74 Widget_Data *wd = elm_widget_data_get(obj);
80 _theme_hook(Evas_Object *obj)
82 Widget_Data *wd = elm_widget_data_get(obj);
85 elm_hoversel_hover_end(obj);
87 snprintf(buf, sizeof(buf), "hoversel_horizontal/%s", elm_widget_style_get(obj));
89 snprintf(buf, sizeof(buf), "hoversel_vertical/%s", elm_widget_style_get(obj));
90 elm_object_style_set(wd->btn, buf);
94 _disable_hook(Evas_Object *obj)
96 Widget_Data *wd = elm_widget_data_get(obj);
98 if (elm_widget_disabled_get(obj))
99 elm_widget_disabled_set(wd->btn, 1);
101 elm_widget_disabled_set(wd->btn, 0);
105 _sizing_eval(Evas_Object *obj)
107 Widget_Data *wd = elm_widget_data_get(obj);
108 Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
110 evas_object_size_hint_min_get(wd->btn, &minw, &minh);
111 evas_object_size_hint_max_get(wd->btn, &maxw, &maxh);
112 evas_object_size_hint_min_set(obj, minw, minh);
113 evas_object_size_hint_max_set(obj, maxw, maxh);
117 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
123 _hover_clicked(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
125 elm_hoversel_hover_end(data);
129 _item_clicked(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
131 Elm_Hoversel_Item *it = data;
132 Evas_Object *obj2 = it->obj;
134 elm_hoversel_hover_end(obj2);
135 if (it->func) it->func(it->data, obj2, it);
136 evas_object_smart_callback_call(obj2, "selected", it);
140 _activate(Evas_Object *obj)
142 Widget_Data *wd = elm_widget_data_get(obj);
143 Evas_Object *bt, *bx, *ic;
145 const Elm_Hoversel_Item *it;
149 if (elm_widget_disabled_get(obj)) return;
150 wd->hover = elm_hover_add(obj);
152 snprintf(buf, sizeof(buf), "hoversel_horizontal/%s", elm_widget_style_get(obj));
154 snprintf(buf, sizeof(buf), "hoversel_vertical/%s", elm_widget_style_get(obj));
155 elm_object_style_set(wd->hover, buf);
156 evas_object_smart_callback_add(wd->hover, "clicked", _hover_clicked, obj);
157 elm_hover_parent_set(wd->hover, wd->hover_parent);
158 elm_hover_target_set(wd->hover, wd->btn);
160 bx = elm_box_add(wd->hover);
161 elm_box_homogenous_set(bx, 1);
163 elm_box_horizontal_set(bx, wd->horizontal);
166 snprintf(buf, sizeof(buf), "hoversel_horizontal_entry/%s",
167 elm_widget_style_get(obj));
169 snprintf(buf, sizeof(buf), "hoversel_vertical_entry/%s",
170 elm_widget_style_get(obj));
171 EINA_LIST_FOREACH(wd->items, l, it)
173 bt = elm_button_add(wd->hover);
174 elm_object_style_set(bt, buf);
175 elm_button_label_set(bt, it->label);
178 ic = elm_icon_add(obj);
179 elm_icon_scale_set(ic, 0, 1);
180 if (it->icon_type == ELM_ICON_FILE)
181 elm_icon_file_set(ic, it->icon_file, it->icon_group);
182 else if (it->icon_type == ELM_ICON_STANDARD)
183 elm_icon_standard_set(ic, it->icon_file);
184 elm_button_icon_set(bt, ic);
185 evas_object_show(ic);
187 evas_object_size_hint_weight_set(bt, 1.0, 0.0);
188 evas_object_size_hint_align_set(bt, -1.0, -1.0);
189 elm_box_pack_end(bx, bt);
190 evas_object_smart_callback_add(bt, "clicked", _item_clicked, it);
191 evas_object_show(bt);
195 elm_hover_content_set(wd->hover,
196 elm_hover_best_content_location_get(wd->hover,
197 ELM_HOVER_AXIS_HORIZONTAL),
200 elm_hover_content_set(wd->hover,
201 elm_hover_best_content_location_get(wd->hover,
202 ELM_HOVER_AXIS_VERTICAL),
204 evas_object_show(bx);
206 evas_object_show(wd->hover);
207 evas_object_smart_callback_call(obj, "clicked", NULL);
209 // if (wd->horizontal) evas_object_hide(wd->btn);
213 _button_clicked(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
219 _parent_del(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
221 Widget_Data *wd = elm_widget_data_get(data);
223 wd->hover_parent = NULL;
227 * Add a new Hoversel object
229 * @param parent The parent object
230 * @return The new object or NULL if it cannot be created
235 elm_hoversel_add(Evas_Object *parent)
242 wd = ELM_NEW(Widget_Data);
243 e = evas_object_evas_get(parent);
244 obj = elm_widget_add(e);
245 ELM_SET_WIDTYPE(widtype, "hoversel");
246 elm_widget_type_set(obj, "hoversel");
247 elm_widget_sub_object_add(parent, obj);
248 elm_widget_data_set(obj, wd);
249 elm_widget_del_pre_hook_set(obj, _del_pre_hook);
250 elm_widget_del_hook_set(obj, _del_hook);
251 elm_widget_theme_hook_set(obj, _theme_hook);
252 elm_widget_disable_hook_set(obj, _disable_hook);
254 wd->btn = elm_button_add(parent);
256 snprintf(buf, sizeof(buf), "hoversel_horizontal/%s", elm_widget_style_get(obj));
258 snprintf(buf, sizeof(buf), "hoversel_vertical/%s", elm_widget_style_get(obj));
259 elm_object_style_set(wd->btn, buf);
260 elm_widget_resize_object_set(obj, wd->btn);
261 evas_object_event_callback_add(wd->btn, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
262 _changed_size_hints, obj);
263 evas_object_smart_callback_add(wd->btn, "clicked", _button_clicked, obj);
264 elm_hoversel_hover_parent_set(obj, parent);
270 * Set the Hover parent
272 * Sets the hover parent object. Should probably be the window that the hoversel
273 * is in. See Hover objects for more information.
275 * @param obj The hoversel object
276 * @param parent The parent to use
281 elm_hoversel_hover_parent_set(Evas_Object *obj, Evas_Object *parent)
283 ELM_CHECK_WIDTYPE(obj, widtype);
284 Widget_Data *wd = elm_widget_data_get(obj);
286 if (wd->hover_parent)
287 evas_object_event_callback_del_full(wd->hover_parent, EVAS_CALLBACK_DEL,
289 wd->hover_parent = parent;
290 if (wd->hover_parent)
291 evas_object_event_callback_add(wd->hover_parent, EVAS_CALLBACK_DEL,
296 * Set the hoversel button label
298 * This sets the label of the button that is always visible (before it is
299 * clicked and expanded). Also see elm_button_label_set().
301 * @param obj The hoversel object
302 * @param label The label text.
307 elm_hoversel_label_set(Evas_Object *obj, const char *label)
309 ELM_CHECK_WIDTYPE(obj, widtype);
310 Widget_Data *wd = elm_widget_data_get(obj);
312 elm_button_label_set(wd->btn, label);
316 * Get the hoversel button label
318 * @param obj The hoversel object
319 * @return The label text.
324 elm_hoversel_label_get(const Evas_Object *obj)
326 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
327 Widget_Data *wd = elm_widget_data_get(obj);
328 if ((!wd) || (!wd->btn)) return NULL;
329 return elm_button_label_get(wd->btn);
333 * This sets the hoversel to expand horizontally. The initial button
334 * will display horizontally regardless of this setting.
336 * @param obj The hoversel object
337 * @param horizontal If true, the hover will expand horizontally to the right.
342 elm_hoversel_horizontal_set(Evas_Object *obj, Eina_Bool horizontal)
344 ELM_CHECK_WIDTYPE(obj, widtype);
345 Widget_Data *wd = elm_widget_data_get(obj);
347 wd->horizontal = !!horizontal;
352 * This returns whether the hoversel is set to expand horizontally.
354 * @param obj The hoversel object
355 * @return If true, the hover will expand horizontally to the right.
360 elm_hoversel_horizontal_get(const Evas_Object *obj)
362 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
363 Widget_Data *wd = elm_widget_data_get(obj);
364 if (!wd) return EINA_FALSE;
365 return wd->horizontal;
369 * Set the icon of the hoversel button
371 * Sets the icon of the button that is always visible (before it is clicked
372 * and expanded). Also see elm_button_icon_set().
374 * @param obj The hoversel object
375 * @param icon The icon object
380 elm_hoversel_icon_set(Evas_Object *obj, Evas_Object *icon)
382 ELM_CHECK_WIDTYPE(obj, widtype);
383 Widget_Data *wd = elm_widget_data_get(obj);
385 elm_button_icon_set(wd->btn, icon);
389 * Get the icon of the hoversel button
391 * Get the icon of the button that is always visible (before it is clicked
392 * and expanded). Also see elm_button_icon_get().
394 * @param obj The hoversel object
395 * @return The icon object
400 elm_hoversel_icon_get(const Evas_Object *obj)
402 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
403 Widget_Data *wd = elm_widget_data_get(obj);
404 if ((!wd) || (!wd->btn)) return NULL;
405 return elm_button_icon_get(wd->btn);
409 * This triggers the hoversel popup from code, the same as though the
410 * user clicked the button.
412 * @param obj The hoversel object
417 elm_hoversel_hover_begin(Evas_Object *obj)
419 ELM_CHECK_WIDTYPE(obj, widtype);
420 Widget_Data *wd = elm_widget_data_get(obj);
422 if (wd->hover) return;
427 * This ends the hoversel popup as though the user clicked outside the hover.
429 * @param obj The hoversel object
434 elm_hoversel_hover_end(Evas_Object *obj)
436 ELM_CHECK_WIDTYPE(obj, widtype);
437 Widget_Data *wd = elm_widget_data_get(obj);
439 if (!wd->hover) return;
440 evas_object_del(wd->hover);
442 evas_object_smart_callback_call(obj, "dismissed", NULL);
446 * Returns whether the hoversel is expanded.
448 * @param obj The hoversel object
449 * @return This will return EINA_TRUE if the hoversel
450 * is expanded or EINA_FALSE if it is not expanded.
455 elm_hoversel_expanded_get(Evas_Object *obj)
457 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
458 Widget_Data *wd = elm_widget_data_get(obj);
459 if (!wd) return EINA_FALSE;
460 return (wd->hover) ? EINA_TRUE : EINA_FALSE;
464 * This will remove all the children items from the hoversel. (should not be
465 * called while the hoversel is active; use elm_hoversel_expanded_get()
468 * @param obj The hoversel object
473 elm_hoversel_clear(Evas_Object *obj)
475 Elm_Hoversel_Item *it;
477 ELM_CHECK_WIDTYPE(obj, widtype);
478 Widget_Data *wd = elm_widget_data_get(obj);
480 EINA_LIST_FOREACH_SAFE(wd->items, l, ll, it) elm_hoversel_item_del(it);
484 * Get the list of items within the given hoversel.
486 * @param obj The hoversel object
487 * @return Returns a list of Elm_Hoversel_Item*
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;
501 * Add an item to the hoversel button
503 * This adds an item to the hoversel to show when it is clicked. Note: if you
504 * need to use an icon from an edje file then use elm_hoversel_item_icon_set()
505 * right after the this function, and set icon_file to NULL here.
507 * @param obj The hoversel object
508 * @param label The text label to use for the item (NULL if not desired)
509 * @param icon_file An image file path on disk to use for the icon or standard
510 * icon name (NULL if not desired)
511 * @param icon_type The icon type if relevant
512 * @param func Convenience function to call when this item is selected
513 * @param data Data to pass to item-related functions
514 * @return A handle to the item added.
518 EAPI Elm_Hoversel_Item *
519 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)
521 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
522 Widget_Data *wd = elm_widget_data_get(obj);
523 if (!wd) return NULL;
524 Elm_Hoversel_Item *it = calloc(1, sizeof(Elm_Hoversel_Item));
525 if (!it) return NULL;
526 wd->items = eina_list_append(wd->items, it);
528 it->label = eina_stringshare_add(label);
529 it->icon_file = eina_stringshare_add(icon_file);
530 it->icon_type = icon_type;
532 it->data = (void *)data;
537 * Delete an item from the hoversel
539 * This deletes the item from the hoversel (should not be called while the
540 * hoversel is active; use elm_hoversel_expanded_get()
543 * @param it The item to delete
548 elm_hoversel_item_del(Elm_Hoversel_Item *it)
551 Widget_Data *wd = elm_widget_data_get(it->obj);
552 if (it->del_cb) it->del_cb((void *)it->data, it->obj, it);
554 elm_hoversel_hover_end(it->obj);
555 wd->items = eina_list_remove(wd->items, it);
556 eina_stringshare_del(it->label);
557 eina_stringshare_del(it->icon_file);
558 eina_stringshare_del(it->icon_group);
563 * Set the function called when an item within the hoversel
564 * is freed. That function will receive these parameters:
567 * Evas_Object *the_item_object
568 * Elm_Hoversel_Item *the_object_struct
570 * @param it The item to set the callback on
571 * @param func The function called
576 elm_hoversel_item_del_cb_set(Elm_Hoversel_Item *it, Evas_Smart_Cb func)
583 * This returns the data pointer supplied with elm_hoversel_item_add() that
584 * will be passed to associated function callbacks.
586 * @param it The item to get the data from
587 * @return The data pointer set with elm_hoversel_item_add()
592 elm_hoversel_item_data_get(Elm_Hoversel_Item *it)
594 if (!it) return NULL;
599 * This returns the label text of the given hoversel item.
601 * @param it The item to get the label
602 * @return The label text of the hoversel item
607 elm_hoversel_item_label_get(Elm_Hoversel_Item *it)
609 if (!it) return NULL;
614 * This sets the icon for the given hoversel item. The icon can be loaded from
615 * the standard set, from an image file, or from an edje file.
617 * @param it The item to set the icon
618 * @param icon_file An image file path on disk to use for the icon or standard
620 * @param icon_group The edje group to use if @p icon_file is an edje file. Set this
621 * to NULL if the icon is not an edje file
622 * @param icon_type The icon type
627 elm_hoversel_item_icon_set(Elm_Hoversel_Item *it, const char *icon_file, const char *icon_group, Elm_Icon_Type icon_type)
630 eina_stringshare_replace(&it->icon_file, icon_file);
631 eina_stringshare_replace(&it->icon_group, icon_group);
632 it->icon_type = icon_type;
636 * Get the icon object of the hoversel item
638 * @param it The item to get the icon from
639 * @param icon_file The image file path on disk used for the icon or standard
641 * @param icon_group The edje group used if @p icon_file is an edje file. NULL
642 * if the icon is not an edje file
643 * @param icon_type The icon type
648 elm_hoversel_item_icon_get(Elm_Hoversel_Item *it, const char **icon_file, const char **icon_group, Elm_Icon_Type *icon_type)
651 if (icon_file) *icon_file = it->icon_file;
652 if (icon_group) *icon_group = it->icon_group;
653 if (icon_type) *icon_type = it->icon_type;