From: Hyoyoung Chang <hyoyoung@gmail.com>
[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 EINA_DEPRECATED EAPI void
215 elm_image_scale_set(Evas_Object *obj, Eina_Bool scale_up, Eina_Bool scale_down)
216 {
217    elm_image_resizable_set(obj, scale_up, scale_down);
218 }
219
220 EINA_DEPRECATED EAPI void
221 elm_image_scale_get(const Evas_Object *obj, Eina_Bool *scale_up, Eina_Bool *scale_down)
222 {
223    elm_image_resizable_get(obj, scale_up, scale_down);
224 }
225
226 EAPI void
227 elm_image_resizable_set(Evas_Object *obj, Eina_Bool size_up, Eina_Bool size_down)
228 {
229    ELM_CHECK_WIDTYPE(obj, widtype);
230    Widget_Data *wd = elm_widget_data_get(obj);
231
232    if (!wd) return;
233    wd->scale_up = size_up;
234    wd->scale_down = size_down;
235    _sizing_eval(obj);
236 }
237
238 EAPI void
239 elm_image_resizable_get(const Evas_Object *obj, Eina_Bool *size_up, Eina_Bool *size_down)
240 {
241    ELM_CHECK_WIDTYPE(obj, widtype);
242    Widget_Data *wd = elm_widget_data_get(obj);
243    if (!wd) return;
244    if (size_up) *size_up = wd->scale_up;
245    if (size_down) *size_down = wd->scale_down;
246 }
247
248 EAPI void
249 elm_image_fill_outside_set(Evas_Object *obj, Eina_Bool fill_outside)
250 {
251    ELM_CHECK_WIDTYPE(obj, widtype);
252    Widget_Data *wd = elm_widget_data_get(obj);
253
254    if (!wd) return;
255    wd->fill_outside = fill_outside;
256    _sizing_eval(obj);
257 }
258
259 EAPI Eina_Bool
260 elm_image_fill_outside_get(const Evas_Object *obj)
261 {
262    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
263    Widget_Data *wd = elm_widget_data_get(obj);
264
265    if (!wd) return EINA_FALSE;
266    return wd->fill_outside;
267 }
268
269 EAPI void
270 elm_image_preload_disabled_set(Evas_Object *obj, Eina_Bool disabled)
271 {
272    ELM_CHECK_WIDTYPE(obj, widtype);
273    Widget_Data *wd = elm_widget_data_get(obj);
274
275    if (!wd) return;
276    _els_smart_icon_preload_set(wd->img, !!disabled);
277 }
278
279 EAPI void
280 elm_image_prescale_set(Evas_Object *obj, int size)
281 {
282    ELM_CHECK_WIDTYPE(obj, widtype);
283    Widget_Data *wd = elm_widget_data_get(obj);
284
285    if (!wd) return;
286    _els_smart_icon_scale_size_set(wd->img, size);
287 }
288
289 EAPI int
290 elm_image_prescale_get(const Evas_Object *obj)
291 {
292    ELM_CHECK_WIDTYPE(obj, widtype) 0;
293    Widget_Data *wd = elm_widget_data_get(obj);
294
295    if (!wd) return 0;
296    return _els_smart_icon_scale_size_get(wd->img);
297 }
298
299 EAPI void
300 elm_image_orient_set(Evas_Object *obj, Elm_Image_Orient orient)
301 {
302    ELM_CHECK_WIDTYPE(obj, widtype);
303    Widget_Data *wd = elm_widget_data_get(obj);
304
305    if (!wd) return;
306    _els_smart_icon_orient_set(wd->img, orient);
307 }
308
309 EAPI Elm_Image_Orient
310 elm_image_orient_get(const Evas_Object *obj)
311 {
312    ELM_CHECK_WIDTYPE(obj, widtype) ELM_IMAGE_ORIENT_NONE;
313    Widget_Data *wd = elm_widget_data_get(obj);
314    if (!wd) return ELM_IMAGE_ORIENT_NONE;
315    return _els_smart_icon_orient_get(wd->img);
316 }
317
318 EAPI void
319 elm_image_editable_set(Evas_Object *obj, Eina_Bool set)
320 {
321    ELM_CHECK_WIDTYPE(obj, widtype);
322    Widget_Data *wd = elm_widget_data_get(obj);
323
324    if (!wd) return;
325    _els_smart_icon_edit_set(wd->img, set, obj);
326 }
327
328 EAPI Eina_Bool
329 elm_image_editable_get(const Evas_Object *obj)
330 {
331    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
332    Widget_Data *wd = elm_widget_data_get(obj);
333    if (!wd) return EINA_FALSE;
334    return _els_smart_icon_edit_get(wd->img);
335 }
336
337 EAPI Evas_Object *
338 elm_image_object_get(const Evas_Object *obj)
339 {
340    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
341    Widget_Data *wd = elm_widget_data_get(obj);
342    if (!wd) return NULL;
343    return _els_smart_icon_object_get(wd->img);
344 }
345
346 EAPI void
347 elm_image_aspect_fixed_set(Evas_Object *obj, Eina_Bool fixed)
348 {
349    ELM_CHECK_WIDTYPE(obj, widtype);
350    Widget_Data *wd = elm_widget_data_get(obj);
351    if (!wd) return;
352    _els_smart_icon_aspect_fixed_set(wd->img, fixed);
353 }
354
355 EAPI Eina_Bool
356 elm_image_aspect_fixed_get(const Evas_Object *obj)
357 {
358    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
359    Widget_Data *wd = elm_widget_data_get(obj);
360    if (!wd) return EINA_FALSE;
361    return _els_smart_icon_aspect_fixed_get(wd->img);
362 }
363
364 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-3f0^-2{2(0W1st0 :*/