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