1 #include <Elementary.h>
2 #include <Elementary_Cursor.h>
5 #ifdef HAVE_ELEMENTARY_X
7 #include <Ecore_X_Cursor.h>
10 #define _cursor_key "_elm_cursor"
15 #ifdef HAVE_ELEMENTARY_X
20 #ifdef HAVE_ELEMENTARY_X
21 #define CURSOR(_name, _xid) \
24 #define CURSOR(_name, _xid) \
28 /* Please keep order in sync with Ecore_X_Cursor.h values! */
29 struct _Cursor_Id _cursors[] =
31 CURSOR(ELM_CURSOR_X , ECORE_X_CURSOR_X ),
32 CURSOR(ELM_CURSOR_ARROW , ECORE_X_CURSOR_ARROW ),
33 CURSOR(ELM_CURSOR_BASED_ARROW_DOWN , ECORE_X_CURSOR_BASED_ARROW_DOWN ),
34 CURSOR(ELM_CURSOR_BASED_ARROW_UP , ECORE_X_CURSOR_UP ),
35 CURSOR(ELM_CURSOR_BOAT , ECORE_X_CURSOR_BOAT ),
36 CURSOR(ELM_CURSOR_BOGOSITY , ECORE_X_CURSOR_BOGOSITY ),
37 CURSOR(ELM_CURSOR_BOTTOM_LEFT_CORNER , ECORE_X_CURSOR_BOTTOM_LEFT_CORNER ),
38 CURSOR(ELM_CURSOR_BOTTOM_RIGHT_CORNER, ECORE_X_CURSOR_BOTTOM_RIGHT_CORNER),
39 CURSOR(ELM_CURSOR_BOTTOM_SIDE , ECORE_X_CURSOR_BOTTOM_SIDE ),
40 CURSOR(ELM_CURSOR_BOTTOM_TEE , ECORE_X_CURSOR_BOTTOM_TEE ),
41 CURSOR(ELM_CURSOR_BOX_SPIRAL , ECORE_X_CURSOR_BOX_SPIRAL ),
42 CURSOR(ELM_CURSOR_CENTER_PTR , ECORE_X_CURSOR_CENTER_PTR ),
43 CURSOR(ELM_CURSOR_CIRCLE , ECORE_X_CURSOR_CIRCLE ),
44 CURSOR(ELM_CURSOR_CLOCK , ECORE_X_CURSOR_CLOCK ),
45 CURSOR(ELM_CURSOR_COFFEE_MUG , ECORE_X_CURSOR_COFFEE_MUG ),
46 CURSOR(ELM_CURSOR_CROSS , ECORE_X_CURSOR_CROSS ),
47 CURSOR(ELM_CURSOR_CROSS_REVERSE , ECORE_X_CURSOR_CROSS_REVERSE ),
48 CURSOR(ELM_CURSOR_CROSSHAIR , ECORE_X_CURSOR_CROSSHAIR ),
49 CURSOR(ELM_CURSOR_DIAMOND_CROSS , ECORE_X_CURSOR_DIAMOND_CROSS ),
50 CURSOR(ELM_CURSOR_DOT , ECORE_X_CURSOR_DOT ),
51 CURSOR(ELM_CURSOR_DOT_BOX_MASK , ECORE_X_CURSOR_DOT_BOX_MASK ),
52 CURSOR(ELM_CURSOR_DOUBLE_ARROW , ECORE_X_CURSOR_DOUBLE_ARROW ),
53 CURSOR(ELM_CURSOR_DRAFT_LARGE , ECORE_X_CURSOR_DRAFT_LARGE ),
54 CURSOR(ELM_CURSOR_DRAFT_SMALL , ECORE_X_CURSOR_DRAFT_SMALL ),
55 CURSOR(ELM_CURSOR_DRAPED_BOX , ECORE_X_CURSOR_DRAPED_BOX ),
56 CURSOR(ELM_CURSOR_EXCHANGE , ECORE_X_CURSOR_EXCHANGE ),
57 CURSOR(ELM_CURSOR_FLEUR , ECORE_X_CURSOR_FLEUR ),
58 CURSOR(ELM_CURSOR_GOBBLER , ECORE_X_CURSOR_GOBBLER ),
59 CURSOR(ELM_CURSOR_GUMBY , ECORE_X_CURSOR_GUMBY ),
60 CURSOR(ELM_CURSOR_HAND1 , ECORE_X_CURSOR_HAND1 ),
61 CURSOR(ELM_CURSOR_HAND2 , ECORE_X_CURSOR_HAND2 ),
62 CURSOR(ELM_CURSOR_HEART , ECORE_X_CURSOR_HEART ),
63 CURSOR(ELM_CURSOR_ICON , ECORE_X_CURSOR_ICON ),
64 CURSOR(ELM_CURSOR_IRON_CROSS , ECORE_X_CURSOR_IRON_CROSS ),
65 CURSOR(ELM_CURSOR_LEFT_PTR , ECORE_X_CURSOR_LEFT_PTR ),
66 CURSOR(ELM_CURSOR_LEFT_SIDE , ECORE_X_CURSOR_LEFT_SIDE ),
67 CURSOR(ELM_CURSOR_LEFT_TEE , ECORE_X_CURSOR_LEFT_TEE ),
68 CURSOR(ELM_CURSOR_LEFTBUTTON , ECORE_X_CURSOR_LEFTBUTTON ),
69 CURSOR(ELM_CURSOR_LL_ANGLE , ECORE_X_CURSOR_LL_ANGLE ),
70 CURSOR(ELM_CURSOR_LR_ANGLE , ECORE_X_CURSOR_LR_ANGLE ),
71 CURSOR(ELM_CURSOR_MAN , ECORE_X_CURSOR_MAN ),
72 CURSOR(ELM_CURSOR_MIDDLEBUTTON , ECORE_X_CURSOR_MIDDLEBUTTON ),
73 CURSOR(ELM_CURSOR_MOUSE , ECORE_X_CURSOR_MOUSE ),
74 CURSOR(ELM_CURSOR_PENCIL , ECORE_X_CURSOR_PENCIL ),
75 CURSOR(ELM_CURSOR_PIRATE , ECORE_X_CURSOR_PIRATE ),
76 CURSOR(ELM_CURSOR_PLUS , ECORE_X_CURSOR_PLUS ),
77 CURSOR(ELM_CURSOR_QUESTION_ARROW , ECORE_X_CURSOR_QUESTION_ARROW ),
78 CURSOR(ELM_CURSOR_RIGHT_PTR , ECORE_X_CURSOR_RIGHT_PTR ),
79 CURSOR(ELM_CURSOR_RIGHT_SIDE , ECORE_X_CURSOR_RIGHT_SIDE ),
80 CURSOR(ELM_CURSOR_RIGHT_TEE , ECORE_X_CURSOR_RIGHT_TEE ),
81 CURSOR(ELM_CURSOR_RIGHTBUTTON , ECORE_X_CURSOR_RIGHTBUTTON ),
82 CURSOR(ELM_CURSOR_RTL_LOGO , ECORE_X_CURSOR_RTL_LOGO ),
83 CURSOR(ELM_CURSOR_SAILBOAT , ECORE_X_CURSOR_SAILBOAT ),
84 CURSOR(ELM_CURSOR_SB_DOWN_ARROW , ECORE_X_CURSOR_SB_DOWN_ARROW ),
85 CURSOR(ELM_CURSOR_SB_H_DOUBLE_ARROW , ECORE_X_CURSOR_SB_H_DOUBLE_ARROW ),
86 CURSOR(ELM_CURSOR_SB_LEFT_ARROW , ECORE_X_CURSOR_SB_LEFT_ARROW ),
87 CURSOR(ELM_CURSOR_SB_RIGHT_ARROW , ECORE_X_CURSOR_SB_RIGHT_ARROW ),
88 CURSOR(ELM_CURSOR_SB_UP_ARROW , ECORE_X_CURSOR_SB_UP_ARROW ),
89 CURSOR(ELM_CURSOR_SB_V_DOUBLE_ARROW , ECORE_X_CURSOR_SB_V_DOUBLE_ARROW ),
90 CURSOR(ELM_CURSOR_SHUTTLE , ECORE_X_CURSOR_SHUTTLE ),
91 CURSOR(ELM_CURSOR_SIZING , ECORE_X_CURSOR_SIZING ),
92 CURSOR(ELM_CURSOR_SPIDER , ECORE_X_CURSOR_SPIDER ),
93 CURSOR(ELM_CURSOR_SPRAYCAN , ECORE_X_CURSOR_SPRAYCAN ),
94 CURSOR(ELM_CURSOR_STAR , ECORE_X_CURSOR_STAR ),
95 CURSOR(ELM_CURSOR_TARGET , ECORE_X_CURSOR_TARGET ),
96 CURSOR(ELM_CURSOR_TCROSS , ECORE_X_CURSOR_TCROSS ),
97 CURSOR(ELM_CURSOR_TOP_LEFT_ARROW , ECORE_X_CURSOR_TOP_LEFT_ARROW ),
98 CURSOR(ELM_CURSOR_TOP_LEFT_CORNER , ECORE_X_CURSOR_TOP_LEFT_CORNER ),
99 CURSOR(ELM_CURSOR_TOP_RIGHT_CORNER , ECORE_X_CURSOR_TOP_RIGHT_CORNER ),
100 CURSOR(ELM_CURSOR_TOP_SIDE , ECORE_X_CURSOR_TOP_SIDE ),
101 CURSOR(ELM_CURSOR_TOP_TEE , ECORE_X_CURSOR_TOP_TEE ),
102 CURSOR(ELM_CURSOR_TREK , ECORE_X_CURSOR_TREK ),
103 CURSOR(ELM_CURSOR_UL_ANGLE , ECORE_X_CURSOR_UL_ANGLE ),
104 CURSOR(ELM_CURSOR_UMBRELLA , ECORE_X_CURSOR_UMBRELLA ),
105 CURSOR(ELM_CURSOR_UR_ANGLE , ECORE_X_CURSOR_UR_ANGLE ),
106 CURSOR(ELM_CURSOR_WATCH , ECORE_X_CURSOR_WATCH ),
107 CURSOR(ELM_CURSOR_XTERM , ECORE_X_CURSOR_XTERM )
109 static const int _cursors_count = sizeof(_cursors)/sizeof(struct _Cursor_Id);
111 #define ELM_CURSOR_GET_OR_RETURN(cur, obj, ...) \
117 CRITICAL("Null pointer: " #obj); \
118 return __VA_ARGS__; \
120 cur = evas_object_data_get((obj), _cursor_key); \
123 ERR("Object does not have cursor: " #obj); \
124 return __VA_ARGS__; \
132 Evas_Object *eventarea, *owner;
133 const char *style, *cursor_name;
137 #ifdef HAVE_ELEMENTARY_X
138 Ecore_X_Cursor cursor;
142 Eina_Bool use_engine:1;
143 Eina_Bool engine_only:1;
147 _elm_cursor_obj_del(void *data, Evas *evas __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
149 Elm_Cursor *cur = data;
151 if (cur) cur->obj = NULL;
155 _elm_cursor_obj_add(Evas_Object *obj, Elm_Cursor *cur)
159 cur->obj = edje_object_add(cur->evas);
164 if (!_elm_theme_object_set(obj, cur->obj, "cursor", cur->cursor_name,
165 cur->style ? cur->style : "default"))
167 evas_object_del(cur->obj);
172 evas_object_event_callback_add(cur->obj, EVAS_CALLBACK_DEL,
173 _elm_cursor_obj_del, cur);
175 edje_object_size_min_get(cur->obj, &x, &y);
176 evas_object_resize(cur->obj, x, y);
181 _elm_cursor_set_hot_spots(Elm_Cursor *cur)
185 str = edje_object_data_get(cur->obj, "hot_x");
186 if (str) cur->hot_x = atoi(str);
189 str = edje_object_data_get(cur->obj, "hot_y");
190 if (str) cur->hot_y = atoi(str);
195 _elm_cursor_mouse_in(void *data, Evas *evas __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
197 Elm_Cursor *cur = data;
199 if (cur->visible) return;
200 evas_event_freeze(cur->evas);
201 cur->visible = EINA_TRUE;
202 if ((!cur->engine_only) && (!cur->use_engine))
205 _elm_cursor_obj_add(cur->eventarea, cur);
206 ecore_evas_object_cursor_set(cur->ee, cur->obj,
207 ELM_OBJECT_LAYER_CURSOR, cur->hot_x,
212 #ifdef HAVE_ELEMENTARY_X
215 ecore_x_window_cursor_set(cur->win, cur->cursor);
219 evas_event_thaw(cur->evas);
223 _elm_cursor_mouse_out(void *data, Evas *evas __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
225 Evas_Object *sobj_parent;
226 Elm_Cursor *pcur = NULL;
227 Elm_Cursor *cur = data;
229 if (!cur->visible) return;
230 evas_event_freeze(cur->evas);
231 cur->visible = EINA_FALSE;
233 sobj_parent = evas_object_data_get(cur->eventarea, "elm-parent");
236 pcur = evas_object_data_get((sobj_parent), _cursor_key);
237 if ((pcur) && (pcur->visible)) break;
238 sobj_parent = evas_object_data_get(sobj_parent, "elm-parent");
243 pcur->visible = EINA_FALSE;
244 evas_event_thaw(cur->evas);
245 _elm_cursor_mouse_in(pcur, NULL, NULL, NULL);
249 if ((!cur->engine_only) || (!cur->use_engine))
251 ecore_evas_object_cursor_set(cur->ee, NULL, ELM_OBJECT_LAYER_CURSOR,
252 cur->hot_x, cur->hot_y);
256 #ifdef HAVE_ELEMENTARY_X
259 ecore_x_window_cursor_set(cur->win, ECORE_X_CURSOR_X);
263 evas_event_thaw(cur->evas);
267 _elm_cursor_del(void *data __UNUSED__, Evas *evas __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
269 elm_object_cursor_unset(obj);
273 _elm_cursor_strcmp(const void *data1, const void *data2)
275 const struct _Cursor_Id *c1 = data1;
276 const struct _Cursor_Id *c2 = data2;
277 return strcmp (c1->name, c2->name);
281 _elm_cursor_cur_set(Elm_Cursor *cur)
283 if (cur->engine_only)
285 INF("Using only engine cursors");
286 cur->use_engine = EINA_TRUE;
288 else if (_elm_cursor_obj_add(cur->eventarea, cur))
290 _elm_cursor_set_hot_spots(cur);
291 cur->use_engine = EINA_FALSE;
292 elm_widget_cursor_add(cur->owner, cur);
296 INF("Cursor couldn't be found on theme: %s", cur->cursor_name);
297 cur->use_engine = EINA_TRUE;
302 #ifdef HAVE_ELEMENTARY_X
303 struct _Cursor_Id *cur_id;
305 cur_id = bsearch(&(cur->cursor_name), _cursors, _cursors_count,
306 sizeof(struct _Cursor_Id), _elm_cursor_strcmp);
308 cur->win = elm_win_xwindow_get(cur->eventarea);
313 INF("X cursor couldn't be found: %s. Using default.",
315 cur->cursor = ecore_x_cursor_shape_get(ECORE_X_CURSOR_X);
318 cur->cursor = ecore_x_cursor_shape_get(cur_id->id);
325 * Set the cursor to be shown when mouse is over the object
327 * Set the cursor that will be displayed when mouse is over the
328 * object. The object can have only one cursor set to it, so if
329 * this function is called twice for an object, the previous set
331 * If using X cursors, a definition of all the valid cursor names
332 * is listed on Elementary_Cursors.h. If an invalid name is set
333 * the default cursor will be used.
335 * This is an internal function that is used by objects with sub-items
336 * that want to provide different cursors for each of them. The @a
337 * owner object should be an elm_widget and will be used to track
338 * theme changes and to feed @a func and @a del_cb. The @a eventarea
339 * may be any object and is the one that should be used later on with
340 * elm_object_cursor apis, such as elm_object_cursor_unset().
342 * @param eventarea the object being attached a cursor.
343 * @param owner the elm_widget that owns this object, will be used to
344 * track theme changes and to be used in @a func or @a del_cb.
345 * @param cursor the cursor name to be used.
351 elm_object_sub_cursor_set(Evas_Object *eventarea, Evas_Object *owner, const char *cursor)
353 Elm_Cursor *cur = NULL;
355 cur = evas_object_data_get(eventarea, _cursor_key);
357 elm_object_cursor_unset(eventarea);
361 cur = ELM_NEW(Elm_Cursor);
365 cur->eventarea = eventarea;
366 cur->engine_only = _elm_config->cursor_engine_only;
367 cur->visible = EINA_FALSE;
369 cur->cursor_name = eina_stringshare_add(cursor);
370 if (!cur->cursor_name)
371 ERR("Could not store cursor name %s", cursor);
373 cur->evas = evas_object_evas_get(eventarea);
374 cur->ee = ecore_evas_ecore_evas_get(cur->evas);
376 _elm_cursor_cur_set(cur);
378 evas_object_data_set(eventarea, _cursor_key, cur);
380 evas_object_event_callback_add(eventarea, EVAS_CALLBACK_MOUSE_IN,
381 _elm_cursor_mouse_in, cur);
382 evas_object_event_callback_add(eventarea, EVAS_CALLBACK_MOUSE_OUT,
383 _elm_cursor_mouse_out, cur);
384 evas_object_event_callback_add(eventarea, EVAS_CALLBACK_DEL,
385 _elm_cursor_del, cur);
389 elm_object_cursor_set(Evas_Object *obj, const char *cursor)
391 EINA_SAFETY_ON_NULL_RETURN(obj);
392 elm_object_sub_cursor_set(obj, obj, cursor);
396 elm_object_cursor_get(const Evas_Object *obj)
398 ELM_CURSOR_GET_OR_RETURN(cur, obj, NULL);
399 return cur->cursor_name;
403 elm_object_cursor_unset(Evas_Object *obj)
405 ELM_CURSOR_GET_OR_RETURN(cur, obj);
407 eina_stringshare_del(cur->cursor_name);
408 eina_stringshare_del(cur->style);
411 elm_widget_cursor_del(cur->owner, cur);
414 evas_object_del(cur->obj);
418 if (!cur->use_engine)
419 ecore_evas_object_cursor_set(cur->ee, NULL, ELM_OBJECT_LAYER_CURSOR,
420 cur->hot_x, cur->hot_y);
421 #ifdef HAVE_ELEMENTARY_X
423 ecore_x_window_cursor_set(cur->win, ECORE_X_CURSOR_X);
427 evas_object_event_callback_del(obj, EVAS_CALLBACK_MOUSE_IN,
428 _elm_cursor_mouse_in);
429 evas_object_event_callback_del(obj, EVAS_CALLBACK_MOUSE_OUT,
430 _elm_cursor_mouse_out);
431 evas_object_event_callback_del(obj, EVAS_CALLBACK_DEL, _elm_cursor_del);
433 evas_object_data_del(obj, _cursor_key);
438 elm_object_cursor_style_set(Evas_Object *obj, const char *style)
440 ELM_CURSOR_GET_OR_RETURN(cur, obj);
442 if (!eina_stringshare_replace(&cur->style, style))
443 ERR("Could not set current style=%s", style);
445 if (cur->use_engine) return;
449 if (!_elm_cursor_obj_add(obj, cur))
450 ERR("Could not create cursor object");
452 _elm_cursor_set_hot_spots(cur);
456 if (!_elm_theme_object_set(obj, cur->obj, "cursor", cur->cursor_name,
458 ERR("Could not apply the theme to the cursor style=%s", style);
460 _elm_cursor_set_hot_spots(cur);
465 elm_object_cursor_style_get(const Evas_Object *obj)
467 ELM_CURSOR_GET_OR_RETURN(cur, obj, NULL);
468 return cur->style ? cur->style : "default";
472 * Notify cursor should recalculate its theme.
476 elm_cursor_theme(Elm_Cursor *cur)
478 if ((!cur) || (!cur->obj)) return;
479 if (!_elm_theme_object_set(cur->eventarea, cur->obj, "cursor",
480 cur->cursor_name, cur->style))
481 ERR("Could not apply the theme to the cursor style=%s", cur->style);
483 _elm_cursor_set_hot_spots(cur);
486 EINA_DEPRECATED EAPI void
487 elm_object_cursor_engine_only_set(Evas_Object *obj, Eina_Bool engine_only)
489 elm_object_cursor_theme_search_enabled_set(obj, engine_only);
492 EAPI void elm_object_cursor_theme_search_enabled_set(Evas_Object *obj, Eina_Bool theme_search)
494 ELM_CURSOR_GET_OR_RETURN(cur, obj);
495 cur->engine_only = theme_search;
498 evas_object_del(cur->obj);
501 _elm_cursor_cur_set(cur);
505 EINA_DEPRECATED EAPI Eina_Bool
506 elm_object_cursor_engine_only_get(const Evas_Object *obj)
508 return elm_object_cursor_theme_search_enabled_get(obj);
512 elm_object_cursor_theme_search_enabled_get(const Evas_Object *obj)
514 ELM_CURSOR_GET_OR_RETURN(cur, obj, EINA_FALSE);
515 return cur->engine_only;