aspect_ratio_retained -> aspect_fixed
[framework/uifw/elementary.git] / src / lib / elm_image.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3 #include "els_icon.h"
4
5 typedef struct _Widget_Data Widget_Data;
6
7 struct _Widget_Data
8 {
9    Evas_Object *img;
10    Eina_Bool scale_up : 1;
11    Eina_Bool scale_down : 1;
12    Eina_Bool smooth : 1;
13    Eina_Bool fill_outside : 1;
14    Eina_Bool no_scale : 1;
15 };
16
17 static const char *widtype = NULL;
18 static void _del_hook(Evas_Object *obj);
19 static void _theme_hook(Evas_Object *obj);
20 static void _sizing_eval(Evas_Object *obj);
21 static void _mouse_up(void *data, Evas *e, Evas_Object *obj, void *event_info);
22
23 static const char SIG_CLICKED[] = "clicked";
24
25 static const Evas_Smart_Cb_Description _signals[] = {
26    {SIG_CLICKED, ""},
27    {NULL, NULL}
28 };
29
30
31 static void
32 _del_hook(Evas_Object *obj)
33 {
34    Widget_Data *wd = elm_widget_data_get(obj);
35
36    if (!wd) return;
37    free(wd);
38 }
39
40 static void
41 _del_pre_hook(Evas_Object *obj)
42 {
43    Widget_Data *wd = elm_widget_data_get(obj);
44
45    if (!wd) return;
46    evas_object_del(wd->img);
47 }
48
49 static void
50 _theme_hook(Evas_Object *obj)
51 {
52    Widget_Data *wd = elm_widget_data_get(obj);
53
54    if (!wd) return;
55    _sizing_eval(obj);
56 }
57
58 static void
59 _sizing_eval(Evas_Object *obj)
60 {
61    Widget_Data *wd = elm_widget_data_get(obj);
62    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
63    int w, h;
64
65    if (!wd) return;
66    _els_smart_icon_size_get(wd->img, &w, &h);
67    _els_smart_icon_scale_up_set(wd->img, wd->scale_up);
68    _els_smart_icon_scale_down_set(wd->img, wd->scale_down);
69    _els_smart_icon_smooth_scale_set(wd->img, wd->smooth);
70    _els_smart_icon_fill_inside_set(wd->img, !(wd->fill_outside));
71    if (wd->no_scale) _els_smart_icon_scale_set(wd->img, 1.0);
72    else
73      {
74         _els_smart_icon_scale_set(wd->img, elm_widget_scale_get(obj) * _elm_config->scale);
75         _els_smart_icon_size_get(wd->img, &w, &h);
76      }
77    if (!wd->scale_down)
78      {
79         minw = w;
80         minh = h;
81      }
82    if (!wd->scale_up)
83      {
84         maxw = w;
85         maxh = h;
86      }
87    evas_object_size_hint_min_set(obj, minw, minh);
88    evas_object_size_hint_max_set(obj, maxw, maxh);
89 }
90
91 static void
92 _mouse_up(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
93 {
94    evas_object_smart_callback_call(data, SIG_CLICKED, NULL);
95 }
96
97 EAPI Evas_Object *
98 elm_image_add(Evas_Object *parent)
99 {
100    Evas_Object *obj;
101    Evas *e;
102    Widget_Data *wd;
103
104    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
105
106    ELM_SET_WIDTYPE(widtype, "image");
107    elm_widget_type_set(obj, "image");
108    elm_widget_sub_object_add(parent, obj);
109    elm_widget_data_set(obj, wd);
110    elm_widget_del_hook_set(obj, _del_hook);
111    elm_widget_del_pre_hook_set(obj, _del_pre_hook);
112    elm_widget_theme_hook_set(obj, _theme_hook);
113    elm_widget_can_focus_set(obj, EINA_FALSE);
114
115    wd->img = _els_smart_icon_add(e);
116    evas_object_event_callback_add(wd->img, EVAS_CALLBACK_MOUSE_UP,
117                                   _mouse_up, obj);
118    evas_object_repeat_events_set(wd->img, EINA_TRUE);
119    elm_widget_resize_object_set(obj, wd->img);
120
121    evas_object_smart_callbacks_descriptions_set(obj, _signals);
122
123    wd->smooth = EINA_TRUE;
124    wd->scale_up = EINA_TRUE;
125    wd->scale_down = EINA_TRUE;
126
127    _els_smart_icon_scale_size_set(wd->img, 0);
128
129    _sizing_eval(obj);
130    return obj;
131 }
132
133 EAPI Eina_Bool
134 elm_image_file_set(Evas_Object *obj, const char *file, const char *group)
135 {
136    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
137    Widget_Data *wd = elm_widget_data_get(obj);
138    Eina_Bool ret;
139    const char *p;
140
141    if (!wd) return EINA_FALSE;
142    EINA_SAFETY_ON_NULL_RETURN_VAL(file, EINA_FALSE);
143    if (((p = strrchr(file, '.'))) && (!strcasecmp(p, ".edj")))
144      ret = _els_smart_icon_file_edje_set(wd->img, file, group);
145    else
146      ret = _els_smart_icon_file_key_set(wd->img, file, group);
147    _sizing_eval(obj);
148    return ret;
149 }
150
151 EAPI void
152 elm_image_file_get(const Evas_Object *obj, const char **file, const char **group)
153 {
154    ELM_CHECK_WIDTYPE(obj, widtype);
155    Widget_Data *wd = elm_widget_data_get(obj);
156    if (!wd) return;
157    _els_smart_icon_file_get(wd->img, file, group);
158 }
159
160 EAPI void
161 elm_image_smooth_set(Evas_Object *obj, Eina_Bool smooth)
162 {
163    ELM_CHECK_WIDTYPE(obj, widtype);
164    Widget_Data *wd = elm_widget_data_get(obj);
165
166    if (!wd) return;
167    wd->smooth = smooth;
168    _sizing_eval(obj);
169 }
170
171 EAPI Eina_Bool
172 elm_image_smooth_get(const Evas_Object *obj)
173 {
174    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
175    Widget_Data *wd = elm_widget_data_get(obj);
176
177    if (!wd) return EINA_FALSE;
178    return wd->smooth;
179 }
180
181 EAPI void
182 elm_image_object_size_get(const Evas_Object *obj, int *w, int *h)
183 {
184    if (w) *w = 0;
185    if (h) *h = 0;
186    ELM_CHECK_WIDTYPE(obj, widtype);
187    Widget_Data *wd = elm_widget_data_get(obj);
188
189    if (!wd) return;
190    _els_smart_icon_size_get(wd->img, w, h);
191 }
192
193 EAPI void
194 elm_image_no_scale_set(Evas_Object *obj, Eina_Bool no_scale)
195 {
196    ELM_CHECK_WIDTYPE(obj, widtype);
197    Widget_Data *wd = elm_widget_data_get(obj);
198
199    if (!wd) return;
200    wd->no_scale = no_scale;
201    _sizing_eval(obj);
202
203 }
204
205 EAPI Eina_Bool
206 elm_image_no_scale_get(const Evas_Object *obj)
207 {
208    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
209    Widget_Data *wd = elm_widget_data_get(obj);
210    if (!wd) return EINA_FALSE;
211    return wd->no_scale;
212 }
213
214 EAPI void
215 elm_image_scale_set(Evas_Object *obj, Eina_Bool scale_up, Eina_Bool scale_down)
216 {
217    ELM_CHECK_WIDTYPE(obj, widtype);
218    Widget_Data *wd = elm_widget_data_get(obj);
219
220    if (!wd) return;
221    wd->scale_up = scale_up;
222    wd->scale_down = scale_down;
223    _sizing_eval(obj);
224 }
225
226 EAPI void
227 elm_image_scale_get(const Evas_Object *obj, Eina_Bool *scale_up, Eina_Bool *scale_down)
228 {
229    ELM_CHECK_WIDTYPE(obj, widtype);
230    Widget_Data *wd = elm_widget_data_get(obj);
231    if (!wd) return;
232    if (scale_up) *scale_up = wd->scale_up;
233    if (scale_down) *scale_down = wd->scale_down;
234 }
235
236 EAPI void
237 elm_image_fill_outside_set(Evas_Object *obj, Eina_Bool fill_outside)
238 {
239    ELM_CHECK_WIDTYPE(obj, widtype);
240    Widget_Data *wd = elm_widget_data_get(obj);
241
242    if (!wd) return;
243    wd->fill_outside = fill_outside;
244    _sizing_eval(obj);
245 }
246
247 EAPI Eina_Bool
248 elm_image_fill_outside_get(const Evas_Object *obj)
249 {
250    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
251    Widget_Data *wd = elm_widget_data_get(obj);
252
253    if (!wd) return EINA_FALSE;
254    return wd->fill_outside;
255 }
256
257 EAPI void
258 elm_image_prescale_set(Evas_Object *obj, int size)
259 {
260    ELM_CHECK_WIDTYPE(obj, widtype);
261    Widget_Data *wd = elm_widget_data_get(obj);
262
263    if (!wd) return;
264    _els_smart_icon_scale_size_set(wd->img, size);
265 }
266
267 EAPI int
268 elm_image_prescale_get(const Evas_Object *obj)
269 {
270    ELM_CHECK_WIDTYPE(obj, widtype) 0;
271    Widget_Data *wd = elm_widget_data_get(obj);
272
273    if (!wd) return 0;
274    return _els_smart_icon_scale_size_get(wd->img);
275 }
276
277 EAPI void
278 elm_image_orient_set(Evas_Object *obj, Elm_Image_Orient orient)
279 {
280    ELM_CHECK_WIDTYPE(obj, widtype);
281    Widget_Data *wd = elm_widget_data_get(obj);
282
283    if (!wd) return;
284    _els_smart_icon_orient_set(wd->img, orient);
285 }
286
287 EAPI Elm_Image_Orient
288 elm_image_orient_get(const Evas_Object *obj)
289 {
290    ELM_CHECK_WIDTYPE(obj, widtype) ELM_IMAGE_ORIENT_NONE;
291    Widget_Data *wd = elm_widget_data_get(obj);
292    if (!wd) return ELM_IMAGE_ORIENT_NONE;
293    return _els_smart_icon_orient_get(wd->img);
294 }
295
296 EAPI void
297 elm_image_editable_set(Evas_Object *obj, Eina_Bool set)
298 {
299    ELM_CHECK_WIDTYPE(obj, widtype);
300    Widget_Data *wd = elm_widget_data_get(obj);
301
302    if (!wd) return;
303    _els_smart_icon_edit_set(wd->img, set, obj);
304 }
305
306 EAPI Eina_Bool
307 elm_image_editable_get(const Evas_Object *obj)
308 {
309    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
310    Widget_Data *wd = elm_widget_data_get(obj);
311    if (!wd) return EINA_FALSE;
312    return _els_smart_icon_edit_get(wd->img);
313 }
314
315 EAPI Evas_Object *
316 elm_image_object_get(const Evas_Object *obj)
317 {
318    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
319    Widget_Data *wd = elm_widget_data_get(obj);
320    if (!wd) return NULL;
321    return _els_smart_icon_object_get(wd->img);
322 }
323
324 EAPI void
325 elm_image_aspect_fixed_set(Evas_Object *obj, Eina_Bool fixed)
326 {
327    ELM_CHECK_WIDTYPE(obj, widtype);
328    Widget_Data *wd = elm_widget_data_get(obj);
329    if (!wd) return;
330    return _els_smart_icon_aspect_fixed_set(wd->img, fixed);
331 }
332
333 EAPI Eina_Bool
334 elm_image_aspect_fixed_get(const Evas_Object *obj)
335 {
336    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
337    Widget_Data *wd = elm_widget_data_get(obj);
338    if (!wd) return EINA_FALSE;
339    return _els_smart_icon_aspect_fixed_get(wd->img);
340 }
341
342 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-3f0^-2{2(0W1st0 :*/