Merge "[Password]: New design based changes, a new style removed password mode contro...
[framework/uifw/elementary.git] / src / lib / elm_icon.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 #ifdef ELM_EFREET
5 #define NON_EXISTING (void *)-1
6 static const char *icon_theme = NULL;
7 #endif
8
9 /**
10  * @defgroup Icon Icon
11  * @ingroup Elementary
12  *
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
16  * channels.
17  *
18  * Signals that you can add callbacks for are:
19  *
20  * "clicked" - This is called when a user has clicked the icon
21  */
22
23 typedef struct _Widget_Data Widget_Data;
24
25 struct _Widget_Data
26 {
27    Evas_Object *img;
28    const char *stdicon;
29    Elm_Icon_Lookup_Order lookup_order;
30 #ifdef ELM_EFREET
31    struct {
32         int requested_size;
33         Eina_Bool use : 1;
34    } freedesktop;
35 #endif
36    Eina_Bool scale_up : 1;
37    Eina_Bool scale_down : 1;
38    Eina_Bool smooth : 1;
39    Eina_Bool fill_outside : 1;
40    Eina_Bool no_scale : 1;
41 };
42
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);
48
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);
51
52 //FIXME: move this code to ecore
53 #ifdef _WIN32
54 static Eina_Bool
55 _path_is_absolute(const char *path)
56 {
57    //TODO: Check if this works with all absolute paths in windows
58    return ((isalpha (*path)) && (*(path + 1) == ':') && ((*(path + 2) == '\\') || (*(path + 2) == '/')));
59 }
60 #else
61 static Eina_Bool
62 _path_is_absolute(const char *path)
63 {
64    return  (*path == '/');
65 }
66 #endif
67
68 static void
69 _del_hook(Evas_Object *obj)
70 {
71    Widget_Data *wd = elm_widget_data_get(obj);
72
73    if (!wd) return;
74    if (wd->stdicon) eina_stringshare_del(wd->stdicon);
75    free(wd);
76 }
77
78 static void
79 _theme_hook(Evas_Object *obj)
80 {
81    Widget_Data *wd = elm_widget_data_get(obj);
82    if (!wd) return;
83    if (wd->stdicon)
84      _elm_theme_object_icon_set(obj, wd->img, wd->stdicon, elm_widget_style_get(obj));
85    _sizing_eval(obj);
86 }
87
88 static void
89 _signal_emit_hook(Evas_Object *obj, const char *emission, const char *source)
90 {
91    Widget_Data *wd = elm_widget_data_get(obj);
92    if (!wd) return;
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);
97 }
98
99 static void
100 _signal_callback_add_hook(Evas_Object *obj, const char *emission, const char *source, Edje_Signal_Cb func_cb, void *data)
101 {
102    Widget_Data *wd = elm_widget_data_get(obj);
103    if (!wd) return;
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);
108 }
109
110 static void
111 _signal_callback_del_hook(Evas_Object *obj, const char *emission, const char *source, Edje_Signal_Cb func_cb, void *data)
112 {
113    Widget_Data *wd = elm_widget_data_get(obj);
114    if (!wd) return;
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,
119                                         data);
120 }
121
122 static void
123 _sizing_eval(Evas_Object *obj)
124 {
125    Widget_Data *wd = elm_widget_data_get(obj);
126    if (!wd) return;
127    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
128    int w, h;
129
130    _els_smart_icon_size_get(wd->img, &w, &h);
131 #ifdef ELM_EFREET
132    if ((wd->freedesktop.use) && (!((w - wd->freedesktop.requested_size) % 16)))
133      {
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);
140      }
141 #endif
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);
147    else
148      {
149         _els_smart_icon_scale_set(wd->img, elm_widget_scale_get(obj) *
150                                   _elm_config->scale);
151         _els_smart_icon_size_get(wd->img, &w, &h);
152      }
153    if (!wd->scale_down)
154      {
155         minw = w;
156         minh = h;
157      }
158    if (!wd->scale_up)
159      {
160         maxw = w;
161         maxh = h;
162      }
163    evas_object_size_hint_min_set(obj, minw, minh);
164    evas_object_size_hint_max_set(obj, maxw, maxh);
165 }
166
167 static void
168 _mouse_up(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
169 {
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);
173 }
174
175 /**
176  * Add a new icon to the parent
177  *
178  * @param parent The parent object
179  * @return The new object or NULL if it cannot be created
180  *
181  * @ingroup Icon
182  */
183 EAPI Evas_Object *
184 elm_icon_add(Evas_Object *parent)
185 {
186    Evas_Object *obj;
187    Evas *e;
188    Widget_Data *wd;
189
190    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
191
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);
202
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,
206                                   _mouse_up, obj);
207    evas_object_repeat_events_set(wd->img, EINA_TRUE);
208    elm_widget_resize_object_set(obj, wd->img);
209
210    wd->smooth = EINA_TRUE;
211    wd->scale_up = EINA_TRUE;
212    wd->scale_down = EINA_TRUE;
213
214    _sizing_eval(obj);
215    return obj;
216 }
217
218 /**
219  * Set the file that will be used as icon
220  *
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
224  *
225  * @return (1 = success, 0 = error)
226  *
227  * @ingroup Icon
228  */
229 EAPI Eina_Bool
230 elm_icon_file_set(Evas_Object *obj, const char *file, const char *group)
231 {
232    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
233    Widget_Data *wd = elm_widget_data_get(obj);
234    Eina_Bool ret;
235    const char *p;
236
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);
240    wd->stdicon = NULL;
241    if (((p = strrchr(file, '.'))) && (!strcasecmp(p, ".edj")))
242      ret = _els_smart_icon_file_edje_set(wd->img, file, group);
243    else
244      ret = _els_smart_icon_file_key_set(wd->img, file, group);
245    _sizing_eval(obj);
246    return ret;
247 }
248
249 /**
250  * Get the file that will be used as icon
251  *
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
255  *
256  * @ingroup Icon
257  */
258 EAPI void
259 elm_icon_file_get(const Evas_Object *obj, const char **file, const char **group)
260 {
261    ELM_CHECK_WIDTYPE(obj, widtype);
262    Widget_Data *wd = elm_widget_data_get(obj);
263    if (!wd) return;
264    _els_smart_icon_file_get(wd->img, file, group);
265 }
266
267 static Eina_Bool
268 _icon_standard_set(Widget_Data *wd, Evas_Object *obj, const char *name)
269 {
270    if (_elm_theme_object_icon_set(obj, wd->img, name, "default"))
271      {
272 #ifdef ELM_EFREET
273         /* TODO: elm_unneed_efreet() */
274         wd->freedesktop.use = EINA_FALSE;
275 #endif
276         return EINA_TRUE;
277      }
278    return EINA_FALSE;
279 }
280
281 static Eina_Bool
282 _icon_file_set(Widget_Data *wd, Evas_Object *obj, const char *path)
283 {
284    if (elm_icon_file_set(obj, path, NULL))
285      {
286 #ifdef ELM_EFREET
287         /* TODO: elm_unneed_efreet() */
288         wd->freedesktop.use = EINA_FALSE;
289 #endif
290         return EINA_TRUE;
291      }
292    return EINA_FALSE;
293 }
294
295 static Eina_Bool
296 _icon_freedesktop_set(Widget_Data *wd, Evas_Object *obj, const char *name, int size)
297 {
298 #ifdef ELM_EFREET
299    const char *path;
300
301    elm_need_efreet();
302    if (icon_theme == NON_EXISTING) return EINA_FALSE;
303    if (!icon_theme)
304      {
305         Efreet_Icon_Theme *theme;
306         /* TODO: Listen for EFREET_EVENT_ICON_CACHE_UPDATE */
307         theme = efreet_icon_theme_find(getenv("E_ICON_THEME"));
308         if (!theme)
309           {
310              const char **itr;
311              static const char *themes[] = {
312                   "gnome", "Human", "oxygen", "hicolor", NULL
313              };
314              for (itr = themes; *itr; itr++)
315                {
316                   theme = efreet_icon_theme_find(*itr);
317                   if (theme) break;
318                }
319           }
320
321         if (!theme)
322           {
323              icon_theme = NON_EXISTING;
324              return EINA_FALSE;
325           }
326         else
327           icon_theme = eina_stringshare_add(theme->name.internal);
328      }
329    path = efreet_icon_path_find(icon_theme, name, size);
330    wd->freedesktop.use = !!path;
331    if (wd->freedesktop.use)
332      {
333         wd->freedesktop.requested_size = size;
334         elm_icon_file_set(obj, path, NULL);
335         return EINA_TRUE;
336      }
337 #endif
338    return EINA_FALSE;
339 }
340
341 static inline int
342 _icon_size_min_get(Evas_Object *icon)
343 {
344    int size;
345    _els_smart_icon_size_get(icon, &size, NULL);
346    return (size < 32) ? 32 : size;
347 }
348
349 /**
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.
353  *
354  * @param obj The icon object
355  * @param name The theme name
356  *
357  * @return (1 = success, 0 = error)
358  *
359  * @ingroup Icon
360  */
361 EAPI Eina_Bool
362 elm_icon_standard_set(Evas_Object *obj, const char *name)
363 {
364    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
365    Widget_Data *wd = elm_widget_data_get(obj);
366    char *tmp;
367    Eina_Bool ret;
368
369    if ((!wd) || (!name)) return EINA_FALSE;
370
371    /* try locating the icon using the specified lookup order */
372    switch (wd->lookup_order)
373      {
374       case ELM_ICON_LOOKUP_FDO:
375          ret = _icon_freedesktop_set(wd, obj, name, _icon_size_min_get(wd->img));
376          break;
377       case ELM_ICON_LOOKUP_THEME:
378          ret = _icon_standard_set(wd, obj, name);
379          break;
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));
383          break;
384       case ELM_ICON_LOOKUP_FDO_THEME:
385       default:
386          ret = _icon_freedesktop_set(wd, obj, name, _icon_size_min_get(wd->img)) ||
387             _icon_standard_set(wd, obj, name);
388          break;
389      }
390
391    if (ret)
392      {
393         eina_stringshare_replace(&wd->stdicon, name);
394         _sizing_eval(obj);
395         return EINA_TRUE;
396      }
397
398    if (_path_is_absolute(name))
399      return _icon_file_set(wd, obj, name);
400
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;
404    ++tmp;
405    if (*tmp) return elm_icon_standard_set(obj, tmp);
406
407    /* give up */
408    return EINA_FALSE;
409 }
410
411 /**
412  * Get the theme, as standard, for a icon
413  *
414  * @param obj The icon object
415  * @return The theme name
416  *
417  * @ingroup Icon
418  */
419 EAPI const char *
420 elm_icon_standard_get(const Evas_Object *obj)
421 {
422    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
423    Widget_Data *wd = elm_widget_data_get(obj);
424    if (!wd) return NULL;
425    return wd->stdicon;
426 }
427
428 /**
429  * Sets icon lookup order, used by elm_icon_standard_set().
430  *
431  * @param obj The icon object
432  * @param order The icon lookup order
433  *
434  * @ingroup Icon
435  */
436 EAPI void
437 elm_icon_order_lookup_set(Evas_Object *obj, Elm_Icon_Lookup_Order order)
438 {
439    ELM_CHECK_WIDTYPE(obj, widtype);
440    Widget_Data *wd = elm_widget_data_get(obj);
441    if (wd) wd->lookup_order = order;
442 }
443
444 /**
445  * Gets the icon lookup order.
446  *
447  * @param obj The icon object
448  * @return The icon lookup order
449  *
450  * @ingroup Icon
451  */
452 EAPI Elm_Icon_Lookup_Order
453 elm_icon_order_lookup_get(const Evas_Object *obj)
454 {
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;
459 }
460
461 /**
462  * Set the smooth effect for a icon
463  *
464  * @param obj The icon object
465  * @param smooth A bool to set (or no) smooth effect
466  * (1 = smooth, 0 = not smooth)
467  *
468  * @ingroup Icon
469  */
470 EAPI void
471 elm_icon_smooth_set(Evas_Object *obj, Eina_Bool smooth)
472 {
473    ELM_CHECK_WIDTYPE(obj, widtype);
474    Widget_Data *wd = elm_widget_data_get(obj);
475
476    if (!wd) return;
477    wd->smooth = smooth;
478    _sizing_eval(obj);
479 }
480
481 /**
482  * Get the smooth effect for a icon
483  *
484  * @param obj The icon object
485  * @return If setted smooth effect
486  *
487  * @ingroup Icon
488  */
489 EAPI Eina_Bool
490 elm_icon_smooth_get(const Evas_Object *obj)
491 {
492    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
493    Widget_Data *wd = elm_widget_data_get(obj);
494
495    if (!wd) return EINA_FALSE;
496    return wd->smooth;
497 }
498
499 /**
500  * Set if the object is scalable
501  *
502  * @param obj The icon object
503  * @param no_scale A bool to set scale (or no)
504  * (1 = no_scale, 0 = scale)
505  *
506  * @ingroup Icon
507  */
508 EAPI void
509 elm_icon_no_scale_set(Evas_Object *obj, Eina_Bool no_scale)
510 {
511    ELM_CHECK_WIDTYPE(obj, widtype);
512    Widget_Data *wd = elm_widget_data_get(obj);
513
514    if (!wd) return;
515    wd->no_scale = no_scale;
516    _sizing_eval(obj);
517 }
518
519 /**
520  * Get if the object isn't scalable
521  *
522  * @param obj The icon object
523  * @return If isn't scalable
524  *
525  * @ingroup Icon
526  */
527 EAPI Eina_Bool
528 elm_icon_no_scale_get(const Evas_Object *obj)
529 {
530    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
531    Widget_Data *wd = elm_widget_data_get(obj);
532    if (!wd) return EINA_FALSE;
533    return wd->no_scale;
534 }
535
536 /**
537  * Set if the object is (up/down) scalable
538  *
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
542  *
543  * @ingroup Icon
544  */
545 EAPI void
546 elm_icon_scale_set(Evas_Object *obj, Eina_Bool scale_up, Eina_Bool scale_down)
547 {
548    ELM_CHECK_WIDTYPE(obj, widtype);
549    Widget_Data *wd = elm_widget_data_get(obj);
550
551    if (!wd) return;
552    wd->scale_up = scale_up;
553    wd->scale_down = scale_down;
554    _sizing_eval(obj);
555 }
556
557 /**
558  * Get if the object is (up/down) scalable
559  *
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
563  *
564  * @ingroup Icon
565  */
566 EAPI void
567 elm_icon_scale_get(const Evas_Object *obj, Eina_Bool *scale_up, Eina_Bool *scale_down)
568 {
569    ELM_CHECK_WIDTYPE(obj, widtype);
570    Widget_Data *wd = elm_widget_data_get(obj);
571    if (!wd) return;
572    if (scale_up) *scale_up = wd->scale_up;
573    if (scale_down) *scale_down = wd->scale_down;
574 }
575
576 /**
577  * Set if the object is filled outside
578  *
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)
582  *
583  * @ingroup Icon
584  */
585 EAPI void
586 elm_icon_fill_outside_set(Evas_Object *obj, Eina_Bool fill_outside)
587 {
588    ELM_CHECK_WIDTYPE(obj, widtype);
589    Widget_Data *wd = elm_widget_data_get(obj);
590
591    if (!wd) return;
592    wd->fill_outside = fill_outside;
593    _sizing_eval(obj);
594 }
595
596 /**
597  * Get if the object is filled outside
598  *
599  * @param obj The icon object
600  * @return If the object is filled outside
601  *
602  * @ingroup Icon
603  */
604 EAPI Eina_Bool
605 elm_icon_fill_outside_get(const Evas_Object *obj)
606 {
607    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
608    Widget_Data *wd = elm_widget_data_get(obj);
609
610    if (!wd) return EINA_FALSE;
611    return wd->fill_outside;
612 }
613
614 /**
615  * Set the prescale size for the icon
616  *
617  * @param obj The icon object
618  * @param size The prescale size
619  *
620  * @ingroup Icon
621  */
622 EAPI void
623 elm_icon_prescale_set(Evas_Object *obj, int size)
624 {
625    ELM_CHECK_WIDTYPE(obj, widtype);
626    Widget_Data *wd = elm_widget_data_get(obj);
627
628    if (!wd) return;
629    _els_smart_icon_scale_size_set(wd->img, size);
630 }
631
632 /**
633  * Get the prescale size for the icon
634  *
635  * @param obj The icon object
636  * @return The prescale size
637  *
638  * @ingroup Icon
639  */
640 EAPI int
641 elm_icon_prescale_get(const Evas_Object *obj)
642 {
643    ELM_CHECK_WIDTYPE(obj, widtype) 0;
644    Widget_Data *wd = elm_widget_data_get(obj);
645
646    if (!wd) return 0;
647    return _els_smart_icon_scale_size_get(wd->img);
648 }