Hi All,
I've enclosed some code that I've been messing with to create a tooltip
widget and smart object that may be used for tooltips. It DOES need some
work as the tips don't currently display, but I think with a little more
coding from a more experienced E dev this could be useful.
Cheers,
devilhorns
/**
* This adds the ability to place a tooltip above or below an object
* when the mouse is over them
*/
#include "e.h"
#define SMART_NAME "e_tooltip"
#define API_ENTRY E_Smart_Data *sd; sd = evas_object_smart_data_get(obj); if ((!obj) || (!sd) || (evas_object_type_get(obj) && strcmp(evas_object_type_get(obj), SMART_NAME)))
#define INTERNAL_ENTRY E_Smart_Data *sd; sd = evas_object_smart_data_get(obj); if (!sd) return;
typedef struct _E_Smart_Data E_Smart_Data;
struct _E_Smart_Data
{
Evas_Coord x, y, w, h;
Evas_Object *smart_obj;
Evas_Object *edje_obj;
char *txt;
Evas_Coord minw, minh;
Ecore_Timer *set_timer;
};
/* local subsystem functions */
static int _e_smart_set_timer(void *data);
static void _e_smart_text_update(E_Smart_Data *sd);
static void _e_smart_text_update_now(E_Smart_Data *sd);
static void _e_smart_reconfigure(E_Smart_Data *sd);
static void _e_smart_add(Evas_Object *obj);
static void _e_smart_del(Evas_Object *obj);
static void _e_smart_move(Evas_Object *obj, Evas_Coord x, Evas_Coord y);
static void _e_smart_resize(Evas_Object *obj, Evas_Coord w, Evas_Coord h);
static void _e_smart_show(Evas_Object *obj);
static void _e_smart_hide(Evas_Object *obj);
static void _e_smart_color_set(Evas_Object *obj, int r, int g, int b, int a);
static void _e_smart_clip_set(Evas_Object *obj, Evas_Object *clip);
static void _e_smart_clip_unset(Evas_Object *obj);
static void _e_smart_init(void);
/* local subsystem globals */
static Evas_Smart *_e_smart = NULL;
/* external functions */
Evas_Object *e_tooltip_add(Evas *evas)
{
_e_smart_init();
return evas_object_smart_add(evas, _e_smart);
}
void e_tooltip_text_set(Evas_Object *obj, const char *txt)
{
int changed = 0;
API_ENTRY return;
if (((sd->txt) && (!txt)) || ((!sd->txt) && (txt))) changed = 1;
E_FREE(sd->txt);
sd->txt = NULL;
if (txt) sd->txt = strdup(txt);
if (changed)
{
if (sd->txt)
{
edje_object_signal_emit(sd->edje_obj, "show_label", "");
}
else
{
edje_object_signal_emit(sd->edje_obj, "hide_label", "");
}
}
_e_smart_text_update(sd);
edje_object_message_signal_process(sd->edje_obj);
edje_object_size_min_calc(sd->edje_obj, &(sd->minw), &(sd->minh));
}
const char *e_tooltip_text_get(Evas_Object *obj)
{
API_ENTRY return;
return sd->txt;
}
void e_tooltip_min_size_get(Evas_Object *obj, Evas_Coord *minw, Evas_Coord *minh)
{
API_ENTRY return;
if (minw) *minw = sd->minw;
if (minh) *minh = sd->minh;
}
Evas_Object *e_tooltip_edje_object_get(Evas_Object *obj)
{
API_ENTRY return NULL;
return sd->edje_obj;
}
/* local functions */
static int _e_smart_set_timer(void *data)
{
return 0;
}
static void _e_smart_text_update(E_Smart_Data *sd)
{
if (sd->txt)
{
char buf[4096];
snprintf(buf, sizeof(buf), "%s", sd->txt);
edje_object_part_text_set(sd->edje_obj, "label", buf);
}
}
static void _e_smart_reconfigure(E_Smart_Data *sd)
{
evas_object_move(sd->edje_obj, sd->x, sd->y);
evas_object_resize(sd->edje_obj, sd->w, sd->h);
}
static void _e_smart_add(Evas_Object *obj)
{
E_Smart_Data *sd;
Evas_Object *o;
sd = calloc(1, sizeof(E_Smart_Data));
if (!sd) return;
evas_object_smart_data_set(obj, sd);
sd->smart_obj = obj;
sd->x = 0;
sd->y = 0;
sd->w = 0;
sd->h = 0;
sd->txt = NULL;
sd->edje_obj = edje_object_add(evas_object_evas_get(obj));
e_theme_edje_object_set(sd->edje_obj, "base/theme/widgets", "widgets/tooltip");
edje_object_size_min_calc(sd->edje_obj, &(sd->minw), &(sd->minh));
evas_object_smart_member_add(sd->edje_obj, obj);
/* Add callback signals */
}
static void _e_smart_del(Evas_Object *obj)
{
INTERNAL_ENTRY;
evas_object_del(sd->edje_obj);
E_FREE(sd->txt);
if (sd->set_timer) ecore_timer_del(sd->set_timer);
free(sd);
}
static void _e_smart_move(Evas_Object *obj, Evas_Coord x, Evas_Coord y)
{
INTERNAL_ENTRY;
if ((sd->x == x) && (sd->y == y)) return;
sd->x = x;
sd->y = y;
_e_smart_reconfigure(sd);
}
static void _e_smart_resize(Evas_Object *obj, Evas_Coord w, Evas_Coord h)
{
INTERNAL_ENTRY;
if ((sd->w == w) && (sd->h == h)) return;
sd->w = w;
sd->h = h;
_e_smart_reconfigure(sd);
}
static void _e_smart_show(Evas_Object *obj)
{
INTERNAL_ENTRY;
evas_object_show(sd->edje_obj);
}
static void _e_smart_hide(Evas_Object *obj)
{
INTERNAL_ENTRY;
evas_object_hide(sd->edje_obj);
}
static void _e_smart_color_set(Evas_Object *obj, int r, int g, int b, int a)
{
INTERNAL_ENTRY;
evas_object_color_set(sd->edje_obj, r, g, b, a);
}
static void _e_smart_clip_set(Evas_Object *obj, Evas_Object *clip)
{
INTERNAL_ENTRY;
evas_object_clip_set(sd->edje_obj, clip);
}
static void _e_smart_clip_unset(Evas_Object *obj)
{
INTERNAL_ENTRY;
evas_object_clip_unset(sd->edje_obj);
}
static void _e_smart_init(void)
{
if (_e_smart) return;
_e_smart = evas_smart_new(SMART_NAME,
_e_smart_add,
_e_smart_del,
NULL, NULL, NULL, NULL, NULL,
_e_smart_move,
_e_smart_resize,
_e_smart_show,
_e_smart_hide,
_e_smart_color_set,
_e_smart_clip_set,
_e_smart_clip_unset,
NULL);
}
#ifdef E_TYPEDEFS
#else
#ifndef E_TOOLTIP_H
#define E_TOOLTIP_H
EAPI Evas_Object *e_tooltip_add(Evas *evas);
EAPI void e_tooltip_text_set(Evas_Object *obj, const char *txt);
EAPI const char *e_tooltip_text_get(Evas_Object *obj);
EAPI Evas_Object *e_tooltip_edje_object_get(Evas_Object *obj);
#endif
#endif
#include "e.h"
typedef struct _E_Widget_Data E_Widget_Data;
struct _E_Widget_Data
{
Evas_Object *o_widget, *o_tooltip;
char *txt;
};
static void _e_wid_del_hook(Evas_Object *obj);
Evas_Object *e_widget_tooltip_add(Evas *evas, char *txt)
{
Evas_Object *obj, *o;
E_Widget_Data *wd;
Evas_Coord mw, mh;
obj = e_widget_add(evas);
e_widget_del_hook_set(obj, _e_wid_del_hook);
wd = calloc(1, sizeof(E_Widget_Data));
e_widget_data_set(obj, wd);
wd->o_widget = obj;
o = e_tooltip_add(evas);
wd->o_tooltip = o;
evas_object_show(o);
e_widget_sub_object_add(obj, o);
e_widget_resize_object_set(obj, o);
e_tooltip_text_set(o, "Test");
e_widget_can_focus_set(obj, 0);
e_tooltip_min_size_get(o, &mw, &mh);
e_widget_min_size_set(obj, mw, mh);
return obj;
}
static void
_e_wid_del_hook(Evas_Object *obj)
{
E_Widget_Data *wd;
wd = e_widget_data_get(obj);
free(wd);
}
#ifdef E_TYPEDEFS
#else
#ifndef E_WIDGET_TOOLTIP_H
#define E_WIDGET_TOOLTIP_H
EAPI Evas_Object *e_widget_tooltip_add(Evas *evas, char *txt);
#endif
#endif
group {
name: "widgets/tooltip";
parts {
part {
name: "label";
type: TEXT;
effect: SHADOW;
mouse_events: 0;
description {
state: "default" 0.0;
visible: 1;
align: 0.5 0.5;
rel1 {
relative: 0.0 0.0;
offset: 0 0;
}
rel2 {
relative: 1.0 1.0;
offset: -1 -1;
}
color: 0 0 0 255;
color3: 255 255 255 128;
text {
text: "Label";
font: "Edje-Vera";
size: 10;
min: 1 1;
align: 0.5 0.5;
}
}
description {
state: "active" 0.0;
inherit: "default" 0.0;
visible: 1;
}
description {
state: "hidden" 0.0;
inherit: "default" 0.0;
visible: 0;
}
}
}
programs {
program {
name: "show_label";
signal: "show_label";
source: "";
action: STATE_SET "active" 0.0;
target: "label";
}
program {
name: "hide_label";
signal: "hide_label";
source: "";
action: STATE_SET "hidden" 0.0;
target: "label";
}
}
}