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;
32 struct _Elm_Hoversel_Item
36 const char *icon_file;
37 const char *icon_group;
38 Elm_Icon_Type icon_type;
44 static const char *widtype = NULL;
45 static void _del_pre_hook(Evas_Object *obj);
46 static void _del_hook(Evas_Object *obj);
47 static void _disable_hook(Evas_Object *obj);
48 static void _sizing_eval(Evas_Object *obj);
49 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
50 static void _parent_del(void *data, Evas *e, Evas_Object *obj, void *event_info);
53 _del_pre_hook(Evas_Object *obj)
55 Elm_Hoversel_Item *it;
56 Widget_Data *wd = elm_widget_data_get(obj);
58 elm_hoversel_hover_end(obj);
59 elm_hoversel_hover_parent_set(obj, NULL);
60 EINA_LIST_FREE(wd->items, it)
62 if (it->del_cb) it->del_cb((void *)it->data, it->obj, it);
63 eina_stringshare_del(it->label);
64 eina_stringshare_del(it->icon_file);
65 eina_stringshare_del(it->icon_group);
71 _del_hook(Evas_Object *obj)
73 Widget_Data *wd = elm_widget_data_get(obj);
79 _theme_hook(Evas_Object *obj)
81 Widget_Data *wd = elm_widget_data_get(obj);
84 elm_hoversel_hover_end(obj);
86 snprintf(buf, sizeof(buf), "hoversel_horizontal/%s", elm_widget_style_get(obj));
88 snprintf(buf, sizeof(buf), "hoversel_vertical/%s", elm_widget_style_get(obj));
89 elm_object_style_set(wd->btn, buf);
93 _disable_hook(Evas_Object *obj)
95 Widget_Data *wd = elm_widget_data_get(obj);
97 if (elm_widget_disabled_get(obj))
98 elm_widget_disabled_set(wd->btn, 1);
100 elm_widget_disabled_set(wd->btn, 0);
104 _sizing_eval(Evas_Object *obj)
106 Widget_Data *wd = elm_widget_data_get(obj);
107 Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
109 evas_object_size_hint_min_get(wd->btn, &minw, &minh);
110 evas_object_size_hint_max_get(wd->btn, &maxw, &maxh);
111 evas_object_size_hint_min_set(obj, minw, minh);
112 evas_object_size_hint_max_set(obj, maxw, maxh);
116 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
122 _hover_clicked(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
124 elm_hoversel_hover_end(data);
128 _item_clicked(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
130 Elm_Hoversel_Item *it = data;
131 Evas_Object *obj2 = it->obj;
133 elm_hoversel_hover_end(obj2);
134 if (it->func) it->func(it->data, obj2, it);
135 evas_object_smart_callback_call(obj2, "selected", it);
139 _activate(Evas_Object *obj)
141 Widget_Data *wd = elm_widget_data_get(obj);
142 Evas_Object *bt, *bx, *ic;
144 const Elm_Hoversel_Item *it;
148 if (elm_widget_disabled_get(obj)) return;
149 wd->hover = elm_hover_add(obj);
151 snprintf(buf, sizeof(buf), "hoversel_horizontal/%s", elm_widget_style_get(obj));
153 snprintf(buf, sizeof(buf), "hoversel_vertical/%s", elm_widget_style_get(obj));
154 elm_object_style_set(wd->hover, buf);
155 evas_object_smart_callback_add(wd->hover, "clicked", _hover_clicked, obj);
156 elm_hover_parent_set(wd->hover, wd->hover_parent);
157 elm_hover_target_set(wd->hover, wd->btn);
159 bx = elm_box_add(wd->hover);
160 elm_box_homogenous_set(bx, 1);
162 elm_box_horizontal_set(bx, wd->horizontal);
165 snprintf(buf, sizeof(buf), "hoversel_horizontal_entry/%s",
166 elm_widget_style_get(obj));
168 snprintf(buf, sizeof(buf), "hoversel_vertical_entry/%s",
169 elm_widget_style_get(obj));
170 EINA_LIST_FOREACH(wd->items, l, it)
172 bt = elm_button_add(wd->hover);
173 elm_object_style_set(bt, buf);
174 elm_button_label_set(bt, it->label);
177 ic = elm_icon_add(obj);
178 elm_icon_scale_set(ic, 0, 1);
179 if (it->icon_type == ELM_ICON_FILE)
180 elm_icon_file_set(ic, it->icon_file, it->icon_group);
181 else if (it->icon_type == ELM_ICON_STANDARD)
182 elm_icon_standard_set(ic, it->icon_file);
183 elm_button_icon_set(bt, ic);
184 evas_object_show(ic);
186 evas_object_size_hint_weight_set(bt, 1.0, 0.0);
187 evas_object_size_hint_align_set(bt, -1.0, -1.0);
188 elm_box_pack_end(bx, bt);
189 evas_object_smart_callback_add(bt, "clicked", _item_clicked, it);
190 evas_object_show(bt);
194 elm_hover_content_set(wd->hover,
195 elm_hover_best_content_location_get(wd->hover,
196 ELM_HOVER_AXIS_HORIZONTAL),
199 elm_hover_content_set(wd->hover,
200 elm_hover_best_content_location_get(wd->hover,
201 ELM_HOVER_AXIS_VERTICAL),
203 evas_object_show(bx);
205 evas_object_show(wd->hover);
206 evas_object_smart_callback_call(obj, "clicked", NULL);
208 // if (wd->horizontal) evas_object_hide(wd->btn);
212 _button_clicked(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
218 _parent_del(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
220 Widget_Data *wd = elm_widget_data_get(data);
222 wd->hover_parent = NULL;
226 * Add a new Hoversel object
228 * @param parent The parent object
229 * @return The new object or NULL if it cannot be created
234 elm_hoversel_add(Evas_Object *parent)
241 wd = ELM_NEW(Widget_Data);
242 e = evas_object_evas_get(parent);
243 obj = elm_widget_add(e);
244 ELM_SET_WIDTYPE(widtype, "hoversel");
245 elm_widget_type_set(obj, "hoversel");
246 elm_widget_sub_object_add(parent, obj);
247 elm_widget_data_set(obj, wd);
248 elm_widget_del_pre_hook_set(obj, _del_pre_hook);
249 elm_widget_del_hook_set(obj, _del_hook);
250 elm_widget_theme_hook_set(obj, _theme_hook);
251 elm_widget_disable_hook_set(obj, _disable_hook);
253 wd->btn = elm_button_add(parent);
255 snprintf(buf, sizeof(buf), "hoversel_horizontal/%s", elm_widget_style_get(obj));
257 snprintf(buf, sizeof(buf), "hoversel_vertical/%s", elm_widget_style_get(obj));
258 elm_object_style_set(wd->btn, buf);
259 elm_widget_resize_object_set(obj, wd->btn);
260 evas_object_event_callback_add(wd->btn, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
261 _changed_size_hints, obj);
262 evas_object_smart_callback_add(wd->btn, "clicked", _button_clicked, obj);
263 elm_hoversel_hover_parent_set(obj, parent);
269 * Set the Hover parent
271 * Sets the hover parent object. Should probably be the window that the hoversel
272 * is in. See Hover objects for more information.
274 * @param obj The hoversel object
275 * @param parent The parent to use
280 elm_hoversel_hover_parent_set(Evas_Object *obj, Evas_Object *parent)
282 ELM_CHECK_WIDTYPE(obj, widtype);
283 Widget_Data *wd = elm_widget_data_get(obj);
285 if (wd->hover_parent)
286 evas_object_event_callback_del_full(wd->hover_parent, EVAS_CALLBACK_DEL,
288 wd->hover_parent = parent;
289 if (wd->hover_parent)
290 evas_object_event_callback_add(wd->hover_parent, EVAS_CALLBACK_DEL,
295 * Set the hoversel button label
297 * This sets the label of the button that is always visible (before it is
298 * clicked and expanded). Also see elm_button_label_set().
300 * @param obj The hoversel object
301 * @param label The label text.
306 elm_hoversel_label_set(Evas_Object *obj, const char *label)
308 ELM_CHECK_WIDTYPE(obj, widtype);
309 Widget_Data *wd = elm_widget_data_get(obj);
311 elm_button_label_set(wd->btn, label);
315 * Get the hoversel button label
317 * @param obj The hoversel object
318 * @return The label text.
323 elm_hoversel_label_get(const Evas_Object *obj)
325 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
326 Widget_Data *wd = elm_widget_data_get(obj);
327 if ((!wd) || (!wd->btn)) return NULL;
328 return elm_button_label_get(wd->btn);
332 * This sets the hoversel to expand horizontally. The initial button
333 * will display horizontally regardless of this setting.
335 * @param obj The hoversel object
336 * @param horizontal If true, the hover will expand horizontally to the right.
341 elm_hoversel_horizontal_set(Evas_Object *obj, Eina_Bool horizontal)
343 ELM_CHECK_WIDTYPE(obj, widtype);
344 Widget_Data *wd = elm_widget_data_get(obj);
346 wd->horizontal = !!horizontal;
351 * This returns whether the hoversel is set to expand horizontally.
353 * @param obj The hoversel object
354 * @return If true, the hover will expand horizontally to the right.
359 elm_hoversel_horizontal_get(const Evas_Object *obj)
361 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
362 Widget_Data *wd = elm_widget_data_get(obj);
363 if (!wd) return EINA_FALSE;
364 return wd->horizontal;
368 * Set the icon of the hoversel button
370 * Sets the icon of the button that is always visible (before it is clicked
371 * and expanded). Also see elm_button_icon_set().
373 * @param obj The hoversel object
374 * @param icon The icon object
379 elm_hoversel_icon_set(Evas_Object *obj, Evas_Object *icon)
381 ELM_CHECK_WIDTYPE(obj, widtype);
382 Widget_Data *wd = elm_widget_data_get(obj);
384 elm_button_icon_set(wd->btn, icon);
388 * Get the icon of the hoversel button
390 * Get the icon of the button that is always visible (before it is clicked
391 * and expanded). Also see elm_button_icon_get().
393 * @param obj The hoversel object
394 * @return The icon object
399 elm_hoversel_icon_get(const Evas_Object *obj)
401 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
402 Widget_Data *wd = elm_widget_data_get(obj);
403 if ((!wd) || (!wd->btn)) return NULL;
404 return elm_button_icon_get(wd->btn);
408 * This triggers the hoversel popup from code, the same as though the
409 * user clicked the button.
411 * @param obj The hoversel object
416 elm_hoversel_hover_begin(Evas_Object *obj)
418 ELM_CHECK_WIDTYPE(obj, widtype);
419 Widget_Data *wd = elm_widget_data_get(obj);
421 if (wd->hover) return;
426 * This ends the hoversel popup as though the user clicked outside the hover.
428 * @param obj The hoversel object
433 elm_hoversel_hover_end(Evas_Object *obj)
435 ELM_CHECK_WIDTYPE(obj, widtype);
436 Widget_Data *wd = elm_widget_data_get(obj);
438 if (!wd->hover) return;
439 evas_object_del(wd->hover);
441 evas_object_smart_callback_call(obj, "dismissed", NULL);
445 * Returns whether the hoversel is expanded.
447 * @param obj The hoversel object
448 * @return This will return EINA_TRUE if the hoversel
449 * is expanded or EINA_FALSE if it is not expanded.
454 elm_hoversel_expanded_get(Evas_Object *obj)
456 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
457 Widget_Data *wd = elm_widget_data_get(obj);
458 if (!wd) return EINA_FALSE;
459 return (wd->hover) ? EINA_TRUE : EINA_FALSE;
463 * This will remove all the children items from the hoversel. (should not be
464 * called while the hoversel is active; use elm_hoversel_expanded_get()
467 * @param obj The hoversel object
472 elm_hoversel_clear(Evas_Object *obj)
474 Elm_Hoversel_Item *it;
476 ELM_CHECK_WIDTYPE(obj, widtype);
477 Widget_Data *wd = elm_widget_data_get(obj);
479 EINA_LIST_FOREACH_SAFE(wd->items, l, ll, it) elm_hoversel_item_del(it);
483 * Get the list of items within the given hoversel.
485 * @param obj The hoversel object
486 * @return Returns a list of Elm_Hoversel_Item*
490 EAPI const Eina_List *
491 elm_hoversel_items_get(const Evas_Object *obj)
493 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
494 Widget_Data *wd = elm_widget_data_get(obj);
495 if (!wd) return NULL;
500 * Add an item to the hoversel button
502 * This adds an item to the hoversel to show when it is clicked. Note: if you
503 * need to use an icon from an edje file then use elm_hoversel_item_icon_set()
504 * right after the this function, and set icon_file to NULL here.
506 * @param obj The hoversel object
507 * @param label The text label to use for the item (NULL if not desired)
508 * @param icon_file An image file path on disk to use for the icon or standard
509 * icon name (NULL if not desired)
510 * @param icon_type The icon type if relevant
511 * @param func Convenience function to call when this item is selected
512 * @param data Data to pass to item-related functions
513 * @return A handle to the item added.
517 EAPI Elm_Hoversel_Item *
518 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)
520 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
521 Widget_Data *wd = elm_widget_data_get(obj);
522 if (!wd) return NULL;
523 Elm_Hoversel_Item *it = calloc(1, sizeof(Elm_Hoversel_Item));
524 if (!it) return NULL;
525 wd->items = eina_list_append(wd->items, it);
527 it->label = eina_stringshare_add(label);
528 it->icon_file = eina_stringshare_add(icon_file);
529 it->icon_type = icon_type;
531 it->data = (void *)data;
536 * Delete an item from the hoversel
538 * This deletes the item from the hoversel (should not be called while the
539 * hoversel is active; use elm_hoversel_expanded_get()
542 * @param it The item to delete
547 elm_hoversel_item_del(Elm_Hoversel_Item *it)
550 Widget_Data *wd = elm_widget_data_get(it->obj);
551 if (it->del_cb) it->del_cb((void *)it->data, it->obj, it);
553 elm_hoversel_hover_end(it->obj);
554 wd->items = eina_list_remove(wd->items, it);
555 eina_stringshare_del(it->label);
556 eina_stringshare_del(it->icon_file);
557 eina_stringshare_del(it->icon_group);
562 * Set the function called when an item within the hoversel
563 * is freed. That function will receive these parameters:
566 * Evas_Object *the_item_object
567 * Elm_Hoversel_Item *the_object_struct
569 * @param it The item to set the callback on
570 * @param func The function called
575 elm_hoversel_item_del_cb_set(Elm_Hoversel_Item *it, Evas_Smart_Cb func)
582 * This returns the data pointer supplied with elm_hoversel_item_add() that
583 * will be passed to associated function callbacks.
585 * @param it The item to get the data from
586 * @return The data pointer set with elm_hoversel_item_add()
591 elm_hoversel_item_data_get(Elm_Hoversel_Item *it)
593 if (!it) return NULL;
598 * This returns the label text of the given hoversel item.
600 * @param it The item to get the label
601 * @return The label text of the hoversel item
606 elm_hoversel_item_label_get(Elm_Hoversel_Item *it)
608 if (!it) return NULL;
613 * This sets the icon for the given hoversel item. The icon can be loaded from
614 * the standard set, from an image file, or from an edje file.
616 * @param it The item to set the icon
617 * @param icon_file An image file path on disk to use for the icon or standard
619 * @param icon_group The edje group to use if @p icon_file is an edje file. Set this
620 * to NULL if the icon is not an edje file
621 * @param icon_type The icon type
626 elm_hoversel_item_icon_set(Elm_Hoversel_Item *it, const char *icon_file, const char *icon_group, Elm_Icon_Type icon_type)
629 eina_stringshare_replace(&it->icon_file, icon_file);
630 eina_stringshare_replace(&it->icon_group, icon_group);
631 it->icon_type = icon_type;
635 * Get the icon object of the hoversel item
637 * @param it The item to get the icon from
638 * @param icon_file The image file path on disk used for the icon or standard
640 * @param icon_group The edje group used if @p icon_file is an edje file. NULL
641 * if the icon is not an edje file
642 * @param icon_type The icon type
647 elm_hoversel_item_icon_get(Elm_Hoversel_Item *it, const char **icon_file, const char **icon_group, Elm_Icon_Type *icon_type)
650 if (icon_file) *icon_file = it->icon_file;
651 if (icon_group) *icon_group = it->icon_group;
652 if (icon_type) *icon_type = it->icon_type;