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