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