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