fixed plugin image size problem
[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
72    evas_object_size_hint_min_get(obj, &minw, &minh);
73
74    if (wd->no_scale) _els_smart_icon_scale_set(wd->img, 1.0);
75    else
76      {
77         _els_smart_icon_scale_set(wd->img, elm_widget_scale_get(obj) * _elm_config->scale);
78         _els_smart_icon_size_get(wd->img, &w, &h);
79      }
80    if (!wd->scale_down)
81      {
82         minw = w;
83         minh = h;
84      }
85    if (!wd->scale_up)
86      {
87         maxw = w;
88         maxh = h;
89      }
90    evas_object_size_hint_min_set(obj, minw, minh);
91    evas_object_size_hint_max_set(obj, maxw, maxh);
92 }
93
94 static void
95 _mouse_up(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
96 {
97    evas_object_smart_callback_call(data, SIG_CLICKED, NULL);
98 }
99
100 EAPI Evas_Object *
101 elm_image_add(Evas_Object *parent)
102 {
103    Evas_Object *obj;
104    Evas *e;
105    Widget_Data *wd;
106
107    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
108
109    ELM_SET_WIDTYPE(widtype, "image");
110    elm_widget_type_set(obj, "image");
111    elm_widget_sub_object_add(parent, obj);
112    elm_widget_data_set(obj, wd);
113    elm_widget_del_hook_set(obj, _del_hook);
114    elm_widget_del_pre_hook_set(obj, _del_pre_hook);
115    elm_widget_theme_hook_set(obj, _theme_hook);
116    elm_widget_can_focus_set(obj, EINA_FALSE);
117
118    wd->img = _els_smart_icon_add(e);
119    evas_object_event_callback_add(wd->img, EVAS_CALLBACK_MOUSE_UP,
120                                   _mouse_up, obj);
121    evas_object_repeat_events_set(wd->img, EINA_TRUE);
122    elm_widget_resize_object_set(obj, wd->img);
123
124    evas_object_smart_callbacks_descriptions_set(obj, _signals);
125
126    wd->smooth = EINA_TRUE;
127    wd->scale_up = EINA_TRUE;
128    wd->scale_down = EINA_TRUE;
129
130    _els_smart_icon_scale_size_set(wd->img, 0);
131
132    _sizing_eval(obj);
133    return obj;
134 }
135
136 EAPI Eina_Bool
137 elm_image_file_set(Evas_Object *obj, const char *file, const char *group)
138 {
139    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
140    Widget_Data *wd = elm_widget_data_get(obj);
141    Eina_Bool ret;
142    const char *p;
143
144    if (!wd) return EINA_FALSE;
145    EINA_SAFETY_ON_NULL_RETURN_VAL(file, EINA_FALSE);
146    if (((p = strrchr(file, '.'))) && (!strcasecmp(p, ".edj")))
147      ret = _els_smart_icon_file_edje_set(wd->img, file, group);
148    else
149      ret = _els_smart_icon_file_key_set(wd->img, file, group);
150    _sizing_eval(obj);
151    return ret;
152 }
153
154 EAPI void
155 elm_image_file_get(const Evas_Object *obj, const char **file, const char **group)
156 {
157    ELM_CHECK_WIDTYPE(obj, widtype);
158    Widget_Data *wd = elm_widget_data_get(obj);
159    if (!wd) return;
160    _els_smart_icon_file_get(wd->img, file, group);
161 }
162
163 EAPI void
164 elm_image_smooth_set(Evas_Object *obj, Eina_Bool smooth)
165 {
166    ELM_CHECK_WIDTYPE(obj, widtype);
167    Widget_Data *wd = elm_widget_data_get(obj);
168
169    if (!wd) return;
170    wd->smooth = smooth;
171    _sizing_eval(obj);
172 }
173
174 EAPI Eina_Bool
175 elm_image_smooth_get(const Evas_Object *obj)
176 {
177    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
178    Widget_Data *wd = elm_widget_data_get(obj);
179
180    if (!wd) return EINA_FALSE;
181    return wd->smooth;
182 }
183
184 EAPI void
185 elm_image_object_size_get(const Evas_Object *obj, int *w, int *h)
186 {
187    if (w) *w = 0;
188    if (h) *h = 0;
189    ELM_CHECK_WIDTYPE(obj, widtype);
190    Widget_Data *wd = elm_widget_data_get(obj);
191
192    if (!wd) return;
193    _els_smart_icon_size_get(wd->img, w, h);
194 }
195
196 EAPI void
197 elm_image_no_scale_set(Evas_Object *obj, Eina_Bool no_scale)
198 {
199    ELM_CHECK_WIDTYPE(obj, widtype);
200    Widget_Data *wd = elm_widget_data_get(obj);
201
202    if (!wd) return;
203    wd->no_scale = no_scale;
204    _sizing_eval(obj);
205
206 }
207
208 EAPI Eina_Bool
209 elm_image_no_scale_get(const Evas_Object *obj)
210 {
211    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
212    Widget_Data *wd = elm_widget_data_get(obj);
213    if (!wd) return EINA_FALSE;
214    return wd->no_scale;
215 }
216
217 EAPI void
218 elm_image_resizable_set(Evas_Object *obj, Eina_Bool size_up, Eina_Bool size_down)
219 {
220    ELM_CHECK_WIDTYPE(obj, widtype);
221    Widget_Data *wd = elm_widget_data_get(obj);
222
223    if (!wd) return;
224    wd->scale_up = size_up;
225    wd->scale_down = size_down;
226    _sizing_eval(obj);
227 }
228
229 EAPI void
230 elm_image_resizable_get(const Evas_Object *obj, Eina_Bool *size_up, Eina_Bool *size_down)
231 {
232    ELM_CHECK_WIDTYPE(obj, widtype);
233    Widget_Data *wd = elm_widget_data_get(obj);
234    if (!wd) return;
235    if (size_up) *size_up = wd->scale_up;
236    if (size_down) *size_down = wd->scale_down;
237 }
238
239 EAPI void
240 elm_image_fill_outside_set(Evas_Object *obj, Eina_Bool fill_outside)
241 {
242    ELM_CHECK_WIDTYPE(obj, widtype);
243    Widget_Data *wd = elm_widget_data_get(obj);
244
245    if (!wd) return;
246    wd->fill_outside = fill_outside;
247    _sizing_eval(obj);
248 }
249
250 EAPI Eina_Bool
251 elm_image_fill_outside_get(const Evas_Object *obj)
252 {
253    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
254    Widget_Data *wd = elm_widget_data_get(obj);
255
256    if (!wd) return EINA_FALSE;
257    return wd->fill_outside;
258 }
259
260 EAPI void
261 elm_image_preload_disabled_set(Evas_Object *obj, Eina_Bool disabled)
262 {
263    ELM_CHECK_WIDTYPE(obj, widtype);
264    Widget_Data *wd = elm_widget_data_get(obj);
265
266    if (!wd) return;
267    _els_smart_icon_preload_set(wd->img, !!disabled);
268 }
269
270 EAPI void
271 elm_image_prescale_set(Evas_Object *obj, int size)
272 {
273    ELM_CHECK_WIDTYPE(obj, widtype);
274    Widget_Data *wd = elm_widget_data_get(obj);
275
276    if (!wd) return;
277    _els_smart_icon_scale_size_set(wd->img, size);
278 }
279
280 EAPI int
281 elm_image_prescale_get(const Evas_Object *obj)
282 {
283    ELM_CHECK_WIDTYPE(obj, widtype) 0;
284    Widget_Data *wd = elm_widget_data_get(obj);
285
286    if (!wd) return 0;
287    return _els_smart_icon_scale_size_get(wd->img);
288 }
289
290 EAPI void
291 elm_image_orient_set(Evas_Object *obj, Elm_Image_Orient orient)
292 {
293    ELM_CHECK_WIDTYPE(obj, widtype);
294    Widget_Data *wd = elm_widget_data_get(obj);
295
296    if (!wd) return;
297    _els_smart_icon_orient_set(wd->img, orient);
298 }
299
300 EAPI Elm_Image_Orient
301 elm_image_orient_get(const Evas_Object *obj)
302 {
303    ELM_CHECK_WIDTYPE(obj, widtype) ELM_IMAGE_ORIENT_NONE;
304    Widget_Data *wd = elm_widget_data_get(obj);
305    if (!wd) return ELM_IMAGE_ORIENT_NONE;
306    return _els_smart_icon_orient_get(wd->img);
307 }
308
309 EAPI void
310 elm_image_editable_set(Evas_Object *obj, Eina_Bool set)
311 {
312    ELM_CHECK_WIDTYPE(obj, widtype);
313    Widget_Data *wd = elm_widget_data_get(obj);
314
315    if (!wd) return;
316    _els_smart_icon_edit_set(wd->img, set, obj);
317 }
318
319 EAPI Eina_Bool
320 elm_image_editable_get(const Evas_Object *obj)
321 {
322    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
323    Widget_Data *wd = elm_widget_data_get(obj);
324    if (!wd) return EINA_FALSE;
325    return _els_smart_icon_edit_get(wd->img);
326 }
327
328 EAPI Evas_Object *
329 elm_image_object_get(const Evas_Object *obj)
330 {
331    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
332    Widget_Data *wd = elm_widget_data_get(obj);
333    if (!wd) return NULL;
334    return _els_smart_icon_object_get(wd->img);
335 }
336
337 EAPI void
338 elm_image_aspect_fixed_set(Evas_Object *obj, Eina_Bool fixed)
339 {
340    ELM_CHECK_WIDTYPE(obj, widtype);
341    Widget_Data *wd = elm_widget_data_get(obj);
342    if (!wd) return;
343    _els_smart_icon_aspect_fixed_set(wd->img, fixed);
344 }
345
346 EAPI Eina_Bool
347 elm_image_aspect_fixed_get(const Evas_Object *obj)
348 {
349    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
350    Widget_Data *wd = elm_widget_data_get(obj);
351    if (!wd) return EINA_FALSE;
352    return _els_smart_icon_aspect_fixed_get(wd->img);
353 }
354
355 /* vim:set ts=8 sw=3 sts=3 expandtab cino=>5n-3f0^-2{2(0W1st0 :*/