elementary/map - map supports language,changed
[framework/uifw/elementary.git] / src / lib / elm_image.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3 #include "elm_widget_image.h"
4
5 #ifdef _WIN32
6 # define FMT_SIZE_T "%Iu"
7 #else
8 # define FMT_SIZE_T "%zu"
9 #endif
10
11 EAPI const char ELM_IMAGE_SMART_NAME[] = "elm_image";
12
13 static const char SIG_DND[] = "drop";
14 static const char SIG_CLICKED[] = "clicked";
15 static const Evas_Smart_Cb_Description _smart_callbacks[] = {
16    {SIG_DND, ""},
17    {SIG_CLICKED, ""},
18    {NULL, NULL}
19 };
20
21 EVAS_SMART_SUBCLASS_NEW
22   (ELM_IMAGE_SMART_NAME, _elm_image, Elm_Image_Smart_Class,
23   Elm_Widget_Smart_Class, elm_widget_smart_class_get, _smart_callbacks);
24
25 static void
26 _on_image_preloaded(void *data,
27                     Evas *e __UNUSED__,
28                     Evas_Object *obj,
29                     void *event __UNUSED__)
30 {
31    Elm_Image_Smart_Data *sd = data;
32
33    sd->preloading = EINA_FALSE;
34
35    if (sd->show) evas_object_show(obj);
36    if (sd->prev_img) evas_object_del(sd->prev_img);
37
38    sd->prev_img = NULL;
39 }
40
41 static void
42 _on_mouse_up(void *data,
43              Evas *e __UNUSED__,
44              Evas_Object *obj __UNUSED__,
45              void *event_info __UNUSED__)
46 {
47    evas_object_smart_callback_call(data, SIG_CLICKED, NULL);
48 }
49
50 static Eina_Bool
51 _elm_image_animate_cb(void *data)
52 {
53    Elm_Image_Smart_Data *sd = data;
54
55    if (!sd->anim) return ECORE_CALLBACK_CANCEL;
56
57    sd->cur_frame++;
58    if (sd->cur_frame > sd->frame_count)
59      sd->cur_frame = sd->cur_frame % sd->frame_count;
60
61    evas_object_image_animated_frame_set(sd->img, sd->cur_frame);
62
63    sd->frame_duration = evas_object_image_animated_frame_duration_get
64        (sd->img, sd->cur_frame, 0);
65
66    if (sd->frame_duration > 0)
67      ecore_timer_interval_set(sd->anim_timer, sd->frame_duration);
68
69    return ECORE_CALLBACK_RENEW;
70 }
71
72 static Evas_Object *
73 _img_new(Evas_Object *obj)
74 {
75    Evas_Object *img;
76
77    ELM_IMAGE_DATA_GET(obj, sd);
78
79    img = evas_object_image_add(evas_object_evas_get(obj));
80    evas_object_image_scale_hint_set(img, EVAS_IMAGE_SCALE_HINT_STATIC);
81    evas_object_repeat_events_set(img, EINA_TRUE);
82    evas_object_event_callback_add
83      (img, EVAS_CALLBACK_IMAGE_PRELOADED, _on_image_preloaded, sd);
84
85    evas_object_smart_member_add(img, obj);
86    elm_widget_sub_object_add(obj, img);
87
88    return img;
89 }
90
91 /* fixme: testar drag and drop depois! */
92
93 static void
94 _elm_image_internal_sizing_eval(Elm_Image_Smart_Data *sd)
95 {
96    Evas_Coord x, y, w, h;
97    const char *type;
98
99    if (!sd->img) return;
100
101    w = sd->img_w;
102    h = sd->img_h;
103
104    type = evas_object_type_get(sd->img);
105    if (!type) return;
106
107    if (!strcmp(type, "edje"))
108      {
109         x = sd->img_x;
110         y = sd->img_y;
111         evas_object_move(sd->img, x, y);
112         evas_object_resize(sd->img, w, h);
113      }
114    else
115      {
116         double alignh = 0.5, alignv = 0.5;
117         int iw = 0, ih = 0;
118
119         evas_object_image_size_get(sd->img, &iw, &ih);
120
121         iw = ((double)iw) * sd->scale;
122         ih = ((double)ih) * sd->scale;
123
124         if (iw < 1) iw = 1;
125         if (ih < 1) ih = 1;
126
127         if (sd->aspect_fixed)
128           {
129              h = ((double)ih * w) / (double)iw;
130              if (sd->fill_inside)
131                {
132                   if (h > sd->img_h)
133                     {
134                        h = sd->img_h;
135                        w = ((double)iw * h) / (double)ih;
136                     }
137                }
138              else
139                {
140                   if (h < sd->img_h)
141                     {
142                        h = sd->img_h;
143                        w = ((double)iw * h) / (double)ih;
144                     }
145                }
146           }
147         if (!sd->resize_up)
148           {
149              if (w > iw) w = iw;
150              if (h > ih) h = ih;
151           }
152         if (!sd->resize_down)
153           {
154              if (w < iw) w = iw;
155              if (h < ih) h = ih;
156           }
157
158         evas_object_size_hint_align_get
159           (ELM_WIDGET_DATA(sd)->obj, &alignh, &alignv);
160
161         if (alignh == EVAS_HINT_FILL) alignh = 0.5;
162         if (alignv == EVAS_HINT_FILL) alignv = 0.5;
163
164         x = sd->img_x + ((sd->img_w - w) * alignh);
165         y = sd->img_y + ((sd->img_h - h) * alignv);
166
167         evas_object_move(sd->img, x, y);
168         evas_object_image_fill_set(sd->img, 0, 0, w, h);
169         evas_object_resize(sd->img, w, h);
170      }
171    evas_object_move(sd->hit_rect, x, y);
172    evas_object_resize(sd->hit_rect, w, h);
173 }
174
175 static void
176 _elm_image_file_set_do(Evas_Object *obj)
177 {
178    Evas_Object *pclip = NULL;
179
180    ELM_IMAGE_DATA_GET(obj, sd);
181
182    if (sd->prev_img) evas_object_del(sd->prev_img);
183    if (sd->img)
184      {
185         pclip = evas_object_clip_get(sd->img);
186         sd->prev_img = sd->img;
187      }
188
189    sd->img = _img_new(obj);
190
191    evas_object_image_load_orientation_set(sd->img, EINA_TRUE);
192
193    evas_object_clip_set(sd->img, pclip);
194
195    sd->edje = EINA_FALSE;
196
197    if (!sd->load_size)
198      evas_object_image_load_size_set(sd->img, sd->load_size, sd->load_size);
199 }
200
201 /* WARNING: whenever you patch this function, remember to do the same
202  * on elm_icon.c:_elm_icon_smart_file_set()'s 2nd half.
203  */
204 static Eina_Bool
205 _elm_image_edje_file_set(Evas_Object *obj,
206                          const char *file,
207                          const char *group)
208 {
209    Evas_Object *pclip;
210
211    ELM_IMAGE_DATA_GET(obj, sd);
212
213    if (sd->prev_img) evas_object_del(sd->prev_img);
214    sd->prev_img = NULL;
215
216    if (!sd->edje)
217      {
218         pclip = evas_object_clip_get(sd->img);
219         if (sd->img) evas_object_del(sd->img);
220
221         /* Edje object instead */
222         sd->img = edje_object_add(evas_object_evas_get(obj));
223         evas_object_smart_member_add(sd->img, obj);
224         if (sd->show) evas_object_show(sd->img);
225         evas_object_clip_set(sd->img, pclip);
226      }
227
228    sd->edje = EINA_TRUE;
229    if (!edje_object_file_set(sd->img, file, group))
230      {
231         ERR("failed to set edje file '%s', group '%s': %s", file, group,
232             edje_load_error_str(edje_object_load_error_get(sd->img)));
233         return EINA_FALSE;
234      }
235
236    /* FIXME: do i want to update icon on file change ? */
237    _elm_image_internal_sizing_eval(sd);
238
239    return EINA_TRUE;
240 }
241
242 static void
243 _elm_image_smart_size_get(const Evas_Object *obj,
244                           int *w,
245                           int *h)
246 {
247    int tw, th;
248    int cw = 0, ch = 0;
249    const char *type;
250
251    ELM_IMAGE_DATA_GET(obj, sd);
252
253    type = evas_object_type_get(sd->img);
254    if (!type) return;
255
256    if (!strcmp(type, "edje"))
257      edje_object_size_min_get(sd->img, &tw, &th);
258    else
259      evas_object_image_size_get(sd->img, &tw, &th);
260
261    if ((sd->resize_up) || (sd->resize_down))
262      evas_object_geometry_get(sd->img, NULL, NULL, &cw, &ch);
263
264    tw = tw > cw ? tw : cw;
265    th = th > ch ? th : ch;
266    tw = ((double)tw) * sd->scale;
267    th = ((double)th) * sd->scale;
268    if (w) *w = tw;
269    if (h) *h = th;
270 }
271
272 static void
273 _elm_image_smart_smooth_scale_set(Evas_Object *obj,
274                                   Eina_Bool smooth)
275 {
276    ELM_IMAGE_DATA_GET(obj, sd);
277
278    if (sd->edje) return;
279
280    evas_object_image_smooth_scale_set(sd->img, smooth);
281 }
282
283 static Eina_Bool
284 _elm_image_smart_smooth_scale_get(const Evas_Object *obj)
285 {
286    ELM_IMAGE_DATA_GET(obj, sd);
287
288    if (sd->edje) return EINA_FALSE;
289
290    return evas_object_image_smooth_scale_get(sd->img);
291 }
292
293 static Evas_Object *
294 _elm_image_smart_object_get(const Evas_Object *obj)
295 {
296    ELM_IMAGE_DATA_GET(obj, sd);
297
298    return sd->img;
299 }
300
301 static void
302 _elm_image_smart_fill_inside_set(Evas_Object *obj,
303                                  Eina_Bool fill_inside)
304 {
305    ELM_IMAGE_DATA_GET(obj, sd);
306
307    fill_inside = !!fill_inside;
308
309    if (sd->fill_inside == fill_inside) return;
310
311    sd->fill_inside = fill_inside;
312
313    _elm_image_internal_sizing_eval(sd);
314 }
315
316 static Eina_Bool
317 _elm_image_smart_fill_inside_get(const Evas_Object *obj)
318 {
319    ELM_IMAGE_DATA_GET(obj, sd);
320
321    return sd->fill_inside;
322 }
323
324 static void
325 _elm_image_smart_resize_up_set(Evas_Object *obj,
326                               Eina_Bool resize_up)
327 {
328    ELM_IMAGE_DATA_GET(obj, sd);
329
330    resize_up = !!resize_up;
331
332    if (sd->resize_up == resize_up) return;
333
334    sd->resize_up = resize_up;
335
336    _elm_image_internal_sizing_eval(sd);
337 }
338
339 static Eina_Bool
340 _elm_image_smart_resize_up_get(const Evas_Object *obj)
341 {
342    ELM_IMAGE_DATA_GET(obj, sd);
343
344    return sd->resize_up;
345 }
346
347 static void
348 _elm_image_smart_resize_down_set(Evas_Object *obj,
349                                 Eina_Bool resize_down)
350 {
351    ELM_IMAGE_DATA_GET(obj, sd);
352
353    resize_down = !!resize_down;
354
355    if (sd->resize_down == resize_down) return;
356
357    sd->resize_down = resize_down;
358
359    _elm_image_internal_sizing_eval(sd);
360 }
361
362 static Eina_Bool
363 _elm_image_smart_resize_down_get(const Evas_Object *obj)
364 {
365    ELM_IMAGE_DATA_GET(obj, sd);
366
367    return sd->resize_up;
368 }
369
370 static void
371 _elm_image_flip_horizontal(Elm_Image_Smart_Data *sd)
372 {
373    unsigned int *p1, *p2, tmp;
374    unsigned int *data;
375    int x, y, iw, ih;
376
377    evas_object_image_size_get(sd->img, &iw, &ih);
378    data = evas_object_image_data_get(sd->img, EINA_TRUE);
379
380    for (y = 0; y < ih; y++)
381      {
382         p1 = data + (y * iw);
383         p2 = data + ((y + 1) * iw) - 1;
384         for (x = 0; x < (iw >> 1); x++)
385           {
386              tmp = *p1;
387              *p1 = *p2;
388              *p2 = tmp;
389              p1++;
390              p2--;
391           }
392      }
393
394    evas_object_image_data_set(sd->img, data);
395    evas_object_image_data_update_add(sd->img, 0, 0, iw, ih);
396
397    _elm_image_internal_sizing_eval(sd);
398 }
399
400 static void
401 _elm_image_flip_vertical(Elm_Image_Smart_Data *sd)
402 {
403    unsigned int *p1, *p2, tmp;
404    unsigned int *data;
405    int x, y, iw, ih;
406
407    evas_object_image_size_get(sd->img, &iw, &ih);
408    data = evas_object_image_data_get(sd->img, EINA_TRUE);
409
410    for (y = 0; y < (ih >> 1); y++)
411      {
412         p1 = data + (y * iw);
413         p2 = data + ((ih - 1 - y) * iw);
414         for (x = 0; x < iw; x++)
415           {
416              tmp = *p1;
417              *p1 = *p2;
418              *p2 = tmp;
419              p1++;
420              p2++;
421           }
422      }
423
424    evas_object_image_data_set(sd->img, data);
425    evas_object_image_data_update_add(sd->img, 0, 0, iw, ih);
426
427    _elm_image_internal_sizing_eval(sd);
428 }
429
430 static void
431 _elm_image_smart_rotate_180(Elm_Image_Smart_Data *sd)
432 {
433    unsigned int *p1, *p2, tmp;
434    unsigned int *data;
435    int x, hw, iw, ih;
436
437    evas_object_image_size_get(sd->img, &iw, &ih);
438    data = evas_object_image_data_get(sd->img, 1);
439
440    hw = iw * ih;
441    x = (hw / 2);
442    p1 = data;
443    p2 = data + hw - 1;
444
445    for (; --x > 0; )
446      {
447         tmp = *p1;
448         *p1 = *p2;
449         *p2 = tmp;
450         p1++;
451         p2--;
452      }
453
454    evas_object_image_data_set(sd->img, data);
455    evas_object_image_data_update_add(sd->img, 0, 0, iw, ih);
456
457    _elm_image_internal_sizing_eval(sd);
458 }
459
460 static void
461 _elm_image_smart_orient_set(Evas_Object *obj,
462                             Elm_Image_Orient orient)
463 {
464    unsigned int *data, *data2 = NULL, *to, *from;
465    int x, y, w, hw, iw, ih;
466
467    ELM_IMAGE_DATA_GET(obj, sd);
468
469    if (sd->edje)
470      return;
471
472    switch (orient)
473      {
474       case ELM_IMAGE_FLIP_HORIZONTAL:
475         _elm_image_flip_horizontal(sd);
476         sd->orient = orient;
477         return;
478
479       case ELM_IMAGE_FLIP_VERTICAL:
480         _elm_image_flip_vertical(sd);
481         sd->orient = orient;
482         return;
483
484       case ELM_IMAGE_ROTATE_180:
485         _elm_image_smart_rotate_180(sd);
486         sd->orient = orient;
487         return;
488
489      case ELM_IMAGE_ORIENT_NONE:
490         sd->orient = orient;
491         return;
492
493       default:
494         break;
495      }
496
497    evas_object_image_size_get(sd->img, &iw, &ih);
498
499    /* we need separate destination memory if we want to rotate 90 or
500     * 270 degree */
501    data = evas_object_image_data_get(sd->img, EINA_FALSE);
502    if (!data) return;
503    data2 = malloc(sizeof(unsigned char) * (iw * ih * 4));
504    if (!data2) return;
505    memcpy(data2, data, sizeof (unsigned char) * (iw * ih * 4));
506
507    w = ih;
508    ih = iw;
509    iw = w;
510    hw = w * ih;
511
512    evas_object_image_size_set(sd->img, iw, ih);
513    data = evas_object_image_data_get(sd->img, EINA_TRUE);
514
515    switch (orient)
516      {
517       case ELM_IMAGE_FLIP_TRANSPOSE:
518         to = data;
519         hw = -hw + 1;
520         sd->orient = orient;
521         break;
522
523       case ELM_IMAGE_FLIP_TRANSVERSE:
524         to = data + hw - 1;
525         w = -w;
526         hw = hw - 1;
527         sd->orient = orient;
528         break;
529
530       case ELM_IMAGE_ROTATE_90:
531         to = data + w - 1;
532         hw = -hw - 1;
533         sd->orient = orient;
534         break;
535
536       case ELM_IMAGE_ROTATE_270:
537         to = data + hw - w;
538         w = -w;
539         hw = hw + 1;
540         sd->orient = orient;
541         break;
542
543       default:
544         ERR("unknown orient %d", orient);
545         evas_object_image_data_set(sd->img, data);  // give it back
546         if (data2) free(data2);
547
548         return;
549      }
550
551    from = data2;
552    for (x = iw; --x >= 0; )
553      {
554         for (y = ih; --y >= 0; )
555           {
556              *to = *from;
557              from++;
558              to += w;
559           }
560         to += hw;
561      }
562    if (data2) free(data2);
563
564    evas_object_image_data_set(sd->img, data);
565    evas_object_image_data_update_add(sd->img, 0, 0, iw, ih);
566
567    _elm_image_internal_sizing_eval(sd);
568 }
569
570 static Elm_Image_Orient
571 _elm_image_smart_orient_get(const Evas_Object *obj)
572 {
573    ELM_IMAGE_DATA_GET(obj, sd);
574
575    return sd->orient;
576 }
577
578 static Eina_Bool
579 _elm_image_drag_n_drop_cb(void *elm_obj,
580                           Evas_Object *obj,
581                           Elm_Selection_Data *drop)
582 {
583    ELM_IMAGE_DATA_GET(obj, sd);
584
585    if (ELM_IMAGE_CLASS(ELM_WIDGET_DATA(sd)->api)->file_set
586          (obj, drop->data, NULL))
587      {
588         printf("dnd: %s, %s, %s", elm_widget_type_get(elm_obj),
589                SIG_DND, (char *)drop->data);
590
591         evas_object_smart_callback_call(elm_obj, SIG_DND, drop->data);
592         return EINA_TRUE;
593      }
594
595    return EINA_FALSE;
596 }
597
598 static void
599 _elm_image_smart_add(Evas_Object *obj)
600 {
601    EVAS_SMART_DATA_ALLOC(obj, Elm_Image_Smart_Data);
602
603    ELM_WIDGET_CLASS(_elm_image_parent_sc)->base.add(obj);
604
605    priv->hit_rect = evas_object_rectangle_add(evas_object_evas_get(obj));
606    evas_object_smart_member_add(priv->hit_rect, obj);
607    elm_widget_sub_object_add(obj, priv->hit_rect);
608
609    evas_object_color_set(priv->hit_rect, 0, 0, 0, 0);
610    evas_object_show(priv->hit_rect);
611    evas_object_repeat_events_set(priv->hit_rect, EINA_TRUE);
612
613    evas_object_event_callback_add
614      (priv->hit_rect, EVAS_CALLBACK_MOUSE_UP, _on_mouse_up, obj);
615
616    /* starts as an Evas image. may switch to an Edje object */
617    priv->img = _img_new(obj);
618    priv->prev_img = NULL;
619
620    priv->smooth = EINA_TRUE;
621    priv->fill_inside = EINA_TRUE;
622    priv->resize_up = EINA_TRUE;
623    priv->resize_down = EINA_TRUE;
624    priv->aspect_fixed = EINA_TRUE;
625
626    priv->load_size = 64;
627    priv->scale = 1.0;
628
629    ELM_IMAGE_CLASS(ELM_WIDGET_DATA(priv)->api)->load_size_set(obj, 0);
630
631    elm_widget_can_focus_set(obj, EINA_FALSE);
632
633    ELM_IMAGE_CLASS(ELM_WIDGET_DATA(priv)->api)->sizing_eval(obj);
634 }
635
636 static void
637 _elm_image_smart_del(Evas_Object *obj)
638 {
639    ELM_IMAGE_DATA_GET(obj, sd);
640
641    if (sd->anim_timer)
642      ecore_timer_del(sd->anim_timer);
643
644    evas_object_del(sd->img);
645    if (sd->prev_img) evas_object_del(sd->prev_img);
646
647    ELM_WIDGET_CLASS(_elm_image_parent_sc)->base.del(obj);
648 }
649
650 static void
651 _elm_image_smart_move(Evas_Object *obj,
652                       Evas_Coord x,
653                       Evas_Coord y)
654 {
655    ELM_IMAGE_DATA_GET(obj, sd);
656
657    ELM_WIDGET_CLASS(_elm_image_parent_sc)->base.move(obj, x, y);
658
659    if ((sd->img_x == x) && (sd->img_y == y)) return;
660    sd->img_x = x;
661    sd->img_y = y;
662
663    /* takes care of moving */
664    _elm_image_internal_sizing_eval(sd);
665 }
666
667 static void
668 _elm_image_smart_resize(Evas_Object *obj,
669                         Evas_Coord w,
670                         Evas_Coord h)
671 {
672    ELM_IMAGE_DATA_GET(obj, sd);
673
674    ELM_WIDGET_CLASS(_elm_image_parent_sc)->base.resize(obj, w, h);
675
676    if ((sd->img_w == w) && (sd->img_h == h)) return;
677
678    sd->img_w = w;
679    sd->img_h = h;
680
681    /* takes care of resizing */
682    _elm_image_internal_sizing_eval(sd);
683 }
684
685 static void
686 _elm_image_smart_show(Evas_Object *obj)
687 {
688    ELM_IMAGE_DATA_GET(obj, sd);
689
690    sd->show = EINA_TRUE;
691    if (sd->preloading)
692      return;
693
694    ELM_WIDGET_CLASS(_elm_image_parent_sc)->base.show(obj);
695
696    evas_object_show(sd->img);
697
698    if (sd->prev_img) evas_object_del(sd->prev_img);
699    sd->prev_img = NULL;
700 }
701
702 static void
703 _elm_image_smart_hide(Evas_Object *obj)
704 {
705    ELM_IMAGE_DATA_GET(obj, sd);
706
707    ELM_WIDGET_CLASS(_elm_image_parent_sc)->base.hide(obj);
708
709    sd->show = EINA_FALSE;
710    evas_object_hide(sd->img);
711
712    if (sd->prev_img) evas_object_del(sd->prev_img);
713    sd->prev_img = NULL;
714 }
715
716 static void
717 _elm_image_smart_member_add(Evas_Object *obj,
718                                Evas_Object *member)
719 {
720    ELM_IMAGE_DATA_GET(obj, sd);
721
722    ELM_WIDGET_CLASS(_elm_image_parent_sc)->base.member_add(obj, member);
723
724    if (sd->hit_rect)
725      evas_object_raise(sd->hit_rect);
726 }
727
728 static void
729 _elm_image_smart_color_set(Evas_Object *obj,
730                            int r,
731                            int g,
732                            int b,
733                            int a)
734 {
735    ELM_IMAGE_DATA_GET(obj, sd);
736
737    ELM_WIDGET_CLASS(_elm_image_parent_sc)->base.color_set(obj, r, g, b, a);
738
739    evas_object_color_set(sd->hit_rect, 0, 0, 0, 0);
740    evas_object_color_set(sd->img, r, g, b, a);
741    if (sd->prev_img) evas_object_color_set(sd->prev_img, r, g, b, a);
742 }
743
744 static void
745 _elm_image_smart_clip_set(Evas_Object *obj,
746                           Evas_Object *clip)
747 {
748    ELM_IMAGE_DATA_GET(obj, sd);
749
750    ELM_WIDGET_CLASS(_elm_image_parent_sc)->base.clip_set(obj, clip);
751
752    evas_object_clip_set(sd->img, clip);
753    if (sd->prev_img) evas_object_clip_set(sd->prev_img, clip);
754 }
755
756 static void
757 _elm_image_smart_clip_unset(Evas_Object *obj)
758 {
759    ELM_IMAGE_DATA_GET(obj, sd);
760
761    ELM_WIDGET_CLASS(_elm_image_parent_sc)->base.clip_unset(obj);
762
763    evas_object_clip_unset(sd->img);
764    if (sd->prev_img) evas_object_clip_unset(sd->prev_img);
765 }
766
767 static Eina_Bool
768 _elm_image_smart_theme(Evas_Object *obj)
769 {
770    ELM_IMAGE_DATA_GET(obj, sd);
771
772    if (!ELM_WIDGET_CLASS(_elm_image_parent_sc)->theme(obj))
773      return EINA_FALSE;
774
775    ELM_IMAGE_CLASS(ELM_WIDGET_DATA(sd)->api)->sizing_eval(obj);
776
777    return EINA_TRUE;
778 }
779
780 static void
781 _elm_image_smart_sizing_eval(Evas_Object *obj)
782 {
783    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
784    int w, h;
785    double ts;
786
787    ELM_IMAGE_DATA_GET(obj, sd);
788
789    _elm_image_internal_sizing_eval(sd);
790
791    ELM_IMAGE_CLASS(ELM_WIDGET_DATA(sd)->api)->smooth_scale_set
792      (obj, sd->smooth);
793
794    if (sd->no_scale)
795      ELM_IMAGE_CLASS(ELM_WIDGET_DATA(sd)->api)->scale_set(obj, 1.0);
796    else
797      ELM_IMAGE_CLASS(ELM_WIDGET_DATA(sd)->api)->scale_set
798        (obj, elm_widget_scale_get(obj) * elm_config_scale_get());
799
800    ts = sd->scale;
801    sd->scale = 1.0;
802    ELM_IMAGE_CLASS(ELM_WIDGET_DATA(sd)->api)->size_get(obj, &w, &h);
803    sd->scale = ts;
804    evas_object_size_hint_min_get(obj, &minw, &minh);
805    
806    if (sd->no_scale)
807      {
808         maxw = minw = w;
809         maxh = minh = h;
810         if ((sd->scale > 1.0) && (sd->resize_up))
811           {
812              maxw = minw = w * sd->scale;
813              maxh = minh = h * sd->scale;
814           }
815         else if ((sd->scale < 1.0) && (sd->resize_down))
816           {
817              maxw = minw = w * sd->scale;
818              maxh = minh = h * sd->scale;
819           }
820      }
821    else
822      {
823         if (!sd->resize_down)
824           {
825              minw = w * sd->scale;
826              minh = h * sd->scale;
827           }
828         if (!sd->resize_up)
829           {
830              maxw = w * sd->scale;
831              maxh = h * sd->scale;
832           }
833      }
834
835    evas_object_size_hint_min_set(obj, minw, minh);
836    evas_object_size_hint_max_set(obj, maxw, maxh);
837 }
838
839 static Eina_Bool
840 _elm_image_smart_memfile_set(Evas_Object *obj,
841                              const void *img,
842                              size_t size,
843                              const char *format,
844                              const char *key)
845 {
846    ELM_IMAGE_DATA_GET(obj, sd);
847
848    _elm_image_file_set_do(obj);
849
850    evas_object_image_memfile_set
851      (sd->img, (void *)img, size, (char *)format, (char *)key);
852
853    sd->preloading = EINA_TRUE;
854    sd->show = EINA_TRUE;
855
856    evas_object_hide(sd->img);
857    evas_object_image_preload(sd->img, EINA_FALSE);
858    if (evas_object_image_load_error_get(sd->img) != EVAS_LOAD_ERROR_NONE)
859      {
860         ERR("Things are going bad for some random " FMT_SIZE_T
861             " byte chunk of memory (%p)", size, sd->img);
862         return EINA_FALSE;
863      }
864
865    _elm_image_internal_sizing_eval(sd);
866
867    return EINA_TRUE;
868 }
869
870 static Eina_Bool
871 _elm_image_smart_file_set(Evas_Object *obj,
872                           const char *file,
873                           const char *key)
874 {
875    Evas_Coord w, h;
876
877    ELM_IMAGE_DATA_GET(obj, sd);
878
879    if (eina_str_has_extension(file, ".edj"))
880      return _elm_image_edje_file_set(obj, file, key);
881
882    _elm_image_file_set_do(obj);
883
884    evas_object_image_file_set(sd->img, file, key);
885
886    sd->preloading = EINA_TRUE;
887    sd->show = EINA_TRUE;
888
889    evas_object_hide(sd->img);
890
891    ELM_IMAGE_CLASS(ELM_WIDGET_DATA(sd)->api)->size_get(obj, &w, &h);
892
893    evas_object_image_load_size_set(sd->img, w, h);
894
895    evas_object_image_preload(sd->img, EINA_FALSE);
896    if (evas_object_image_load_error_get(sd->img) != EVAS_LOAD_ERROR_NONE)
897      {
898         ERR("Things are going bad for '%s' (%p)", file, sd->img);
899         return EINA_FALSE;
900      }
901
902    _elm_image_internal_sizing_eval(sd);
903
904    return EINA_TRUE;
905 }
906
907 static void
908 _elm_image_smart_file_get(const Evas_Object *obj,
909                           const char **file,
910                           const char **key)
911 {
912    ELM_IMAGE_DATA_GET(obj, sd);
913
914    if (sd->edje)
915      edje_object_file_get(sd->img, file, key);
916    else
917      evas_object_image_file_get(sd->img, file, key);
918 }
919
920 static void
921 _elm_image_smart_preload_set(Evas_Object *obj,
922                              Eina_Bool disable)
923 {
924    ELM_IMAGE_DATA_GET(obj, sd);
925
926    if (sd->edje || !sd->preloading) return;
927
928    evas_object_image_preload(sd->img, disable);
929    sd->preloading = !disable;
930
931    if (disable)
932      {
933         if (sd->show && sd->img) evas_object_show(sd->img);
934         if (sd->prev_img)
935           {
936              evas_object_del(sd->prev_img);
937              sd->prev_img = NULL;
938           }
939      }
940 }
941
942 static void
943 _elm_image_smart_load_size_set(Evas_Object *obj,
944                                int size)
945 {
946    ELM_IMAGE_DATA_GET(obj, sd);
947
948    sd->load_size = size;
949    if (!sd->img || sd->edje) return;
950
951    evas_object_image_load_size_set(sd->img, sd->load_size, sd->load_size);
952 }
953
954 static int
955 _elm_image_smart_load_size_get(const Evas_Object *obj)
956 {
957    ELM_IMAGE_DATA_GET(obj, sd);
958
959    return sd->load_size;
960 }
961
962 static void
963 _elm_image_smart_scale_set(Evas_Object *obj,
964                            double scale)
965 {
966    ELM_IMAGE_DATA_GET(obj, sd);
967
968    sd->scale = scale;
969
970    _elm_image_internal_sizing_eval(sd);
971 }
972
973 static double
974 _elm_image_smart_scale_get(const Evas_Object *obj)
975 {
976    ELM_IMAGE_DATA_GET(obj, sd);
977
978    return sd->scale;
979 }
980
981 /**
982  * Turns on editing through drag and drop and copy and paste.
983  */
984 static void
985 _elm_image_smart_edit_set(Evas_Object *obj,
986                           Eina_Bool edit,
987                           Evas_Object *parent)
988 {
989    ELM_IMAGE_DATA_GET(obj, sd);
990
991    if (sd->edje)
992      {
993         printf("No editing edje objects yet (ever)\n");
994         return;
995      }
996
997    edit = !!edit;
998
999    if (edit == sd->edit) return;
1000
1001    sd->edit = edit;
1002
1003    if (sd->edit)
1004      elm_drop_target_add
1005        (obj, ELM_SEL_FORMAT_IMAGE, _elm_image_drag_n_drop_cb, parent);
1006    else
1007      elm_drop_target_del(obj);
1008 }
1009
1010 static Eina_Bool
1011 _elm_image_smart_edit_get(const Evas_Object *obj)
1012 {
1013    ELM_IMAGE_DATA_GET(obj, sd);
1014
1015    return sd->edit;
1016 }
1017
1018 static void
1019 _elm_image_smart_aspect_fixed_set(Evas_Object *obj,
1020                                   Eina_Bool fixed)
1021 {
1022    ELM_IMAGE_DATA_GET(obj, sd);
1023
1024    fixed = !!fixed;
1025    if (sd->aspect_fixed == fixed) return;
1026
1027    sd->aspect_fixed = fixed;
1028
1029    _elm_image_internal_sizing_eval(sd);
1030 }
1031
1032 static Eina_Bool
1033 _elm_image_smart_aspect_fixed_get(const Evas_Object *obj)
1034 {
1035    ELM_IMAGE_DATA_GET(obj, sd);
1036
1037    return sd->aspect_fixed;
1038 }
1039
1040 static void
1041 _elm_image_smart_set_user(Elm_Image_Smart_Class *sc)
1042 {
1043    ELM_WIDGET_CLASS(sc)->base.add = _elm_image_smart_add;
1044    ELM_WIDGET_CLASS(sc)->base.del = _elm_image_smart_del,
1045    ELM_WIDGET_CLASS(sc)->base.move = _elm_image_smart_move,
1046    ELM_WIDGET_CLASS(sc)->base.resize = _elm_image_smart_resize,
1047    ELM_WIDGET_CLASS(sc)->base.show = _elm_image_smart_show,
1048    ELM_WIDGET_CLASS(sc)->base.hide = _elm_image_smart_hide,
1049    ELM_WIDGET_CLASS(sc)->base.member_add = _elm_image_smart_member_add,
1050    ELM_WIDGET_CLASS(sc)->base.color_set = _elm_image_smart_color_set,
1051    ELM_WIDGET_CLASS(sc)->base.clip_set = _elm_image_smart_clip_set,
1052    ELM_WIDGET_CLASS(sc)->base.clip_unset = _elm_image_smart_clip_unset,
1053
1054    ELM_WIDGET_CLASS(sc)->theme = _elm_image_smart_theme;
1055
1056    sc->aspect_fixed_get = _elm_image_smart_aspect_fixed_get;
1057    sc->aspect_fixed_set = _elm_image_smart_aspect_fixed_set;
1058    sc->edit_get = _elm_image_smart_edit_get;
1059    sc->edit_set = _elm_image_smart_edit_set;
1060    sc->file_get = _elm_image_smart_file_get;
1061    sc->file_set = _elm_image_smart_file_set;
1062    sc->fill_inside_get = _elm_image_smart_fill_inside_get;
1063    sc->fill_inside_set = _elm_image_smart_fill_inside_set;
1064    sc->image_object_get = _elm_image_smart_object_get;
1065    sc->load_size_get = _elm_image_smart_load_size_get;
1066    sc->load_size_set = _elm_image_smart_load_size_set;
1067    sc->memfile_set = _elm_image_smart_memfile_set;
1068    sc->orient_get = _elm_image_smart_orient_get;
1069    sc->orient_set = _elm_image_smart_orient_set;
1070    sc->preload_set = _elm_image_smart_preload_set;
1071    sc->resize_down_get = _elm_image_smart_resize_down_get;
1072    sc->resize_down_set = _elm_image_smart_resize_down_set;
1073    sc->scale_get = _elm_image_smart_scale_get;
1074    sc->scale_set = _elm_image_smart_scale_set;
1075    sc->resize_up_get = _elm_image_smart_resize_up_get;
1076    sc->resize_up_set = _elm_image_smart_resize_up_set;
1077    sc->size_get = _elm_image_smart_size_get;
1078    sc->sizing_eval = _elm_image_smart_sizing_eval;
1079    sc->smooth_scale_get = _elm_image_smart_smooth_scale_get;
1080    sc->smooth_scale_set = _elm_image_smart_smooth_scale_set;
1081 }
1082
1083 EAPI const Elm_Image_Smart_Class *
1084 elm_image_smart_class_get(void)
1085 {
1086    static Elm_Image_Smart_Class _sc =
1087      ELM_IMAGE_SMART_CLASS_INIT_NAME_VERSION(ELM_IMAGE_SMART_NAME);
1088    static const Elm_Image_Smart_Class *class = NULL;
1089    Evas_Smart_Class *esc = (Evas_Smart_Class *)&_sc;
1090
1091    if (class)
1092      return class;
1093
1094    _elm_image_smart_set(&_sc);
1095    esc->callbacks = _smart_callbacks;
1096    class = &_sc;
1097
1098    return class;
1099 }
1100
1101 EAPI Evas_Object *
1102 elm_image_add(Evas_Object *parent)
1103 {
1104    Evas_Object *obj;
1105
1106    EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
1107
1108    obj = elm_widget_add(_elm_image_smart_class_new(), parent);
1109    if (!obj) return NULL;
1110
1111    if (!elm_widget_sub_object_add(parent, obj))
1112      ERR("could not add %p as sub object of %p", obj, parent);
1113
1114    return obj;
1115 }
1116
1117 EAPI Eina_Bool
1118 elm_image_memfile_set(Evas_Object *obj,
1119                       const void *img,
1120                       size_t size,
1121                       const char *format,
1122                       const char *key)
1123 {
1124    ELM_IMAGE_CHECK(obj) EINA_FALSE;
1125    ELM_IMAGE_DATA_GET(obj, sd);
1126
1127    EINA_SAFETY_ON_NULL_RETURN_VAL(img, EINA_FALSE);
1128    EINA_SAFETY_ON_TRUE_RETURN_VAL(!size, EINA_FALSE);
1129
1130    return ELM_IMAGE_CLASS(ELM_WIDGET_DATA(sd)->api)->memfile_set
1131             (obj, img, size, format, key);
1132 }
1133
1134 EAPI Eina_Bool
1135 elm_image_file_set(Evas_Object *obj,
1136                    const char *file,
1137                    const char *group)
1138 {
1139    Eina_Bool ret;
1140
1141    ELM_IMAGE_CHECK(obj) EINA_FALSE;
1142    ELM_IMAGE_DATA_GET(obj, sd);
1143
1144    EINA_SAFETY_ON_NULL_RETURN_VAL(file, EINA_FALSE);
1145
1146    ret = ELM_IMAGE_CLASS(ELM_WIDGET_DATA(sd)->api)->file_set(obj, file, group);
1147
1148    ELM_IMAGE_CLASS(ELM_WIDGET_DATA(sd)->api)->sizing_eval(obj);
1149
1150    return ret;
1151 }
1152
1153 EAPI void
1154 elm_image_file_get(const Evas_Object *obj,
1155                    const char **file,
1156                    const char **group)
1157 {
1158    ELM_IMAGE_CHECK(obj);
1159    ELM_IMAGE_DATA_GET(obj, sd);
1160
1161    ELM_IMAGE_CLASS(ELM_WIDGET_DATA(sd)->api)->file_get(obj, file, group);
1162 }
1163
1164 EAPI void
1165 elm_image_smooth_set(Evas_Object *obj,
1166                      Eina_Bool smooth)
1167 {
1168    ELM_IMAGE_CHECK(obj);
1169    ELM_IMAGE_DATA_GET(obj, sd);
1170
1171    sd->smooth = smooth;
1172
1173    ELM_IMAGE_CLASS(ELM_WIDGET_DATA(sd)->api)->sizing_eval(obj);
1174 }
1175
1176 EAPI Eina_Bool
1177 elm_image_smooth_get(const Evas_Object *obj)
1178 {
1179    ELM_IMAGE_CHECK(obj) EINA_FALSE;
1180    ELM_IMAGE_DATA_GET(obj, sd);
1181
1182    return sd->smooth;
1183 }
1184
1185 EAPI void
1186 elm_image_object_size_get(const Evas_Object *obj,
1187                           int *w,
1188                           int *h)
1189 {
1190    if (w) *w = 0;
1191    if (h) *h = 0;
1192
1193    ELM_IMAGE_CHECK(obj);
1194    ELM_IMAGE_DATA_GET(obj, sd);
1195
1196    ELM_IMAGE_CLASS(ELM_WIDGET_DATA(sd)->api)->size_get(obj, w, h);
1197 }
1198
1199 EAPI void
1200 elm_image_no_scale_set(Evas_Object *obj,
1201                        Eina_Bool no_scale)
1202 {
1203    ELM_IMAGE_CHECK(obj);
1204    ELM_IMAGE_DATA_GET(obj, sd);
1205
1206    sd->no_scale = no_scale;
1207
1208    ELM_IMAGE_CLASS(ELM_WIDGET_DATA(sd)->api)->sizing_eval(obj);
1209 }
1210
1211 EAPI Eina_Bool
1212 elm_image_no_scale_get(const Evas_Object *obj)
1213 {
1214    ELM_IMAGE_CHECK(obj) EINA_FALSE;
1215    ELM_IMAGE_DATA_GET(obj, sd);
1216
1217    return sd->no_scale;
1218 }
1219
1220 EAPI void
1221 elm_image_resizable_set(Evas_Object *obj,
1222                         Eina_Bool up,
1223                         Eina_Bool down)
1224 {
1225    ELM_IMAGE_CHECK(obj);
1226    ELM_IMAGE_DATA_GET(obj, sd);
1227
1228    sd->resize_up = !!up;
1229    sd->resize_down = !!down;
1230
1231    ELM_IMAGE_CLASS(ELM_WIDGET_DATA(sd)->api)->sizing_eval(obj);
1232 }
1233
1234 EAPI void
1235 elm_image_resizable_get(const Evas_Object *obj,
1236                         Eina_Bool *size_up,
1237                         Eina_Bool *size_down)
1238 {
1239    ELM_IMAGE_CHECK(obj);
1240    ELM_IMAGE_DATA_GET(obj, sd);
1241
1242    if (size_up) *size_up = sd->resize_up;
1243    if (size_down) *size_down = sd->resize_down;
1244 }
1245
1246 EAPI void
1247 elm_image_fill_outside_set(Evas_Object *obj,
1248                            Eina_Bool fill_outside)
1249 {
1250    ELM_IMAGE_CHECK(obj);
1251    ELM_IMAGE_DATA_GET(obj, sd);
1252
1253    sd->fill_inside = !fill_outside;
1254
1255    ELM_IMAGE_CLASS(ELM_WIDGET_DATA(sd)->api)->sizing_eval(obj);
1256 }
1257
1258 EAPI Eina_Bool
1259 elm_image_fill_outside_get(const Evas_Object *obj)
1260 {
1261    ELM_IMAGE_CHECK(obj) EINA_FALSE;
1262    ELM_IMAGE_DATA_GET(obj, sd);
1263
1264    return !sd->fill_inside;
1265 }
1266
1267 EAPI void
1268 elm_image_preload_disabled_set(Evas_Object *obj,
1269                                Eina_Bool disabled)
1270 {
1271    ELM_IMAGE_CHECK(obj);
1272    ELM_IMAGE_DATA_GET(obj, sd);
1273
1274    ELM_IMAGE_CLASS(ELM_WIDGET_DATA(sd)->api)->preload_set(obj, !!disabled);
1275 }
1276
1277 EAPI void
1278 elm_image_prescale_set(Evas_Object *obj,
1279                        int size)
1280 {
1281    ELM_IMAGE_CHECK(obj);
1282    ELM_IMAGE_DATA_GET(obj, sd);
1283
1284    ELM_IMAGE_CLASS(ELM_WIDGET_DATA(sd)->api)->load_size_set(obj, size);
1285 }
1286
1287 EAPI int
1288 elm_image_prescale_get(const Evas_Object *obj)
1289 {
1290    ELM_IMAGE_CHECK(obj) 0;
1291    ELM_IMAGE_DATA_GET(obj, sd);
1292
1293    return ELM_IMAGE_CLASS(ELM_WIDGET_DATA(sd)->api)->load_size_get(obj);
1294 }
1295
1296 EAPI void
1297 elm_image_orient_set(Evas_Object *obj,
1298                      Elm_Image_Orient orient)
1299 {
1300    ELM_IMAGE_CHECK(obj);
1301    ELM_IMAGE_DATA_GET(obj, sd);
1302
1303    ELM_IMAGE_CLASS(ELM_WIDGET_DATA(sd)->api)->orient_set(obj, orient);
1304 }
1305
1306 EAPI Elm_Image_Orient
1307 elm_image_orient_get(const Evas_Object *obj)
1308 {
1309    ELM_IMAGE_CHECK(obj) ELM_IMAGE_ORIENT_NONE;
1310    ELM_IMAGE_DATA_GET(obj, sd);
1311
1312    return ELM_IMAGE_CLASS(ELM_WIDGET_DATA(sd)->api)->orient_get(obj);
1313 }
1314
1315 EAPI void
1316 elm_image_editable_set(Evas_Object *obj,
1317                        Eina_Bool set)
1318 {
1319    ELM_IMAGE_CHECK(obj);
1320    ELM_IMAGE_DATA_GET(obj, sd);
1321
1322    ELM_IMAGE_CLASS(ELM_WIDGET_DATA(sd)->api)->edit_set(obj, set, obj);
1323 }
1324
1325 EAPI Eina_Bool
1326 elm_image_editable_get(const Evas_Object *obj)
1327 {
1328    ELM_IMAGE_CHECK(obj) EINA_FALSE;
1329    ELM_IMAGE_DATA_GET(obj, sd);
1330
1331    return ELM_IMAGE_CLASS(ELM_WIDGET_DATA(sd)->api)->edit_get(obj);
1332 }
1333
1334 EAPI Evas_Object *
1335 elm_image_object_get(const Evas_Object *obj)
1336 {
1337    ELM_IMAGE_CHECK(obj) NULL;
1338    ELM_IMAGE_DATA_GET(obj, sd);
1339
1340    return ELM_IMAGE_CLASS(ELM_WIDGET_DATA(sd)->api)->image_object_get(obj);
1341 }
1342
1343 EAPI void
1344 elm_image_aspect_fixed_set(Evas_Object *obj,
1345                            Eina_Bool fixed)
1346 {
1347    ELM_IMAGE_CHECK(obj);
1348    ELM_IMAGE_DATA_GET(obj, sd);
1349
1350    ELM_IMAGE_CLASS(ELM_WIDGET_DATA(sd)->api)->aspect_fixed_set(obj, fixed);
1351 }
1352
1353 EAPI Eina_Bool
1354 elm_image_aspect_fixed_get(const Evas_Object *obj)
1355 {
1356    ELM_IMAGE_CHECK(obj) EINA_FALSE;
1357    ELM_IMAGE_DATA_GET(obj, sd);
1358
1359    return ELM_IMAGE_CLASS(ELM_WIDGET_DATA(sd)->api)->aspect_fixed_get(obj);
1360 }
1361
1362 EAPI Eina_Bool
1363 elm_image_animated_available_get(const Evas_Object *obj)
1364 {
1365    ELM_IMAGE_CHECK(obj) EINA_FALSE;
1366    ELM_IMAGE_DATA_GET(obj, sd);
1367
1368    if (sd->edje) return EINA_FALSE;
1369
1370    return evas_object_image_animated_get(elm_image_object_get(obj));
1371 }
1372
1373 EAPI void
1374 elm_image_animated_set(Evas_Object *obj,
1375                        Eina_Bool anim)
1376 {
1377    ELM_IMAGE_CHECK(obj);
1378    ELM_IMAGE_DATA_GET(obj, sd);
1379
1380    anim = !!anim;
1381    if (sd->anim == anim) return;
1382
1383    if (sd->edje) return;
1384
1385    sd->img = elm_image_object_get(obj);
1386    if (!evas_object_image_animated_get(sd->img)) return;
1387
1388    if (anim)
1389      {
1390         sd->frame_count = evas_object_image_animated_frame_count_get(sd->img);
1391         sd->cur_frame = 1;
1392         sd->frame_duration =
1393           evas_object_image_animated_frame_duration_get
1394             (sd->img, sd->cur_frame, 0);
1395         evas_object_image_animated_frame_set(sd->img, sd->cur_frame);
1396      }
1397    else
1398      {
1399         sd->frame_count = -1;
1400         sd->cur_frame = -1;
1401         sd->frame_duration = -1;
1402      }
1403    sd->anim = anim;
1404
1405    return;
1406 }
1407
1408 EAPI Eina_Bool
1409 elm_image_animated_get(const Evas_Object *obj)
1410 {
1411    ELM_IMAGE_CHECK(obj) EINA_FALSE;
1412    ELM_IMAGE_DATA_GET(obj, sd);
1413
1414    return sd->anim;
1415 }
1416
1417 EAPI void
1418 elm_image_animated_play_set(Evas_Object *obj,
1419                             Eina_Bool play)
1420 {
1421    ELM_IMAGE_CHECK(obj);
1422    ELM_IMAGE_DATA_GET(obj, sd);
1423
1424    if (!sd->anim) return;
1425    if (sd->play == play) return;
1426
1427    if (sd->edje) return;
1428
1429    if (play)
1430      {
1431         sd->anim_timer = ecore_timer_add
1432             (sd->frame_duration, _elm_image_animate_cb, sd);
1433      }
1434    else
1435      {
1436         if (sd->anim_timer)
1437           {
1438              ecore_timer_del(sd->anim_timer);
1439              sd->anim_timer = NULL;
1440           }
1441      }
1442    sd->play = play;
1443 }
1444
1445 EAPI Eina_Bool
1446 elm_image_animated_play_get(const Evas_Object *obj)
1447 {
1448    ELM_IMAGE_CHECK(obj) EINA_FALSE;
1449    ELM_IMAGE_DATA_GET(obj, sd);
1450
1451    return sd->play;
1452 }