1 #include <Elementary.h>
7 * A ctxpopup is a widget that, when shown, pops up a list of items.
8 * It automatically chooses an area inside its parent object's view
9 * (set via elm_ctxpopup_add() and elm_ctxpopup_hover_parent_set()) to
10 * optimally fit into it. In the default theme, it will also point an
11 * arrow to the cursor position at the time one shows it. Ctxpopup
12 * items have a label and/or an icon. It is intended for a small
13 * number of items (hence the use of list, not genlist).
15 * Signals that you can add callbacks for are:
17 * dismissed - the hover was dismissed
20 typedef struct _Widget_Data Widget_Data;
22 struct _Elm_Ctxpopup_Item
34 Eina_Bool disabled : 1;
39 Evas_Object *hover_parent;
47 Eina_Bool scroller_disabled : 1;
48 Eina_Bool horizontal : 1;
51 static const char *widtype = NULL;
52 static void _del_hook(Evas_Object *obj);
53 static void _del_pre_hook(Evas_Object *obj);
54 static void _theme_hook(Evas_Object *obj);
55 static void _hover_clicked_cb(void *data,
58 static void _parent_resize_cb(void *data,
62 static void _ctxpopup_show(void *data,
66 static void _ctxpopup_hide(void *data,
71 static const char SIG_DISMISSED[] = "dismissed";
72 static const Evas_Smart_Cb_Description _signals[] = {
77 #define ELM_CTXPOPUP_ITEM_CHECK_RETURN(it, ...) \
78 ELM_WIDGET_ITEM_CHECK_OR_RETURN((Elm_Widget_Item *)it, __VA_ARGS__); \
79 ELM_CHECK_WIDTYPE(item->base.widget, widtype) __VA_ARGS__;
81 static Elm_Ctxpopup_Item *
82 _item_new(Evas_Object *obj,
88 Elm_Ctxpopup_Item *it;
90 it = elm_widget_item_new(obj, Elm_Ctxpopup_Item);
94 it->label = eina_stringshare_add(label);
103 _item_free(Elm_Ctxpopup_Item *it)
105 eina_stringshare_del(it->label);
106 elm_widget_item_del(it);
110 _del_pre_hook(Evas_Object *obj)
112 Widget_Data *wd = elm_widget_data_get(obj);
115 evas_object_event_callback_del_full(wd->hover_parent, EVAS_CALLBACK_RESIZE,
116 _parent_resize_cb, obj);
120 _del_hook(Evas_Object *obj)
122 Elm_Ctxpopup_Item *it;
125 wd = elm_widget_data_get(obj);
129 EINA_LIST_FREE(wd->items, it)
136 _event_hook(Evas_Object *obj,
137 Evas_Object *src __UNUSED__,
138 Evas_Callback_Type type,
141 if (type != EVAS_CALLBACK_KEY_DOWN)
144 Evas_Event_Key_Down *ev = event_info;
146 Widget_Data *wd = elm_widget_data_get(obj);
150 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD)
153 if (strcmp(ev->keyname, "Escape"))
156 evas_object_hide(obj);
157 ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
162 _on_focus_hook(void *data __UNUSED__,
165 Widget_Data *wd = elm_widget_data_get(obj);
169 if (elm_widget_focus_get(obj))
171 elm_object_signal_emit(wd->list, "elm,action,focus", "elm");
172 evas_object_focus_set(wd->list, EINA_TRUE);
176 elm_object_signal_emit(wd->list, "elm,action,unfocus", "elm");
177 evas_object_focus_set(wd->list, EINA_FALSE);
182 _theme_hook(Evas_Object *obj)
187 wd = elm_widget_data_get(obj);
191 elm_widget_style_set(wd->list, "ctxpopup");
193 snprintf(buf, sizeof(buf), "ctxpopup/%s", elm_widget_style_get(obj));
194 elm_object_style_set(wd->hover, buf);
198 _signal_emit_hook(Evas_Object *obj,
199 const char *emission,
202 Widget_Data *wd = elm_widget_data_get(obj);
204 elm_object_signal_emit(wd->list, emission, source);
205 elm_object_signal_emit(wd->hover, emission, source);
209 _signal_callback_add_hook(Evas_Object *obj,
210 const char *emission,
212 void (*func_cb)(void *data,
214 const char *emission,
218 Widget_Data *wd = elm_widget_data_get(obj);
220 elm_object_signal_callback_add(wd->list, emission, source, func_cb, data);
221 elm_object_signal_callback_add(wd->hover, emission, source, func_cb, data);
225 _signal_callback_del_hook(Evas_Object *obj,
226 const char *emission,
228 void (*func_cb)(void *data,
230 const char *emission,
232 void *data __UNUSED__)
234 Widget_Data *wd = elm_widget_data_get(obj);
235 elm_object_signal_callback_del(wd->list, emission, source, func_cb);
236 elm_object_signal_callback_del(wd->hover, emission, source, func_cb);
240 _item_func_cb(void *data,
241 Evas_Object *obj __UNUSED__,
244 Elm_Ctxpopup_Item *it;
246 elm_list_item_selected_set(event_info, EINA_FALSE);
250 it->func((void *)it->base.data, it->base.widget, it);
254 _hover_clicked_cb(void *data,
255 Evas_Object *obj __UNUSED__,
256 void *event_info __UNUSED__)
258 evas_object_hide(data);
262 _content_placement_changed_cb(void *data,
263 Evas_Object *obj __UNUSED__,
266 const char *new_slot;
270 wd = elm_widget_data_get(data);
271 new_slot = event_info;
273 snprintf(buf, sizeof(buf), "elm,action,slot,%s,show", new_slot);
274 elm_widget_signal_emit(wd->hover, buf, "elm");
278 _ctxpopup_show(void *data __UNUSED__,
279 Evas *evas __UNUSED__,
281 void *event_info __UNUSED__)
287 wd = elm_widget_data_get(obj);
288 if (eina_list_count(wd->items) < 1)
291 elm_widget_focus_steal(obj);
293 evas_pointer_canvas_xy_get(wd->evas, &px, &py);
294 evas_object_geometry_get(wd->target, NULL, NULL, &w, &h);
295 evas_object_move(wd->target, px - (w / 2), py - (h / 2));
298 elm_list_item_show(eina_list_data_get(elm_list_items_get(wd->list)));
300 evas_object_show(wd->hover);
304 _ctxpopup_hide(void *data __UNUSED__,
305 Evas *evas __UNUSED__,
307 void *event_info __UNUSED__)
309 Widget_Data *wd = elm_widget_data_get(obj);
313 evas_object_hide(wd->hover);
314 evas_object_smart_callback_call(obj, SIG_DISMISSED, NULL);
318 _parent_del(void *data,
320 Evas_Object *obj __UNUSED__,
321 void *event_info __UNUSED__)
323 Widget_Data *wd = elm_widget_data_get(data);
327 wd->hover_parent = NULL;
331 _parent_resize_cb(void *data,
333 Evas_Object *obj __UNUSED__,
334 void *event_info __UNUSED__)
339 wd = elm_widget_data_get(data);
343 evas_object_geometry_get(wd->hover_parent, NULL, NULL, &w, &h);
344 evas_object_size_hint_max_set(wd->list, w * 0.666667, h / 2);
348 * Add a new Ctxpopup object to the parent.
350 * @param parent Parent object
351 * @return New object or @c NULL, if it cannot be created
356 elm_ctxpopup_add(Evas_Object *parent)
362 EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
364 wd = ELM_NEW(Widget_Data);
365 wd->evas = evas_object_evas_get(parent);
366 obj = elm_widget_add(wd->evas);
367 ELM_SET_WIDTYPE(widtype, "ctxpopup");
368 elm_widget_type_set(obj, "ctxpopup");
369 elm_widget_sub_object_add(parent, obj);
370 elm_widget_data_set(obj, wd);
371 elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
372 elm_widget_can_focus_set(obj, EINA_TRUE);
373 elm_widget_del_pre_hook_set(obj, _del_pre_hook);
374 elm_widget_del_hook_set(obj, _del_hook);
375 elm_widget_theme_hook_set(obj, _theme_hook);
376 elm_widget_event_hook_set(obj, _event_hook);
377 elm_widget_signal_emit_hook_set(obj, _signal_emit_hook);
378 elm_widget_signal_callback_add_hook_set(obj, _signal_callback_add_hook);
379 elm_widget_signal_callback_del_hook_set(obj, _signal_callback_del_hook);
381 wd->list = elm_list_add(obj);
382 elm_widget_style_set(wd->list, "ctxpopup");
384 elm_list_mode_set(wd->list, ELM_LIST_EXPAND);
385 evas_object_show(wd->list);
387 wd->target = elm_icon_add(obj); /* has to be an elm_widget (dummy,
388 in this case) because we gotta
390 elm_widget_hover_object_set() */
391 evas_object_resize(wd->target, elm_finger_size_get(),
392 elm_finger_size_get());
393 evas_object_show(wd->target);
394 evas_object_layer_set(wd->target, EVAS_LAYER_MIN);
396 wd->hover = elm_hover_add(obj);
398 snprintf(buf, sizeof(buf), "ctxpopup/%s", elm_widget_style_get(obj));
399 elm_object_style_set(wd->hover, buf);
401 evas_object_smart_callback_add(wd->hover, "smart,changed",
402 _content_placement_changed_cb, obj);
404 elm_ctxpopup_hover_parent_set(obj, parent);
405 elm_hover_target_set(wd->hover, wd->target);
407 evas_object_smart_callback_add(wd->hover, "clicked", _hover_clicked_cb, obj);
408 elm_hover_content_set(wd->hover, "smart", wd->list);
410 evas_object_event_callback_add(obj, EVAS_CALLBACK_SHOW, _ctxpopup_show,
412 evas_object_event_callback_add(obj, EVAS_CALLBACK_HIDE, _ctxpopup_hide,
415 elm_widget_sub_object_add(obj, wd->list);
416 elm_widget_sub_object_add(obj, wd->hover);
417 elm_widget_sub_object_add(obj, wd->target);
418 evas_object_smart_callbacks_descriptions_set(obj, _signals);
423 * This ends the Ctxpopup's popup as if the user had clicked
426 * @param obj The ctxpopup object
431 elm_ctxpopup_hover_end(Evas_Object *obj)
433 ELM_CHECK_WIDTYPE(obj, widtype);
435 Widget_Data *wd = elm_widget_data_get(obj);
442 evas_object_hide(wd->hover);
446 * Get the icon object for the given ctxpopup item.
448 * @param item Ctxpopup item
449 * @return icon object or @c NULL, if the item does not have icon or
455 elm_ctxpopup_item_icon_get(const Elm_Ctxpopup_Item *item)
457 ELM_CTXPOPUP_ITEM_CHECK_RETURN(item, NULL);
462 * Sets the side icon associated with the ctxpopup item.
464 * Once the icon object is set, a previously set one will be deleted.
465 * You probably don't want, then, to have the <b>same</b> icon object
466 * set for more than one item of the list (when replacing one of its
469 * @param item Ctxpopup item
470 * @param icon Icon object to be set
475 elm_ctxpopup_item_icon_set(Elm_Ctxpopup_Item *item,
478 ELM_CTXPOPUP_ITEM_CHECK_RETURN(item);
482 wd = elm_widget_data_get(item->base.widget);
488 evas_object_del(item->icon);
493 elm_list_item_icon_set(item->lptr, icon);
494 elm_list_go(wd->list);
498 * Get the label object for the given ctxpopup item.
500 * @param item Ctxpopup item
501 * @return label object or @c NULL, if the item does not have label or
507 elm_ctxpopup_item_label_get(const Elm_Ctxpopup_Item *item)
509 ELM_CTXPOPUP_ITEM_CHECK_RETURN(item, NULL);
514 * (Re)set the label on the given ctxpopup item.
516 * @param obj Ctxpopup item
517 * @param label String to set as label
522 elm_ctxpopup_item_label_set(Elm_Ctxpopup_Item *item,
525 ELM_CTXPOPUP_ITEM_CHECK_RETURN(item);
529 wd = elm_widget_data_get(item->base.widget);
533 if (!eina_stringshare_replace(&item->label, label))
536 elm_list_item_label_set(item->lptr, label);
537 elm_list_go(wd->list);
541 * Set the Ctxpopup's parent.
543 * Sets the hover's parent object (it would much probably be the
544 * window that the ctxpopup is in). See Hover objects for more
547 * @param obj The ctxpopup object
548 * @param parent The parent to use
550 * @note elm_ctxpopup_add() will automatically call this function
551 * with its @c parent argument.
556 elm_ctxpopup_hover_parent_set(Evas_Object *obj,
559 ELM_CHECK_WIDTYPE(obj, widtype);
563 wd = elm_widget_data_get(obj);
567 EINA_SAFETY_ON_NULL_RETURN(parent);
569 elm_hover_parent_set(wd->hover, parent);
571 if (wd->hover_parent)
573 evas_object_event_callback_del_full(wd->hover_parent, EVAS_CALLBACK_DEL,
575 evas_object_event_callback_del_full(wd->hover_parent,
576 EVAS_CALLBACK_RESIZE,
577 _parent_resize_cb, obj);
580 wd->hover_parent = parent;
581 evas_object_event_callback_add(wd->hover_parent, EVAS_CALLBACK_DEL,
583 evas_object_event_callback_add(wd->hover_parent, EVAS_CALLBACK_RESIZE,
584 _parent_resize_cb, obj);
586 _parent_resize_cb(obj, NULL, NULL, NULL);
590 * Get the Ctxpopup's parent object.
592 * @param obj The ctxpopup object
593 * @param parent The parent to use
595 * See elm_ctxpopup_hover_parent_set() for more information.
600 elm_ctxpopup_hover_parent_get(const Evas_Object *obj)
602 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
606 wd = elm_widget_data_get(obj);
610 return wd->hover_parent;
614 * Clear all items in the given ctxpopup object.
616 * @param obj Ctxpopup object
621 elm_ctxpopup_clear(Evas_Object *obj)
623 ELM_CHECK_WIDTYPE(obj, widtype);
625 Elm_Ctxpopup_Item *item;
628 wd = elm_widget_data_get(obj);
629 if ((!wd) || (!wd->items))
632 EINA_LIST_FREE(wd->items, item)
634 elm_list_item_del(item->lptr);
638 evas_object_hide(wd->hover);
642 * Change the ctxpopup's orientation to horizontal or vertical.
644 * @param obj Ctxpopup object
645 * @param horizontal @c EINA_TRUE for horizontal mode, @c EINA_FALSE
651 elm_ctxpopup_horizontal_set(Evas_Object *obj,
652 Eina_Bool horizontal)
654 ELM_CHECK_WIDTYPE(obj, widtype);
658 wd = elm_widget_data_get(obj);
662 if (wd->horizontal == horizontal)
665 wd->horizontal = horizontal;
666 elm_list_horizontal_set(wd->list, horizontal);
670 * Get the value of current ctxpopup object's orientation.
672 * @param obj Ctxpopup object
673 * @return @c EINA_TRUE for horizontal mode, @c EINA_FALSE for
674 * vertical mode (or errors)
679 elm_ctxpopup_horizontal_get(const Evas_Object *obj)
681 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
685 wd = elm_widget_data_get(obj);
689 return wd->horizontal;
693 * Append a new item to a ctxpopup object.
695 * @param obj Ctxpopup object
696 * @param label The label of the new item
697 * @param icon Icon to be set on new item
698 * @param func Convenience function called when item selected
699 * @param data Data passed to @p func above
700 * @return A handle to the item added or @c NULL, on errors
704 EAPI Elm_Ctxpopup_Item *
705 elm_ctxpopup_item_append(Evas_Object *obj,
711 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
713 Elm_Ctxpopup_Item *item;
716 wd = elm_widget_data_get(obj);
720 item = _item_new(obj, label, icon, func, data);
724 wd->items = eina_list_append(wd->items, item);
725 item->lptr = elm_list_item_append(wd->list, label, icon, NULL,
726 _item_func_cb, item);
727 elm_list_go(wd->list);
732 * Delete the given item in a ctxpopup object.
734 * @param item Ctxpopup item to be deleted
739 elm_ctxpopup_item_del(Elm_Ctxpopup_Item *item)
741 ELM_CTXPOPUP_ITEM_CHECK_RETURN(item);
745 wd = elm_widget_data_get(item->base.widget);
749 wd->items = eina_list_remove(wd->items, item);
751 elm_list_item_del(item->lptr);
754 if (eina_list_count(wd->items) < 1)
755 evas_object_hide(wd->hover);
761 * Set the ctxpopup item's state as disabled or enabled
763 * @param item Ctxpopup item to be enabled/disabled
764 * @param disabled @c EINA_TRUE to disable it, @c EINA_FALSE to enable
769 elm_ctxpopup_item_disabled_set(Elm_Ctxpopup_Item *item,
772 ELM_CTXPOPUP_ITEM_CHECK_RETURN(item);
776 wd = elm_widget_data_get(item->base.widget);
780 if (disabled == item->disabled)
783 item->disabled = disabled;
784 elm_list_item_disabled_set(item->lptr, disabled);
788 * Get the ctxpopup item's disabled/enabled state.
790 * @param item Ctxpopup item to be enabled/disabled
791 * @return @c EINA_TRUE, if disabled, @c EINA_FALSE otherwise
796 elm_ctxpopup_item_disabled_get(const Elm_Ctxpopup_Item *item)
798 ELM_CTXPOPUP_ITEM_CHECK_RETURN(item, EINA_FALSE);
800 return item->disabled;