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, void (*func_cb) (void *data, Evas_Object *o, const char *emission, const char *source), 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, void (*func_cb) (void *data, Evas_Object *o, const char *emission, const char *source), 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 EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
192 wd = ELM_NEW(Widget_Data);
193 e = evas_object_evas_get(parent);
195 obj = elm_widget_add(e);
196 ELM_SET_WIDTYPE(widtype, "icon");
197 elm_widget_type_set(obj, "icon");
198 elm_widget_can_focus_set(obj, EINA_FALSE);
199 elm_widget_sub_object_add(parent, obj);
200 elm_widget_data_set(obj, wd);
201 elm_widget_del_hook_set(obj, _del_hook);
202 elm_widget_theme_hook_set(obj, _theme_hook);
203 elm_widget_signal_emit_hook_set(obj, _signal_emit_hook);
204 elm_widget_signal_callback_add_hook_set(obj, _signal_callback_add_hook);
205 elm_widget_signal_callback_del_hook_set(obj, _signal_callback_del_hook);
207 wd->lookup_order = ELM_ICON_LOOKUP_THEME_FDO;
208 wd->img = _els_smart_icon_add(e);
209 evas_object_event_callback_add(wd->img, EVAS_CALLBACK_MOUSE_UP,
211 evas_object_repeat_events_set(wd->img, EINA_TRUE);
212 elm_widget_resize_object_set(obj, wd->img);
214 wd->smooth = EINA_TRUE;
215 wd->scale_up = EINA_TRUE;
216 wd->scale_down = EINA_TRUE;
223 * Set the file that will be used as icon
225 * @param obj The icon object
226 * @param file The path to file that will be used as icon
227 * @param group The group that the icon belongs in edje file
229 * @return (1 = success, 0 = error)
234 elm_icon_file_set(Evas_Object *obj, const char *file, const char *group)
236 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
237 Widget_Data *wd = elm_widget_data_get(obj);
241 if (!wd) return EINA_FALSE;
242 EINA_SAFETY_ON_NULL_RETURN_VAL(file, EINA_FALSE);
243 if (wd->stdicon) eina_stringshare_del(wd->stdicon);
245 if (((p = strrchr(file, '.'))) && (!strcasecmp(p, ".edj")))
246 ret = _els_smart_icon_file_edje_set(wd->img, file, group);
248 ret = _els_smart_icon_file_key_set(wd->img, file, group);
254 * Get the file that will be used as icon
256 * @param obj The icon object
257 * @param file The path to file that will be used as icon
258 * @param group The group that the icon belongs in edje file
263 elm_icon_file_get(const Evas_Object *obj, const char **file, const char **group)
265 ELM_CHECK_WIDTYPE(obj, widtype);
266 Widget_Data *wd = elm_widget_data_get(obj);
268 _els_smart_icon_file_get(wd->img, file, group);
272 _icon_standard_set(Widget_Data *wd, Evas_Object *obj, const char *name)
274 if (_elm_theme_object_icon_set(obj, wd->img, name, "default"))
277 /* TODO: elm_unneed_efreet() */
278 wd->freedesktop.use = EINA_FALSE;
286 _icon_file_set(Widget_Data *wd, Evas_Object *obj, const char *path)
288 if (elm_icon_file_set(obj, path, NULL))
291 /* TODO: elm_unneed_efreet() */
292 wd->freedesktop.use = EINA_FALSE;
300 _icon_freedesktop_set(Widget_Data *wd, Evas_Object *obj, const char *name, int size)
306 if (icon_theme == NON_EXISTING) return EINA_FALSE;
309 Efreet_Icon_Theme *theme;
310 /* TODO: Listen for EFREET_EVENT_ICON_CACHE_UPDATE */
311 theme = efreet_icon_theme_find(getenv("E_ICON_THEME"));
315 static const char *themes[] = {
316 "gnome", "Human", "oxygen", "hicolor", NULL
318 for (itr = themes; *itr; itr++)
320 theme = efreet_icon_theme_find(*itr);
327 icon_theme = NON_EXISTING;
331 icon_theme = eina_stringshare_add(theme->name.internal);
333 path = efreet_icon_path_find(icon_theme, name, size);
334 wd->freedesktop.use = !!path;
335 if (wd->freedesktop.use)
337 wd->freedesktop.requested_size = size;
338 elm_icon_file_set(obj, path, NULL);
346 _icon_size_min_get(Evas_Object *icon)
349 _els_smart_icon_size_get(icon, &size, NULL);
350 return (size < 32) ? 32 : size;
354 * Set the theme, as standard, for a icon.
355 * If theme was not found and it is the absolute path of an image file, this
356 * image will be used.
358 * @param obj The icon object
359 * @param name The theme name
361 * @return (1 = success, 0 = error)
366 elm_icon_standard_set(Evas_Object *obj, const char *name)
368 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
369 Widget_Data *wd = elm_widget_data_get(obj);
373 if ((!wd) || (!name)) return EINA_FALSE;
375 /* try locating the icon using the specified lookup order */
376 switch (wd->lookup_order)
378 case ELM_ICON_LOOKUP_FDO:
379 ret = _icon_freedesktop_set(wd, obj, name, _icon_size_min_get(wd->img));
381 case ELM_ICON_LOOKUP_THEME:
382 ret = _icon_standard_set(wd, obj, name);
384 case ELM_ICON_LOOKUP_THEME_FDO:
385 ret = _icon_standard_set(wd, obj, name) ||
386 _icon_freedesktop_set(wd, obj, name, _icon_size_min_get(wd->img));
388 case ELM_ICON_LOOKUP_FDO_THEME:
390 ret = _icon_freedesktop_set(wd, obj, name, _icon_size_min_get(wd->img)) ||
391 _icon_standard_set(wd, obj, name);
397 eina_stringshare_replace(&wd->stdicon, name);
402 if (_path_is_absolute(name))
403 return _icon_file_set(wd, obj, name);
405 /* if that fails, see if icon name is in the format size/name. if so,
406 try locating a fallback without the size specification */
407 if (!(tmp = strchr(name, '/'))) return EINA_FALSE;
409 if (*tmp) return elm_icon_standard_set(obj, tmp);
416 * Get the theme, as standard, for a icon
418 * @param obj The icon object
419 * @return The theme name
424 elm_icon_standard_get(const Evas_Object *obj)
426 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
427 Widget_Data *wd = elm_widget_data_get(obj);
428 if (!wd) return NULL;
433 * Sets icon lookup order, used by elm_icon_standard_set().
435 * @param obj The icon object
436 * @param order The icon lookup order
441 elm_icon_order_lookup_set(Evas_Object *obj, Elm_Icon_Lookup_Order order)
443 ELM_CHECK_WIDTYPE(obj, widtype);
444 Widget_Data *wd = elm_widget_data_get(obj);
445 if (wd) wd->lookup_order = order;
449 * Gets the icon lookup order.
451 * @param obj The icon object
452 * @return The icon lookup order
456 EAPI Elm_Icon_Lookup_Order
457 elm_icon_order_lookup_get(const Evas_Object *obj)
459 ELM_CHECK_WIDTYPE(obj, widtype) ELM_ICON_LOOKUP_THEME_FDO;
460 Widget_Data *wd = elm_widget_data_get(obj);
461 if (!wd) return ELM_ICON_LOOKUP_THEME_FDO;
462 return wd->lookup_order;
466 * Set the smooth effect for a icon
468 * @param obj The icon object
469 * @param smooth A bool to set (or no) smooth effect
470 * (1 = smooth, 0 = not smooth)
475 elm_icon_smooth_set(Evas_Object *obj, Eina_Bool smooth)
477 ELM_CHECK_WIDTYPE(obj, widtype);
478 Widget_Data *wd = elm_widget_data_get(obj);
486 * Get the smooth effect for a icon
488 * @param obj The icon object
489 * @return If setted smooth effect
494 elm_icon_smooth_get(const Evas_Object *obj)
496 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
497 Widget_Data *wd = elm_widget_data_get(obj);
499 if (!wd) return EINA_FALSE;
504 * Set if the object is scalable
506 * @param obj The icon object
507 * @param no_scale A bool to set scale (or no)
508 * (1 = no_scale, 0 = scale)
513 elm_icon_no_scale_set(Evas_Object *obj, Eina_Bool no_scale)
515 ELM_CHECK_WIDTYPE(obj, widtype);
516 Widget_Data *wd = elm_widget_data_get(obj);
519 wd->no_scale = no_scale;
524 * Get if the object isn't scalable
526 * @param obj The icon object
527 * @return If isn't scalable
532 elm_icon_no_scale_get(const Evas_Object *obj)
534 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
535 Widget_Data *wd = elm_widget_data_get(obj);
536 if (!wd) return EINA_FALSE;
541 * Set if the object is (up/down) scalable
543 * @param obj The icon object
544 * @param scale_up A bool to set if the object is scalable up
545 * @param scale_down A bool to set if the object is scalable down
550 elm_icon_scale_set(Evas_Object *obj, Eina_Bool scale_up, Eina_Bool scale_down)
552 ELM_CHECK_WIDTYPE(obj, widtype);
553 Widget_Data *wd = elm_widget_data_get(obj);
556 wd->scale_up = scale_up;
557 wd->scale_down = scale_down;
562 * Get if the object is (up/down) scalable
564 * @param obj The icon object
565 * @param scale_up A bool to set if the object is scalable up
566 * @param scale_down A bool to set if the object is scalable down
571 elm_icon_scale_get(const Evas_Object *obj, Eina_Bool *scale_up, Eina_Bool *scale_down)
573 ELM_CHECK_WIDTYPE(obj, widtype);
574 Widget_Data *wd = elm_widget_data_get(obj);
576 if (scale_up) *scale_up = wd->scale_up;
577 if (scale_down) *scale_down = wd->scale_down;
581 * Set if the object is filled outside
583 * @param obj The icon object
584 * @param fill_outside A bool to set if the object is filled outside
585 * (1 = filled, 0 = no filled)
590 elm_icon_fill_outside_set(Evas_Object *obj, Eina_Bool fill_outside)
592 ELM_CHECK_WIDTYPE(obj, widtype);
593 Widget_Data *wd = elm_widget_data_get(obj);
596 wd->fill_outside = fill_outside;
601 * Get if the object is filled outside
603 * @param obj The icon object
604 * @return If the object is filled outside
609 elm_icon_fill_outside_get(const Evas_Object *obj)
611 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
612 Widget_Data *wd = elm_widget_data_get(obj);
614 if (!wd) return EINA_FALSE;
615 return wd->fill_outside;
619 * Set the prescale size for the icon
621 * @param obj The icon object
622 * @param size The prescale size
627 elm_icon_prescale_set(Evas_Object *obj, int size)
629 ELM_CHECK_WIDTYPE(obj, widtype);
630 Widget_Data *wd = elm_widget_data_get(obj);
633 _els_smart_icon_scale_size_set(wd->img, size);
637 * Get the prescale size for the icon
639 * @param obj The icon object
640 * @return The prescale size
645 elm_icon_prescale_get(const Evas_Object *obj)
647 ELM_CHECK_WIDTYPE(obj, widtype) 0;
648 Widget_Data *wd = elm_widget_data_get(obj);
651 return _els_smart_icon_scale_size_get(wd->img);