be911a54b602545c3cf10027c05dd455d4d76085
[framework/uifw/elementary.git] / src / lib / elm_icon.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3 #include "els_icon.h"
4
5 #ifdef ELM_EFREET
6 #define NON_EXISTING (void *)-1
7 static const char *icon_theme = NULL;
8 #endif
9
10 typedef struct _Widget_Data Widget_Data;
11
12 struct _Widget_Data
13 {
14    Evas_Object *img;
15    const char *stdicon;
16    Elm_Icon_Lookup_Order lookup_order;
17
18 #ifdef HAVE_ELEMENTARY_ETHUMB
19    struct {
20       int id;
21
22       struct {
23          const char *path;
24          const char *key;
25       } file, thumb;
26
27       Ethumb_Exists *exists;
28
29       Ecore_Event_Handler *eeh;
30
31       Ethumb_Thumb_Format format;
32
33       Eina_Bool retry : 1;
34    } thumb;
35 #endif
36
37 #ifdef ELM_EFREET
38    struct {
39         int requested_size;
40         Eina_Bool use : 1;
41    } freedesktop;
42 #endif
43    Eina_Bool scale_up : 1;
44    Eina_Bool scale_down : 1;
45    Eina_Bool smooth : 1;
46    Eina_Bool fill_outside : 1;
47    Eina_Bool no_scale : 1;
48
49    /* for animation feature */
50    Ecore_Timer *timer;
51    int frame_count;
52    int cur_frame;
53    double duration;
54    Eina_Bool anim : 1;
55    Eina_Bool play : 1;
56
57 };
58
59 #ifdef HAVE_ELEMENTARY_ETHUMB
60 static Eina_List *_elm_icon_retry = NULL;
61 static int _icon_pending_request = 0;
62
63 static void _icon_thumb_exists(Ethumb_Client *client __UNUSED__, Ethumb_Exists *thread, Eina_Bool exists, void *data);
64 #endif
65
66 static const char *widtype = NULL;
67 static void _del_hook(Evas_Object *obj);
68 static void _theme_hook(Evas_Object *obj);
69 static void _sizing_eval(Evas_Object *obj);
70 static void _mouse_up(void *data, Evas *e, Evas_Object *obj, void *event_info);
71
72 static Eina_Bool _icon_standard_set(Widget_Data *wd, Evas_Object *obj, const char *name);
73 static Eina_Bool _icon_freedesktop_set(Widget_Data *wd, Evas_Object *obj, const char *name, int size);
74
75 static const char SIG_CLICKED[] = "clicked";
76
77 static const Evas_Smart_Cb_Description _signals[] = {
78    {SIG_CLICKED, ""},
79    {NULL, NULL}
80 };
81
82
83 //FIXME: move this code to ecore
84 #ifdef _WIN32
85 static Eina_Bool
86 _path_is_absolute(const char *path)
87 {
88    //TODO: Check if this works with all absolute paths in windows
89    return ((isalpha (*path)) && (*(path + 1) == ':') && ((*(path + 2) == '\\') || (*(path + 2) == '/')));
90 }
91 #else
92 static Eina_Bool
93 _path_is_absolute(const char *path)
94 {
95    return  (*path == '/');
96 }
97 #endif
98
99 static inline int
100 _icon_size_min_get(Evas_Object *icon)
101 {
102    int size;
103    _els_smart_icon_size_get(icon, &size, NULL);
104    return (size < 32) ? 32 : size;
105 }
106
107 #ifdef HAVE_ELEMENTARY_ETHUMB
108 static void
109 _icon_thumb_stop(Widget_Data *wd, void *ethumbd)
110 {
111    if (wd->thumb.id >= 0)
112      {
113         ethumb_client_generate_cancel(ethumbd, wd->thumb.id, NULL, NULL, NULL);
114         wd->thumb.id = -1;
115         _icon_pending_request--;
116      }
117
118    if (wd->thumb.exists)
119      {
120         ethumb_client_thumb_exists_cancel(wd->thumb.exists, _icon_thumb_exists, wd);
121         wd->thumb.exists = NULL;
122         _icon_pending_request--;
123      }
124
125    if (wd->thumb.retry)
126      {
127         _elm_icon_retry = eina_list_remove(_elm_icon_retry, wd);
128         wd->thumb.retry = EINA_FALSE;
129      }
130 }
131
132 static Eina_Bool
133 _icon_thumb_display(Widget_Data *wd)
134 {
135    Eina_Bool ret = EINA_FALSE;
136
137    if (wd->thumb.format == ETHUMB_THUMB_EET)
138      {
139         static const char *extensions[] = {
140           ".avi", ".mp4", ".ogv", ".mov", ".mpg", ".wmv", NULL
141         };
142         const char **ext, *ptr;
143         int prefix_size;
144         Eina_Bool video = EINA_FALSE;
145
146         prefix_size = eina_stringshare_strlen(wd->thumb.file.path) - 4;
147         if (prefix_size >= 0)
148           {
149              ptr = wd->thumb.file.path + prefix_size;
150              for (ext = extensions; *ext; ++ext)
151                if (!strcasecmp(ptr, *ext))
152                  {
153                     video = EINA_TRUE;
154                     break;
155                  }
156           }
157
158         if (video)
159           ret = _els_smart_icon_file_edje_set(wd->img, wd->thumb.thumb.path, wd->thumb.thumb.key);
160      }
161
162    if (!ret)
163      ret = _els_smart_icon_file_key_set(wd->img, wd->thumb.thumb.path, wd->thumb.thumb.key);
164
165    return ret;
166 }
167
168 static Eina_Bool
169 _icon_thumb_retry(Widget_Data *wd)
170 {
171    return _icon_thumb_display(wd);
172 }
173
174 static void
175 _icon_thumb_cleanup(Ethumb_Client *ethumbd)
176 {
177    Eina_List *l, *ll;
178    Widget_Data *wd;
179
180    EINA_LIST_FOREACH_SAFE(_elm_icon_retry, l, ll, wd)
181      if (_icon_thumb_retry(wd))
182        {
183           _elm_icon_retry = eina_list_remove_list(_elm_icon_retry, l);
184           wd->thumb.retry = EINA_FALSE;
185        }
186
187    if (_icon_pending_request == 0)
188      EINA_LIST_FREE(_elm_icon_retry, wd)
189        _icon_thumb_stop(wd, ethumbd);
190 }
191
192 static void
193 _icon_thumb_finish(Widget_Data *wd, Ethumb_Client *ethumbd)
194 {
195    const char *file = NULL, *group = NULL;
196    Eina_Bool ret;
197
198    _els_smart_icon_file_get(wd->img, &file, &group);
199    file = eina_stringshare_ref(file);
200    group = eina_stringshare_ref(group);
201
202    ret = _icon_thumb_display(wd);
203
204    if (!ret && file)
205      {
206         const char *p;
207
208         if (!wd->thumb.retry)
209           {
210              _elm_icon_retry = eina_list_append(_elm_icon_retry, wd);
211              wd->thumb.retry = EINA_TRUE;
212           }
213
214         /* Back to previous image */
215         if (((p = strrchr(file, '.'))) && (!strcasecmp(p, ".edj")))
216           _els_smart_icon_file_edje_set(wd->img, file, group);
217         else
218           _els_smart_icon_file_key_set(wd->img, file, group);
219      }
220
221    _icon_thumb_cleanup(ethumbd);
222
223    eina_stringshare_del(file);
224    eina_stringshare_del(group);
225 }
226
227 static void
228 _icon_thumb_cb(void *data,
229                Ethumb_Client *ethumbd,
230                int id,
231                const char *file __UNUSED__,
232                const char *key __UNUSED__,
233                const char *thumb_path,
234                const char *thumb_key,
235                Eina_Bool success)
236 {
237    Widget_Data *wd = data;
238
239    EINA_SAFETY_ON_FALSE_RETURN(wd->thumb.id == id);
240    wd->thumb.id = -1;
241
242    _icon_pending_request--;
243
244    if (success)
245      {
246         eina_stringshare_replace(&wd->thumb.thumb.path, thumb_path);
247         eina_stringshare_replace(&wd->thumb.thumb.key, thumb_key);
248         wd->thumb.format = ethumb_client_format_get(ethumbd);
249
250         _icon_thumb_finish(wd, ethumbd);
251      }
252    else
253      {
254         ERR("could not generate thumbnail for %s (key: %s)", file, key);
255         _icon_thumb_cleanup(ethumbd);
256      }
257 }
258
259 static void
260 _icon_thumb_exists(Ethumb_Client *client __UNUSED__, Ethumb_Exists *thread, Eina_Bool exists, void *data)
261 {
262    Widget_Data *wd = data;
263    Ethumb_Client *ethumbd;
264
265    if (ethumb_client_thumb_exists_check(thread))
266      return ;
267
268    wd->thumb.exists = NULL;
269
270    ethumbd = elm_thumb_ethumb_client_get();
271
272    if (exists)
273      {
274         const char *thumb_path, *thumb_key;
275
276         _icon_pending_request--;
277         ethumb_client_thumb_path_get(ethumbd, &thumb_path, &thumb_key);
278         eina_stringshare_replace(&wd->thumb.thumb.path, thumb_path);
279         eina_stringshare_replace(&wd->thumb.thumb.key, thumb_key);
280         wd->thumb.format = ethumb_client_format_get(ethumbd);
281
282         _icon_thumb_finish(wd, ethumbd);
283      }
284    else if ((wd->thumb.id = ethumb_client_generate(ethumbd, _icon_thumb_cb, wd, NULL)) == -1)
285      {
286         ERR("Generate was unable to start !");
287         /* Failed to generate thumbnail */
288         _icon_pending_request--;
289      }
290 }
291
292 static void
293 _icon_thumb_apply(Widget_Data *wd)
294 {
295    Ethumb_Client *ethumbd;
296
297    ethumbd = elm_thumb_ethumb_client_get();
298
299    _icon_thumb_stop(wd, ethumbd);
300
301    if (!wd->thumb.file.path) return ;
302
303    _icon_pending_request++;
304    if (!ethumb_client_file_set(ethumbd, wd->thumb.file.path, wd->thumb.file.key)) return ;
305    ethumb_client_size_set(ethumbd, _icon_size_min_get(wd->img), _icon_size_min_get(wd->img));
306    wd->thumb.exists = ethumb_client_thumb_exists(ethumbd, _icon_thumb_exists, wd);
307 }
308
309 static Eina_Bool
310 _icon_thumb_apply_cb(void *data, int type __UNUSED__, void *ev __UNUSED__)
311 {
312    Widget_Data *wd = data;
313
314    _icon_thumb_apply(wd);
315    return ECORE_CALLBACK_RENEW;
316 }
317 #endif
318
319 static void
320 _del_hook(Evas_Object *obj)
321 {
322    Widget_Data *wd = elm_widget_data_get(obj);
323
324 #ifdef HAVE_ELEMENTARY_ETHUMB
325    Ethumb_Client *ethumbd;
326 #endif
327
328    if (!wd) return;
329    if (wd->stdicon) eina_stringshare_del(wd->stdicon);
330
331 #ifdef HAVE_ELEMENTARY_ETHUMB
332    ethumbd = elm_thumb_ethumb_client_get();
333    _icon_thumb_stop(wd, ethumbd);
334
335    eina_stringshare_del(wd->thumb.file.path);
336    eina_stringshare_del(wd->thumb.file.key);
337    eina_stringshare_del(wd->thumb.thumb.path);
338    eina_stringshare_del(wd->thumb.thumb.key);
339
340    if (wd->thumb.eeh)
341      ecore_event_handler_del(wd->thumb.eeh);
342 #endif
343
344    if (wd->timer)
345      ecore_timer_del(wd->timer);
346    free(wd);
347 }
348
349 static void
350 _theme_hook(Evas_Object *obj)
351 {
352    Widget_Data *wd = elm_widget_data_get(obj);
353    if (!wd) return;
354    if (wd->stdicon)
355      _elm_theme_object_icon_set(obj, wd->img, wd->stdicon, elm_widget_style_get(obj));
356    _sizing_eval(obj);
357 }
358
359 static void
360 _signal_emit_hook(Evas_Object *obj, const char *emission, const char *source)
361 {
362    Widget_Data *wd = elm_widget_data_get(obj);
363    if (!wd) return;
364    Evas_Object *icon_edje;
365    icon_edje = _els_smart_icon_edje_get(wd->img);
366    if (!icon_edje) return;
367    edje_object_signal_emit(icon_edje, emission, source);
368 }
369
370 static void
371 _signal_callback_add_hook(Evas_Object *obj, const char *emission, const char *source, Edje_Signal_Cb func_cb, void *data)
372 {
373    Widget_Data *wd = elm_widget_data_get(obj);
374    if (!wd) return;
375    Evas_Object *icon_edje;
376    icon_edje = _els_smart_icon_edje_get(wd->img);
377    if (!icon_edje) return;
378    edje_object_signal_callback_add(icon_edje, emission, source, func_cb, data);
379 }
380
381 static void
382 _signal_callback_del_hook(Evas_Object *obj, const char *emission, const char *source, Edje_Signal_Cb func_cb, void *data)
383 {
384    Widget_Data *wd = elm_widget_data_get(obj);
385    if (!wd) return;
386    Evas_Object *icon_edje;
387    icon_edje = _els_smart_icon_edje_get(wd->img);
388    if (!icon_edje) return;
389    edje_object_signal_callback_del_full(icon_edje, emission, source, func_cb,
390                                         data);
391 }
392
393 static void
394 _sizing_eval(Evas_Object *obj)
395 {
396    Widget_Data *wd = elm_widget_data_get(obj);
397    if (!wd) return;
398    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
399    int w, h;
400
401    _els_smart_icon_size_get(wd->img, &w, &h);
402 #ifdef ELM_EFREET
403    if ((wd->freedesktop.use) && (!((w - wd->freedesktop.requested_size) % 16)))
404      {
405         /* This icon has been set to a freedesktop icon, and the requested
406            appears to have a different size than the requested size, so try to
407            request another, higher resolution, icon.
408 FIXME: Find a better heuristic to determine if there should be
409 an icon with a different resolution. */
410         _icon_freedesktop_set(wd, obj, wd->stdicon, w);
411      }
412 #endif
413    _els_smart_icon_scale_up_set(wd->img, wd->scale_up);
414    _els_smart_icon_scale_down_set(wd->img, wd->scale_down);
415    _els_smart_icon_smooth_scale_set(wd->img, wd->smooth);
416    _els_smart_icon_fill_inside_set(wd->img, !(wd->fill_outside));
417    if (wd->no_scale) _els_smart_icon_scale_set(wd->img, 1.0);
418    else
419      {
420         _els_smart_icon_scale_set(wd->img, elm_widget_scale_get(obj) *
421                                   _elm_config->scale);
422         _els_smart_icon_size_get(wd->img, &w, &h);
423      }
424    if (!wd->scale_down)
425      {
426         minw = w;
427         minh = h;
428      }
429    if (!wd->scale_up)
430      {
431         maxw = w;
432         maxh = h;
433      }
434    evas_object_size_hint_min_set(obj, minw, minh);
435    evas_object_size_hint_max_set(obj, maxw, maxh);
436 }
437
438 static void
439 _mouse_up(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
440 {
441    Evas_Event_Mouse_Up *ev = event_info;
442    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return;
443    evas_object_smart_callback_call(data, SIG_CLICKED, event_info);
444 }
445
446 static Eina_Bool
447 _elm_icon_animate_cb(void *data)
448 {
449    Widget_Data  *wd = data;
450    Evas_Object  *img_obj;
451
452    if (!wd) return ECORE_CALLBACK_CANCEL;
453    if (!wd->anim) return ECORE_CALLBACK_CANCEL;
454
455    img_obj = _els_smart_icon_object_get(wd->img);
456    wd->cur_frame++;
457    if (wd->cur_frame > wd->frame_count)
458      wd->cur_frame = wd->cur_frame % wd->frame_count;
459    evas_object_image_animated_frame_set(img_obj, wd->cur_frame);
460
461    wd->duration = evas_object_image_animated_frame_duration_get(img_obj, wd->cur_frame, 0);
462
463    if (wd->duration > 0)
464      ecore_timer_interval_set(wd->timer, wd->duration);
465    return ECORE_CALLBACK_RENEW;
466 }
467
468 EAPI Evas_Object *
469 elm_icon_add(Evas_Object *parent)
470 {
471    Evas_Object *obj;
472    Evas *e;
473    Widget_Data *wd;
474
475    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
476
477    ELM_SET_WIDTYPE(widtype, "icon");
478    elm_widget_type_set(obj, "icon");
479    elm_widget_can_focus_set(obj, EINA_FALSE);
480    elm_widget_sub_object_add(parent, obj);
481    elm_widget_data_set(obj, wd);
482    elm_widget_del_hook_set(obj, _del_hook);
483    elm_widget_theme_hook_set(obj, _theme_hook);
484    elm_widget_signal_emit_hook_set(obj, _signal_emit_hook);
485    elm_widget_signal_callback_add_hook_set(obj, _signal_callback_add_hook);
486    elm_widget_signal_callback_del_hook_set(obj, _signal_callback_del_hook);
487
488    wd->lookup_order = ELM_ICON_LOOKUP_THEME_FDO;
489    wd->img = _els_smart_icon_add(e);
490    evas_object_event_callback_add(wd->img, EVAS_CALLBACK_MOUSE_UP,
491                                   _mouse_up, obj);
492    evas_object_repeat_events_set(wd->img, EINA_TRUE);
493    elm_widget_resize_object_set(obj, wd->img);
494
495    evas_object_smart_callbacks_descriptions_set(obj, _signals);
496
497 #ifdef HAVE_ELEMENTARY_ETHUMB
498    wd->thumb.id = -1;
499 #endif
500
501    wd->smooth = EINA_TRUE;
502    wd->scale_up = EINA_TRUE;
503    wd->scale_down = EINA_TRUE;
504
505    _sizing_eval(obj);
506    return obj;
507 }
508
509 EAPI Eina_Bool
510 elm_icon_memfile_set(Evas_Object *obj, const void *img, size_t size, const char *format, const char *key)
511 {
512    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
513    Widget_Data *wd = elm_widget_data_get(obj);
514    Eina_Bool ret;
515
516    if (!wd) return EINA_FALSE;
517    EINA_SAFETY_ON_NULL_RETURN_VAL(img, EINA_FALSE);
518    EINA_SAFETY_ON_TRUE_RETURN_VAL(!size, EINA_FALSE);
519    eina_stringshare_del(wd->stdicon);
520    wd->stdicon = NULL;
521    ret = _els_smart_icon_memfile_set(wd->img, img, size, format, key);
522    _sizing_eval(obj);
523    return ret;
524 }
525
526 EAPI Eina_Bool
527 elm_icon_file_set(Evas_Object *obj, const char *file, const char *group)
528 {
529    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
530    Widget_Data *wd = elm_widget_data_get(obj);
531    Eina_Bool ret;
532    const char *p;
533
534    if (!wd) return EINA_FALSE;
535    EINA_SAFETY_ON_NULL_RETURN_VAL(file, EINA_FALSE);
536    if (wd->stdicon) eina_stringshare_del(wd->stdicon);
537    wd->stdicon = NULL;
538    if (((p = strrchr(file, '.'))) && (!strcasecmp(p, ".edj")))
539      ret = _els_smart_icon_file_edje_set(wd->img, file, group);
540    else
541      ret = _els_smart_icon_file_key_set(wd->img, file, group);
542    _sizing_eval(obj);
543    return ret;
544 }
545
546 EAPI void
547 elm_icon_file_get(const Evas_Object *obj, const char **file, const char **group)
548 {
549    ELM_CHECK_WIDTYPE(obj, widtype);
550    Widget_Data *wd = elm_widget_data_get(obj);
551    if (!wd) return;
552    _els_smart_icon_file_get(wd->img, file, group);
553 }
554
555 EAPI void
556 elm_icon_thumb_set(Evas_Object *obj, const char *file, const char *group)
557 {
558    ELM_CHECK_WIDTYPE(obj, widtype);
559    Widget_Data *wd = elm_widget_data_get(obj);
560    if (!wd) return;
561
562 #ifdef HAVE_ELEMENTARY_ETHUMB
563    eina_stringshare_replace(&wd->thumb.file.path, file);
564    eina_stringshare_replace(&wd->thumb.file.key, group);
565
566    if (elm_thumb_ethumb_client_connected())
567      {
568         _icon_thumb_apply(wd);
569         return ;
570      }
571
572    if (!wd->thumb.eeh)
573      {
574         wd->thumb.eeh = ecore_event_handler_add(ELM_ECORE_EVENT_ETHUMB_CONNECT, _icon_thumb_apply_cb, wd);
575      }
576 #else
577    (void) obj;
578    (void) file;
579    (void) group;
580 #endif
581 }
582
583 EAPI Eina_Bool
584 elm_icon_animated_available_get(const Evas_Object *obj)
585 {
586    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
587    Evas_Object *img_obj ;
588    Widget_Data *wd = elm_widget_data_get(obj);
589    if (!wd) return EINA_FALSE;
590
591    img_obj = _els_smart_icon_object_get(wd->img);
592
593    return evas_object_image_animated_get(img_obj);
594 }
595
596 EAPI void
597 elm_icon_animated_set(Evas_Object *obj, Eina_Bool anim)
598 {
599    ELM_CHECK_WIDTYPE(obj, widtype);
600    Evas_Object *img_obj ;
601    Widget_Data *wd = elm_widget_data_get(obj);
602    if (!wd) return;
603    if (wd->anim == anim) return;
604
605    img_obj = _els_smart_icon_object_get(wd->img);
606    if (!evas_object_image_animated_get(img_obj)) return;
607    if (anim)
608      {
609         wd->frame_count = evas_object_image_animated_frame_count_get(img_obj);
610         wd->cur_frame = 1;
611         wd->duration = evas_object_image_animated_frame_duration_get(img_obj, wd->cur_frame, 0);
612         evas_object_image_animated_frame_set(img_obj, wd->cur_frame);
613      }
614    else
615      {
616         wd->frame_count = -1;
617         wd->cur_frame = -1;
618         wd->duration = -1;
619      }
620    wd->anim = anim;
621    return;
622 }
623
624 EAPI Eina_Bool
625 elm_icon_animated_get(const Evas_Object *obj)
626 {
627    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
628    Widget_Data *wd = elm_widget_data_get(obj);
629    if (!wd) return EINA_FALSE;
630    return wd->anim;
631 }
632
633 EAPI void
634 elm_icon_animated_play_set(Evas_Object *obj, Eina_Bool play)
635 {
636    ELM_CHECK_WIDTYPE(obj, widtype);
637    Widget_Data *wd = elm_widget_data_get(obj);
638    if (!wd) return;
639    if (!wd->anim) return;
640    if (wd->play == play) return;
641
642    if (play)
643      {
644         wd->timer = ecore_timer_add(wd->duration, _elm_icon_animate_cb, wd);
645      }
646    else
647      {
648         if (wd->timer)
649           {
650              ecore_timer_del(wd->timer);
651              wd->timer = NULL;
652           }
653      }
654    wd->play = play;
655
656 }
657
658 EAPI Eina_Bool
659 elm_icon_animated_play_get(const Evas_Object *obj)
660 {
661    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
662    Widget_Data *wd = elm_widget_data_get(obj);
663    if (!wd) return EINA_FALSE;
664    return wd->play;
665 }
666
667 /* compatibility code to prevent ABI break */
668 EAPI Eina_Bool
669 elm_icon_anim_available_get(const Evas_Object *obj)
670 {
671   return elm_icon_animated_available_get(obj);
672 }
673
674 EAPI void
675 elm_icon_anim_set(Evas_Object *obj, Eina_Bool anim)
676 {
677   elm_icon_animated_set(obj, anim);
678 }
679
680 EAPI Eina_Bool
681 elm_icon_anim_get(const Evas_Object *obj)
682 {
683   return elm_icon_animated_get(obj);
684 }
685
686 EAPI void
687 elm_icon_anim_play_set(Evas_Object *obj, Eina_Bool play)
688 {
689   elm_icon_animated_play_set(obj, play);
690 }
691
692 EAPI Eina_Bool
693 elm_icon_anim_play_get(const Evas_Object *obj)
694 {
695   return elm_icon_animated_play_get(obj);
696 }
697
698 static Eina_Bool
699 _icon_standard_set(Widget_Data *wd, Evas_Object *obj, const char *name)
700 {
701    if (_elm_theme_object_icon_set(obj, wd->img, name, "default"))
702      {
703 #ifdef ELM_EFREET
704         /* TODO: elm_unneed_efreet() */
705         wd->freedesktop.use = EINA_FALSE;
706 #endif
707         return EINA_TRUE;
708      }
709    return EINA_FALSE;
710 }
711
712 static Eina_Bool
713 _icon_file_set(Widget_Data *wd, Evas_Object *obj, const char *path)
714 {
715    if (elm_icon_file_set(obj, path, NULL))
716      {
717 #ifdef ELM_EFREET
718         /* TODO: elm_unneed_efreet() */
719         wd->freedesktop.use = EINA_FALSE;
720 #endif
721         return EINA_TRUE;
722      }
723    return EINA_FALSE;
724 }
725
726 static Eina_Bool
727 _icon_freedesktop_set(Widget_Data *wd, Evas_Object *obj, const char *name, int size)
728 {
729 #ifdef ELM_EFREET
730    const char *path;
731
732    elm_need_efreet();
733    if (icon_theme == NON_EXISTING) return EINA_FALSE;
734    if (!icon_theme)
735      {
736         Efreet_Icon_Theme *theme;
737         /* TODO: Listen for EFREET_EVENT_ICON_CACHE_UPDATE */
738         theme = efreet_icon_theme_find(getenv("E_ICON_THEME"));
739         if (!theme)
740           {
741              const char **itr;
742              static const char *themes[] = {
743                   "gnome", "Human", "oxygen", "hicolor", NULL
744              };
745              for (itr = themes; *itr; itr++)
746                {
747                   theme = efreet_icon_theme_find(*itr);
748                   if (theme) break;
749                }
750           }
751
752         if (!theme)
753           {
754              icon_theme = NON_EXISTING;
755              return EINA_FALSE;
756           }
757         else
758           icon_theme = eina_stringshare_add(theme->name.internal);
759      }
760    path = efreet_icon_path_find(icon_theme, name, size);
761    wd->freedesktop.use = !!path;
762    if (wd->freedesktop.use)
763      {
764         wd->freedesktop.requested_size = size;
765         elm_icon_file_set(obj, path, NULL);
766         return EINA_TRUE;
767      }
768 #endif
769    return EINA_FALSE;
770 }
771
772 static Eina_Bool
773 _elm_icon_standard_set(Widget_Data *wd, Evas_Object *obj, const char *name, Eina_Bool *fdo)
774 {
775    char *tmp;
776    Eina_Bool ret;
777
778    /* try locating the icon using the specified lookup order */
779    switch (wd->lookup_order)
780      {
781       case ELM_ICON_LOOKUP_FDO:
782          ret = _icon_freedesktop_set(wd, obj, name, _icon_size_min_get(wd->img));
783          if (ret && fdo) *fdo = EINA_TRUE;
784          break;
785       case ELM_ICON_LOOKUP_THEME:
786          ret = _icon_standard_set(wd, obj, name);
787          break;
788       case ELM_ICON_LOOKUP_THEME_FDO:
789          ret = _icon_standard_set(wd, obj, name);
790          if (!ret)
791            {
792               ret = _icon_freedesktop_set(wd, obj, name, _icon_size_min_get(wd->img));
793               if (ret && fdo) *fdo = EINA_TRUE;
794            }
795          break;
796       case ELM_ICON_LOOKUP_FDO_THEME:
797       default:
798          ret = _icon_freedesktop_set(wd, obj, name, _icon_size_min_get(wd->img));
799          if (!ret)
800            ret = _icon_standard_set(wd, obj, name);
801          else if (fdo)
802            *fdo = EINA_TRUE;
803          break;
804      }
805
806    if (ret)
807      {
808         eina_stringshare_replace(&wd->stdicon, name);
809         _sizing_eval(obj);
810         return EINA_TRUE;
811      }
812
813    if (_path_is_absolute(name))
814      return _icon_file_set(wd, obj, name);
815
816    /* if that fails, see if icon name is in the format size/name. if so,
817       try locating a fallback without the size specification */
818    if (!(tmp = strchr(name, '/'))) return EINA_FALSE;
819    ++tmp;
820    if (*tmp) return elm_icon_standard_set(obj, tmp);
821    /* give up */
822    return EINA_FALSE;
823 }
824
825 static void
826 _elm_icon_standard_resize(void *data,
827                           Evas *e __UNUSED__,
828                           Evas_Object *obj,
829                           void *event_info __UNUSED__)
830 {
831    Widget_Data *wd = data;
832    const char *refup = eina_stringshare_ref(wd->stdicon);
833    Eina_Bool fdo = EINA_FALSE;
834
835    if (!_elm_icon_standard_set(wd, obj, wd->stdicon, &fdo) || (!fdo))
836      evas_object_event_callback_del_full(obj, EVAS_CALLBACK_RESIZE,
837                                          _elm_icon_standard_resize, wd);
838 #ifdef HAVE_ELEMENTARY_ETHUMB
839    if (wd->thumb.file.path)
840      elm_icon_thumb_set(obj, wd->thumb.file.path, wd->thumb.file.key);
841 #endif
842
843    eina_stringshare_del(refup);
844 }
845
846 EAPI Eina_Bool
847 elm_icon_standard_set(Evas_Object *obj, const char *name)
848 {
849    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
850    Widget_Data *wd = elm_widget_data_get(obj);
851    Eina_Bool fdo = EINA_FALSE;
852    Eina_Bool ret;
853
854    if ((!wd) || (!name)) return EINA_FALSE;
855
856    evas_object_event_callback_del_full(obj, EVAS_CALLBACK_RESIZE,
857                                        _elm_icon_standard_resize, wd);
858
859    ret = _elm_icon_standard_set(wd, obj, name, &fdo);
860
861    if (fdo)
862      evas_object_event_callback_add(obj, EVAS_CALLBACK_RESIZE,
863                                     _elm_icon_standard_resize, wd);
864
865    return ret;
866 }
867
868 EAPI const char *
869 elm_icon_standard_get(const Evas_Object *obj)
870 {
871    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
872    Widget_Data *wd = elm_widget_data_get(obj);
873    if (!wd) return NULL;
874    return wd->stdicon;
875 }
876
877 EAPI void
878 elm_icon_order_lookup_set(Evas_Object *obj, Elm_Icon_Lookup_Order order)
879 {
880    ELM_CHECK_WIDTYPE(obj, widtype);
881    Widget_Data *wd = elm_widget_data_get(obj);
882    if (wd) wd->lookup_order = order;
883 }
884
885 EAPI Elm_Icon_Lookup_Order
886 elm_icon_order_lookup_get(const Evas_Object *obj)
887 {
888    ELM_CHECK_WIDTYPE(obj, widtype) ELM_ICON_LOOKUP_THEME_FDO;
889    Widget_Data *wd = elm_widget_data_get(obj);
890    if (!wd) return ELM_ICON_LOOKUP_THEME_FDO;
891    return wd->lookup_order;
892 }
893
894 EAPI void
895 elm_icon_smooth_set(Evas_Object *obj, Eina_Bool smooth)
896 {
897    ELM_CHECK_WIDTYPE(obj, widtype);
898    Widget_Data *wd = elm_widget_data_get(obj);
899
900    if (!wd) return;
901    wd->smooth = smooth;
902    _sizing_eval(obj);
903 }
904
905 EAPI Eina_Bool
906 elm_icon_smooth_get(const Evas_Object *obj)
907 {
908    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
909    Widget_Data *wd = elm_widget_data_get(obj);
910
911    if (!wd) return EINA_FALSE;
912    return wd->smooth;
913 }
914
915 EAPI void
916 elm_icon_no_scale_set(Evas_Object *obj, Eina_Bool no_scale)
917 {
918    ELM_CHECK_WIDTYPE(obj, widtype);
919    Widget_Data *wd = elm_widget_data_get(obj);
920
921    if (!wd) return;
922    wd->no_scale = no_scale;
923    _sizing_eval(obj);
924 }
925
926 EAPI Eina_Bool
927 elm_icon_no_scale_get(const Evas_Object *obj)
928 {
929    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
930    Widget_Data *wd = elm_widget_data_get(obj);
931    if (!wd) return EINA_FALSE;
932    return wd->no_scale;
933 }
934
935 EAPI void
936 elm_icon_scale_set(Evas_Object *obj, Eina_Bool scale_up, Eina_Bool scale_down)
937 {
938    ELM_CHECK_WIDTYPE(obj, widtype);
939    Widget_Data *wd = elm_widget_data_get(obj);
940
941    if (!wd) return;
942    wd->scale_up = scale_up;
943    wd->scale_down = scale_down;
944    _sizing_eval(obj);
945 }
946
947 EAPI void
948 elm_icon_scale_get(const Evas_Object *obj, Eina_Bool *scale_up, Eina_Bool *scale_down)
949 {
950    ELM_CHECK_WIDTYPE(obj, widtype);
951    Widget_Data *wd = elm_widget_data_get(obj);
952    if (!wd) return;
953    if (scale_up) *scale_up = wd->scale_up;
954    if (scale_down) *scale_down = wd->scale_down;
955 }
956
957 EAPI void
958 elm_icon_fill_outside_set(Evas_Object *obj, Eina_Bool fill_outside)
959 {
960    ELM_CHECK_WIDTYPE(obj, widtype);
961    Widget_Data *wd = elm_widget_data_get(obj);
962
963    if (!wd) return;
964    wd->fill_outside = fill_outside;
965    _sizing_eval(obj);
966 }
967
968 EAPI Eina_Bool
969 elm_icon_fill_outside_get(const Evas_Object *obj)
970 {
971    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
972    Widget_Data *wd = elm_widget_data_get(obj);
973
974    if (!wd) return EINA_FALSE;
975    return wd->fill_outside;
976 }
977
978 EAPI void
979 elm_icon_prescale_set(Evas_Object *obj, int size)
980 {
981    ELM_CHECK_WIDTYPE(obj, widtype);
982    Widget_Data *wd = elm_widget_data_get(obj);
983
984    if (!wd) return;
985    _els_smart_icon_scale_size_set(wd->img, size);
986 }
987
988 EAPI int
989 elm_icon_prescale_get(const Evas_Object *obj)
990 {
991    ELM_CHECK_WIDTYPE(obj, widtype) 0;
992    Widget_Data *wd = elm_widget_data_get(obj);
993
994    if (!wd) return 0;
995    return _els_smart_icon_scale_size_get(wd->img);
996 }
997
998 EAPI Evas_Object *
999 elm_icon_object_get(Evas_Object *obj)
1000 {
1001    ELM_CHECK_WIDTYPE(obj, widtype) 0;
1002    Widget_Data *wd = elm_widget_data_get(obj);
1003
1004    if (!wd) return NULL;
1005    return wd->img;
1006 }
1007
1008 EAPI void
1009 elm_icon_preload_set(Evas_Object *obj, Eina_Bool disable)
1010 {
1011    ELM_CHECK_WIDTYPE(obj, widtype);
1012    Widget_Data *wd = elm_widget_data_get(obj);
1013
1014    if (!wd) return;
1015    _els_smart_icon_preload_set(wd->img, disable);
1016 }