1 #include <Elementary.h>
5 * @defgroup Tooltips Tooltips
7 * The Tooltip is an (internal, for now) smart object used to show a
8 * content in a frame on mouse hover of objects(or widgets), with
9 * tips/information about them.
12 static const char _tooltip_key[] = "_elm_tooltip";
14 #define ELM_TOOLTIP_GET_OR_RETURN(tt, obj, ...) \
20 CRITICAL("Null pointer: " #obj); \
23 tt = evas_object_data_get((obj), _tooltip_key); \
26 ERR("Object does not have tooltip: " #obj); \
34 Elm_Tooltip_Content_Cb func;
39 Evas_Object *eventarea, *owner;
40 Evas_Object *tooltip, *content;
41 Ecore_Timer *show_timer;
42 Ecore_Timer *hide_timer;
43 Ecore_Job *reconfigure_job;
45 Evas_Coord x, y, bx, by;
50 double hide_timeout; /* from theme */
51 Eina_Bool visible_lock:1;
52 Eina_Bool changed_style:1;
55 static void _elm_tooltip_reconfigure(Elm_Tooltip *tt);
56 static void _elm_tooltip_reconfigure_job_start(Elm_Tooltip *tt);
57 static void _elm_tooltip_reconfigure_job_stop(Elm_Tooltip *tt);
58 static void _elm_tooltip_hide_anim_start(Elm_Tooltip *tt);
59 static void _elm_tooltip_hide_anim_stop(Elm_Tooltip *tt);
60 static void _elm_tooltip_show_timer_stop(Elm_Tooltip *tt);
61 static void _elm_tooltip_hide(Elm_Tooltip *tt);
62 static void _elm_tooltip_data_clean(Elm_Tooltip *tt);
66 _elm_tooltip_content_changed_hints_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
68 _elm_tooltip_reconfigure_job_start(data);
72 _elm_tooltip_content_del_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
74 Elm_Tooltip *tt = data;
76 tt->visible_lock = EINA_FALSE;
77 _elm_tooltip_hide(tt);
81 _elm_tooltip_obj_move_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
83 Elm_Tooltip *tt = data;
84 _elm_tooltip_reconfigure_job_start(tt);
88 _elm_tooltip_obj_resize_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
90 Elm_Tooltip *tt = data;
91 _elm_tooltip_reconfigure_job_start(tt);
95 _elm_tooltip_obj_mouse_move_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
97 Elm_Tooltip *tt = data;
98 _elm_tooltip_reconfigure_job_start(tt);
102 _elm_tooltip_show(Elm_Tooltip *tt)
104 _elm_tooltip_show_timer_stop(tt);
105 _elm_tooltip_hide_anim_stop(tt);
109 _elm_tooltip_reconfigure_job_start(tt);
112 tt->tooltip = edje_object_add(tt->evas);
113 if (!tt->tooltip) return;
115 evas_object_layer_set(tt->tooltip, ELM_OBJECT_LAYER_TOOLTIP);
117 evas_object_event_callback_add
118 (tt->eventarea, EVAS_CALLBACK_MOVE, _elm_tooltip_obj_move_cb, tt);
119 evas_object_event_callback_add
120 (tt->eventarea, EVAS_CALLBACK_RESIZE, _elm_tooltip_obj_resize_cb, tt);
121 evas_object_event_callback_add
122 (tt->eventarea, EVAS_CALLBACK_MOUSE_MOVE, _elm_tooltip_obj_mouse_move_cb, tt);
124 evas_object_pass_events_set(tt->tooltip, EINA_TRUE);
125 tt->changed_style = EINA_TRUE;
126 _elm_tooltip_reconfigure_job_start(tt);
130 _elm_tooltip_content_del(Elm_Tooltip *tt)
132 if (!tt->content) return;
134 evas_object_event_callback_del_full
135 (tt->content, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
136 _elm_tooltip_content_changed_hints_cb, tt);
137 evas_object_event_callback_del_full
138 (tt->content, EVAS_CALLBACK_DEL,
139 _elm_tooltip_content_del_cb, tt);
140 evas_object_del(tt->content);
146 _elm_tooltip_hide(Elm_Tooltip *tt)
148 _elm_tooltip_show_timer_stop(tt);
149 _elm_tooltip_hide_anim_stop(tt);
150 _elm_tooltip_reconfigure_job_stop(tt);
152 if (!tt->tooltip) return;
153 if (tt->visible_lock) return;
155 _elm_tooltip_content_del(tt);
157 evas_object_event_callback_del_full
158 (tt->eventarea, EVAS_CALLBACK_MOVE, _elm_tooltip_obj_move_cb, tt);
159 evas_object_event_callback_del_full
160 (tt->eventarea, EVAS_CALLBACK_RESIZE, _elm_tooltip_obj_resize_cb, tt);
161 evas_object_event_callback_del_full
162 (tt->eventarea, EVAS_CALLBACK_MOUSE_MOVE, _elm_tooltip_obj_mouse_move_cb, tt);
164 evas_object_del(tt->tooltip);
169 _elm_tooltip_reconfigure_job(void *data)
171 Elm_Tooltip *tt = data;
172 tt->reconfigure_job = NULL;
173 _elm_tooltip_reconfigure(data);
177 _elm_tooltip_reconfigure_job_stop(Elm_Tooltip *tt)
179 if (!tt->reconfigure_job) return;
180 ecore_job_del(tt->reconfigure_job);
181 tt->reconfigure_job = NULL;
185 _elm_tooltip_reconfigure_job_start(Elm_Tooltip *tt)
187 if (tt->reconfigure_job) ecore_job_del(tt->reconfigure_job);
188 tt->reconfigure_job = ecore_job_add
189 (_elm_tooltip_reconfigure_job, tt);
193 _elm_tooltip_hide_anim_cb(void *data)
195 Elm_Tooltip *tt = data;
196 tt->hide_timer = NULL;
197 _elm_tooltip_hide(tt);
202 _elm_tooltip_hide_anim_start(Elm_Tooltip *tt)
204 if (tt->hide_timer) return;
205 edje_object_signal_emit(tt->tooltip, "elm,action,hide", "elm");
206 tt->hide_timer = ecore_timer_add
207 (tt->hide_timeout, _elm_tooltip_hide_anim_cb, tt);
211 _elm_tooltip_hide_anim_stop(Elm_Tooltip *tt)
213 if (!tt->hide_timer) return;
215 edje_object_signal_emit(tt->tooltip, "elm,action,show", "elm");
216 ecore_timer_del(tt->hide_timer);
217 tt->hide_timer = NULL;
221 _elm_tooltip_reconfigure(Elm_Tooltip *tt)
223 Evas_Coord ox, oy, ow, oh, px, py, tx, ty, tw, th, cw, ch;
224 Evas_Coord eminw, eminh, ominw, ominh;
226 Eina_Bool inside_eventarea;
228 _elm_tooltip_reconfigure_job_stop(tt);
230 if (tt->hide_timer) return;
231 if (!tt->tooltip) return;
232 if (tt->changed_style)
234 const char *style = tt->style ? tt->style : "default";
236 if (!_elm_theme_object_set
237 (tt->owner, tt->tooltip, "tooltip", "base", style))
239 ERR("Could not apply the theme to the tooltip! style=%s", style);
240 evas_object_del(tt->tooltip);
252 tt->hide_timeout = 0.0;
254 str = edje_object_data_get(tt->tooltip, "pad_x");
255 if (str) tt->pad.x = atoi(str);
256 str = edje_object_data_get(tt->tooltip, "pad_y");
257 if (str) tt->pad.y = atoi(str);
259 str = edje_object_data_get(tt->tooltip, "pad_border_x");
260 if (str) tt->pad.bx = atoi(str);
261 str = edje_object_data_get(tt->tooltip, "pad_border_y");
262 if (str) tt->pad.by = atoi(str);
264 str = edje_object_data_get(tt->tooltip, "hide_timeout");
267 tt->hide_timeout = atof(str);
268 if (tt->hide_timeout < 0.0) tt->hide_timeout = 0.0;
271 evas_object_pass_events_set(tt->tooltip, EINA_TRUE);
272 tt->changed_style = EINA_FALSE;
274 edje_object_part_swallow
275 (tt->tooltip, "elm.swallow.content", tt->content);
277 edje_object_signal_emit(tt->tooltip, "elm,action,show", "elm");
282 tt->content = tt->func((void *)tt->data, tt->owner);
285 WRN("could not create tooltip content!");
286 evas_object_del(tt->tooltip);
290 evas_object_layer_set(tt->content, ELM_OBJECT_LAYER_TOOLTIP);
291 evas_object_pass_events_set(tt->content, EINA_TRUE);
292 edje_object_part_swallow
293 (tt->tooltip, "elm.swallow.content", tt->content);
294 evas_object_event_callback_add
295 (tt->content, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
296 _elm_tooltip_content_changed_hints_cb, tt);
297 evas_object_event_callback_add
298 (tt->content, EVAS_CALLBACK_DEL,
299 _elm_tooltip_content_del_cb, tt);
303 evas_object_size_hint_min_get(tt->content, &ominw, &ominh);
304 edje_object_size_min_get(tt->tooltip, &eminw, &eminh);
306 if (ominw < eminw) ominw = eminw;
307 if (ominh < eminh) ominh = eminh;
309 if (ominw < 1) ominw = 10; /* at least it is noticeable */
310 if (ominh < 1) ominh = 10; /* at least it is noticeable */
312 edje_object_size_min_restricted_calc
313 (tt->tooltip, &tw, &th, ominw, ominh);
315 evas_output_size_get(tt->evas, &cw, &ch);
316 evas_pointer_canvas_xy_get(tt->evas, &px, &py);
318 evas_object_geometry_get(tt->eventarea, &ox, &oy, &ow, &oh);
320 inside_eventarea = ((px >= ox) && (py >= oy) &&
321 (px <= ox + ow) && (py <= oy + oh));
322 if (inside_eventarea)
327 if (tx + tw + tt->pad.x < cw) tx += tt->pad.x;
328 if (ty + th + tt->pad.y < ch) ty += tt->pad.y;
332 tx = ox + (ow / 2) - (tw / 2);
333 if (ch < (th + oy + oh)) ty = oy - th;
337 if (tt->pad.bx * 2 + tw < cw)
339 if (tx < tt->pad.bx) tx = tt->pad.bx;
340 else if (tx + tw >= cw - tt->pad.bx) tx = cw - tw - tt->pad.bx;
343 if (tt->pad.by * 2 + th < ch)
345 if (ty < tt->pad.by) ty = tt->pad.by;
346 else if (ty + th >= ch - tt->pad.by) ty = ch - th - tt->pad.by;
349 evas_object_move(tt->tooltip, tx, ty);
350 evas_object_resize(tt->tooltip, tw, th);
351 evas_object_show(tt->tooltip);
353 if (inside_eventarea)
355 rel_x = (px - tx) / (double)tw;
356 rel_y = (py - ty) / (double)th;
360 rel_x = (ox + (ow / 2) - tx) / (double)tw;
361 rel_y = (oy + (oh / 2) - ty) / (double)th;
364 #define FDIF(a, b) (fabs((a) - (b)) > 0.0001)
365 if ((FDIF(rel_x, tt->rel_pos.x)) || (FDIF(rel_y, tt->rel_pos.y)))
367 Edje_Message_Float_Set *msg;
369 msg = alloca(sizeof(Edje_Message_Float_Set) + sizeof(double));
373 tt->rel_pos.x = rel_x;
374 tt->rel_pos.y = rel_y;
376 edje_object_message_send(tt->tooltip, EDJE_MESSAGE_FLOAT_SET, 1, msg);
382 _elm_tooltip_show_timer_stop(Elm_Tooltip *tt)
384 if (!tt->show_timer) return;
385 ecore_timer_del(tt->show_timer);
386 tt->show_timer = NULL;
390 _elm_tooltip_timer_show_cb(void *data)
392 Elm_Tooltip *tt = data;
393 tt->show_timer = NULL;
394 _elm_tooltip_show(tt);
395 return ECORE_CALLBACK_CANCEL;
399 _elm_tooltip_obj_mouse_in_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
401 Elm_Tooltip *tt = data;
403 _elm_tooltip_hide_anim_stop(tt);
405 if ((tt->show_timer) || (tt->tooltip)) return;
407 tt->show_timer = ecore_timer_add
408 (_elm_config->tooltip_delay, _elm_tooltip_timer_show_cb, tt);
412 _elm_tooltip_obj_mouse_out_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
414 Elm_Tooltip *tt = data;
416 if (tt->visible_lock) return;
420 _elm_tooltip_show_timer_stop(tt);
424 _elm_tooltip_hide_anim_start(tt);
427 static void _elm_tooltip_obj_del_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__);
430 _elm_tooltip_unset(Elm_Tooltip *tt)
432 tt->visible_lock = EINA_FALSE;
433 _elm_tooltip_hide(tt);
434 _elm_tooltip_data_clean(tt);
438 evas_object_event_callback_del_full
439 (tt->eventarea, EVAS_CALLBACK_MOUSE_IN,
440 _elm_tooltip_obj_mouse_in_cb, tt);
441 evas_object_event_callback_del_full
442 (tt->eventarea, EVAS_CALLBACK_MOUSE_OUT,
443 _elm_tooltip_obj_mouse_out_cb, tt);
444 evas_object_event_callback_del_full
445 (tt->eventarea, EVAS_CALLBACK_DEL, _elm_tooltip_obj_del_cb, tt);
447 evas_object_data_del(tt->eventarea, _tooltip_key);
451 evas_object_event_callback_del_full
452 (tt->owner, EVAS_CALLBACK_DEL, _elm_tooltip_obj_del_cb, tt);
453 elm_widget_tooltip_del(tt->owner, tt);
456 eina_stringshare_del(tt->style);
461 _elm_tooltip_obj_del_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
463 Elm_Tooltip *tt = data;
464 if (tt->eventarea == obj) tt->eventarea = NULL;
465 if (tt->owner == obj) tt->owner = NULL;
466 _elm_tooltip_unset(tt);
470 _elm_tooltip_label_create(void *data, Evas_Object *obj)
472 Evas_Object *label = elm_label_add(obj);
475 elm_object_style_set(label, "tooltip");
476 elm_label_label_set(label, data);
481 _elm_tooltip_label_del_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
483 eina_stringshare_del(data);
487 _elm_tooltip_data_clean(Elm_Tooltip *tt)
489 if (tt->del_cb) tt->del_cb((void *)tt->data, tt->owner, NULL);
491 _elm_tooltip_content_del(tt);
498 * Notify tooltip should recalculate its theme.
502 elm_tooltip_theme(Elm_Tooltip *tt)
504 if (!tt->tooltip) return;
505 tt->changed_style = EINA_TRUE;
506 _elm_tooltip_reconfigure_job_start(tt);
511 * Set the content to be shown in the tooltip object for specific event area.
513 * Setup the tooltip to object. The object @a eventarea can have only
514 * one tooltip, so any previous tooltip data is removed. @p func(with
515 * @p data) will be called every time that need show the tooltip and
516 * it should return a valid Evas_Object. This object is then managed
517 * fully by tooltip system and is deleted when the tooltip is gone.
519 * This is an internal function that is used by objects with sub-items
520 * that want to provide different tooltips for each of them. The @a
521 * owner object should be an elm_widget and will be used to track
522 * theme changes and to feed @a func and @a del_cb. The @a eventarea
523 * may be any object and is the one that should be used later on with
524 * elm_object_tooltip apis, such as elm_object_tooltip_hide(),
525 * elm_object_tooltip_show() or elm_object_tooltip_unset().
527 * @param eventarea the object being attached a tooltip.
528 * @param owner the elm_widget that owns this object, will be used to
529 * track theme changes and to be used in @a func or @a del_cb.
530 * @param func the function used to create the tooltip contents. The
531 * @a Evas_Object parameters will receive @a owner as value.
532 * @param data what to provide to @a func as callback data/context.
533 * @param del_cb called when data is not needed anymore, either when
534 * another callback replaces @func, the tooltip is unset with
535 * elm_object_tooltip_unset() or the owner object @a obj
536 * dies. This callback receives as the first parameter the
537 * given @a data, and @c event_info is NULL.
543 elm_object_sub_tooltip_content_cb_set(Evas_Object *eventarea, Evas_Object *owner, Elm_Tooltip_Content_Cb func, const void *data, Evas_Smart_Cb del_cb)
545 Elm_Tooltip *tt = NULL;
546 Eina_Bool just_created;
548 EINA_SAFETY_ON_NULL_GOTO(owner, error);
549 EINA_SAFETY_ON_NULL_GOTO(eventarea, error);
553 elm_object_tooltip_unset(eventarea);
557 tt = evas_object_data_get(eventarea, _tooltip_key);
560 if (tt->owner != owner)
562 if (tt->owner != eventarea)
563 evas_object_event_callback_del_full
564 (tt->owner, EVAS_CALLBACK_DEL, _elm_tooltip_obj_del_cb, tt);
566 elm_widget_tooltip_del(tt->owner, tt);
568 if (owner != eventarea)
569 evas_object_event_callback_add
570 (owner, EVAS_CALLBACK_DEL, _elm_tooltip_obj_del_cb, tt);
572 elm_widget_tooltip_add(tt->owner, tt);
575 if ((tt->func == func) && (tt->data == data) &&
576 (tt->del_cb == del_cb))
578 _elm_tooltip_data_clean(tt);
579 just_created = EINA_FALSE;
583 tt = ELM_NEW(Elm_Tooltip);
587 tt->eventarea = eventarea;
588 tt->evas = evas_object_evas_get(eventarea);
589 evas_object_data_set(eventarea, _tooltip_key, tt);
591 just_created = EINA_TRUE;
593 evas_object_event_callback_add
594 (eventarea, EVAS_CALLBACK_MOUSE_IN,
595 _elm_tooltip_obj_mouse_in_cb, tt);
596 evas_object_event_callback_add
597 (eventarea, EVAS_CALLBACK_MOUSE_OUT,
598 _elm_tooltip_obj_mouse_out_cb, tt);
599 evas_object_event_callback_add
600 (eventarea, EVAS_CALLBACK_DEL, _elm_tooltip_obj_del_cb, tt);
602 if (owner != eventarea)
603 evas_object_event_callback_add
604 (owner, EVAS_CALLBACK_DEL, _elm_tooltip_obj_del_cb, tt);
606 elm_widget_tooltip_add(tt->owner, tt);
613 if (!just_created) _elm_tooltip_reconfigure_job_start(tt);
617 if (del_cb) del_cb((void *)data, owner, NULL);
621 * Force show tooltip of object
623 * @param obj Target object
625 * Force show the tooltip and disable hide on mouse_out.
626 * If another content is set as tooltip, the visible tooltip will hididen and
627 * showed again with new content.
628 * This can force show more than one tooltip at a time.
633 elm_object_tooltip_show(Evas_Object *obj)
635 ELM_TOOLTIP_GET_OR_RETURN(tt, obj);
636 tt->visible_lock = EINA_TRUE;
637 _elm_tooltip_show(tt);
641 * Force hide tooltip of object
643 * @param obj Target object
645 * Force hide the tooltip and (re)enable future mouse interations.
650 elm_object_tooltip_hide(Evas_Object *obj)
652 ELM_TOOLTIP_GET_OR_RETURN(tt, obj);
653 tt->visible_lock = EINA_FALSE;
654 _elm_tooltip_hide_anim_start(tt);
658 * Set the text to be shown in the tooltip object
660 * @param obj Target object
661 * @param text The text to set in the content
663 * Setup the text as tooltip to object. The object can have only one tooltip,
664 * so any previous tooltip data is removed.
665 * This method call internaly the elm_tooltip_content_cb_set().
670 elm_object_tooltip_text_set(Evas_Object *obj, const char *text)
672 EINA_SAFETY_ON_NULL_RETURN(obj);
673 EINA_SAFETY_ON_NULL_RETURN(text);
675 text = eina_stringshare_add(text);
676 elm_object_tooltip_content_cb_set
677 (obj, _elm_tooltip_label_create, text, _elm_tooltip_label_del_cb);
681 * Set the content to be shown in the tooltip object
683 * Setup the tooltip to object. The object can have only one tooltip,
684 * so any previous tooltip data is removed. @p func(with @p data) will
685 * be called every time that need show the tooltip and it should
686 * return a valid Evas_Object. This object is then managed fully by
687 * tooltip system and is deleted when the tooltip is gone.
689 * @param obj the object being attached a tooltip.
690 * @param func the function used to create the tooltip contents.
691 * @param data what to provide to @a func as callback data/context.
692 * @param del_cb called when data is not needed anymore, either when
693 * another callback replaces @func, the tooltip is unset with
694 * elm_object_tooltip_unset() or the owner object @a obj
695 * dies. This callback receives as the first parameter the
696 * given @a data, and @c event_info is NULL.
701 elm_object_tooltip_content_cb_set(Evas_Object *obj, Elm_Tooltip_Content_Cb func, const void *data, Evas_Smart_Cb del_cb)
703 elm_object_sub_tooltip_content_cb_set(obj, obj, func, data, del_cb);
707 * Unset tooltip from object
709 * @param obj Target object
711 * Remove tooltip from object. The callback provided as del_cb to
712 * elm_object_tooltip_content_cb_set() will be called to notify it is
715 * @see elm_object_tooltip_content_cb_set()
720 elm_object_tooltip_unset(Evas_Object *obj)
722 ELM_TOOLTIP_GET_OR_RETURN(tt, obj);
723 _elm_tooltip_unset(tt);
727 * Sets a different style for this object tooltip.
729 * @note before you set a style you should define a tooltip with
730 * elm_object_tooltip_content_cb_set() or
731 * elm_object_tooltip_text_set().
733 * @param obj an object with tooltip already set.
734 * @param style the theme style to use (default, transparent, ...)
737 elm_object_tooltip_style_set(Evas_Object *obj, const char *style)
739 ELM_TOOLTIP_GET_OR_RETURN(tt, obj);
740 if (!eina_stringshare_replace(&tt->style, style)) return;
741 elm_tooltip_theme(tt);
745 * Get the style for this object tooltip.
747 * @param obj an object with tooltip already set.
748 * @return style the theme style in use, defaults to "default". If the
749 * object does not have a tooltip set, then NULL is returned.
752 elm_object_tooltip_style_get(const Evas_Object *obj)
754 ELM_TOOLTIP_GET_OR_RETURN(tt, obj, NULL);
755 return tt->style ? tt->style : "default";
759 * Get the configured tooltip delay
761 * This gets the globally configured tooltip delay in seconds
763 * @return The tooltip delay
767 elm_tooltip_delay_get(void)
769 return _elm_config->tooltip_delay;
773 * Set the configured tooltip delay
775 * This sets the globally configured delay to tooltip
777 * @param delay The delay to show the tooltip
778 * @return EINA_TRUE if value is valid and setted
782 elm_tooltip_delay_set(double delay)
784 if (delay < 0.0) return EINA_FALSE;
785 _elm_config->tooltip_delay = delay;