svn update: 48945 (latest:48959)
[framework/uifw/elementary.git] / src / lib / elm_icon.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 /**
5  * @defgroup Icon Icon
6  *
7  * A standard icon that may be provided by the theme (delete, edit,
8  * arrows etc.) or a custom file (PNG, JPG, EDJE etc.) used for an
9  * icon. The Icon may scale or not and of course... support alpha
10  * channels.
11  * 
12  * Signals that you can add callbacks for are:
13  * 
14  * clicked - This is called when a user has clicked the icon
15  */
16
17 typedef struct _Widget_Data Widget_Data;
18
19 struct _Widget_Data
20 {
21    Evas_Object *img;
22    const char *stdicon;
23    Eina_Bool scale_up : 1;
24    Eina_Bool scale_down : 1;
25    Eina_Bool smooth : 1;
26    Eina_Bool fill_outside : 1;
27    Eina_Bool no_scale : 1;
28 };
29
30 static const char *widtype = NULL;
31 static void _del_hook(Evas_Object *obj);
32 static void _theme_hook(Evas_Object *obj);
33 static void _sizing_eval(Evas_Object *obj);
34 static void _mouse_up(void *data, Evas *e, Evas_Object *obj, void *event_info);
35
36 static void
37 _del_hook(Evas_Object *obj)
38 {
39    Widget_Data *wd = elm_widget_data_get(obj);
40
41    if (!wd) return;
42    if (wd->stdicon) eina_stringshare_del(wd->stdicon);
43    free(wd);
44 }
45
46 static void
47 _theme_hook(Evas_Object *obj)
48 {
49    Widget_Data *wd = elm_widget_data_get(obj);
50    if (!wd) return;
51    if (wd->stdicon)
52      _elm_theme_object_icon_set(obj, wd->img, wd->stdicon, "default");
53    _sizing_eval(obj);
54 }
55
56 static void
57 _sizing_eval(Evas_Object *obj)
58 {
59    Widget_Data *wd = elm_widget_data_get(obj);
60    if (!wd) return;
61    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
62    int w, h;
63
64    _els_smart_icon_size_get(wd->img, &w, &h);
65    _els_smart_icon_scale_up_set(wd->img, wd->scale_up);
66    _els_smart_icon_scale_down_set(wd->img, wd->scale_down);
67    _els_smart_icon_smooth_scale_set(wd->img, wd->smooth);
68    _els_smart_icon_fill_inside_set(wd->img, !(wd->fill_outside));
69    if (wd->no_scale) _els_smart_icon_scale_set(wd->img, 1.0);
70    else
71      {
72         _els_smart_icon_scale_set(wd->img, elm_widget_scale_get(obj) *
73                                   _elm_config->scale);
74         _els_smart_icon_size_get(wd->img, &w, &h);
75      }
76    if (!wd->scale_down)
77      {
78         minw = w;
79         minh = h;
80      }
81    if (!wd->scale_up)
82      {
83         maxw = w;
84         maxh = h;
85      }
86    evas_object_size_hint_min_set(obj, minw, minh);
87    evas_object_size_hint_max_set(obj, maxw, maxh);
88 }
89
90 static void
91 _mouse_up(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
92 {
93    Evas_Event_Mouse_Up *ev = event_info;
94    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return;
95    evas_object_smart_callback_call(data, "clicked", event_info);
96 }
97
98 /**
99  * Add a new icon to the parent
100  *
101  * @param parent The parent object
102  * @return The new object or NULL if it cannot be created
103  *
104  * @ingroup Icon
105  */
106 EAPI Evas_Object *
107 elm_icon_add(Evas_Object *parent)
108 {
109    Evas_Object *obj;
110    Evas *e;
111    Widget_Data *wd;
112
113    wd = ELM_NEW(Widget_Data);
114    e = evas_object_evas_get(parent);
115    obj = elm_widget_add(e);
116    ELM_SET_WIDTYPE(widtype, "icon");
117    elm_widget_type_set(obj, "icon");
118    elm_widget_sub_object_add(parent, obj);
119    elm_widget_data_set(obj, wd);
120    elm_widget_del_hook_set(obj, _del_hook);
121    elm_widget_theme_hook_set(obj, _theme_hook);
122    elm_widget_can_focus_set(obj, 0);
123
124    wd->img = _els_smart_icon_add(e);
125    evas_object_event_callback_add(wd->img, EVAS_CALLBACK_MOUSE_UP,
126                                   _mouse_up, obj);
127    evas_object_repeat_events_set(wd->img, 1);
128    elm_widget_resize_object_set(obj, wd->img);
129
130    wd->smooth = EINA_TRUE;
131    wd->scale_up = EINA_TRUE;
132    wd->scale_down = EINA_TRUE;
133
134    _sizing_eval(obj);
135    return obj;
136 }
137
138 /**
139  * Set the file that will be used as icon
140  *
141  * @param obj The icon object
142  * @param file The path to file that will be used as icon
143  * @param group The group that the icon belongs in edje file
144  *
145  * @return (1 = sucess, 0 = error)
146  *
147  * @ingroup Icon
148  */
149 EAPI Eina_Bool
150 elm_icon_file_set(Evas_Object *obj, const char *file, const char *group)
151 {
152    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
153    Widget_Data *wd = elm_widget_data_get(obj);
154    Eina_Bool ret;
155    const char *p;
156
157    if ((!wd) || (!file)) return EINA_FALSE;
158    if (wd->stdicon) eina_stringshare_del(wd->stdicon);
159    wd->stdicon = NULL;
160    if (((p = strrchr(file, '.'))) && (!strcasecmp(p, ".edj")))
161      ret = _els_smart_icon_file_edje_set(wd->img, file, group);
162    else
163      ret = _els_smart_icon_file_key_set(wd->img, file, group);
164    _sizing_eval(obj);
165    return ret;
166 }
167
168 /**
169  * Set the theme, as standard, for a icon
170  *
171  * @param obj The icon object
172  * @param name The theme name
173  *
174  * @return (1 = sucess, 0 = error)
175  *
176  * @ingroup Icon
177  */
178 EAPI Eina_Bool
179 elm_icon_standard_set(Evas_Object *obj, const char *name)
180 {
181    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
182    Widget_Data *wd = elm_widget_data_get(obj);
183    Eina_Bool ret;
184
185    if ((!wd) || (!name)) return EINA_FALSE;
186    eina_stringshare_replace(&wd->stdicon, name);
187    ret = _elm_theme_object_icon_set(obj, wd->img, name, "default");
188    _sizing_eval(obj);
189    return ret;
190 }
191
192 /**
193  * Set the smooth effect for a icon
194  *
195  * @param obj The icon object
196  * @param smooth A bool to set (or no) smooth effect
197  * (1 = smooth, 0 = not smooth)
198  *
199  * @ingroup Icon
200  */
201 EAPI void
202 elm_icon_smooth_set(Evas_Object *obj, Eina_Bool smooth)
203 {
204    ELM_CHECK_WIDTYPE(obj, widtype);
205    Widget_Data *wd = elm_widget_data_get(obj);
206
207    if (!wd) return;
208    wd->smooth = smooth;
209    _sizing_eval(obj);
210 }
211
212 /**
213  * Set if the object are scalable
214  *
215  * @param obj The icon object
216  * @param no_scale A bool to set scale (or no)
217  * (1 = no_scale, 0 = scale)
218  *
219  * @ingroup Icon
220  */
221 EAPI void
222 elm_icon_no_scale_set(Evas_Object *obj, Eina_Bool no_scale)
223 {
224    ELM_CHECK_WIDTYPE(obj, widtype);
225    Widget_Data *wd = elm_widget_data_get(obj);
226
227    if (!wd) return;
228    wd->no_scale = no_scale;
229    _sizing_eval(obj);
230 }
231
232 /**
233  * Set if the object is (up/down) scalable
234  *
235  * @param obj The icon object
236  * @param scale_up A bool to set if the object is scalable up
237  * @param scale_down A bool to set if the object is scalable down
238  *
239  * @ingroup Icon
240  */
241 EAPI void
242 elm_icon_scale_set(Evas_Object *obj, Eina_Bool scale_up, Eina_Bool scale_down)
243 {
244    ELM_CHECK_WIDTYPE(obj, widtype);
245    Widget_Data *wd = elm_widget_data_get(obj);
246
247    if (!wd) return;
248    wd->scale_up = scale_up;
249    wd->scale_down = scale_down;
250    _sizing_eval(obj);
251 }
252
253 /**
254  * Set if the object is filled outside
255  *
256  * @param obj The icon object
257  * @param fill_outside A bool to set if the object is filled outside
258  * (1 = filled, 0 = no filled)
259  *
260  * @ingroup Icon
261  */
262 EAPI void
263 elm_icon_fill_outside_set(Evas_Object *obj, Eina_Bool fill_outside)
264 {
265    ELM_CHECK_WIDTYPE(obj, widtype);
266    Widget_Data *wd = elm_widget_data_get(obj);
267
268    if (!wd) return;
269    wd->fill_outside = fill_outside;
270    _sizing_eval(obj);
271 }
272
273
274 /**
275  * Set the prescale size for the icon
276  *
277  * @param obj The icon object
278  * @param size The prescale size
279  *
280  * @ingroup Icon
281  */
282 EAPI void
283 elm_icon_prescale_set(Evas_Object *obj, int size)
284 {
285    ELM_CHECK_WIDTYPE(obj, widtype);
286    Widget_Data *wd = elm_widget_data_get(obj);
287
288    if (!wd) return;
289    _els_smart_icon_scale_size_set(wd->img, size);
290 }