Merge branch 'master' into svn_merge
[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  */
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 const char SIG_CLICKED[] = "clicked";
37
38 static const Evas_Smart_Cb_Description _signals[] = {
39    {SIG_CLICKED, ""},
40    {NULL, NULL}
41 };
42
43
44 static void
45 _del_hook(Evas_Object *obj)
46 {
47    Widget_Data *wd = elm_widget_data_get(obj);
48
49    if (!wd) return;
50    free(wd);
51 }
52
53 static void
54 _del_pre_hook(Evas_Object *obj)
55 {
56    Widget_Data *wd = elm_widget_data_get(obj);
57
58    if (!wd) return;
59    evas_object_del(wd->img);
60 }
61
62 static void
63 _theme_hook(Evas_Object *obj)
64 {
65    Widget_Data *wd = elm_widget_data_get(obj);
66
67    if (!wd) return;
68    _sizing_eval(obj);
69 }
70
71 static void
72 _sizing_eval(Evas_Object *obj)
73 {
74    Widget_Data *wd = elm_widget_data_get(obj);
75    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
76    int w, h;
77
78    if (!wd) return;
79    _els_smart_icon_size_get(wd->img, &w, &h);
80    _els_smart_icon_scale_up_set(wd->img, wd->scale_up);
81    _els_smart_icon_scale_down_set(wd->img, wd->scale_down);
82    _els_smart_icon_smooth_scale_set(wd->img, wd->smooth);
83    _els_smart_icon_fill_inside_set(wd->img, !(wd->fill_outside));
84    if (wd->no_scale) _els_smart_icon_scale_set(wd->img, 1.0);
85    else
86      {
87         _els_smart_icon_scale_set(wd->img, elm_widget_scale_get(obj) * _elm_config->scale);
88         _els_smart_icon_size_get(wd->img, &w, &h);
89      }
90    if (!wd->scale_down)
91      {
92         minw = w;
93         minh = h;
94      }
95    if (!wd->scale_up)
96      {
97         maxw = w;
98         maxh = h;
99      }
100    evas_object_size_hint_min_set(obj, minw, minh);
101    evas_object_size_hint_max_set(obj, maxw, maxh);
102 }
103
104 static void
105 _mouse_up(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
106 {
107    evas_object_smart_callback_call(data, SIG_CLICKED, NULL);
108 }
109
110 /**
111  * Add a new image to the parent
112  *
113  * @param parent The parent object
114  * @return The new object or NULL if it cannot be created
115  *
116  * @ingroup Image
117  */
118 EAPI Evas_Object *
119 elm_image_add(Evas_Object *parent)
120 {
121    Evas_Object *obj;
122    Evas *e;
123    Widget_Data *wd;
124
125    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
126
127    ELM_SET_WIDTYPE(widtype, "image");
128    elm_widget_type_set(obj, "image");
129    elm_widget_sub_object_add(parent, obj);
130    elm_widget_data_set(obj, wd);
131    elm_widget_del_hook_set(obj, _del_hook);
132    elm_widget_del_pre_hook_set(obj, _del_pre_hook);
133    elm_widget_theme_hook_set(obj, _theme_hook);
134    elm_widget_can_focus_set(obj, EINA_FALSE);
135
136    wd->img = _els_smart_icon_add(e);
137    evas_object_event_callback_add(wd->img, EVAS_CALLBACK_MOUSE_UP,
138                                   _mouse_up, obj);
139    evas_object_repeat_events_set(wd->img, EINA_TRUE);
140    elm_widget_resize_object_set(obj, wd->img);
141
142    evas_object_smart_callbacks_descriptions_set(obj, _signals);
143
144    wd->smooth = EINA_TRUE;
145    wd->scale_up = EINA_TRUE;
146    wd->scale_down = EINA_TRUE;
147
148    _els_smart_icon_scale_size_set(wd->img, 0);
149
150    _sizing_eval(obj);
151    return obj;
152 }
153
154 /**
155  * Set the file that will be used as image
156  *
157  * @param obj The image object
158  * @param file The path to file that will be used as image
159  * @param group The group that the image belongs in edje file
160  *
161  * @return (1 = success, 0 = error)
162  *
163  * @ingroup Image
164  */
165 EAPI Eina_Bool
166 elm_image_file_set(Evas_Object *obj, const char *file, const char *group)
167 {
168    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
169    Widget_Data *wd = elm_widget_data_get(obj);
170    Eina_Bool ret;
171    const char *p;
172
173    if (!wd) return EINA_FALSE;
174    EINA_SAFETY_ON_NULL_RETURN_VAL(file, EINA_FALSE);
175    if (((p = strrchr(file, '.'))) && (!strcasecmp(p, ".edj")))
176      ret = _els_smart_icon_file_edje_set(wd->img, file, group);
177    else
178      ret = _els_smart_icon_file_key_set(wd->img, file, group);
179    _sizing_eval(obj);
180    return ret;
181 }
182
183 /**
184  * Get the file that will be used as image
185  *
186  * @param obj The image object
187  * @param file The path to file
188  * @param group The group that the image belongs in edje file
189  *
190  * @ingroup Image
191  */
192 EAPI void
193 elm_image_file_get(const Evas_Object *obj, const char **file, const char **group)
194 {
195    ELM_CHECK_WIDTYPE(obj, widtype);
196    Widget_Data *wd = elm_widget_data_get(obj);
197    if (!wd) return;
198    _els_smart_icon_file_get(wd->img, file, group);
199 }
200
201 /**
202  * Set the smooth effect for a image
203  *
204  * @param obj The image object
205  * @param smooth A bool to set (or no) smooth effect
206  * (1 = smooth, 0 = not smooth)
207  *
208  * @ingroup Image
209  */
210 EAPI void
211 elm_image_smooth_set(Evas_Object *obj, Eina_Bool smooth)
212 {
213    ELM_CHECK_WIDTYPE(obj, widtype);
214    Widget_Data *wd = elm_widget_data_get(obj);
215
216    if (!wd) return;
217    wd->smooth = smooth;
218    _sizing_eval(obj);
219 }
220
221 /**
222  * Get the smooth effect for a image
223  *
224  * @param obj The image object
225  * @return If setted smooth effect
226  *
227  * @ingroup Image
228  */
229 EAPI Eina_Bool
230 elm_image_smooth_get(const Evas_Object *obj)
231 {
232    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
233    Widget_Data *wd = elm_widget_data_get(obj);
234
235    if (!wd) return EINA_FALSE;
236    return wd->smooth;
237 }
238
239 /**
240  * Gets the current size of the image.
241  *
242  * Either width or height (or both) may be NULL.
243  *
244  * On error, neither w or h will be written too.
245  *
246  * @param obj The image object.
247  * @param w Pointer to store width, or NULL.
248  * @param h Pointer to store height, or NULL.
249  *
250  * @ingroup Image
251  */
252 EAPI void
253 elm_image_object_size_get(const Evas_Object *obj, int *w, int *h)
254 {
255    ELM_CHECK_WIDTYPE(obj, widtype);
256    Widget_Data *wd = elm_widget_data_get(obj);
257
258    if (!wd) return;
259    _els_smart_icon_size_get(wd->img, w, h);
260 }
261
262 /**
263  * Set if the object are scalable
264  *
265  * @param obj The image object.
266  * @param no_scale A bool to set scale (or no).
267  * (1 = no_scale, 0 = scale)
268  *
269  * @ingroup Image
270  */
271 EAPI void
272 elm_image_no_scale_set(Evas_Object *obj, Eina_Bool no_scale)
273 {
274    ELM_CHECK_WIDTYPE(obj, widtype);
275    Widget_Data *wd = elm_widget_data_get(obj);
276
277    if (!wd) return;
278    wd->no_scale = no_scale;
279    _sizing_eval(obj);
280
281 }
282
283 /**
284  * Get if the object isn't scalable
285  *
286  * @param obj The image object
287  * @return If isn't scalable
288  *
289  * @ingroup Image
290  */
291 EAPI Eina_Bool
292 elm_image_no_scale_get(const Evas_Object *obj)
293 {
294    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
295    Widget_Data *wd = elm_widget_data_get(obj);
296    if (!wd) return EINA_FALSE;
297    return wd->no_scale;
298 }
299
300 /**
301  * Set if the object is (up/down) scalable
302  *
303  * @param obj The image object
304  * @param scale_up A bool to set if the object is scalable up
305  * @param scale_down A bool to set if the object is scalable down
306  *
307  * @ingroup Image
308  */
309 EAPI void
310 elm_image_scale_set(Evas_Object *obj, Eina_Bool scale_up, Eina_Bool scale_down)
311 {
312    ELM_CHECK_WIDTYPE(obj, widtype);
313    Widget_Data *wd = elm_widget_data_get(obj);
314
315    if (!wd) return;
316    wd->scale_up = scale_up;
317    wd->scale_down = scale_down;
318    _sizing_eval(obj);
319 }
320
321 /**
322  * Get if the object is (up/down) scalable
323  *
324  * @param obj The image object
325  * @param scale_up A bool to set if the object is scalable up
326  * @param scale_down A bool to set if the object is scalable down
327  *
328  * @ingroup Image
329  */
330 EAPI void
331 elm_image_scale_get(const Evas_Object *obj, Eina_Bool *scale_up, Eina_Bool *scale_down)
332 {
333    ELM_CHECK_WIDTYPE(obj, widtype);
334    Widget_Data *wd = elm_widget_data_get(obj);
335    if (!wd) return;
336    if (scale_up) *scale_up = wd->scale_up;
337    if (scale_down) *scale_down = wd->scale_down;
338 }
339
340 /**
341  * Set if the object is filled outside
342  *
343  * @param obj The image object
344  * @param fill_outside A bool to set if the object is filled outside
345  * (1 = filled, 0 = no filled)
346  *
347  * @ingroup Image
348  */
349 EAPI void
350 elm_image_fill_outside_set(Evas_Object *obj, Eina_Bool fill_outside)
351 {
352    ELM_CHECK_WIDTYPE(obj, widtype);
353    Widget_Data *wd = elm_widget_data_get(obj);
354
355    if (!wd) return;
356    wd->fill_outside = fill_outside;
357    _sizing_eval(obj);
358 }
359
360 /**
361  * Get if the object is filled outside
362  *
363  * @param obj The image object
364  * @return If the object is filled outside
365  *
366  * @ingroup Image
367  */
368 EAPI Eina_Bool
369 elm_image_fill_outside_get(const Evas_Object *obj)
370 {
371    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
372    Widget_Data *wd = elm_widget_data_get(obj);
373
374    if (!wd) return EINA_FALSE;
375    return wd->fill_outside;
376 }
377
378 /**
379  * Set the prescale size for the image
380  *
381  * @param obj The image object
382  * @param size The prescale size
383  *
384  * @ingroup Image
385  */
386 EAPI void
387 elm_image_prescale_set(Evas_Object *obj, int size)
388 {
389    ELM_CHECK_WIDTYPE(obj, widtype);
390    Widget_Data *wd = elm_widget_data_get(obj);
391
392    if (!wd) return;
393    _els_smart_icon_scale_size_set(wd->img, size);
394 }
395
396 /**
397  * Get the prescale size for the image
398  *
399  * @param obj The image object
400  * @return The prescale size
401  *
402  * @ingroup Image
403  */
404 EAPI int
405 elm_image_prescale_get(const Evas_Object *obj)
406 {
407    ELM_CHECK_WIDTYPE(obj, widtype) 0;
408    Widget_Data *wd = elm_widget_data_get(obj);
409
410    if (!wd) return 0;
411    return _els_smart_icon_scale_size_get(wd->img);
412 }
413
414 /**
415  * Set the image orient
416  *
417  * @param obj The image object
418  * @param orient The image orient
419  * (ELM_IMAGE_ORIENT_NONE, ELM_IMAGE_ROTATE_90_CW,
420  *  ELM_IMAGE_ROTATE_180_CW, ELM_IMAGE_ROTATE_90_CCW,
421  *  ELM_IMAGE_FLIP_HORIZONTAL,ELM_IMAGE_FLIP_VERTICAL,
422  *  ELM_IMAGE_FLIP_TRANSPOSE, ELM_IMAGE_FLIP_TRANSVERSE)
423  *
424  * @ingroup Image
425  */
426 EAPI void
427 elm_image_orient_set(Evas_Object *obj, Elm_Image_Orient orient)
428 {
429    ELM_CHECK_WIDTYPE(obj, widtype);
430    Widget_Data *wd = elm_widget_data_get(obj);
431
432    if (!wd) return;
433    _els_smart_icon_orient_set(wd->img, orient);
434 }
435
436 /**
437  * Get the image orient
438  *
439  * @param obj The image object
440  * @return The image orient
441  * (ELM_IMAGE_ORIENT_NONE, ELM_IMAGE_ROTATE_90_CW,
442  *  ELM_IMAGE_ROTATE_180_CW, ELM_IMAGE_ROTATE_90_CCW,
443  *  ELM_IMAGE_FLIP_HORIZONTAL,ELM_IMAGE_FLIP_VERTICAL,
444  *  ELM_IMAGE_FLIP_TRANSPOSE, ELM_IMAGE_FLIP_TRANSVERSE)
445  *
446  * @ingroup Image
447  */
448 EAPI Elm_Image_Orient
449 elm_image_orient_get(const Evas_Object *obj)
450 {
451    ELM_CHECK_WIDTYPE(obj, widtype) ELM_IMAGE_ORIENT_NONE;
452    Widget_Data *wd = elm_widget_data_get(obj);
453    if (!wd) return ELM_IMAGE_ORIENT_NONE;
454    return _els_smart_icon_orient_get(wd->img);
455 }
456
457 /**
458  * Make the image 'editable'.
459  *
460  * This means the image is a valid drag target for drag and drop, and can be
461  * cut or pasted too.
462  *
463  * @param obj Image object.
464  * @param set Turn on or off editability.
465  *
466  * @ingroup Image
467  */
468 EAPI void
469 elm_image_editable_set(Evas_Object *obj, Eina_Bool set)
470 {
471    ELM_CHECK_WIDTYPE(obj, widtype);
472    Widget_Data *wd = elm_widget_data_get(obj);
473
474    if (!wd) return;
475    _els_smart_icon_edit_set(wd->img, set, obj);
476 }
477
478 /**
479  * Make the image 'editable'.
480  *
481  * This means the image is a valid drag target for drag and drop, and can be
482  * cut or pasted too.
483  *
484  * @param obj Image object.
485  * @return Editability.
486  */
487 EAPI Eina_Bool
488 elm_image_editable_get(const Evas_Object *obj)
489 {
490    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
491    Widget_Data *wd = elm_widget_data_get(obj);
492    if (!wd) return EINA_FALSE;
493    return _els_smart_icon_edit_get(wd->img);
494 }
495
496 /**
497  * Get the image object
498  *
499  * When you create a image with elm_image_add(). You can get this object (be
500  * careful to not manipulate it as it is under control of elementary), and use
501  * it to do things like get pixel data, save the image to a file, etc.
502  *
503  * @param obj The image object to get the inlined image from
504  * @return The inlined image object, or NULL if none exists
505  *
506  * @ingroup Image
507  */
508 EAPI Evas_Object *
509 elm_image_object_get(const Evas_Object *obj)
510 {
511    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
512    Widget_Data *wd = elm_widget_data_get(obj);
513    if (!wd) return NULL;
514    return _els_smart_icon_object_get(wd->img);
515 }
516
517 /**
518  * Enable/disable retaining up the aspect ratio of the image.
519  *
520  * @param obj The image object.
521  * @param retained Retaining or Non retaining.
522  *
523  * @ingroup Image
524  */
525 EAPI void
526 elm_image_aspect_ratio_retained_set(Evas_Object *obj, Eina_Bool retained)
527 {
528    ELM_CHECK_WIDTYPE(obj, widtype);
529    Widget_Data *wd = elm_widget_data_get(obj);
530    if (!wd) return;
531    return _els_smart_icon_aspect_ratio_retained_set(wd->img, retained);
532 }
533
534 /**
535  * Get if the object retains the aspect ratio.
536  *
537  * @param obj The image object.
538  * @return If the object retains the aspect ratio.
539  *
540  * @ingroup Image
541  */
542 EAPI Eina_Bool
543 elm_image_aspect_ratio_retained_get(const Evas_Object *obj)
544 {
545    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
546    Widget_Data *wd = elm_widget_data_get(obj);
547    if (!wd) return EINA_FALSE;
548    return _els_smart_icon_aspect_ratio_retained_get(wd->img);
549 }
550
551 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-3f0^-2{2(0W1st0 :*/