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