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