Elementary segment_control: Introduced new widget by Govindaraju and Prince.
[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  * "drop" - Something has been dropped on 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 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    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
118
119    ELM_SET_WIDTYPE(widtype, "image");
120    elm_widget_type_set(obj, "image");
121    elm_widget_sub_object_add(parent, obj);
122    elm_widget_data_set(obj, wd);
123    elm_widget_del_hook_set(obj, _del_hook);
124    elm_widget_del_pre_hook_set(obj, _del_pre_hook);
125    elm_widget_theme_hook_set(obj, _theme_hook);
126    elm_widget_can_focus_set(obj, EINA_FALSE);
127
128    wd->img = _els_smart_icon_add(e);
129    evas_object_event_callback_add(wd->img, EVAS_CALLBACK_MOUSE_UP,
130                                   _mouse_up, obj);
131    evas_object_repeat_events_set(wd->img, EINA_TRUE);
132    elm_widget_resize_object_set(obj, wd->img);
133
134    wd->smooth = EINA_TRUE;
135    wd->scale_up = EINA_TRUE;
136    wd->scale_down = EINA_TRUE;
137
138    _els_smart_icon_scale_size_set(wd->img, 0);
139
140    _sizing_eval(obj);
141    return obj;
142 }
143
144 /**
145  * Set the file that will be used as image
146  *
147  * @param obj The image object
148  * @param file The path to file that will be used as image
149  * @param group The group that the image belongs in edje file
150  *
151  * @return (1 = success, 0 = error)
152  *
153  * @ingroup Image
154  */
155 EAPI Eina_Bool
156 elm_image_file_set(Evas_Object *obj, const char *file, const char *group)
157 {
158    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
159    Widget_Data *wd = elm_widget_data_get(obj);
160    Eina_Bool ret;
161    const char *p;
162
163    if (!wd) return EINA_FALSE;
164    EINA_SAFETY_ON_NULL_RETURN_VAL(file, 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  * Get the file that will be used as image
175  *
176  * @param obj The image object
177  * @param file The path to file
178  * @param group The group that the image belongs in edje file
179  *
180  * @ingroup Image
181  */
182 EAPI void
183 elm_image_file_get(const Evas_Object *obj, const char **file, const char **group)
184 {
185    ELM_CHECK_WIDTYPE(obj, widtype);
186    Widget_Data *wd = elm_widget_data_get(obj);
187    if (!wd) return;
188    _els_smart_icon_file_get(wd->img, file, group);
189 }
190
191 /**
192  * Set the smooth effect for a image
193  *
194  * @param obj The image object
195  * @param smooth A bool to set (or no) smooth effect
196  * (1 = smooth, 0 = not smooth)
197  *
198  * @ingroup Image
199  */
200 EAPI void
201 elm_image_smooth_set(Evas_Object *obj, Eina_Bool smooth)
202 {
203    ELM_CHECK_WIDTYPE(obj, widtype);
204    Widget_Data *wd = elm_widget_data_get(obj);
205
206    if (!wd) return;
207    wd->smooth = smooth;
208    _sizing_eval(obj);
209 }
210
211 /**
212  * Get the smooth effect for a image
213  *
214  * @param obj The image object
215  * @return If setted smooth effect
216  *
217  * @ingroup Image
218  */
219 EAPI Eina_Bool
220 elm_image_smooth_get(const Evas_Object *obj)
221 {
222    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
223    Widget_Data *wd = elm_widget_data_get(obj);
224
225    if (!wd) return EINA_FALSE;
226    return wd->smooth;
227 }
228
229 /**
230  * Gets the current size of the image.
231  *
232  * Either width or height (or both) may be NULL.
233  *
234  * On error, neither w or h will be written too.
235  *
236  * @param obj The image object.
237  * @param w Pointer to store width, or NULL.
238  * @param h Pointer to store height, or NULL.
239  */
240 EAPI void
241 elm_image_object_size_get(const Evas_Object *obj, int *w, int *h)
242 {
243    ELM_CHECK_WIDTYPE(obj, widtype);
244    Widget_Data *wd = elm_widget_data_get(obj);
245
246    if (!wd) return;
247    _els_smart_icon_size_get(wd->img, w, h);
248 }
249
250 /**
251  * Set if the object are scalable
252  *
253  * @param obj The image object.
254  * @param no_scale A bool to set scale (or no).
255  * (1 = no_scale, 0 = scale)
256  *
257  * @ingroup Image
258  */
259 EAPI void
260 elm_image_no_scale_set(Evas_Object *obj, Eina_Bool no_scale)
261 {
262    ELM_CHECK_WIDTYPE(obj, widtype);
263    Widget_Data *wd = elm_widget_data_get(obj);
264
265    if (!wd) return;
266    wd->no_scale = no_scale;
267    _sizing_eval(obj);
268
269 }
270
271 /**
272  * Get if the object isn't scalable
273  *
274  * @param obj The image object
275  * @return If isn't scalable
276  *
277  * @ingroup Image
278  */
279 EAPI Eina_Bool
280 elm_image_no_scale_get(const Evas_Object *obj)
281 {
282    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
283    Widget_Data *wd = elm_widget_data_get(obj);
284    if (!wd) return EINA_FALSE;
285    return wd->no_scale;
286 }
287
288 /**
289  * Set if the object is (up/down) scalable
290  *
291  * @param obj The image object
292  * @param scale_up A bool to set if the object is scalable up
293  * @param scale_down A bool to set if the object is scalable down
294  *
295  * @ingroup Image
296  */
297 EAPI void
298 elm_image_scale_set(Evas_Object *obj, Eina_Bool scale_up, Eina_Bool scale_down)
299 {
300    ELM_CHECK_WIDTYPE(obj, widtype);
301    Widget_Data *wd = elm_widget_data_get(obj);
302
303    if (!wd) return;
304    wd->scale_up = scale_up;
305    wd->scale_down = scale_down;
306    _sizing_eval(obj);
307 }
308
309 /**
310  * Get if the object is (up/down) scalable
311  *
312  * @param obj The image object
313  * @param scale_up A bool to set if the object is scalable up
314  * @param scale_down A bool to set if the object is scalable down
315  *
316  * @ingroup Image
317  */
318 EAPI void
319 elm_image_scale_get(const Evas_Object *obj, Eina_Bool *scale_up, Eina_Bool *scale_down)
320 {
321    ELM_CHECK_WIDTYPE(obj, widtype);
322    Widget_Data *wd = elm_widget_data_get(obj);
323    if (!wd) return;
324    if (scale_up) *scale_up = wd->scale_up;
325    if (scale_down) *scale_down = wd->scale_down;
326 }
327
328 /**
329  * Set if the object is filled outside
330  *
331  * @param obj The image object
332  * @param fill_outside A bool to set if the object is filled outside
333  * (1 = filled, 0 = no filled)
334  *
335  * @ingroup Image
336  */
337 EAPI void
338 elm_image_fill_outside_set(Evas_Object *obj, Eina_Bool fill_outside)
339 {
340    ELM_CHECK_WIDTYPE(obj, widtype);
341    Widget_Data *wd = elm_widget_data_get(obj);
342
343    if (!wd) return;
344    wd->fill_outside = fill_outside;
345    _sizing_eval(obj);
346 }
347
348 /**
349  * Get if the object is filled outside
350  *
351  * @param obj The image object
352  * @return If the object is filled outside
353  *
354  * @ingroup Image
355  */
356 EAPI Eina_Bool
357 elm_image_fill_outside_get(const Evas_Object *obj)
358 {
359    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
360    Widget_Data *wd = elm_widget_data_get(obj);
361
362    if (!wd) return EINA_FALSE;
363    return wd->fill_outside;
364 }
365
366 /**
367  * Set the prescale size for the image
368  *
369  * @param obj The image object
370  * @param size The prescale size
371  *
372  * @ingroup Image
373  */
374 EAPI void
375 elm_image_prescale_set(Evas_Object *obj, int size)
376 {
377    ELM_CHECK_WIDTYPE(obj, widtype);
378    Widget_Data *wd = elm_widget_data_get(obj);
379
380    if (!wd) return;
381    _els_smart_icon_scale_size_set(wd->img, size);
382 }
383
384 /**
385  * Get the prescale size for the image
386  *
387  * @param obj The image object
388  * @return The prescale size
389  *
390  * @ingroup Image
391  */
392 EAPI int
393 elm_image_prescale_get(const Evas_Object *obj)
394 {
395    ELM_CHECK_WIDTYPE(obj, widtype) 0;
396    Widget_Data *wd = elm_widget_data_get(obj);
397
398    if (!wd) return 0;
399    return _els_smart_icon_scale_size_get(wd->img);
400 }
401
402 /**
403  * Set the image orient
404  *
405  * @param obj The image object
406  * @param orient The image orient
407  * (ELM_IMAGE_ORIENT_NONE, ELM_IMAGE_ROTATE_90_CW,
408  *  ELM_IMAGE_ROTATE_180_CW, ELM_IMAGE_ROTATE_90_CCW,
409  *  ELM_IMAGE_FLIP_HORIZONTAL,ELM_IMAGE_FLIP_VERTICAL,
410  *  ELM_IMAGE_FLIP_TRANSPOSE, ELM_IMAGE_FLIP_TRANSVERSE)
411  *
412  * @ingroup Image
413  */
414 EAPI void
415 elm_image_orient_set(Evas_Object *obj, Elm_Image_Orient orient)
416 {
417    ELM_CHECK_WIDTYPE(obj, widtype);
418    Widget_Data *wd = elm_widget_data_get(obj);
419
420    if (!wd) return;
421    _els_smart_icon_orient_set(wd->img, orient);
422 }
423
424 /**
425  * Get the image orient
426  *
427  * @param obj The image object
428  * @return The image orient
429  * (ELM_IMAGE_ORIENT_NONE, ELM_IMAGE_ROTATE_90_CW,
430  *  ELM_IMAGE_ROTATE_180_CW, ELM_IMAGE_ROTATE_90_CCW,
431  *  ELM_IMAGE_FLIP_HORIZONTAL,ELM_IMAGE_FLIP_VERTICAL,
432  *  ELM_IMAGE_FLIP_TRANSPOSE, ELM_IMAGE_FLIP_TRANSVERSE)
433  *
434  * @ingroup Image
435  */
436 EAPI Elm_Image_Orient
437 elm_image_orient_get(const Evas_Object *obj)
438 {
439    ELM_CHECK_WIDTYPE(obj, widtype) ELM_IMAGE_ORIENT_NONE;
440    Widget_Data *wd = elm_widget_data_get(obj);
441    if (!wd) return ELM_IMAGE_ORIENT_NONE;
442    return _els_smart_icon_orient_get(wd->img);
443 }
444
445 /**
446  * Make the image 'editable'.
447  *
448  * This means the image is a valid drag target for drag and drop, and can be
449  * cut or pasted too.
450  *
451  * @param obj Image object.
452  * @param set Turn on or off editability.
453  */
454 EAPI void
455 elm_image_editable_set(Evas_Object *obj, Eina_Bool set)
456 {
457    ELM_CHECK_WIDTYPE(obj, widtype);
458    Widget_Data *wd = elm_widget_data_get(obj);
459
460    if (!wd) return;
461    _els_smart_icon_edit_set(wd->img, set, obj);
462 }
463
464 /**
465  * Make the image 'editable'.
466  *
467  * This means the image is a valid drag target for drag and drop, and can be
468  * cut or pasted too.
469  *
470  * @param obj Image object.
471  * @return Editability.
472  */
473 EAPI Eina_Bool
474 elm_image_editable_get(const Evas_Object *obj)
475 {
476    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
477    Widget_Data *wd = elm_widget_data_get(obj);
478    if (!wd) return EINA_FALSE;
479    return _els_smart_icon_edit_get(wd->img);
480 }
481
482
483 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-2f0^-2{2(0W1st0 :*/