1 #include <Elementary.h>
5 #define NON_EXISTING (void *)-1
6 static const char *icon_theme = NULL;
13 * A standard icon that may be provided by the theme (delete, edit,
14 * arrows etc.) or a custom file (PNG, JPG, EDJE etc.) used for an
15 * icon. The Icon may scale or not and of course... support alpha
18 * Signals that you can add callbacks for are:
20 * "clicked" - This is called when a user has clicked the icon
23 typedef struct _Widget_Data Widget_Data;
29 Elm_Icon_Lookup_Order lookup_order;
36 Eina_Bool scale_up : 1;
37 Eina_Bool scale_down : 1;
39 Eina_Bool fill_outside : 1;
40 Eina_Bool no_scale : 1;
43 static const char *widtype = NULL;
44 static void _del_hook(Evas_Object *obj);
45 static void _theme_hook(Evas_Object *obj);
46 static void _sizing_eval(Evas_Object *obj);
47 static void _mouse_up(void *data, Evas *e, Evas_Object *obj, void *event_info);
49 static Eina_Bool _icon_standard_set(Widget_Data *wd, Evas_Object *obj, const char *name);
50 static Eina_Bool _icon_freedesktop_set(Widget_Data *wd, Evas_Object *obj, const char *name, int size);
52 //FIXME: move this code to ecore
55 _path_is_absolute(const char *path)
57 //TODO: Check if this works with all absolute paths in windows
58 return ((isalpha (*path)) && (*(path + 1) == ':') && ((*(path + 2) == '\\') || (*(path + 2) == '/')));
62 _path_is_absolute(const char *path)
64 return (*path == '/');
69 _del_hook(Evas_Object *obj)
71 Widget_Data *wd = elm_widget_data_get(obj);
74 if (wd->stdicon) eina_stringshare_del(wd->stdicon);
79 _theme_hook(Evas_Object *obj)
81 Widget_Data *wd = elm_widget_data_get(obj);
84 _elm_theme_object_icon_set(obj, wd->img, wd->stdicon, elm_widget_style_get(obj));
89 _signal_emit_hook(Evas_Object *obj, const char *emission, const char *source)
91 Widget_Data *wd = elm_widget_data_get(obj);
93 Evas_Object *icon_edje;
94 icon_edje = _els_smart_icon_edje_get(wd->img);
95 if (!icon_edje) return;
96 edje_object_signal_emit(icon_edje, emission, source);
100 _signal_callback_add_hook(Evas_Object *obj, const char *emission, const char *source, Edje_Signal_Cb func_cb, void *data)
102 Widget_Data *wd = elm_widget_data_get(obj);
104 Evas_Object *icon_edje;
105 icon_edje = _els_smart_icon_edje_get(wd->img);
106 if (!icon_edje) return;
107 edje_object_signal_callback_add(icon_edje, emission, source, func_cb, data);
111 _signal_callback_del_hook(Evas_Object *obj, const char *emission, const char *source, Edje_Signal_Cb func_cb, void *data)
113 Widget_Data *wd = elm_widget_data_get(obj);
115 Evas_Object *icon_edje;
116 icon_edje = _els_smart_icon_edje_get(wd->img);
117 if (!icon_edje) return;
118 edje_object_signal_callback_del_full(icon_edje, emission, source, func_cb,
123 _sizing_eval(Evas_Object *obj)
125 Widget_Data *wd = elm_widget_data_get(obj);
127 Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
130 _els_smart_icon_size_get(wd->img, &w, &h);
132 if ((wd->freedesktop.use) && (!((w - wd->freedesktop.requested_size) % 16)))
134 /* This icon has been set to a freedesktop icon, and the requested
135 appears to have a different size than the requested size, so try to
136 request another, higher resolution, icon.
137 FIXME: Find a better heuristic to determine if there should be
138 an icon with a different resolution. */
139 _icon_freedesktop_set(wd, obj, wd->stdicon, w);
142 _els_smart_icon_scale_up_set(wd->img, wd->scale_up);
143 _els_smart_icon_scale_down_set(wd->img, wd->scale_down);
144 _els_smart_icon_smooth_scale_set(wd->img, wd->smooth);
145 _els_smart_icon_fill_inside_set(wd->img, !(wd->fill_outside));
146 if (wd->no_scale) _els_smart_icon_scale_set(wd->img, 1.0);
149 _els_smart_icon_scale_set(wd->img, elm_widget_scale_get(obj) *
151 _els_smart_icon_size_get(wd->img, &w, &h);
163 evas_object_size_hint_min_set(obj, minw, minh);
164 evas_object_size_hint_max_set(obj, maxw, maxh);
168 _mouse_up(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
170 Evas_Event_Mouse_Up *ev = event_info;
171 if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return;
172 evas_object_smart_callback_call(data, "clicked", event_info);
176 * Add a new icon to the parent
178 * @param parent The parent object
179 * @return The new object or NULL if it cannot be created
184 elm_icon_add(Evas_Object *parent)
190 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
192 ELM_SET_WIDTYPE(widtype, "icon");
193 elm_widget_type_set(obj, "icon");
194 elm_widget_can_focus_set(obj, EINA_FALSE);
195 elm_widget_sub_object_add(parent, obj);
196 elm_widget_data_set(obj, wd);
197 elm_widget_del_hook_set(obj, _del_hook);
198 elm_widget_theme_hook_set(obj, _theme_hook);
199 elm_widget_signal_emit_hook_set(obj, _signal_emit_hook);
200 elm_widget_signal_callback_add_hook_set(obj, _signal_callback_add_hook);
201 elm_widget_signal_callback_del_hook_set(obj, _signal_callback_del_hook);
203 wd->lookup_order = ELM_ICON_LOOKUP_THEME_FDO;
204 wd->img = _els_smart_icon_add(e);
205 evas_object_event_callback_add(wd->img, EVAS_CALLBACK_MOUSE_UP,
207 evas_object_repeat_events_set(wd->img, EINA_TRUE);
208 elm_widget_resize_object_set(obj, wd->img);
210 wd->smooth = EINA_TRUE;
211 wd->scale_up = EINA_TRUE;
212 wd->scale_down = EINA_TRUE;
219 * Set the file that will be used as icon
221 * @param obj The icon object
222 * @param file The path to file that will be used as icon
223 * @param group The group that the icon belongs in edje file
225 * @return (1 = success, 0 = error)
230 elm_icon_file_set(Evas_Object *obj, const char *file, const char *group)
232 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
233 Widget_Data *wd = elm_widget_data_get(obj);
237 if (!wd) return EINA_FALSE;
238 EINA_SAFETY_ON_NULL_RETURN_VAL(file, EINA_FALSE);
239 if (wd->stdicon) eina_stringshare_del(wd->stdicon);
241 if (((p = strrchr(file, '.'))) && (!strcasecmp(p, ".edj")))
242 ret = _els_smart_icon_file_edje_set(wd->img, file, group);
244 ret = _els_smart_icon_file_key_set(wd->img, file, group);
250 * Get the file that will be used as icon
252 * @param obj The icon object
253 * @param file The path to file that will be used as icon
254 * @param group The group that the icon belongs in edje file
259 elm_icon_file_get(const Evas_Object *obj, const char **file, const char **group)
261 ELM_CHECK_WIDTYPE(obj, widtype);
262 Widget_Data *wd = elm_widget_data_get(obj);
264 _els_smart_icon_file_get(wd->img, file, group);
268 _icon_standard_set(Widget_Data *wd, Evas_Object *obj, const char *name)
270 if (_elm_theme_object_icon_set(obj, wd->img, name, "default"))
273 /* TODO: elm_unneed_efreet() */
274 wd->freedesktop.use = EINA_FALSE;
282 _icon_file_set(Widget_Data *wd, Evas_Object *obj, const char *path)
284 if (elm_icon_file_set(obj, path, NULL))
287 /* TODO: elm_unneed_efreet() */
288 wd->freedesktop.use = EINA_FALSE;
296 _icon_freedesktop_set(Widget_Data *wd, Evas_Object *obj, const char *name, int size)
302 if (icon_theme == NON_EXISTING) return EINA_FALSE;
305 Efreet_Icon_Theme *theme;
306 /* TODO: Listen for EFREET_EVENT_ICON_CACHE_UPDATE */
307 theme = efreet_icon_theme_find(getenv("E_ICON_THEME"));
311 static const char *themes[] = {
312 "gnome", "Human", "oxygen", "hicolor", NULL
314 for (itr = themes; *itr; itr++)
316 theme = efreet_icon_theme_find(*itr);
323 icon_theme = NON_EXISTING;
327 icon_theme = eina_stringshare_add(theme->name.internal);
329 path = efreet_icon_path_find(icon_theme, name, size);
330 wd->freedesktop.use = !!path;
331 if (wd->freedesktop.use)
333 wd->freedesktop.requested_size = size;
334 elm_icon_file_set(obj, path, NULL);
342 _icon_size_min_get(Evas_Object *icon)
345 _els_smart_icon_size_get(icon, &size, NULL);
346 return (size < 32) ? 32 : size;
350 * Set the theme, as standard, for a icon.
351 * If theme was not found and it is the absolute path of an image file, this
352 * image will be used.
354 * @param obj The icon object
355 * @param name The theme name
357 * @return (1 = success, 0 = error)
362 elm_icon_standard_set(Evas_Object *obj, const char *name)
364 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
365 Widget_Data *wd = elm_widget_data_get(obj);
369 if ((!wd) || (!name)) return EINA_FALSE;
371 /* try locating the icon using the specified lookup order */
372 switch (wd->lookup_order)
374 case ELM_ICON_LOOKUP_FDO:
375 ret = _icon_freedesktop_set(wd, obj, name, _icon_size_min_get(wd->img));
377 case ELM_ICON_LOOKUP_THEME:
378 ret = _icon_standard_set(wd, obj, name);
380 case ELM_ICON_LOOKUP_THEME_FDO:
381 ret = _icon_standard_set(wd, obj, name) ||
382 _icon_freedesktop_set(wd, obj, name, _icon_size_min_get(wd->img));
384 case ELM_ICON_LOOKUP_FDO_THEME:
386 ret = _icon_freedesktop_set(wd, obj, name, _icon_size_min_get(wd->img)) ||
387 _icon_standard_set(wd, obj, name);
393 eina_stringshare_replace(&wd->stdicon, name);
398 if (_path_is_absolute(name))
399 return _icon_file_set(wd, obj, name);
401 /* if that fails, see if icon name is in the format size/name. if so,
402 try locating a fallback without the size specification */
403 if (!(tmp = strchr(name, '/'))) return EINA_FALSE;
405 if (*tmp) return elm_icon_standard_set(obj, tmp);
412 * Get the theme, as standard, for a icon
414 * @param obj The icon object
415 * @return The theme name
420 elm_icon_standard_get(const Evas_Object *obj)
422 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
423 Widget_Data *wd = elm_widget_data_get(obj);
424 if (!wd) return NULL;
429 * Sets icon lookup order, used by elm_icon_standard_set().
431 * @param obj The icon object
432 * @param order The icon lookup order
437 elm_icon_order_lookup_set(Evas_Object *obj, Elm_Icon_Lookup_Order order)
439 ELM_CHECK_WIDTYPE(obj, widtype);
440 Widget_Data *wd = elm_widget_data_get(obj);
441 if (wd) wd->lookup_order = order;
445 * Gets the icon lookup order.
447 * @param obj The icon object
448 * @return The icon lookup order
452 EAPI Elm_Icon_Lookup_Order
453 elm_icon_order_lookup_get(const Evas_Object *obj)
455 ELM_CHECK_WIDTYPE(obj, widtype) ELM_ICON_LOOKUP_THEME_FDO;
456 Widget_Data *wd = elm_widget_data_get(obj);
457 if (!wd) return ELM_ICON_LOOKUP_THEME_FDO;
458 return wd->lookup_order;
462 * Set the smooth effect for a icon
464 * @param obj The icon object
465 * @param smooth A bool to set (or no) smooth effect
466 * (1 = smooth, 0 = not smooth)
471 elm_icon_smooth_set(Evas_Object *obj, Eina_Bool smooth)
473 ELM_CHECK_WIDTYPE(obj, widtype);
474 Widget_Data *wd = elm_widget_data_get(obj);
482 * Get the smooth effect for a icon
484 * @param obj The icon object
485 * @return If setted smooth effect
490 elm_icon_smooth_get(const Evas_Object *obj)
492 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
493 Widget_Data *wd = elm_widget_data_get(obj);
495 if (!wd) return EINA_FALSE;
500 * Set if the object is scalable
502 * @param obj The icon object
503 * @param no_scale A bool to set scale (or no)
504 * (1 = no_scale, 0 = scale)
509 elm_icon_no_scale_set(Evas_Object *obj, Eina_Bool no_scale)
511 ELM_CHECK_WIDTYPE(obj, widtype);
512 Widget_Data *wd = elm_widget_data_get(obj);
515 wd->no_scale = no_scale;
520 * Get if the object isn't scalable
522 * @param obj The icon object
523 * @return If isn't scalable
528 elm_icon_no_scale_get(const Evas_Object *obj)
530 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
531 Widget_Data *wd = elm_widget_data_get(obj);
532 if (!wd) return EINA_FALSE;
537 * Set if the object is (up/down) scalable
539 * @param obj The icon object
540 * @param scale_up A bool to set if the object is scalable up
541 * @param scale_down A bool to set if the object is scalable down
546 elm_icon_scale_set(Evas_Object *obj, Eina_Bool scale_up, Eina_Bool scale_down)
548 ELM_CHECK_WIDTYPE(obj, widtype);
549 Widget_Data *wd = elm_widget_data_get(obj);
552 wd->scale_up = scale_up;
553 wd->scale_down = scale_down;
558 * Get if the object is (up/down) scalable
560 * @param obj The icon object
561 * @param scale_up A bool to set if the object is scalable up
562 * @param scale_down A bool to set if the object is scalable down
567 elm_icon_scale_get(const Evas_Object *obj, Eina_Bool *scale_up, Eina_Bool *scale_down)
569 ELM_CHECK_WIDTYPE(obj, widtype);
570 Widget_Data *wd = elm_widget_data_get(obj);
572 if (scale_up) *scale_up = wd->scale_up;
573 if (scale_down) *scale_down = wd->scale_down;
577 * Set if the object is filled outside
579 * @param obj The icon object
580 * @param fill_outside A bool to set if the object is filled outside
581 * (1 = filled, 0 = no filled)
586 elm_icon_fill_outside_set(Evas_Object *obj, Eina_Bool fill_outside)
588 ELM_CHECK_WIDTYPE(obj, widtype);
589 Widget_Data *wd = elm_widget_data_get(obj);
592 wd->fill_outside = fill_outside;
597 * Get if the object is filled outside
599 * @param obj The icon object
600 * @return If the object is filled outside
605 elm_icon_fill_outside_get(const Evas_Object *obj)
607 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
608 Widget_Data *wd = elm_widget_data_get(obj);
610 if (!wd) return EINA_FALSE;
611 return wd->fill_outside;
615 * Set the prescale size for the icon
617 * @param obj The icon object
618 * @param size The prescale size
623 elm_icon_prescale_set(Evas_Object *obj, int size)
625 ELM_CHECK_WIDTYPE(obj, widtype);
626 Widget_Data *wd = elm_widget_data_get(obj);
629 _els_smart_icon_scale_size_set(wd->img, size);
633 * Get the prescale size for the icon
635 * @param obj The icon object
636 * @return The prescale size
641 elm_icon_prescale_get(const Evas_Object *obj)
643 ELM_CHECK_WIDTYPE(obj, widtype) 0;
644 Widget_Data *wd = elm_widget_data_get(obj);
647 return _els_smart_icon_scale_size_get(wd->img);