elementary: add ELM_PHOTOCAM_ZOOM_MODE_AUTO_FIT_IN a new policy for zoom in elm_photocam
[framework/uifw/elementary.git] / src / lib / elm_photocam.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3 #include "els_scroller.h"
4
5 /*
6  * TODO (maybe - optional future stuff):
7  *
8  * 1. wrap photo in theme edje so u can have styling around photo (like white
9  *    photo bordering).
10  * 2. exif handling
11  * 3. rotation flags in exif handling (nasty! should have rot in evas)
12  */
13
14 typedef struct _Widget_Data Widget_Data;
15 typedef struct _Pan Pan;
16 typedef struct _Grid Grid;
17 typedef struct _Grid_Item Grid_Item;
18
19 struct _Grid_Item
20 {
21    Widget_Data *wd;
22    Evas_Object *img;
23    struct
24    {
25       int x, y, w, h;
26    } src, out;
27    Eina_Bool want : 1;
28    Eina_Bool have : 1;
29 };
30
31 struct _Grid
32 {
33    int tsize; // size of tile (tsize x tsize pixels)
34    int zoom; // zoom level tiles want for optimal display (1, 2, 4, 8)
35    int iw, ih; // size of image in pixels
36    int w, h; // size of grid image in pixels (represented by grid)
37    int gw, gh; // size of grid in tiles
38    Grid_Item *grid; // the grid (gw * gh items)
39    Eina_Bool dead : 1; // old grid. will die as soon as anim is over
40 };
41
42 struct _Widget_Data
43 {
44    Evas_Object *obj;
45    Evas_Object *scr;
46    Evas_Object *pan_smart;
47    Pan *pan;
48    Evas_Coord pan_x, pan_y, minw, minh;
49
50    double zoom;
51    Elm_Photocam_Zoom_Mode mode;
52    const char *file;
53
54    Ecore_Job *calc_job;
55    Ecore_Timer *scr_timer;
56    Ecore_Timer *long_timer;
57    Ecore_Animator *zoom_animator;
58    double t_start, t_end;
59    struct
60    {
61       int imw, imh;
62       int w, h;
63       int ow, oh, nw, nh;
64       struct
65       {
66          double x, y;
67       } spos;
68    } size;
69    struct
70    {
71       Eina_Bool show : 1;
72       Evas_Coord x, y ,w ,h;
73    } show;
74    int tsize;
75    Evas_Object *img; // low res version of image (scale down == 8)
76    int nosmooth;
77    int preload_num;
78    Eina_List *grids;
79    Eina_Bool main_load_pending : 1;
80    Eina_Bool resized : 1;
81    Eina_Bool longpressed : 1;
82    Eina_Bool on_hold : 1;
83    Eina_Bool paused : 1;
84 };
85
86 struct _Pan
87 {
88    Evas_Object_Smart_Clipped_Data __clipped_data;
89    Widget_Data *wd;
90 };
91
92 static const char *widtype = NULL;
93 static void _del_hook(Evas_Object *obj);
94 static void _theme_hook(Evas_Object *obj);
95 static void _on_focus_hook(void *data, Evas_Object *obj);
96 //static void _show_region_hook(void *data, Evas_Object *obj);
97 static void _sizing_eval(Evas_Object *obj);
98 static void _calc_job(void *data);
99 static Eina_Bool _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__,
100                              Evas_Callback_Type type, void *event_info);
101 static void grid_place(Evas_Object *obj, Grid *g, Evas_Coord px, Evas_Coord py, Evas_Coord ox, Evas_Coord oy, Evas_Coord ow, Evas_Coord oh);
102 static void grid_clear(Evas_Object *obj, Grid *g);
103 static Grid *grid_create(Evas_Object *obj);
104 static void grid_load(Evas_Object *obj, Grid *g);
105
106 static const char SIG_CLICKED[] = "clicked";
107 static const char SIG_PRESS[] = "press";
108 static const char SIG_LONGPRESSED[] = "longpressed";
109 static const char SIG_CLICKED_DOUBLE[] = "clicked,double";
110 static const char SIG_LOAD[] = "load";
111 static const char SIG_LOADED[] = "loaded";
112 static const char SIG_LOAD_DETAIL[] = "load,detail";
113 static const char SIG_LOADED_DETAIL[] = "loaded,detail";
114 static const char SIG_ZOOM_START[] = "zoom,start";
115 static const char SIG_ZOOM_STOP[] = "zoom,stop";
116 static const char SIG_ZOOM_CHANGE[] = "zoom,change";
117 static const char SIG_SCROLL[] = "scroll";
118 static const char SIG_SCROLL_ANIM_START[] = "scroll,anim,start";
119 static const char SIG_SCROLL_ANIM_STOP[] = "scroll,anim,stop";
120 static const char SIG_SCROLL_DRAG_START[] = "scroll,drag,start";
121 static const char SIG_SCROLL_DRAG_STOP[] = "scroll,drag,stop";
122
123 static const Evas_Smart_Cb_Description _signals[] = {
124    {SIG_CLICKED, ""},
125    {SIG_PRESS, ""},
126    {SIG_LONGPRESSED, ""},
127    {SIG_CLICKED_DOUBLE, ""},
128    {SIG_LOAD, ""},
129    {SIG_LOADED, ""},
130    {SIG_LOAD_DETAIL, ""},
131    {SIG_LOADED_DETAIL, ""},
132    {SIG_ZOOM_START, ""},
133    {SIG_ZOOM_STOP, ""},
134    {SIG_ZOOM_CHANGE, ""},
135    {SIG_SCROLL, ""},
136    {SIG_SCROLL_ANIM_START, ""},
137    {SIG_SCROLL_ANIM_STOP, ""},
138    {SIG_SCROLL_DRAG_START, ""},
139    {SIG_SCROLL_DRAG_STOP, ""},
140    {NULL, NULL}
141 };
142
143
144 static int
145 nearest_pow2(int num)
146 {
147    unsigned int n = num - 1;
148    n |= n >> 1;
149    n |= n >> 2;
150    n |= n >> 4;
151    n |= n >> 8;
152    n |= n >> 16;
153    return n + 1;
154 }
155
156 static void
157 img_place(Evas_Object *obj, Evas_Coord px, Evas_Coord py, Evas_Coord ox, Evas_Coord oy, Evas_Coord ow, Evas_Coord oh)
158 {
159    Widget_Data *wd = elm_widget_data_get(obj);
160    Evas_Coord ax, ay, gw, gh;
161    if (!wd) return;
162    ax = 0;
163    ay = 0;
164    gw = wd->size.w;
165    gh = wd->size.h;
166    if (ow > gw) ax = (ow - gw) / 2;
167    if (oh > gh) ay = (oh - gh) / 2;
168    evas_object_move(wd->img, ox + 0 - px + ax, oy + 0 - py + ay);
169    evas_object_resize(wd->img, gw, gh);
170
171    if (wd->show.show)
172      {
173         wd->show.show = EINA_FALSE;
174         elm_smart_scroller_child_region_show(wd->scr, wd->show.x, wd->show.y, wd->show.w, wd->show.h);
175      }
176 }
177
178 static void
179 grid_place(Evas_Object *obj, Grid *g, Evas_Coord px, Evas_Coord py, Evas_Coord ox, Evas_Coord oy, Evas_Coord ow, Evas_Coord oh)
180 {
181    Widget_Data *wd = elm_widget_data_get(obj);
182    Evas_Coord ax, ay, gw, gh, tx, ty;
183    int x, y;
184    if (!wd) return;
185    ax = 0;
186    ay = 0;
187    gw = wd->size.w;
188    gh = wd->size.h;
189    if (ow > gw) ax = (ow - gw) / 2;
190    if (oh > gh) ay = (oh - gh) / 2;
191    for (y = 0; y < g->gh; y++)
192      {
193         for (x = 0; x < g->gw; x++)
194           {
195              int tn, xx, yy, ww, hh;
196
197              tn = (y * g->gw) + x;
198              xx = g->grid[tn].out.x;
199              yy = g->grid[tn].out.y;
200              ww = g->grid[tn].out.w;
201              hh = g->grid[tn].out.h;
202              if ((gw != g->w) && (g->w > 0))
203                {
204                   tx = xx;
205                   xx = (gw * xx) / g->w;
206                   ww = ((gw * (tx + ww)) / g->w) - xx;
207                }
208              if ((gh != g->h) && (g->h > 0))
209                {
210                   ty = yy;
211                   yy = (gh * yy) / g->h;
212                   hh = ((gh * (ty + hh)) / g->h) - yy;
213                }
214              evas_object_move(g->grid[tn].img,
215                               ox + xx - px + ax,
216                               oy + yy - py + ay);
217              evas_object_resize(g->grid[tn].img, ww, hh);
218           }
219      }
220 }
221
222 static void
223 grid_clear(Evas_Object *obj, Grid *g)
224 {
225    Widget_Data *wd = elm_widget_data_get(obj);
226    int x, y;
227    if (!wd) return;
228    if (!g->grid) return;
229    for (y = 0; y < g->gh; y++)
230      {
231         for (x = 0; x < g->gw; x++)
232           {
233              int tn;
234
235              tn = (y * g->gw) + x;
236              evas_object_del(g->grid[tn].img);
237              if (g->grid[tn].want)
238                {
239                   wd->preload_num--;
240                   if (!wd->preload_num)
241                     {
242                        edje_object_signal_emit(elm_smart_scroller_edje_object_get(wd->scr),
243                                                "elm,state,busy,stop", "elm");
244                        evas_object_smart_callback_call(obj, SIG_LOAD_DETAIL, NULL);
245                     }
246                }
247           }
248      }
249    free(g->grid);
250    g->grid = NULL;
251    g->gw = 0;
252    g->gh = 0;
253 }
254
255 static void
256 _tile_preloaded(void *data, Evas *e __UNUSED__, Evas_Object *o __UNUSED__, void *event_info __UNUSED__)
257 {
258    Grid_Item *git = data;
259
260    if (git->want)
261      {
262         git->want = 0;
263         evas_object_show(git->img);
264         git->have = 1;
265         git->wd->preload_num--;
266         if (!git->wd->preload_num)
267           {
268              edje_object_signal_emit(elm_smart_scroller_edje_object_get(git->wd->scr),
269                                      "elm,state,busy,stop", "elm");
270              evas_object_smart_callback_call(git->wd->obj, SIG_LOADED_DETAIL, NULL);
271           }
272      }
273 }
274
275 static int
276 grid_zoom_calc(double zoom)
277 {
278    int z = zoom;
279    if (z < 1) z = 1;
280    return nearest_pow2(z);
281 }
282
283 static Grid *
284 grid_create(Evas_Object *obj)
285 {
286    Widget_Data *wd = elm_widget_data_get(obj);
287    int x, y;
288    Grid *g;
289
290    if (!wd) return NULL;
291    g = calloc(1, sizeof(Grid));
292    if (!g) return NULL;
293
294    g->zoom = grid_zoom_calc(wd->zoom);
295    g->tsize = wd->tsize;
296    g->iw = wd->size.imw;
297    g->ih = wd->size.imh;
298
299    g->w = g->iw / g->zoom;
300    g->h = g->ih / g->zoom;
301    if (g->zoom >= 8)
302      {
303         free(g);
304         return NULL;
305      }
306    g->gw = (g->w + g->tsize - 1) / g->tsize;
307    g->gh = (g->h + g->tsize - 1) / g->tsize;
308    g->grid = calloc(1, sizeof(Grid_Item) * g->gw * g->gh);
309    if (!g->grid)
310      {
311         g->gw = 0;
312         g->gh = 0;
313         return g;
314      }
315    for (y = 0; y < g->gh; y++)
316      {
317         for (x = 0; x < g->gw; x++)
318           {
319              int tn;
320
321              tn = (y * g->gw) + x;
322              g->grid[tn].src.x = x * g->tsize;
323              if (x == (g->gw - 1))
324                g->grid[tn].src.w = g->w - ((g->gw - 1) * g->tsize);
325              else
326                g->grid[tn].src.w = g->tsize;
327              g->grid[tn].src.y = y * g->tsize;
328              if (y == (g->gh - 1))
329                g->grid[tn].src.h = g->h - ((g->gh - 1) * g->tsize);
330              else
331                g->grid[tn].src.h = g->tsize;
332
333              g->grid[tn].out.x = g->grid[tn].src.x;
334              g->grid[tn].out.y = g->grid[tn].src.y;
335              g->grid[tn].out.w = g->grid[tn].src.w;
336              g->grid[tn].out.h = g->grid[tn].src.h;
337
338              g->grid[tn].wd = wd;
339              g->grid[tn].img =
340                 evas_object_image_add(evas_object_evas_get(obj));
341              evas_object_image_load_orientation_set(g->grid[tn].img, EINA_TRUE);
342              evas_object_image_scale_hint_set
343                 (g->grid[tn].img, EVAS_IMAGE_SCALE_HINT_DYNAMIC);
344              evas_object_pass_events_set(g->grid[tn].img, EINA_TRUE);
345              evas_object_smart_member_add(g->grid[tn].img,
346                                           wd->pan_smart);
347              elm_widget_sub_object_add(obj, g->grid[tn].img);
348              evas_object_image_filled_set(g->grid[tn].img, 1);
349              evas_object_event_callback_add(g->grid[tn].img,
350                                             EVAS_CALLBACK_IMAGE_PRELOADED,
351                                             _tile_preloaded,
352                                             &(g->grid[tn]));
353           }
354      }
355    return g;
356 }
357
358 static void
359 grid_load(Evas_Object *obj, Grid *g)
360 {
361    Widget_Data *wd = elm_widget_data_get(obj);
362    int x, y;
363    Evas_Coord ox, oy, ow, oh, cvx, cvy, cvw, cvh, gw, gh, tx, ty;
364    if (!wd) return;
365    evas_object_geometry_get(wd->pan_smart, &ox, &oy, &ow, &oh);
366    evas_output_viewport_get(evas_object_evas_get(wd->obj), &cvx, &cvy, &cvw, &cvh);
367    gw = wd->size.w;
368    gh = wd->size.h;
369    for (y = 0; y < g->gh; y++)
370      {
371         for (x = 0; x < g->gw; x++)
372           {
373              int tn, xx, yy, ww, hh;
374              Eina_Bool visible = EINA_FALSE;
375
376              tn = (y * g->gw) + x;
377              xx = g->grid[tn].out.x;
378              yy = g->grid[tn].out.y;
379              ww = g->grid[tn].out.w;
380              hh = g->grid[tn].out.h;
381              if ((gw != g->w) && (g->w > 0))
382                {
383                   tx = xx;
384                   xx = (gw * xx) / g->w;
385                   ww = ((gw * (tx + ww)) / g->w) - xx;
386                }
387              if ((gh != g->h) && (g->h > 0))
388                {
389                   ty = yy;
390                   yy = (gh * yy) / g->h;
391                   hh = ((gh * (ty + hh)) / g->h) - yy;
392                }
393              if (ELM_RECTS_INTERSECT(xx - wd->pan_x + ox,
394                                      yy  - wd->pan_y + oy,
395                                      ww, hh,
396                                      cvx, cvy, cvw, cvh))
397                visible = 1;
398              if ((visible) && (!g->grid[tn].have) && (!g->grid[tn].want))
399                {
400                   g->grid[tn].want = 1;
401                   evas_object_hide(g->grid[tn].img);
402                   evas_object_image_file_set(g->grid[tn].img, NULL, NULL);
403                   evas_object_image_load_scale_down_set(g->grid[tn].img, g->zoom);
404                   evas_object_image_load_region_set(g->grid[tn].img,
405                                                     g->grid[tn].src.x,
406                                                     g->grid[tn].src.y,
407                                                     g->grid[tn].src.w,
408                                                     g->grid[tn].src.h);
409                   evas_object_image_file_set(g->grid[tn].img, wd->file, NULL);
410                   evas_object_image_preload(g->grid[tn].img, 0);
411                   wd->preload_num++;
412                   if (wd->preload_num == 1)
413                     {
414                        edje_object_signal_emit(elm_smart_scroller_edje_object_get(wd->scr),
415                                                "elm,state,busy,start", "elm");
416                        evas_object_smart_callback_call(obj, SIG_LOAD_DETAIL, NULL);
417                     }
418                }
419              else if ((g->grid[tn].want) && (!visible))
420                {
421                   wd->preload_num--;
422                   if (!wd->preload_num)
423                     {
424                        edje_object_signal_emit(elm_smart_scroller_edje_object_get(wd->scr),
425                                                "elm,state,busy,stop", "elm");
426                        evas_object_smart_callback_call(obj, SIG_LOADED_DETAIL, NULL);
427                     }
428                   g->grid[tn].want = 0;
429                   evas_object_hide(g->grid[tn].img);
430                   evas_object_image_preload(g->grid[tn].img, 1);
431                   evas_object_image_file_set(g->grid[tn].img, NULL, NULL);
432                }
433              else if ((g->grid[tn].have) && (!visible))
434                {
435                   g->grid[tn].have = 0;
436                   evas_object_hide(g->grid[tn].img);
437                   evas_object_image_preload(g->grid[tn].img, 1);
438                   evas_object_image_file_set(g->grid[tn].img, NULL, NULL);
439                }
440           }
441      }
442 }
443
444 static void
445 grid_clearall(Evas_Object *obj)
446 {
447    Widget_Data *wd = elm_widget_data_get(obj);
448    Grid *g;
449    if (!wd) return;
450    EINA_LIST_FREE(wd->grids, g)
451      {
452         grid_clear(obj, g);
453         free(g);
454      }
455 }
456
457 static void
458 _smooth_update(Evas_Object *obj)
459 {
460    Widget_Data *wd = elm_widget_data_get(obj);
461    int x, y;
462    Eina_List *l;
463    Grid *g;
464    if (!wd) return;
465    EINA_LIST_FOREACH(wd->grids, l, g)
466      {
467         for (y = 0; y < g->gh; y++)
468           {
469              for (x = 0; x < g->gw; x++)
470                {
471                   int tn;
472
473                   tn = (y * g->gw) + x;
474                   evas_object_image_smooth_scale_set(g->grid[tn].img, (!wd->nosmooth));
475                }
476           }
477      }
478    evas_object_image_smooth_scale_set(wd->img, (!wd->nosmooth));
479 }
480
481 static void
482 _grid_raise(Grid *g)
483 {
484    int x, y;
485
486    for (y = 0; y < g->gh; y++)
487      {
488         for (x = 0; x < g->gw; x++)
489           {
490              int tn;
491
492              tn = (y * g->gw) + x;
493              evas_object_raise(g->grid[tn].img);
494           }
495      }
496 }
497
498 static Eina_Bool
499 _scr_timeout(void *data)
500 {
501    Widget_Data *wd = elm_widget_data_get(data);
502    if (!wd) return ECORE_CALLBACK_CANCEL;
503    wd->nosmooth--;
504    if (!wd->nosmooth) _smooth_update(data);
505    wd->scr_timer = NULL;
506    return ECORE_CALLBACK_CANCEL;
507 }
508
509 static void
510 _scr(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
511 {
512    Widget_Data *wd = elm_widget_data_get(data);
513    if (!wd) return;
514    if (!wd->scr_timer)
515      {
516         wd->nosmooth++;
517         if (wd->nosmooth == 1) _smooth_update(data);
518      }
519    if (wd->scr_timer) ecore_timer_del(wd->scr_timer);
520    wd->scr_timer = ecore_timer_add(0.5, _scr_timeout, data);
521 }
522
523 static void
524 _main_preloaded(void *data, Evas *e __UNUSED__, Evas_Object *o __UNUSED__, void *event_info __UNUSED__)
525 {
526    Evas_Object *obj = data;
527    Widget_Data *wd = elm_widget_data_get(obj);
528    Grid *g;
529    if (!wd) return;
530    evas_object_show(wd->img);
531    wd->main_load_pending = 0;
532    g = grid_create(obj);
533    if (g)
534      {
535         wd->grids = eina_list_prepend(wd->grids, g);
536         grid_load(wd->obj, g);
537      }
538    if (wd->calc_job) ecore_job_del(wd->calc_job);
539    wd->calc_job = ecore_job_add(_calc_job, wd);
540    evas_object_smart_callback_call(data, SIG_LOADED, NULL);
541    wd->preload_num--;
542    if (!wd->preload_num)
543      {
544         edje_object_signal_emit(elm_smart_scroller_edje_object_get(wd->scr),
545                                 "elm,state,busy,stop", "elm");
546         evas_object_smart_callback_call(obj, SIG_LOADED_DETAIL, NULL);
547      }
548 }
549
550 static Eina_Bool
551 zoom_do(Evas_Object *obj, double t)
552 {
553    Widget_Data *wd = elm_widget_data_get(obj);
554    Evas_Coord xx, yy, ow, oh;
555    if (!wd) return ECORE_CALLBACK_CANCEL;
556    wd->size.w = (wd->size.ow * (1.0 - t)) + (wd->size.nw * t);
557    wd->size.h = (wd->size.oh * (1.0 - t)) + (wd->size.nh * t);
558    elm_smart_scroller_child_viewport_size_get(wd->scr, &ow, &oh);
559    xx = (wd->size.spos.x * wd->size.w) - (ow / 2);
560    yy = (wd->size.spos.y * wd->size.h) - (oh / 2);
561    if (xx < 0) xx = 0;
562    else if (xx > (wd->size.w - ow)) xx = wd->size.w - ow;
563    if (yy < 0) yy = 0;
564    else if (yy > (wd->size.h - oh)) yy = wd->size.h - oh;
565
566    wd->show.show = EINA_TRUE;
567    wd->show.x = xx;
568    wd->show.y = yy;
569    wd->show.w = ow;
570    wd->show.h = oh;
571
572    if (wd->calc_job) ecore_job_del(wd->calc_job);
573    wd->calc_job = ecore_job_add(_calc_job, wd);
574    if (t >= 1.0)
575      {
576         Eina_List *l, *l_next;
577         Grid *g;
578
579         EINA_LIST_FOREACH_SAFE(wd->grids, l, l_next, g)
580           {
581              if (g->dead)
582                {
583                   wd->grids = eina_list_remove_list(wd->grids, l);
584                   grid_clear(obj, g);
585                   free(g);
586                }
587           }
588         return ECORE_CALLBACK_CANCEL;
589      }
590    return ECORE_CALLBACK_RENEW;
591 }
592
593
594 static Eina_Bool
595 _zoom_anim(void *data)
596 {
597    Evas_Object *obj = data;
598    Widget_Data *wd = elm_widget_data_get(obj);
599    double t;
600    Eina_Bool go;
601    if (!wd) return ECORE_CALLBACK_CANCEL;
602    t = ecore_loop_time_get();
603    if (t >= wd->t_end)
604      t = 1.0;
605    else if (wd->t_end > wd->t_start)
606      t = (t - wd->t_start) / (wd->t_end - wd->t_start);
607    else
608      t = 1.0;
609    t = 1.0 - t;
610    t = 1.0 - (t * t);
611    go = zoom_do(obj, t);
612    if (!go)
613      {
614         wd->nosmooth--;
615         if (!wd->nosmooth) _smooth_update(data);
616         wd->zoom_animator = NULL;
617         evas_object_smart_callback_call(obj, SIG_ZOOM_STOP, NULL);
618      }
619    return go;
620 }
621
622 static void
623 _mouse_move(void *data, Evas *evas __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
624 {
625    Widget_Data *wd = elm_widget_data_get(data);
626    //   Evas_Event_Mouse_Move *ev = event_info;
627    if (!wd) return;
628 }
629
630 static Eina_Bool
631 _long_press(void *data)
632 {
633    Widget_Data *wd = elm_widget_data_get(data);
634    if (!wd) return ECORE_CALLBACK_CANCEL;
635    wd->long_timer = NULL;
636    wd->longpressed = EINA_TRUE;
637    evas_object_smart_callback_call(data, SIG_LONGPRESSED, NULL);
638    return ECORE_CALLBACK_CANCEL;
639 }
640
641 static void
642 _mouse_down(void *data, Evas *evas __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
643 {
644    Widget_Data *wd = elm_widget_data_get(data);
645    Evas_Event_Mouse_Down *ev = event_info;
646    if (!wd) return;
647    if (ev->button != 1) return;
648    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) wd->on_hold = EINA_TRUE;
649    else wd->on_hold = EINA_FALSE;
650    if (ev->flags & EVAS_BUTTON_DOUBLE_CLICK)
651      evas_object_smart_callback_call(data, SIG_CLICKED_DOUBLE, NULL);
652    else
653      evas_object_smart_callback_call(data, SIG_PRESS, NULL);
654    wd->longpressed = EINA_FALSE;
655    if (wd->long_timer) ecore_timer_del(wd->long_timer);
656    wd->long_timer = ecore_timer_add(_elm_config->longpress_timeout, _long_press, data);
657 }
658
659 static void
660 _mouse_up(void *data, Evas *evas __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
661 {
662    Widget_Data *wd = elm_widget_data_get(data);
663    Evas_Event_Mouse_Up *ev = event_info;
664    if (!wd) return;
665    if (ev->button != 1) return;
666    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) wd->on_hold = EINA_TRUE;
667    else wd->on_hold = EINA_FALSE;
668    if (wd->long_timer)
669      {
670         ecore_timer_del(wd->long_timer);
671         wd->long_timer = NULL;
672      }
673    if (!wd->on_hold)
674      evas_object_smart_callback_call(data, SIG_CLICKED, NULL);
675    wd->on_hold = EINA_FALSE;
676 }
677
678 static Evas_Smart_Class _pan_sc = EVAS_SMART_CLASS_INIT_NULL;
679
680 static void
681 _del_hook(Evas_Object *obj)
682 {
683    Widget_Data *wd = elm_widget_data_get(obj);
684    Grid *g;
685    if (!wd) return;
686    EINA_LIST_FREE(wd->grids, g)
687      {
688         if (g->grid) free(g->grid);
689         free(g);
690      }
691    evas_object_del(wd->pan_smart);
692    wd->pan_smart = NULL;
693    if (wd->file) eina_stringshare_del(wd->file);
694    if (wd->calc_job) ecore_job_del(wd->calc_job);
695    if (wd->scr_timer) ecore_timer_del(wd->scr_timer);
696    if (wd->zoom_animator) ecore_animator_del(wd->zoom_animator);
697    if (wd->long_timer) ecore_timer_del(wd->long_timer);
698    free(wd);
699 }
700
701 static void
702 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
703 {
704    Widget_Data *wd = elm_widget_data_get(obj);
705    if (!wd) return;
706    if (elm_widget_focus_get(obj))
707      {
708         edje_object_signal_emit(elm_smart_scroller_edje_object_get(wd->scr), "elm,action,focus", "elm");
709         evas_object_focus_set(wd->obj, EINA_TRUE);
710      }
711    else
712      {
713         edje_object_signal_emit(elm_smart_scroller_edje_object_get(wd->scr), "elm,action,unfocus", "elm");
714         evas_object_focus_set(wd->obj, EINA_FALSE);
715      }
716 }
717
718 static void
719 _theme_hook(Evas_Object *obj)
720 {
721    Widget_Data *wd = elm_widget_data_get(obj);
722    if (!wd) return;
723    elm_smart_scroller_object_theme_set(obj, wd->scr, "photocam", "base", elm_widget_style_get(obj));
724    //   edje_object_scale_set(wd->scr, elm_widget_scale_get(obj) * _elm_config->scale);
725    _sizing_eval(obj);
726 }
727
728 /*
729 static void
730 _show_region_hook(void *data, Evas_Object *obj)
731 {
732    Widget_Data *wd = elm_widget_data_get(data);
733    Evas_Coord x, y, w, h;
734    if (!wd) return;
735    elm_widget_show_region_get(obj, &x, &y, &w, &h);
736    elm_smart_scroller_child_region_show(wd->scr, x, y, w, h);
737 }
738 */
739
740 static void
741 _sizing_eval(Evas_Object *obj)
742 {
743    Widget_Data *wd = elm_widget_data_get(obj);
744    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
745    if (!wd) return;
746    //   evas_object_size_hint_min_get(wd->scr, &minw, &minh);
747    evas_object_size_hint_max_get(wd->scr, &maxw, &maxh);
748    //   minw = -1;
749    //   minh = -1;
750    //   if (wd->mode != ELM_LIST_LIMIT) minw = -1;
751    evas_object_size_hint_min_set(obj, minw, minh);
752    evas_object_size_hint_max_set(obj, maxw, maxh);
753 }
754
755 static void
756 _calc_job(void *data)
757 {
758    Widget_Data *wd = data;
759    Evas_Coord minw, minh;
760    if (!wd) return;
761    minw = wd->size.w;
762    minh = wd->size.h;
763    if (wd->resized)
764      {
765         wd->resized = 0;
766         if (wd->mode != ELM_PHOTOCAM_ZOOM_MODE_MANUAL)
767           {
768              double tz = wd->zoom;
769              wd->zoom = 0.0;
770              elm_photocam_zoom_set(wd->obj, tz);
771           }
772      }
773    if ((minw != wd->minw) || (minh != wd->minh))
774      {
775         wd->minw = minw;
776         wd->minh = minh;
777         evas_object_smart_callback_call(wd->pan_smart, "changed", NULL);
778         _sizing_eval(wd->obj);
779      }
780    wd->calc_job = NULL;
781    evas_object_smart_changed(wd->pan_smart);
782 }
783
784 static void
785 _pan_set(Evas_Object *obj, Evas_Coord x, Evas_Coord y)
786 {
787    Pan *sd = evas_object_smart_data_get(obj);
788    if (!sd) return;
789    if ((x == sd->wd->pan_x) && (y == sd->wd->pan_y)) return;
790    sd->wd->pan_x = x;
791    sd->wd->pan_y = y;
792    evas_object_smart_changed(obj);
793 }
794
795 static void
796 _pan_get(Evas_Object *obj, Evas_Coord *x, Evas_Coord *y)
797 {
798    Pan *sd = evas_object_smart_data_get(obj);
799    if (!sd) return;
800    if (x) *x = sd->wd->pan_x;
801    if (y) *y = sd->wd->pan_y;
802 }
803
804 static void
805 _pan_max_get(Evas_Object *obj, Evas_Coord *x, Evas_Coord *y)
806 {
807    Pan *sd = evas_object_smart_data_get(obj);
808    Evas_Coord ow, oh;
809    if (!sd) return;
810    evas_object_geometry_get(obj, NULL, NULL, &ow, &oh);
811    ow = sd->wd->minw - ow;
812    if (ow < 0) ow = 0;
813    oh = sd->wd->minh - oh;
814    if (oh < 0) oh = 0;
815    if (x) *x = ow;
816    if (y) *y = oh;
817 }
818
819 static void
820 _pan_min_get(Evas_Object *obj __UNUSED__, Evas_Coord *x, Evas_Coord *y)
821 {
822    if (x) *x = 0;
823    if (y) *y = 0;
824 }
825
826 static void
827 _pan_child_size_get(Evas_Object *obj, Evas_Coord *w, Evas_Coord *h)
828 {
829    Pan *sd = evas_object_smart_data_get(obj);
830    if (!sd) return;
831    if (w) *w = sd->wd->minw;
832    if (h) *h = sd->wd->minh;
833 }
834
835 static void
836 _pan_add(Evas_Object *obj)
837 {
838    Pan *sd;
839    Evas_Object_Smart_Clipped_Data *cd;
840    _pan_sc.add(obj);
841    cd = evas_object_smart_data_get(obj);
842    if (!cd) return;
843    sd = calloc(1, sizeof(Pan));
844    if (!sd) return;
845    sd->__clipped_data = *cd;
846    free(cd);
847    evas_object_smart_data_set(obj, sd);
848 }
849
850 static void
851 _pan_del(Evas_Object *obj)
852 {
853    Pan *sd = evas_object_smart_data_get(obj);
854    if (!sd) return;
855    _pan_sc.del(obj);
856 }
857
858 static void
859 _pan_resize(Evas_Object *obj, Evas_Coord w, Evas_Coord h)
860 {
861    Pan *sd = evas_object_smart_data_get(obj);
862    Evas_Coord ow, oh;
863    if (!sd) return;
864    evas_object_geometry_get(obj, NULL, NULL, &ow, &oh);
865    if ((ow == w) && (oh == h)) return;
866    sd->wd->resized = 1;
867    if (sd->wd->calc_job) ecore_job_del(sd->wd->calc_job);
868    sd->wd->calc_job = ecore_job_add(_calc_job, sd->wd);
869 }
870
871 static void
872 _pan_calculate(Evas_Object *obj)
873 {
874    Pan *sd = evas_object_smart_data_get(obj);
875    Evas_Coord ox, oy, ow, oh;
876    Eina_List *l;
877    Grid *g;
878    if (!sd) return;
879    evas_object_geometry_get(obj, &ox, &oy, &ow, &oh);
880    img_place(sd->wd->obj, sd->wd->pan_x, sd->wd->pan_y, ox, oy, ow, oh);
881    EINA_LIST_FOREACH(sd->wd->grids, l, g)
882      {
883         grid_load(sd->wd->obj, g);
884         grid_place(sd->wd->obj, g, sd->wd->pan_x, sd->wd->pan_y, ox, oy, ow, oh);
885      }
886 }
887
888 static void
889 _pan_move(Evas_Object *obj, Evas_Coord x __UNUSED__, Evas_Coord y __UNUSED__)
890 {
891    Pan *sd = evas_object_smart_data_get(obj);
892    if (!sd) return;
893    if (sd->wd->calc_job) ecore_job_del(sd->wd->calc_job);
894    sd->wd->calc_job = ecore_job_add(_calc_job, sd->wd);
895 }
896
897 static void
898 _hold_on(void *data __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
899 {
900    Widget_Data *wd = elm_widget_data_get(obj);
901    if (!wd) return;
902    elm_smart_scroller_hold_set(wd->scr, 1);
903 }
904
905 static void
906 _hold_off(void *data __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
907 {
908    Widget_Data *wd = elm_widget_data_get(obj);
909    if (!wd) return;
910    elm_smart_scroller_hold_set(wd->scr, 0);
911 }
912
913 static void
914 _freeze_on(void *data __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
915 {
916    Widget_Data *wd = elm_widget_data_get(obj);
917    if (!wd) return;
918    elm_smart_scroller_freeze_set(wd->scr, 1);
919 }
920
921 static void
922 _freeze_off(void *data __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
923 {
924    Widget_Data *wd = elm_widget_data_get(obj);
925    if (!wd) return;
926    elm_smart_scroller_freeze_set(wd->scr, 0);
927 }
928
929 static void
930 _scr_anim_start(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
931 {
932    evas_object_smart_callback_call(data, SIG_SCROLL_ANIM_START, NULL);
933 }
934
935 static void
936 _scr_anim_stop(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
937 {
938    evas_object_smart_callback_call(data, SIG_SCROLL_ANIM_STOP, NULL);
939 }
940
941 static void
942 _scr_drag_start(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
943 {
944    evas_object_smart_callback_call(data, SIG_SCROLL_DRAG_START, NULL);
945 }
946
947 static void
948 _scr_drag_stop(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
949 {
950    evas_object_smart_callback_call(data, SIG_SCROLL_DRAG_STOP, NULL);
951 }
952
953 static void
954 _scr_scroll(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
955 {
956    evas_object_smart_callback_call(data, SIG_SCROLL, NULL);
957 }
958
959 static Eina_Bool
960 _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__,
961             Evas_Callback_Type type, void *event_info)
962 {
963    double zoom;
964    if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
965    Evas_Event_Key_Down *ev = event_info;
966    Widget_Data *wd = elm_widget_data_get(obj);
967    if (!wd) return EINA_FALSE;
968    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
969
970    Evas_Coord x = 0;
971    Evas_Coord y = 0;
972    Evas_Coord step_x = 0;
973    Evas_Coord step_y = 0;
974    Evas_Coord v_w = 0;
975    Evas_Coord v_h = 0;
976    Evas_Coord page_x = 0;
977    Evas_Coord page_y = 0;
978
979    elm_smart_scroller_child_pos_get(wd->scr, &x, &y);
980    elm_smart_scroller_step_size_get(wd->scr, &step_x, &step_y);
981    elm_smart_scroller_page_size_get(wd->scr, &page_x, &page_y);
982    elm_smart_scroller_child_viewport_size_get(wd->scr, &v_w, &v_h);
983
984    if ((!strcmp(ev->keyname, "Left")) ||
985        (!strcmp(ev->keyname, "KP_Left")))
986      {
987         x -= step_x;
988      }
989    else if ((!strcmp(ev->keyname, "Right")) ||
990             (!strcmp(ev->keyname, "KP_Right")))
991      {
992         x += step_x;
993      }
994    else if ((!strcmp(ev->keyname, "Up"))  ||
995             (!strcmp(ev->keyname, "KP_Up")))
996      {
997         y -= step_y;
998      }
999    else if ((!strcmp(ev->keyname, "Down")) ||
1000             (!strcmp(ev->keyname, "KP_Down")))
1001      {
1002         y += step_y;
1003      }
1004    else if ((!strcmp(ev->keyname, "Prior")) ||
1005             (!strcmp(ev->keyname, "KP_Prior")))
1006      {
1007         if (page_y < 0)
1008           y -= -(page_y * v_h) / 100;
1009         else
1010           y -= page_y;
1011      }
1012    else if ((!strcmp(ev->keyname, "Next")) ||
1013             (!strcmp(ev->keyname, "KP_Next")))
1014      {
1015         if (page_y < 0)
1016           y += -(page_y * v_h) / 100;
1017         else
1018           y += page_y;
1019      }
1020    else if ((!strcmp(ev->keyname, "KP_Add")))
1021      {
1022         zoom = elm_photocam_zoom_get(obj);
1023         zoom -= 0.5;
1024         elm_photocam_zoom_mode_set(obj, ELM_PHOTOCAM_ZOOM_MODE_MANUAL);
1025         elm_photocam_zoom_set(obj, zoom);
1026         return EINA_TRUE;
1027      }
1028    else if ((!strcmp(ev->keyname, "KP_Subtract")))
1029      {
1030         zoom = elm_photocam_zoom_get(obj);
1031         zoom += 0.5;
1032         elm_photocam_zoom_mode_set(obj, ELM_PHOTOCAM_ZOOM_MODE_MANUAL);
1033         elm_photocam_zoom_set(obj, zoom);
1034         return EINA_TRUE;
1035      }
1036    else return EINA_FALSE;
1037
1038    ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
1039    elm_smart_scroller_child_pos_set(wd->scr, x, y);
1040
1041    return EINA_TRUE;
1042 }
1043
1044 EAPI Evas_Object *
1045 elm_photocam_add(Evas_Object *parent)
1046 {
1047    Evas_Object *obj;
1048    Evas *e;
1049    Widget_Data *wd;
1050    Evas_Coord minw, minh;
1051    static Evas_Smart *smart = NULL;
1052    Eina_Bool bounce = _elm_config->thumbscroll_bounce_enable;
1053
1054    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
1055
1056    ELM_SET_WIDTYPE(widtype, "photocam");
1057    elm_widget_type_set(obj, "photocam");
1058    elm_widget_sub_object_add(parent, obj);
1059    elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
1060    elm_widget_data_set(obj, wd);
1061    elm_widget_del_hook_set(obj, _del_hook);
1062    elm_widget_theme_hook_set(obj, _theme_hook);
1063    elm_widget_can_focus_set(obj, EINA_TRUE);
1064    elm_widget_event_hook_set(obj, _event_hook);
1065
1066    wd->scr = elm_smart_scroller_add(e);
1067    elm_smart_scroller_widget_set(wd->scr, obj);
1068    elm_smart_scroller_object_theme_set(obj, wd->scr, "photocam", "base", "default");
1069    evas_object_smart_callback_add(wd->scr, "scroll", _scr, obj);
1070    evas_object_smart_callback_add(wd->scr, "drag", _scr, obj);
1071    elm_widget_resize_object_set(obj, wd->scr);
1072
1073    evas_object_smart_callback_add(wd->scr, "animate,start", _scr_anim_start, obj);
1074    evas_object_smart_callback_add(wd->scr, "animate,stop", _scr_anim_stop, obj);
1075    evas_object_smart_callback_add(wd->scr, "drag,start", _scr_drag_start, obj);
1076    evas_object_smart_callback_add(wd->scr, "drag,stop", _scr_drag_stop, obj);
1077    evas_object_smart_callback_add(wd->scr, "scroll", _scr_scroll, obj);
1078
1079    elm_smart_scroller_bounce_allow_set(wd->scr, bounce, bounce);
1080
1081    wd->obj = obj;
1082
1083    evas_object_smart_callback_add(obj, "scroll-hold-on", _hold_on, obj);
1084    evas_object_smart_callback_add(obj, "scroll-hold-off", _hold_off, obj);
1085    evas_object_smart_callback_add(obj, "scroll-freeze-on", _freeze_on, obj);
1086    evas_object_smart_callback_add(obj, "scroll-freeze-off", _freeze_off, obj);
1087
1088    if (!smart)
1089      {
1090         static Evas_Smart_Class sc;
1091
1092         evas_object_smart_clipped_smart_set(&_pan_sc);
1093         sc = _pan_sc;
1094         sc.name = "elm_photocam_pan";
1095         sc.version = EVAS_SMART_CLASS_VERSION;
1096         sc.add = _pan_add;
1097         sc.del = _pan_del;
1098         sc.resize = _pan_resize;
1099         sc.move = _pan_move;
1100         sc.calculate = _pan_calculate;
1101         smart = evas_smart_class_new(&sc);
1102      }
1103    if (smart)
1104      {
1105         wd->pan_smart = evas_object_smart_add(e, smart);
1106         wd->pan = evas_object_smart_data_get(wd->pan_smart);
1107         wd->pan->wd = wd;
1108      }
1109
1110    elm_smart_scroller_extern_pan_set(wd->scr, wd->pan_smart,
1111                                      _pan_set, _pan_get, _pan_max_get,
1112                                      _pan_min_get, _pan_child_size_get);
1113
1114    wd->zoom = 1;
1115    wd->mode = ELM_PHOTOCAM_ZOOM_MODE_MANUAL;
1116
1117    wd->tsize = 512;
1118
1119    wd->img = evas_object_image_add(e);
1120    evas_object_image_load_orientation_set(wd->img, EINA_TRUE);
1121    evas_object_image_scale_hint_set(wd->img, EVAS_IMAGE_SCALE_HINT_DYNAMIC);
1122    evas_object_event_callback_add(wd->img, EVAS_CALLBACK_MOUSE_DOWN,
1123                                   _mouse_down, obj);
1124    evas_object_event_callback_add(wd->img, EVAS_CALLBACK_MOUSE_UP,
1125                                   _mouse_up, obj);
1126    evas_object_event_callback_add(wd->img, EVAS_CALLBACK_MOUSE_MOVE,
1127                                   _mouse_move, obj);
1128    evas_object_image_scale_hint_set(wd->img, EVAS_IMAGE_SCALE_HINT_STATIC);
1129    evas_object_smart_member_add(wd->img, wd->pan_smart);
1130    elm_widget_sub_object_add(obj, wd->img);
1131    evas_object_image_filled_set(wd->img, 1);
1132    evas_object_event_callback_add(wd->img, EVAS_CALLBACK_IMAGE_PRELOADED,
1133                                   _main_preloaded, obj);
1134
1135    edje_object_size_min_calc(elm_smart_scroller_edje_object_get(wd->scr),
1136                              &minw, &minh);
1137    evas_object_size_hint_min_set(obj, minw, minh);
1138
1139    evas_object_smart_callbacks_descriptions_set(obj, _signals);
1140
1141    _sizing_eval(obj);
1142    return obj;
1143 }
1144
1145 EAPI Evas_Load_Error
1146 elm_photocam_file_set(Evas_Object *obj, const char *file)
1147 {
1148    ELM_CHECK_WIDTYPE(obj, widtype) EVAS_LOAD_ERROR_NONE;
1149    Widget_Data *wd = elm_widget_data_get(obj);
1150    int w, h;
1151    if (!wd) return EVAS_LOAD_ERROR_GENERIC;
1152    if (!eina_stringshare_replace(&wd->file, file)) return EVAS_LOAD_ERROR_NONE;
1153    grid_clearall(obj);
1154
1155    evas_object_hide(wd->img);
1156    evas_object_image_smooth_scale_set(wd->img, (wd->nosmooth == 0));
1157    evas_object_image_file_set(wd->img, NULL, NULL);
1158    evas_object_image_load_scale_down_set(wd->img, 0);
1159    evas_object_image_file_set(wd->img, wd->file, NULL);
1160    evas_object_image_size_get(wd->img, &w, &h);
1161    wd->size.imw = w;
1162    wd->size.imh = h;
1163    wd->size.w = wd->size.imw / wd->zoom;
1164    wd->size.h = wd->size.imh / wd->zoom;
1165    if (wd->zoom_animator)
1166      {
1167         wd->nosmooth--;
1168         if (wd->nosmooth == 0) _smooth_update(obj);
1169         ecore_animator_del(wd->zoom_animator);
1170         wd->zoom_animator = NULL;
1171      }
1172    evas_object_image_file_set(wd->img, NULL, NULL);
1173    evas_object_image_load_scale_down_set(wd->img, 8);
1174    evas_object_image_file_set(wd->img, wd->file, NULL);
1175    evas_object_image_preload(wd->img, 0);
1176    wd->main_load_pending = 1;
1177    if (wd->calc_job) ecore_job_del(wd->calc_job);
1178    wd->calc_job = ecore_job_add(_calc_job, wd);
1179    evas_object_smart_callback_call(obj, SIG_LOAD, NULL);
1180    wd->preload_num++;
1181    if (wd->preload_num == 1)
1182      {
1183         edje_object_signal_emit(elm_smart_scroller_edje_object_get(wd->scr),
1184                                 "elm,state,busy,start", "elm");
1185         evas_object_smart_callback_call(obj, SIG_LOAD_DETAIL, NULL);
1186      }
1187      {
1188         double tz = wd->zoom;
1189         wd->zoom = 0.0;
1190         elm_photocam_zoom_set(wd->obj, tz);
1191      }
1192    return evas_object_image_load_error_get(wd->img);
1193 }
1194
1195 EAPI const char *
1196 elm_photocam_file_get(const Evas_Object *obj)
1197 {
1198    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1199    Widget_Data *wd = elm_widget_data_get(obj);
1200    if (!wd) return NULL;
1201    return wd->file;
1202 }
1203
1204 EAPI void
1205 elm_photocam_zoom_set(Evas_Object *obj, double zoom)
1206 {
1207    ELM_CHECK_WIDTYPE(obj, widtype);
1208    Widget_Data *wd = elm_widget_data_get(obj);
1209    Eina_List *l;
1210    Grid *g, *g_zoom = NULL;
1211    Evas_Coord pw, ph, rx, ry, rw, rh;
1212    int z;
1213    int zoom_changed = 0, started = 0;
1214    Ecore_Animator *an;
1215    if (!wd) return;
1216    if (zoom <= (1.0 / 256.0)) zoom = (1.0 / 256.0);
1217    if (zoom == wd->zoom) return;
1218    wd->zoom = zoom;
1219    wd->size.ow = wd->size.w;
1220    wd->size.oh = wd->size.h;
1221    elm_smart_scroller_child_pos_get(wd->scr, &rx, &ry);
1222    elm_smart_scroller_child_viewport_size_get(wd->scr, &rw, &rh);
1223    if ((rw <= 0) || (rh <= 0)) return;
1224
1225    if (wd->mode == ELM_PHOTOCAM_ZOOM_MODE_MANUAL)
1226      {
1227         wd->size.nw = (double)wd->size.imw / wd->zoom;
1228         wd->size.nh = (double)wd->size.imh / wd->zoom;
1229      }
1230    else if (wd->mode == ELM_PHOTOCAM_ZOOM_MODE_AUTO_FIT)
1231      {
1232         if ((wd->size.imw < 1) || (wd->size.imh < 1))
1233           {
1234              wd->size.nw = 0;
1235              wd->size.nh = 0;
1236           }
1237         else
1238           {
1239              ph = (wd->size.imh * rw) / wd->size.imw;
1240              if (ph > rh)
1241                {
1242                   pw = (wd->size.imw * rh) / wd->size.imh;
1243                   ph = rh;
1244                }
1245              else
1246                {
1247                   pw = rw;
1248                }
1249              if (wd->size.imw > wd->size.imh)
1250                z = wd->size.imw / pw;
1251              else
1252                z = wd->size.imh / ph;
1253              if      (z >= 8) z = 8;
1254              else if (z >= 4) z = 4;
1255              else if (z >= 2) z = 2;
1256              else             z = 1;
1257              if (z != wd->zoom) zoom_changed = 1;
1258              wd->zoom = z;
1259              wd->size.nw = pw;
1260              wd->size.nh = ph;
1261           }
1262      }
1263    else if (wd->mode == ELM_PHOTOCAM_ZOOM_MODE_AUTO_FILL)
1264      {
1265         if ((wd->size.imw < 1) || (wd->size.imh < 1))
1266           {
1267              wd->size.nw = 0;
1268              wd->size.nw = 0;
1269           }
1270         else
1271           {
1272              ph = (wd->size.imh * rw) / wd->size.imw;
1273              if (ph < rh)
1274                {
1275                   pw = (wd->size.imw * rh) / wd->size.imh;
1276                   ph = rh;
1277                }
1278              else
1279                {
1280                   pw = rw;
1281                }
1282              if (wd->size.imw > wd->size.imh)
1283                z = wd->size.imw / pw;
1284              else
1285                z = wd->size.imh / ph;
1286              if      (z >= 8) z = 8;
1287              else if (z >= 4) z = 4;
1288              else if (z >= 2) z = 2;
1289              else             z = 1;
1290              if (z != wd->zoom) zoom_changed = 1;
1291              wd->zoom = z;
1292              wd->size.nw = pw;
1293              wd->size.nh = ph;
1294           }
1295      }
1296    else if (wd->mode == ELM_PHOTOCAM_ZOOM_MODE_AUTO_FIT_IN)
1297      {
1298         if ((wd->size.imw < 1) || (wd->size.imh < 1))
1299           {
1300              wd->size.nw = 0;
1301              wd->size.nh = 0;
1302           }
1303         else if ((wd->size.imw < rw) && (wd->size.imh < rh))
1304           {
1305              if (1 != wd->zoom) zoom_changed = 1;
1306              wd->zoom = 1;
1307              wd->size.nw = wd->size.imw;
1308              wd->size.nh = wd->size.imh;
1309           }
1310         else
1311           {
1312              ph = (wd->size.imh * rw) / wd->size.imw;
1313              if (ph > rh)
1314                {
1315                   pw = (wd->size.imw * rh) / wd->size.imh;
1316                   ph = rh;
1317                }
1318              else
1319                pw = rw;
1320              if (wd->size.imw > wd->size.imh)
1321                z = wd->size.imw / pw;
1322              else
1323                z = wd->size.imh / ph;
1324              if (z != wd->zoom) zoom_changed = 1;
1325              wd->zoom = z;
1326              wd->size.nw = pw;
1327              wd->size.nh = ph;
1328           }
1329      }
1330    if (wd->main_load_pending)
1331      {
1332         wd->size.w = wd->size.nw;
1333         wd->size.h = wd->size.nh;
1334         goto done;
1335      }
1336    EINA_LIST_FOREACH(wd->grids, l, g)
1337      {
1338         if (g->zoom == grid_zoom_calc(wd->zoom))
1339           {
1340              wd->grids = eina_list_remove(wd->grids, g);
1341              wd->grids = eina_list_prepend(wd->grids, g);
1342              _grid_raise(g);
1343              goto done;
1344           }
1345      }
1346    g = grid_create(obj);
1347    if (g)
1348      {
1349         if (eina_list_count(wd->grids) > 1)
1350           {
1351              g_zoom = eina_list_last(wd->grids)->data;
1352              wd->grids = eina_list_remove(wd->grids, g_zoom);
1353              grid_clear(obj, g_zoom);
1354              free(g_zoom);
1355              EINA_LIST_FOREACH(wd->grids, l, g_zoom)
1356                {
1357                   g_zoom->dead = 1;
1358                }
1359           }
1360         wd->grids = eina_list_prepend(wd->grids, g);
1361      }
1362    else
1363      {
1364         EINA_LIST_FREE(wd->grids, g)
1365           {
1366              grid_clear(obj, g);
1367              free(g);
1368           }
1369      }
1370 done:
1371    wd->t_start = ecore_loop_time_get();
1372    wd->t_end = wd->t_start + _elm_config->zoom_friction;
1373    if ((wd->size.w > 0) && (wd->size.h > 0))
1374      {
1375         wd->size.spos.x = (double)(rx + (rw / 2)) / (double)wd->size.w;
1376         wd->size.spos.y = (double)(ry + (rh / 2)) / (double)wd->size.h;
1377      }
1378    else
1379      {
1380         wd->size.spos.x = 0.5;
1381         wd->size.spos.y = 0.5;
1382      }
1383    if (rw > wd->size.w) wd->size.spos.x = 0.5;
1384    if (rh > wd->size.h) wd->size.spos.y = 0.5;
1385    if (wd->size.spos.x > 1.0) wd->size.spos.x = 1.0;
1386    if (wd->size.spos.y > 1.0) wd->size.spos.y = 1.0;
1387    if (wd->paused)
1388      {
1389         zoom_do(obj, 1.0);
1390      }
1391    else
1392      {
1393         if (!wd->zoom_animator)
1394           {
1395              wd->zoom_animator = ecore_animator_add(_zoom_anim, obj);
1396              wd->nosmooth++;
1397              if (wd->nosmooth == 1) _smooth_update(obj);
1398              started = 1;
1399           }
1400      }
1401    an = wd->zoom_animator;
1402    if (an)
1403      {
1404         if (!_zoom_anim(obj))
1405           {
1406              ecore_animator_del(an);
1407              an = NULL;
1408           }
1409      }
1410    if (wd->calc_job) ecore_job_del(wd->calc_job);
1411    wd->calc_job = ecore_job_add(_calc_job, wd);
1412    if (!wd->paused)
1413      {
1414         if (started)
1415           evas_object_smart_callback_call(obj, SIG_ZOOM_START, NULL);
1416         if (!an)
1417           evas_object_smart_callback_call(obj, SIG_ZOOM_STOP, NULL);
1418      }
1419    if (zoom_changed)
1420      evas_object_smart_callback_call(obj, SIG_ZOOM_CHANGE, NULL);
1421 }
1422
1423 EAPI double
1424 elm_photocam_zoom_get(const Evas_Object *obj)
1425 {
1426    ELM_CHECK_WIDTYPE(obj, widtype) 1.0;
1427    Widget_Data *wd = elm_widget_data_get(obj);
1428    if (!wd) return 1.0;
1429    return wd->zoom;
1430 }
1431
1432 EAPI void
1433 elm_photocam_zoom_mode_set(Evas_Object *obj, Elm_Photocam_Zoom_Mode mode)
1434 {
1435    ELM_CHECK_WIDTYPE(obj, widtype);
1436    Widget_Data *wd = elm_widget_data_get(obj);
1437    if (!wd) return;
1438    if (wd->mode == mode) return;
1439    wd->mode = mode;
1440      {
1441         double tz = wd->zoom;
1442         wd->zoom = 0.0;
1443         elm_photocam_zoom_set(wd->obj, tz);
1444      }
1445 }
1446
1447 EAPI Elm_Photocam_Zoom_Mode
1448 elm_photocam_zoom_mode_get(const Evas_Object *obj)
1449 {
1450    ELM_CHECK_WIDTYPE(obj, widtype) ELM_PHOTOCAM_ZOOM_MODE_LAST;
1451    Widget_Data *wd = elm_widget_data_get(obj);
1452    if (!wd) return ELM_PHOTOCAM_ZOOM_MODE_LAST;
1453    return wd->mode;
1454 }
1455
1456 EAPI void
1457 elm_photocam_image_size_get(const Evas_Object *obj, int *w, int *h)
1458 {
1459    ELM_CHECK_WIDTYPE(obj, widtype);
1460    Widget_Data *wd = elm_widget_data_get(obj);
1461    if (!wd) return;
1462    if (w) *w = wd->size.imw;
1463    if (h) *h = wd->size.imh;
1464 }
1465
1466 EAPI void
1467 elm_photocam_region_get(const Evas_Object *obj, int *x, int *y, int *w, int *h)
1468 {
1469    ELM_CHECK_WIDTYPE(obj, widtype);
1470    Widget_Data *wd = elm_widget_data_get(obj);
1471    Evas_Coord sx, sy, sw, sh;
1472    if (!wd) return;
1473    elm_smart_scroller_child_pos_get(wd->scr, &sx, &sy);
1474    elm_smart_scroller_child_viewport_size_get(wd->scr, &sw, &sh);
1475    if (wd->size.w > 0)
1476      {
1477         if (x)
1478           {
1479              *x = (wd->size.imw * sx) / wd->size.w;
1480              if (*x > wd->size.imw) *x = wd->size.imw;
1481              else if (*x < 0) *x = 0;
1482           }
1483         if (w)
1484           {
1485              *w = (wd->size.imw * sw) / wd->size.w;
1486              if (*w > wd->size.imw) *w = wd->size.imw;
1487              else if (*w < 0) *w = 0;
1488           }
1489      }
1490    else
1491      {
1492         if (x) *x = 0;
1493         if (w) *w = 0;
1494      }
1495
1496    if (wd->size.h > 0)
1497      {
1498         if (y)
1499           {
1500              *y = (wd->size.imh * sy) / wd->size.h;
1501              if (*y > wd->size.imh) *y = wd->size.imh;
1502              else if (*y < 0) *y = 0;
1503           }
1504         if (h)
1505           {
1506              *h = (wd->size.imh * sh) / wd->size.h;
1507              if (*h > wd->size.imh) *h = wd->size.imh;
1508              else if (*h < 0) *h = 0;
1509           }
1510      }
1511    else
1512      {
1513         if (y) *y = 0;
1514         if (h) *h = 0;
1515      }
1516 }
1517
1518 EAPI void
1519 elm_photocam_image_region_show(Evas_Object *obj, int x, int y, int w, int h __UNUSED__)
1520 {
1521    ELM_CHECK_WIDTYPE(obj, widtype);
1522    Widget_Data *wd = elm_widget_data_get(obj);
1523    int rx, ry, rw, rh;
1524    if (!wd) return;
1525    if ((wd->size.imw < 1) || (wd->size.imh < 1)) return;
1526    rx = (x * wd->size.w) / wd->size.imw;
1527    ry = (y * wd->size.h) / wd->size.imh;
1528    rw = (w * wd->size.w) / wd->size.imw;
1529    rh = (h * wd->size.h) / wd->size.imh;
1530    if (rw < 1) rw = 1;
1531    if (rh < 1) rh = 1;
1532    if ((rx + rw) > wd->size.w) rx = wd->size.w - rw;
1533    if ((ry + rh) > wd->size.h) ry = wd->size.h - rh;
1534    if (wd->zoom_animator)
1535      {
1536         wd->nosmooth--;
1537         ecore_animator_del(wd->zoom_animator);
1538         wd->zoom_animator = NULL;
1539         zoom_do(obj, 1.0);
1540         evas_object_smart_callback_call(obj, SIG_ZOOM_STOP, NULL);
1541      }
1542    elm_smart_scroller_child_region_show(wd->scr, rx, ry, rw, rh);
1543 }
1544
1545 EAPI void
1546 elm_photocam_image_region_bring_in(Evas_Object *obj, int x, int y, int w, int h __UNUSED__)
1547 {
1548    ELM_CHECK_WIDTYPE(obj, widtype);
1549    Widget_Data *wd = elm_widget_data_get(obj);
1550    int rx, ry, rw, rh;
1551    if (!wd) return;
1552    if ((wd->size.imw < 1) || (wd->size.imh < 1)) return;
1553    rx = (x * wd->size.w) / wd->size.imw;
1554    ry = (y * wd->size.h) / wd->size.imh;
1555    rw = (w * wd->size.w) / wd->size.imw;
1556    rh = (h * wd->size.h) / wd->size.imh;
1557    if (rw < 1) rw = 1;
1558    if (rh < 1) rh = 1;
1559    if ((rx + rw) > wd->size.w) rx = wd->size.w - rw;
1560    if ((ry + rh) > wd->size.h) ry = wd->size.h - rh;
1561    if (wd->zoom_animator)
1562      {
1563         wd->nosmooth--;
1564         if (!wd->nosmooth) _smooth_update(obj);
1565         ecore_animator_del(wd->zoom_animator);
1566         wd->zoom_animator = NULL;
1567         zoom_do(obj, 1.0);
1568         evas_object_smart_callback_call(obj, SIG_ZOOM_STOP, NULL);
1569      }
1570    elm_smart_scroller_region_bring_in(wd->scr, rx, ry, rw, rh);
1571 }
1572
1573 EAPI void
1574 elm_photocam_paused_set(Evas_Object *obj, Eina_Bool paused)
1575 {
1576    ELM_CHECK_WIDTYPE(obj, widtype);
1577    Widget_Data *wd = elm_widget_data_get(obj);
1578    if (!wd) return;
1579    if (wd->paused == !!paused) return;
1580    wd->paused = paused;
1581    if (wd->paused)
1582      {
1583         if (wd->zoom_animator)
1584           {
1585              ecore_animator_del(wd->zoom_animator);
1586              wd->zoom_animator = NULL;
1587              zoom_do(obj, 1.0);
1588              evas_object_smart_callback_call(obj, SIG_ZOOM_STOP, NULL);
1589           }
1590      }
1591 }
1592
1593 EAPI Eina_Bool
1594 elm_photocam_paused_get(const Evas_Object *obj)
1595 {
1596    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
1597    Widget_Data *wd = elm_widget_data_get(obj);
1598    if (!wd) return EINA_FALSE;
1599    return wd->paused;
1600 }
1601
1602 EAPI Evas_Object *
1603 elm_photocam_internal_image_get(const Evas_Object *obj)
1604 {
1605    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1606    Widget_Data *wd = elm_widget_data_get(obj);
1607    if (!wd) return NULL;
1608    return wd->img;
1609 }
1610
1611 EAPI void
1612 elm_photocam_bounce_set(Evas_Object *obj, Eina_Bool h_bounce, Eina_Bool v_bounce)
1613 {
1614    ELM_CHECK_WIDTYPE(obj, widtype);
1615    Widget_Data *wd = elm_widget_data_get(obj);
1616    if (!wd) return;
1617    elm_smart_scroller_bounce_allow_set(wd->scr, h_bounce, v_bounce);
1618 }
1619
1620 EAPI void
1621 elm_photocam_bounce_get(const Evas_Object *obj, Eina_Bool *h_bounce, Eina_Bool *v_bounce)
1622 {
1623    ELM_CHECK_WIDTYPE(obj, widtype);
1624    Widget_Data *wd = elm_widget_data_get(obj);
1625    if (!wd) return;
1626    elm_smart_scroller_bounce_allow_get(wd->scr, h_bounce, v_bounce);
1627 }
1628