Merge "[genlist] do not emit unselect if item's renamed"
[framework/uifw/elementary.git] / src / lib / els_scroller.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3 #include "els_scroller.h"
4 #include "els_pan.h"
5
6 #define SMART_NAME "els_scroller"
7 #define API_ENTRY Smart_Data *sd; sd = evas_object_smart_data_get(obj); if ((!obj) || (!sd) || (evas_object_type_get(obj) && strcmp(evas_object_type_get(obj), SMART_NAME)))
8 #define INTERNAL_ENTRY Smart_Data *sd; sd = evas_object_smart_data_get(obj); if (!sd) return;
9 typedef struct _Smart_Data Smart_Data;
10
11 #define EVTIME 1
12 //#define SCROLLDBG 1
13
14 struct _Smart_Data
15 {
16    Evas_Coord   x, y, w, h;
17    Evas_Coord   wx, wy, ww, wh; /* Last "wanted" geometry */
18
19    Evas_Object *smart_obj;
20    Evas_Object *child_obj;
21    Evas_Object *pan_obj;
22    Evas_Object *edje_obj;
23    Evas_Object *event_obj;
24
25    Evas_Object *widget;
26
27    Elm_Smart_Scroller_Policy hbar_flags, vbar_flags;
28
29    struct {
30         Evas_Coord x, y;
31         Evas_Coord sx, sy;
32         Evas_Coord dx, dy;
33         Evas_Coord pdx, pdy;
34         Evas_Coord bx, by;
35         Evas_Coord ax, ay;
36         Evas_Coord bx0, by0;
37         Evas_Coord b0x, b0y;
38         Evas_Coord b2x, b2y;
39         struct {
40              Evas_Coord    x, y;
41              double        timestamp, localtimestamp;
42         } history[60];
43         struct {
44            double tadd, dxsum, dysum;
45            double est_timestamp_diff;
46         } hist;
47       double anim_start;
48       double anim_start2;
49       double anim_start3;
50       double onhold_vx, onhold_vy, onhold_tlast, onhold_vxe, onhold_vye;
51       double extra_time;
52       Evas_Coord hold_x, hold_y;
53       Ecore_Animator *hold_animator;
54       Ecore_Animator *onhold_animator;
55       Ecore_Animator *momentum_animator;
56       Ecore_Animator *bounce_x_animator;
57       Ecore_Animator *bounce_y_animator;
58       Evas_Coord locked_x, locked_y;
59       int hdir, vdir;
60       unsigned char now : 1;
61       unsigned char cancelled : 1;
62       unsigned char hold : 1;
63       unsigned char hold_parent : 1;
64       unsigned char want_dragged : 1;
65       unsigned char dragged : 1;
66       unsigned char dragged_began : 1;
67       unsigned char dir_x : 1;
68       unsigned char dir_y : 1;
69       unsigned char locked : 1;
70       unsigned char bounce_x_hold : 1;
71       unsigned char bounce_y_hold : 1;
72       unsigned char scroll : 1;
73       unsigned char want_reset : 1;
74    } down;
75
76    struct {
77       Evas_Coord w, h;
78    } child;
79    struct {
80       Evas_Coord x, y;
81    } step, page;
82
83    struct {
84       void (*set) (Evas_Object *obj, Evas_Coord x, Evas_Coord y);
85       void (*get) (Evas_Object *obj, Evas_Coord *x, Evas_Coord *y);
86       void (*max_get) (Evas_Object *obj, Evas_Coord *x, Evas_Coord *y);
87       void (*min_get) (Evas_Object *obj, Evas_Coord *x, Evas_Coord *y);
88       void (*child_size_get) (Evas_Object *obj, Evas_Coord *x, Evas_Coord *y);
89       void (*gravity_set) (Evas_Object *obj, double x, double y);
90       void (*gravity_get) (Evas_Object *obj, double *x, double *y);
91    } pan_func;
92
93    struct {
94       struct {
95          Evas_Coord start, end;
96          double t_start, t_end;
97          Ecore_Animator *animator;
98       } x, y;
99    } scrollto;
100
101    double pagerel_h, pagerel_v;
102    Evas_Coord pagesize_h, pagesize_v;
103
104    unsigned char hbar_visible : 1;
105    unsigned char vbar_visible : 1;
106    unsigned char extern_pan : 1;
107    unsigned char one_dir_at_a_time : 1;
108    unsigned char hold : 1;
109    unsigned char freeze : 1;
110    unsigned char bouncemex : 1;
111    unsigned char bouncemey : 1;
112    unsigned char bounce_horiz : 1;
113    unsigned char bounce_vert : 1;
114    unsigned char event_propagation :1;
115    Eina_Bool momentum_animator_disabled :1;
116    Eina_Bool bounce_animator_disabled :1;
117    Eina_Bool is_mirrored : 1;
118    Eina_Bool wheel_disabled : 1;
119 };
120
121 /* local subsystem functions */
122 static void _smart_child_del_hook(void *data, Evas *e, Evas_Object *obj, void *event_info);
123 static void _smart_pan_changed_hook(void *data, Evas_Object *obj, void *event_info);
124 static void _smart_pan_pan_changed_hook(void *data, Evas_Object *obj, void *event_info);
125 static void _smart_event_wheel(void *data, Evas *e, Evas_Object *obj, void *event_info);
126 static void _smart_event_mouse_down(void *data, Evas *e, Evas_Object *obj, void *event_info);
127 static Eina_Bool  _smart_hold_animator(void *data);
128 static Eina_Bool  _smart_momentum_animator(void *data);
129 static void _smart_event_mouse_up(void *data, Evas *e, Evas_Object *obj, void *event_info);
130 static Eina_Bool  _smart_onhold_animator(void *data);
131 static void _smart_event_mouse_move(void *data, Evas *e, Evas_Object *obj, void *event_info);
132 static void _smart_edje_drag_v_start(void *data, Evas_Object *obj, const char *emission, const char *source);
133 static void _smart_edje_drag_v_stop(void *data, Evas_Object *obj, const char *emission, const char *source);
134 static void _smart_edje_drag_v(void *data, Evas_Object *obj, const char *emission, const char *source);
135 static void _smart_edje_drag_h_start(void *data, Evas_Object *obj, const char *emission, const char *source);
136 static void _smart_edje_drag_h_stop(void *data, Evas_Object *obj, const char *emission, const char *source);
137 static void _smart_edje_drag_h(void *data, Evas_Object *obj, const char *emission, const char *source);
138 static void _smart_scrollbar_read(Smart_Data *sd);
139 static void _smart_scrollbar_reset(Smart_Data *sd);
140 static int  _smart_scrollbar_bar_h_visibility_adjust(Smart_Data *sd);
141 static int  _smart_scrollbar_bar_v_visibility_adjust(Smart_Data *sd);
142 static void _smart_scrollbar_bar_visibility_adjust(Smart_Data *sd);
143 static void _smart_scrollbar_size_adjust(Smart_Data *sd);
144 static void _smart_reconfigure(Smart_Data *sd);
145 static void _smart_add(Evas_Object *obj);
146 static void _smart_del(Evas_Object *obj);
147 static void _smart_move(Evas_Object *obj, Evas_Coord x, Evas_Coord y);
148 static void _smart_resize(Evas_Object *obj, Evas_Coord w, Evas_Coord h);
149 static void _smart_show(Evas_Object *obj);
150 static void _smart_hide(Evas_Object *obj);
151 static void _smart_color_set(Evas_Object *obj, int r, int g, int b, int a);
152 static void _smart_clip_set(Evas_Object *obj, Evas_Object *clip);
153 static void _smart_clip_unset(Evas_Object *obj);
154 static void _smart_init(void);
155
156 /* local subsystem globals */
157 static Evas_Smart *_smart = NULL;
158
159 /* externally accessible functions */
160 Evas_Object *
161 elm_smart_scroller_add(Evas *evas)
162 {
163    _smart_init();
164    return evas_object_smart_add(evas, _smart);
165 }
166
167 static Evas_Coord
168 _elm_smart_scroller_x_mirrored_get(Evas_Object *obj, Evas_Coord x)
169 {
170    API_ENTRY return x;
171
172    Evas_Coord cw, ch, w, ret;
173    elm_smart_scroller_child_viewport_size_get(obj, &w, NULL);
174    sd->pan_func.child_size_get(sd->pan_obj, &cw, &ch);
175    ret = (cw - (x + w));
176    return (ret >= 0) ? ret : 0;
177 }
178
179 void
180 elm_smart_scroller_mirrored_set(Evas_Object *obj, Eina_Bool mirrored)
181 {
182    API_ENTRY return;
183    Evas_Coord wx;
184    if (sd->is_mirrored == mirrored)
185      return;
186
187    sd->is_mirrored = mirrored;
188    edje_object_mirrored_set(sd->edje_obj, mirrored);
189
190    if (sd->is_mirrored)
191      wx = _elm_smart_scroller_x_mirrored_get(sd->smart_obj, sd->wx);
192    else
193      wx = sd->wx;
194
195    elm_smart_scroller_child_pos_set(sd->smart_obj, wx, sd->wy);
196 }
197
198 void
199 elm_smart_scroller_child_set(Evas_Object *obj, Evas_Object *child)
200 {
201    Evas_Coord w, h;
202    Evas_Object *o;
203
204    API_ENTRY return;
205    if (sd->child_obj)
206      {
207         _elm_smart_pan_child_set(sd->pan_obj, NULL);
208         evas_object_event_callback_del_full(sd->child_obj, EVAS_CALLBACK_DEL, _smart_child_del_hook, sd);
209      }
210
211    sd->child_obj = child;
212    sd->wx = sd->wy = 0;
213    /* (-1) means want viewports size */
214    sd->ww = sd->wh = -1;
215    if (!child) return;
216
217    if (!sd->pan_obj)
218      {
219         o = _elm_smart_pan_add(evas_object_evas_get(obj));
220         sd->pan_obj = o;
221         evas_object_smart_callback_add(o, "changed", _smart_pan_changed_hook, sd);
222         evas_object_smart_callback_add(o, "pan_changed", _smart_pan_pan_changed_hook, sd);
223         edje_object_part_swallow(sd->edje_obj, "elm.swallow.content", o);
224      }
225
226    sd->pan_func.set = _elm_smart_pan_set;
227    sd->pan_func.get = _elm_smart_pan_get;
228    sd->pan_func.max_get = _elm_smart_pan_max_get;
229    sd->pan_func.min_get = _elm_smart_pan_min_get;
230    sd->pan_func.child_size_get = _elm_smart_pan_child_size_get;
231    sd->pan_func.gravity_set = _elm_smart_pan_gravity_set;
232    sd->pan_func.gravity_get = _elm_smart_pan_gravity_get;
233
234    evas_object_event_callback_add(child, EVAS_CALLBACK_DEL, _smart_child_del_hook, sd);
235    _elm_smart_pan_child_set(sd->pan_obj, child);
236    sd->pan_func.child_size_get(sd->pan_obj, &w, &h);
237    sd->child.w = w;
238    sd->child.h = h;
239    _smart_scrollbar_size_adjust(sd);
240    _smart_scrollbar_reset(sd);
241 }
242
243 void
244 elm_smart_scroller_extern_pan_set(Evas_Object *obj, Evas_Object *pan,
245                                   void (*pan_set) (Evas_Object *obj, Evas_Coord x, Evas_Coord y),
246                                   void (*pan_get) (Evas_Object *obj, Evas_Coord *x, Evas_Coord *y),
247                                   void (*pan_max_get) (Evas_Object *obj, Evas_Coord *x, Evas_Coord *y),
248                                   void (*pan_min_get) (Evas_Object *obj, Evas_Coord *x, Evas_Coord *y),
249                                   void (*pan_child_size_get) (Evas_Object *obj, Evas_Coord *x, Evas_Coord *y))
250 {
251    API_ENTRY return;
252
253    elm_smart_scroller_child_set(obj, NULL);
254
255    if (sd->pan_obj)
256      {
257         evas_object_smart_callback_del(sd->pan_obj, "changed", _smart_pan_changed_hook);
258         evas_object_smart_callback_del(sd->pan_obj, "pan_changed", _smart_pan_pan_changed_hook);
259      }
260
261    if (sd->extern_pan)
262      {
263         if (sd->pan_obj)
264           {
265              edje_object_part_unswallow(sd->edje_obj, sd->pan_obj);
266              sd->pan_obj = NULL;
267           }
268      }
269    else
270      {
271         if (sd->pan_obj)
272           {
273              evas_object_del(sd->pan_obj);
274              sd->pan_obj = NULL;
275           }
276      }
277    if (!pan)
278      {
279         sd->extern_pan = 0;
280         return;
281      }
282
283    sd->pan_obj = pan;
284    sd->pan_func.set = pan_set;
285    sd->pan_func.get = pan_get;
286    sd->pan_func.max_get = pan_max_get;
287    sd->pan_func.min_get = pan_min_get;
288    sd->pan_func.child_size_get = pan_child_size_get;
289    sd->pan_func.gravity_set = _elm_smart_pan_gravity_set;
290    sd->pan_func.gravity_get = _elm_smart_pan_gravity_get;
291    sd->extern_pan = 1;
292    evas_object_smart_callback_add(sd->pan_obj, "changed", _smart_pan_changed_hook, sd);
293    evas_object_smart_callback_add(sd->pan_obj, "pan_changed", _smart_pan_pan_changed_hook, sd);
294    edje_object_part_swallow(sd->edje_obj, "elm.swallow.content", sd->pan_obj);
295    evas_object_show(sd->pan_obj);
296 }
297
298 void
299 elm_smart_scroller_custom_edje_file_set(Evas_Object *obj, char *file, char *group)
300 {
301    API_ENTRY return;
302
303    edje_object_file_set(sd->edje_obj, file, group);
304    if (sd->pan_obj)
305      edje_object_part_swallow(sd->edje_obj, "elm.swallow.content", sd->pan_obj);
306    sd->vbar_visible = !sd->vbar_visible;
307    sd->hbar_visible = !sd->hbar_visible;
308    _smart_scrollbar_bar_visibility_adjust(sd);
309    if (sd->hbar_flags == ELM_SMART_SCROLLER_POLICY_ON)
310      edje_object_signal_emit(sd->edje_obj, "elm,action,show_always,hbar", "elm");
311    else if (sd->hbar_flags == ELM_SMART_SCROLLER_POLICY_OFF)
312      edje_object_signal_emit(sd->edje_obj, "elm,action,hide,hbar", "elm");
313    else
314      edje_object_signal_emit(sd->edje_obj, "elm,action,show_notalways,hbar", "elm");
315    if (sd->vbar_flags == ELM_SMART_SCROLLER_POLICY_ON)
316      edje_object_signal_emit(sd->edje_obj, "elm,action,show_always,vbar", "elm");
317    else if (sd->vbar_flags == ELM_SMART_SCROLLER_POLICY_OFF)
318      edje_object_signal_emit(sd->edje_obj, "elm,action,hide,vbar", "elm");
319    else
320      edje_object_signal_emit(sd->edje_obj, "elm,action,show_notalways,vbar", "elm");
321 }
322
323 Eina_Bool
324 elm_smart_scroller_momentum_animator_disabled_get(Evas_Object *obj)
325 {
326    API_ENTRY return EINA_FALSE;
327    return sd->momentum_animator_disabled;
328 }
329
330 void
331 elm_smart_scroller_momentum_animator_disabled_set(Evas_Object *obj, Eina_Bool disabled)
332 {
333    API_ENTRY return;
334    sd->momentum_animator_disabled = disabled;
335    if (sd->momentum_animator_disabled)
336      {
337         if (sd->down.momentum_animator)
338           {
339              ecore_animator_del(sd->down.momentum_animator);
340              sd->down.momentum_animator = NULL;
341           }
342      }
343 }
344
345 Eina_Bool
346 elm_smart_scroller_bounce_animator_disabled_get(Evas_Object *obj)
347 {
348    API_ENTRY return EINA_FALSE;
349    return sd->bounce_animator_disabled;
350 }
351
352 void
353 elm_smart_scroller_bounce_animator_disabled_set(Evas_Object *obj, Eina_Bool disabled)
354 {
355    API_ENTRY return;
356    sd->bounce_animator_disabled = disabled;
357    if (sd->bounce_animator_disabled)
358      {
359         if (sd->scrollto.x.animator)
360           {
361              ecore_animator_del(sd->scrollto.x.animator);
362              sd->scrollto.x.animator = NULL;
363           }
364
365         if (sd->scrollto.y.animator)
366           {
367              ecore_animator_del(sd->scrollto.y.animator);
368              sd->scrollto.y.animator = NULL;
369           }
370      }
371 }
372
373 Eina_Bool
374 elm_smart_scroller_wheel_disabled_get(Evas_Object *obj)
375 {
376    API_ENTRY return EINA_FALSE;
377    return sd->wheel_disabled;
378 }
379
380 void
381 elm_smart_scroller_wheel_disabled_set(Evas_Object *obj, Eina_Bool disabled)
382 {
383    API_ENTRY return;
384    if ((!sd->wheel_disabled) && (disabled))
385      evas_object_event_callback_del_full(sd->event_obj, EVAS_CALLBACK_MOUSE_WHEEL, _smart_event_wheel, sd);
386    else if ((sd->wheel_disabled) && (!disabled))
387      evas_object_event_callback_add(sd->event_obj, EVAS_CALLBACK_MOUSE_WHEEL, _smart_event_wheel, sd);
388    sd->wheel_disabled = disabled;
389 }
390
391 /* Update the wanted coordinates according to the x, y passed
392  * widget directionality, child size and etc. */
393 static void
394 _update_wanted_coordinates(Smart_Data *sd, Evas_Coord x, Evas_Coord y)
395 {
396    Evas_Coord cw, ch;
397
398    sd->pan_func.child_size_get(sd->pan_obj, &cw, &ch);
399
400    /* Update wx/y/w/h - and if the requested positions aren't legal
401     * adjust a bit. */
402    elm_smart_scroller_child_viewport_size_get(sd->smart_obj, &sd->ww, &sd->wh);
403    if (x < 0)
404      sd->wx = 0;
405    else if ((x + sd->ww) > cw)
406      sd->wx = cw - sd->ww;
407    else if (sd->is_mirrored)
408      sd->wx = _elm_smart_scroller_x_mirrored_get(sd->smart_obj, x);
409    else
410      sd->wx = x;
411    if (y < 0) sd->wy = 0;
412    else if ((y + sd->wh) > ch) sd->wy = ch - sd->wh;
413    else sd->wy = y;
414 }
415
416 static void
417 _smart_anim_start(Evas_Object *obj)
418 {
419    evas_object_smart_callback_call(obj, "animate,start", NULL);
420 }
421
422 static void
423 _smart_anim_stop(Evas_Object *obj)
424 {
425    evas_object_smart_callback_call(obj, "animate,stop", NULL);
426 }
427
428 static void
429 _smart_drag_start(Evas_Object *obj)
430 {
431    evas_object_smart_callback_call(obj, "drag,start", NULL);
432 }
433
434 static void
435 _smart_drag_stop(Evas_Object *obj)
436 {
437    evas_object_smart_callback_call(obj, "drag,stop", NULL);
438 }
439
440 static Eina_Bool
441 _smart_scrollto_x_animator(void *data)
442 {
443    Smart_Data *sd = data;
444    Evas_Coord px, py;
445    double t, tt;
446
447    t = ecore_loop_time_get();
448    tt = (t - sd->scrollto.x.t_start) / (sd->scrollto.x.t_end - sd->scrollto.x.t_start);
449    tt = 1.0 - tt;
450    tt = 1.0 - (tt * tt);
451    sd->pan_func.get(sd->pan_obj, &px, &py);
452    px = (sd->scrollto.x.start * (1.0 - tt)) +
453       (sd->scrollto.x.end * tt);
454    if (t >= sd->scrollto.x.t_end)
455      {
456         px = sd->scrollto.x.end;
457         elm_smart_scroller_child_pos_set(sd->smart_obj, px, py);
458         _update_wanted_coordinates(sd, px, py);
459         sd->scrollto.x.animator = NULL;
460         if ((!sd->scrollto.y.animator) && (!sd->down.bounce_y_animator))
461           _smart_anim_stop(sd->smart_obj);
462         return ECORE_CALLBACK_CANCEL;
463      }
464    elm_smart_scroller_child_pos_set(sd->smart_obj, px, py);
465    _update_wanted_coordinates(sd, px, py);
466    return ECORE_CALLBACK_RENEW;
467 }
468
469 static void
470 _smart_momentum_end(Smart_Data *sd)
471 {
472    if ((sd->down.bounce_x_animator) || (sd->down.bounce_y_animator)) return;
473    if (sd->down.momentum_animator)
474      {
475         Evas_Coord px, py;
476         elm_smart_scroller_child_pos_get(sd->smart_obj, &px, &py);
477         _update_wanted_coordinates(sd, px, py);
478
479         ecore_animator_del(sd->down.momentum_animator);
480         sd->down.momentum_animator = NULL;
481         sd->down.bounce_x_hold = 0;
482         sd->down.bounce_y_hold = 0;
483         sd->down.ax = 0;
484         sd->down.ay = 0;
485         sd->down.dx = 0;
486         sd->down.dy = 0;
487         sd->down.pdx = 0;
488         sd->down.pdy = 0;
489      }
490 }
491
492 static void
493 _smart_scrollto_x(Smart_Data *sd, double t_in, Evas_Coord pos_x)
494 {
495    Evas_Coord px, py, x, y, w, h;
496    double t;
497
498    if (sd->freeze) return;
499    if (t_in <= 0.0)
500      {
501         elm_smart_scroller_child_pos_get(sd->smart_obj, &x, &y);
502         elm_smart_scroller_child_viewport_size_get(sd->smart_obj, &w, &h);
503         x = pos_x;
504         elm_smart_scroller_child_region_set(sd->smart_obj, x, y, w, h);
505         return;
506      }
507    t = ecore_loop_time_get();
508    sd->pan_func.get(sd->pan_obj, &px, &py);
509    sd->scrollto.x.start = px;
510    sd->scrollto.x.end = pos_x;
511    sd->scrollto.x.t_start = t;
512    sd->scrollto.x.t_end = t + t_in;
513    if (!sd->scrollto.x.animator)
514      {
515         sd->scrollto.x.animator = ecore_animator_add(_smart_scrollto_x_animator, sd);
516         if (!sd->scrollto.y.animator)
517           _smart_anim_start(sd->smart_obj);
518      }
519    if (sd->down.bounce_x_animator)
520      {
521         ecore_animator_del(sd->down.bounce_x_animator);
522         sd->down.bounce_x_animator = NULL;
523         _smart_momentum_end(sd);
524      }
525    sd->bouncemex = 0;
526 }
527
528 static Eina_Bool
529 _smart_scrollto_y_animator(void *data)
530 {
531    Smart_Data *sd = data;
532    Evas_Coord px, py;
533    double t, tt;
534
535    t = ecore_loop_time_get();
536    tt = (t - sd->scrollto.y.t_start) / (sd->scrollto.y.t_end - sd->scrollto.y.t_start);
537    tt = 1.0 - tt;
538    tt = 1.0 - (tt * tt);
539    sd->pan_func.get(sd->pan_obj, &px, &py);
540    py = (sd->scrollto.y.start * (1.0 - tt)) +
541       (sd->scrollto.y.end * tt);
542    if (t >= sd->scrollto.y.t_end)
543      {
544         py = sd->scrollto.y.end;
545         elm_smart_scroller_child_pos_set(sd->smart_obj, px, py);
546         _update_wanted_coordinates(sd, px, py);
547         sd->scrollto.y.animator = NULL;
548         if ((!sd->scrollto.x.animator) && (!sd->down.bounce_x_animator))
549           _smart_anim_stop(sd->smart_obj);
550         return ECORE_CALLBACK_CANCEL;
551      }
552    elm_smart_scroller_child_pos_set(sd->smart_obj, px, py);
553    _update_wanted_coordinates(sd, px, py);
554
555    return ECORE_CALLBACK_RENEW;
556 }
557
558 static void
559 _smart_scrollto_y(Smart_Data *sd, double t_in, Evas_Coord pos_y)
560 {
561    Evas_Coord px, py, x, y, w, h;
562    double t;
563
564    if (sd->freeze) return;
565    if (t_in <= 0.0)
566      {
567         elm_smart_scroller_child_pos_get(sd->smart_obj, &x, &y);
568         elm_smart_scroller_child_viewport_size_get(sd->smart_obj, &w, &h);
569         y = pos_y;
570         elm_smart_scroller_child_region_set(sd->smart_obj, x, y, w, h);
571         return;
572      }
573    t = ecore_loop_time_get();
574    sd->pan_func.get(sd->pan_obj, &px, &py);
575    sd->scrollto.y.start = py;
576    sd->scrollto.y.end = pos_y;
577    sd->scrollto.y.t_start = t;
578    sd->scrollto.y.t_end = t + t_in;
579    if (!sd->scrollto.y.animator)
580      {
581         sd->scrollto.y.animator = ecore_animator_add(_smart_scrollto_y_animator, sd);
582         if (!sd->scrollto.x.animator)
583           _smart_anim_start(sd->smart_obj);
584      }
585    if (sd->down.bounce_y_animator)
586      {
587         ecore_animator_del(sd->down.bounce_y_animator);
588         sd->down.bounce_y_animator = NULL;
589         _smart_momentum_end(sd);
590      }
591    sd->bouncemey = 0;
592 }
593
594 static Eina_Bool
595 _smart_do_page(Smart_Data *sd)
596 {
597    if ((sd->pagerel_h == 0.0) && (!sd->pagesize_h) &&
598        (sd->pagerel_v == 0.0) && (!sd->pagesize_v))
599      return EINA_FALSE;
600    return EINA_TRUE;
601 }
602
603 static Evas_Coord
604 _smart_page_x_get(Smart_Data *sd, int offset)
605 {
606    Evas_Coord x, y, w, h, cw, ch, minx = 0;
607
608    elm_smart_scroller_child_pos_get(sd->smart_obj, &x, &y);
609    elm_smart_scroller_child_viewport_size_get(sd->smart_obj, &w, &h);
610    sd->pan_func.child_size_get(sd->pan_obj, &cw, &ch);
611    sd->pan_func.min_get(sd->pan_obj, &minx, NULL);
612
613    x += offset;
614
615    if (sd->pagerel_h > 0.0)
616      sd->pagesize_h = w * sd->pagerel_h;
617    if (sd->pagesize_h > 0)
618      {
619         x = x + (sd->pagesize_h * 0.5);
620         x = x / (sd->pagesize_h);
621         x = x * (sd->pagesize_h);
622      }
623    if ((x + w) > cw) x = cw - w;
624    if (x < minx) x = minx;
625    return x;
626 }
627
628 static Evas_Coord
629 _smart_page_y_get(Smart_Data *sd, int offset)
630 {
631    Evas_Coord x, y, w, h, cw, ch, miny = 0;
632
633    elm_smart_scroller_child_pos_get(sd->smart_obj, &x, &y);
634    elm_smart_scroller_child_viewport_size_get(sd->smart_obj, &w, &h);
635    sd->pan_func.child_size_get(sd->pan_obj, &cw, &ch);
636    sd->pan_func.min_get(sd->pan_obj, NULL, &miny);
637
638    y += offset;
639
640    if (sd->pagerel_v > 0.0)
641      sd->pagesize_v = h * sd->pagerel_v;
642    if (sd->pagesize_v > 0)
643      {
644         y = y + (sd->pagesize_v * 0.5);
645         y = y / (sd->pagesize_v);
646         y = y * (sd->pagesize_v);
647      }
648    if ((y + h) > ch) y = ch - h;
649    if (y < miny) y = miny;
650    return y;
651 }
652
653 static void
654 _smart_page_adjust(Smart_Data *sd)
655 {
656    Evas_Coord x, y, w, h;
657
658    if (!_smart_do_page(sd)) return;
659
660    elm_smart_scroller_child_viewport_size_get(sd->smart_obj, &w, &h);
661
662    x = _smart_page_x_get(sd, 0);
663    y = _smart_page_y_get(sd, 0);
664
665    elm_smart_scroller_child_region_set(sd->smart_obj, x, y, w, h);
666 }
667
668 static Eina_Bool
669 _smart_bounce_x_animator(void *data)
670 {
671    Smart_Data *sd;
672    Evas_Coord x, y, dx, w, odx, ed, md;
673    double t, p, dt, pd, r;
674
675    sd = data;
676    t = ecore_loop_time_get();
677    dt = t - sd->down.anim_start2;
678    if (dt >= 0.0)
679      {
680         dt = dt / _elm_config->thumbscroll_bounce_friction;
681         odx = sd->down.b2x - sd->down.bx;
682         elm_smart_scroller_child_viewport_size_get(sd->smart_obj, &w, NULL);
683         if (!sd->down.momentum_animator && (w > abs(odx)))
684           {
685              pd = (double)odx / (double)w;
686              pd = (pd > 0) ? pd : -pd;
687              pd = 1.0 - ((1.0 - pd) * (1.0 - pd));
688              dt = dt / pd;
689           }
690         if (dt > 1.0) dt = 1.0;
691         p = 1.0 - ((1.0 - dt) * (1.0 - dt));
692         elm_smart_scroller_child_pos_get(sd->smart_obj, &x, &y);
693         dx = (odx * p);
694         r = 1.0;
695         if (sd->down.momentum_animator)
696           {
697              ed = abs(sd->down.dx * (_elm_config->thumbscroll_friction + sd->down.extra_time) - sd->down.b0x);
698              md = abs(_elm_config->thumbscroll_friction * 5 * w);
699              if (ed > md) r = (double)(md)/(double)ed;
700           }
701         x = sd->down.b2x + (int)((double)(dx - odx) * r);
702         if (!sd->down.cancelled)
703           elm_smart_scroller_child_pos_set(sd->smart_obj, x, y);
704         if (dt >= 1.0)
705           {
706              if (sd->down.momentum_animator)
707                sd->down.bounce_x_hold = 1;
708              else if ((!sd->down.bounce_y_animator) &&
709                       (!sd->scrollto.y.animator))
710                _smart_anim_stop(sd->smart_obj);
711              sd->down.bounce_x_animator = NULL;
712              sd->down.pdx = 0;
713              sd->bouncemex = 0;
714              _smart_momentum_end(sd);
715              return ECORE_CALLBACK_CANCEL;
716           }
717      }
718    return ECORE_CALLBACK_RENEW;
719 }
720
721 static Eina_Bool
722 _smart_bounce_y_animator(void *data)
723 {
724    Smart_Data *sd;
725    Evas_Coord x, y, dy, h, ody, ed, md;
726    double t, p, dt, pd, r;
727
728    sd = data;
729    t = ecore_loop_time_get();
730    dt = t - sd->down.anim_start3;
731    if (dt >= 0.0)
732      {
733         dt = dt / _elm_config->thumbscroll_bounce_friction;
734         ody = sd->down.b2y - sd->down.by;
735         elm_smart_scroller_child_viewport_size_get(sd->smart_obj, NULL, &h);
736         if (!sd->down.momentum_animator && (h > abs(ody)))
737           {
738              pd = (double)ody / (double)h;
739              pd = (pd > 0) ? pd : -pd;
740              pd = 1.0 - ((1.0 - pd) * (1.0 - pd));
741              dt = dt / pd;
742           }
743         if (dt > 1.0) dt = 1.0;
744         p = 1.0 - ((1.0 - dt) * (1.0 - dt));
745         elm_smart_scroller_child_pos_get(sd->smart_obj, &x, &y);
746         dy = (ody * p);
747         r = 1.0;
748         if (sd->down.momentum_animator)
749           {
750              ed = abs(sd->down.dy * (_elm_config->thumbscroll_friction + sd->down.extra_time) - sd->down.b0y);
751              md = abs(_elm_config->thumbscroll_friction * 5 * h);
752              if (ed > md) r = (double)(md)/(double)ed;
753           }
754         y = sd->down.b2y + (int)((double)(dy - ody) * r);
755         if (!sd->down.cancelled)
756           elm_smart_scroller_child_pos_set(sd->smart_obj, x, y);
757         if (dt >= 1.0)
758           {
759              if (sd->down.momentum_animator)
760                sd->down.bounce_y_hold = 1;
761              else if ((!sd->down.bounce_x_animator) &&
762                  (!sd->scrollto.y.animator))
763                _smart_anim_stop(sd->smart_obj);
764              sd->down.bounce_y_animator = NULL;
765              sd->down.pdy = 0;
766              sd->bouncemey = 0;
767              _smart_momentum_end(sd);
768              return ECORE_CALLBACK_CANCEL;
769           }
770      }
771    return ECORE_CALLBACK_RENEW;
772 }
773
774 #define LEFT 0
775 #define RIGHT 1
776 #define UP 2
777 #define DOWN 3
778 static Eina_Bool
779 can_scroll(Smart_Data *sd, int dir)
780 {
781    Evas_Coord mx = 0, my = 0, px = 0, py = 0, minx = 0, miny = 0;
782
783    sd->pan_func.max_get(sd->pan_obj, &mx, &my);
784    sd->pan_func.min_get(sd->pan_obj, &minx, &miny);
785    sd->pan_func.get(sd->pan_obj, &px, &py);
786    switch (dir)
787      {
788       case LEFT:
789         if (px > minx) return EINA_TRUE;
790         break;
791       case RIGHT:
792         if ((px - minx) < mx) return EINA_TRUE;
793         break;
794       case UP:
795         if (py > miny) return EINA_TRUE;
796         break;
797       case DOWN:
798         if ((py - miny) < my) return EINA_TRUE;
799         break;
800       default:
801         break;
802      }
803    return EINA_FALSE;
804 }
805
806 static Eina_Bool
807 _smart_momentum_animator(void *data)
808 {
809    Smart_Data *sd;
810    double t, dt, p;
811    Evas_Coord x, y, dx, dy, px, py, maxx, maxy, minx, miny;
812    Eina_Bool no_bounce_x_end = EINA_FALSE, no_bounce_y_end = EINA_FALSE;
813
814    sd = data;
815    t = ecore_loop_time_get();
816    dt = t - sd->down.anim_start;
817    if (dt >= 0.0)
818      {
819         /*
820         if (sd->down.hold_parent)
821           {
822              if ((sd->down.dir_x) && !can_scroll(sd, sd->down.hdir))
823                {
824                   sd->down.dir_x = 0;
825                }
826              if ((sd->down.dir_y) && !can_scroll(sd, sd->down.vdir))
827                {
828                   sd->down.dir_y = 0;
829                   }
830                   }
831                   if ((!sd->down.dir_x) && (!sd->down.dir_y))
832                   {
833                   sd->down.cancelled = 1;
834                   }
835          */
836         dt = dt / (_elm_config->thumbscroll_friction + sd->down.extra_time);
837         if (dt > 1.0) dt = 1.0;
838         p = 1.0 - ((1.0 - dt) * (1.0 - dt));
839         dx = (sd->down.dx * (_elm_config->thumbscroll_friction + sd->down.extra_time) * p);
840         dy = (sd->down.dy * (_elm_config->thumbscroll_friction + sd->down.extra_time) * p);
841         sd->down.ax = dx;
842         sd->down.ay = dy;
843         x = sd->down.sx - dx;
844         y = sd->down.sy - dy;
845         elm_smart_scroller_child_pos_get(sd->smart_obj, &px, &py);
846         if ((sd->down.bounce_x_animator) ||
847             (sd->down.bounce_x_hold))
848           {
849              sd->down.bx = sd->down.bx0 - dx + sd->down.b0x;
850              x = px;
851           }
852         if ((sd->down.bounce_y_animator) ||
853             (sd->down.bounce_y_hold))
854           {
855              sd->down.by = sd->down.by0 - dy + sd->down.b0y;
856              y = py;
857           }
858         elm_smart_scroller_child_pos_set(sd->smart_obj, x, y);
859         _update_wanted_coordinates(sd, x, y);
860         sd->pan_func.max_get(sd->pan_obj, &maxx, &maxy);
861         sd->pan_func.min_get(sd->pan_obj, &minx, &miny);
862         if (!sd->bounce_horiz)
863           {
864              if (x <= minx) no_bounce_x_end = EINA_TRUE;
865              if ((x - minx) >= maxx) no_bounce_x_end = EINA_TRUE;
866           }
867         if (!sd->bounce_vert)
868           {
869              if (y <= miny) no_bounce_y_end = EINA_TRUE;
870              if ((y - miny) >= maxy) no_bounce_y_end = EINA_TRUE;
871           }
872         if ((dt >= 1.0) ||
873             ((sd->down.bounce_x_hold) && (sd->down.bounce_y_hold)) ||
874             (no_bounce_x_end && no_bounce_y_end))
875           {
876              _smart_anim_stop(sd->smart_obj);
877
878              sd->down.momentum_animator = NULL;
879              sd->down.bounce_x_hold = 0;
880              sd->down.bounce_y_hold = 0;
881              sd->down.ax = 0;
882              sd->down.ay = 0;
883              sd->down.pdx = 0;
884              sd->down.pdy = 0;
885              return ECORE_CALLBACK_CANCEL;
886           }
887      }
888    return ECORE_CALLBACK_RENEW;
889 }
890
891 static void
892 bounce_eval(Smart_Data *sd)
893 {
894    Evas_Coord mx, my, px, py, bx, by, b2x, b2y, minx = 0, miny = 0;
895
896    if (sd->freeze) return;
897    if ((!sd->bouncemex) && (!sd->bouncemey)) return;
898    if (sd->down.now) return; // down bounce while still held down
899    if (sd->down.onhold_animator)
900      {
901         ecore_animator_del(sd->down.onhold_animator);
902         sd->down.onhold_animator = NULL;
903      }
904    if (sd->down.hold_animator)
905      {
906         ecore_animator_del(sd->down.hold_animator);
907         sd->down.hold_animator = NULL;
908      }
909    sd->pan_func.max_get(sd->pan_obj, &mx, &my);
910    sd->pan_func.min_get(sd->pan_obj, &minx, &miny);
911    sd->pan_func.get(sd->pan_obj, &px, &py);
912    bx = px;
913    by = py;
914    if (px < minx) px = minx;
915    if ((px - minx) > mx) px = mx + minx;
916    if (py < miny) py = miny;
917    if ((py - miny) > my) py = my + miny;
918    b2x = px;
919    b2y = py;
920    if ((!sd->widget) ||
921        (!elm_widget_drag_child_locked_x_get(sd->widget)))
922      {
923         if ((!sd->down.bounce_x_animator) && (!sd->bounce_animator_disabled))
924           {
925              if (sd->bouncemex)
926                {
927                   if (sd->scrollto.x.animator)
928                     {
929                        ecore_animator_del(sd->scrollto.x.animator);
930                        sd->scrollto.x.animator = NULL;
931                     }
932                   sd->down.bounce_x_animator = ecore_animator_add(_smart_bounce_x_animator, sd);
933                   sd->down.anim_start2 = ecore_loop_time_get();
934                   sd->down.bx = bx;
935                   sd->down.bx0 = bx;
936                   sd->down.b2x = b2x;
937                   if (sd->down.momentum_animator) sd->down.b0x = sd->down.ax;
938                   else sd->down.b0x = 0;
939                }
940           }
941      }
942    if ((!sd->widget) ||
943        (!elm_widget_drag_child_locked_y_get(sd->widget)))
944      {
945         if ((!sd->down.bounce_y_animator) && (!sd->bounce_animator_disabled))
946           {
947              if (sd->bouncemey)
948                {
949                   if (sd->scrollto.y.animator)
950                     {
951                        ecore_animator_del(sd->scrollto.y.animator);
952                        sd->scrollto.y.animator = NULL;
953                     }
954                   sd->down.bounce_y_animator = ecore_animator_add(_smart_bounce_y_animator, sd);
955                   sd->down.anim_start3 = ecore_loop_time_get();
956                   sd->down.by = by;
957                   sd->down.by0 = by;
958                   sd->down.b2y = b2y;
959                   if (sd->down.momentum_animator) sd->down.b0y = sd->down.ay;
960                   else sd->down.b0y = 0;
961                }
962           }
963      }
964 }
965
966 void
967 elm_smart_scroller_child_pos_set(Evas_Object *obj, Evas_Coord x, Evas_Coord y)
968 {
969    Evas_Coord mx = 0, my = 0, px = 0, py = 0, minx = 0, miny = 0;
970    double vx, vy;
971
972    API_ENTRY return;
973    // FIXME: allow for bounce outside of range
974    sd->pan_func.max_get(sd->pan_obj, &mx, &my);
975    sd->pan_func.min_get(sd->pan_obj, &minx, &miny);
976    if (mx > 0) vx = (double)(x - minx) / (double)mx;
977    else vx = 0.0;
978    if (vx < 0.0) vx = 0.0;
979    else if (vx > 1.0) vx = 1.0;
980    if (my > 0) vy = (double)(y - miny) / (double)my;
981    else vy = 0.0;
982    if (vy < 0.0) vy = 0.0;
983    else if (vy > 1.0) vy = 1.0;
984    edje_object_part_drag_value_set(sd->edje_obj, "elm.dragable.vbar", 0.0, vy);
985    edje_object_part_drag_value_set(sd->edje_obj, "elm.dragable.hbar", vx, 0.0);
986    sd->pan_func.get(sd->pan_obj, &px, &py);
987    if (!_elm_config->thumbscroll_bounce_enable)
988      {
989         if (x < minx) x = minx;
990         if ((x - minx) > mx) x = mx + minx;
991         if (y < miny) y = miny;
992         if ((y - miny) > my) y = my + miny;
993      }
994
995    if (!sd->bounce_horiz)
996      {
997         if (x < minx) x = minx;
998         if ((x - minx) > mx) x = mx + minx;
999      }
1000    if (!sd->bounce_vert)
1001      {
1002         if (y < miny) y = miny;
1003         if (y - miny > my) y = my + miny;
1004      }
1005
1006    sd->pan_func.set(sd->pan_obj, x, y);
1007    if ((px != x) || (py != y))
1008      edje_object_signal_emit(sd->edje_obj, "elm,action,scroll", "elm");
1009    if (!sd->down.bounce_x_animator)
1010      {
1011         if (((x < minx) && (0 <= sd->down.dx)) ||
1012             ((x > (mx + minx)) && (0 >= sd->down.dx)))
1013           {
1014              sd->bouncemex = 1;
1015              bounce_eval(sd);
1016           }
1017         else
1018           sd->bouncemex = 0;
1019      }
1020    if (!sd->down.bounce_y_animator)
1021      {
1022         if (((y < miny) && (0 <= sd->down.dy)) ||
1023             ((y > (my + miny)) && (0 >= sd->down.dy)))
1024           {
1025              sd->bouncemey = 1;
1026              bounce_eval(sd);
1027           }
1028         else
1029           sd->bouncemey = 0;
1030      }
1031    if ((x != px) || (y != py))
1032      {
1033         evas_object_smart_callback_call(obj, "scroll", NULL);
1034      }
1035    if ((x != px)/* && (!sd->bouncemex)*/)
1036      {
1037         if (x == minx)
1038           evas_object_smart_callback_call(obj, "edge,left", NULL);
1039         if (x == (mx + minx))
1040           evas_object_smart_callback_call(obj, "edge,right", NULL);
1041      }
1042    if ((y != py)/* && (!sd->bouncemey)*/)
1043      {
1044         if (y == miny)
1045           evas_object_smart_callback_call(obj, "edge,top", NULL);
1046         if (y == my + miny)
1047           evas_object_smart_callback_call(obj, "edge,bottom", NULL);
1048      }
1049 }
1050
1051 void
1052 elm_smart_scroller_child_pos_get(Evas_Object *obj, Evas_Coord *x, Evas_Coord *y)
1053 {
1054    API_ENTRY return;
1055    sd->pan_func.get(sd->pan_obj, x, y);
1056 }
1057
1058 /* returns TRUE when we need to move the scroller, FALSE otherwise.
1059  * Updates w and h either way, so save them if you need them. */
1060 static Eina_Bool
1061 _elm_smart_scroller_child_region_show_internal(Evas_Object *obj, Evas_Coord *_x, Evas_Coord *_y, Evas_Coord w, Evas_Coord h)
1062 {
1063    Evas_Coord mx = 0, my = 0, cw = 0, ch = 0, px = 0, py = 0, nx, ny, minx = 0, miny = 0, pw = 0, ph = 0, x = *_x, y = *_y;
1064
1065    API_ENTRY return EINA_FALSE;
1066    sd->pan_func.max_get(sd->pan_obj, &mx, &my);
1067    sd->pan_func.min_get(sd->pan_obj, &minx, &miny);
1068    sd->pan_func.child_size_get(sd->pan_obj, &cw, &ch);
1069    sd->pan_func.get(sd->pan_obj, &px, &py);
1070    evas_object_geometry_get(sd->pan_obj, NULL, NULL, &pw, &ph);
1071
1072    nx = px;
1073    if ((x < px) && ((x + w) < (px + (cw - mx)))) nx = x;
1074    else if ((x > px) && ((x + w) > (px + (cw - mx)))) nx = x + w - (cw - mx);
1075    ny = py;
1076    if ((y < py) && ((y + h) < (py + (ch - my)))) ny = y;
1077    else if ((y > py) && ((y + h) > (py + (ch - my)))) ny = y + h - (ch - my);
1078
1079    if ((sd->down.bounce_x_animator) || (sd->down.bounce_y_animator) ||
1080        (sd->scrollto.x.animator) || (sd->scrollto.y.animator))
1081      {
1082         _smart_anim_stop(sd->smart_obj);
1083      }
1084    if (sd->scrollto.x.animator)
1085      {
1086         ecore_animator_del(sd->scrollto.x.animator);
1087         sd->scrollto.x.animator = NULL;
1088      }
1089    if (sd->scrollto.y.animator)
1090      {
1091         ecore_animator_del(sd->scrollto.y.animator);
1092         sd->scrollto.y.animator = NULL;
1093      }
1094    if (sd->down.bounce_x_animator)
1095      {
1096         ecore_animator_del(sd->down.bounce_x_animator);
1097         sd->down.bounce_x_animator = NULL;
1098         sd->bouncemex = 0;
1099      }
1100    if (sd->down.bounce_y_animator)
1101      {
1102         ecore_animator_del(sd->down.bounce_y_animator);
1103         sd->down.bounce_y_animator = NULL;
1104         sd->bouncemey = 0;
1105      }
1106    if (sd->down.hold_animator)
1107      {
1108         ecore_animator_del(sd->down.hold_animator);
1109         sd->down.hold_animator = NULL;
1110         _smart_drag_stop(sd->smart_obj);
1111      }
1112    if (sd->down.momentum_animator)
1113      {
1114         ecore_animator_del(sd->down.momentum_animator);
1115         sd->down.momentum_animator = NULL;
1116         sd->down.bounce_x_hold = 0;
1117         sd->down.bounce_y_hold = 0;
1118         sd->down.ax = 0;
1119         sd->down.ay = 0;
1120         sd->down.pdx = 0;
1121         sd->down.pdy = 0;
1122      }
1123
1124    x = nx;
1125    if ((x + pw) > cw) x = cw - pw;
1126    if (x < minx) x = minx;
1127    y = ny;
1128    if ((y + ph) > ch) y = ch - ph;
1129    if (y < miny) y = miny;
1130
1131    if ((x == px) && (y == py)) return EINA_FALSE;
1132    *_x = x;
1133    *_y = y;
1134    return EINA_TRUE;
1135 }
1136
1137 /* Set should be used for calculated positions, for example, when we move
1138  * because of an animation or because this is the correct position after
1139  * constraints. */
1140 void
1141 elm_smart_scroller_child_region_set(Evas_Object *obj, Evas_Coord x, Evas_Coord y, Evas_Coord w, Evas_Coord h)
1142 {
1143    if (_elm_smart_scroller_child_region_show_internal(obj, &x, &y, w, h))
1144      elm_smart_scroller_child_pos_set(obj, x, y);
1145 }
1146
1147 /* Set should be used for setting the wanted position, for example a user scroll
1148  * or moving the cursor in an entry. */
1149 void
1150 elm_smart_scroller_child_region_show(Evas_Object *obj, Evas_Coord x, Evas_Coord y, Evas_Coord w, Evas_Coord h)
1151 {
1152    API_ENTRY return;
1153    sd->wx = x;
1154    sd->wy = y;
1155    sd->ww = w;
1156    sd->wh = h;
1157    if (_elm_smart_scroller_child_region_show_internal(obj, &x, &y, w, h))
1158      elm_smart_scroller_child_pos_set(obj, x, y);
1159 }
1160
1161 void
1162 elm_smart_scroller_child_viewport_size_get(Evas_Object *obj, Evas_Coord *w, Evas_Coord *h)
1163 {
1164    API_ENTRY return;
1165    if (!sd->pan_obj) return;
1166    edje_object_calc_force(sd->edje_obj);
1167    evas_object_geometry_get(sd->pan_obj, NULL, NULL, w, h);
1168 }
1169
1170 void
1171 elm_smart_scroller_step_size_set(Evas_Object *obj, Evas_Coord x, Evas_Coord y)
1172 {
1173    API_ENTRY return;
1174    if (x < 1) x = 1;
1175    if (y < 1) y = 1;
1176    sd->step.x = x;
1177    sd->step.y = y;
1178    _smart_scrollbar_size_adjust(sd);
1179 }
1180
1181 void
1182 elm_smart_scroller_step_size_get(Evas_Object *obj, Evas_Coord *x, Evas_Coord *y)
1183 {
1184    API_ENTRY return;
1185    if (x) *x = sd->step.x;
1186    if (y) *y = sd->step.y;
1187 }
1188
1189 void
1190 elm_smart_scroller_page_size_set(Evas_Object *obj, Evas_Coord x, Evas_Coord y)
1191 {
1192    API_ENTRY return;
1193    sd->page.x = x;
1194    sd->page.y = y;
1195    _smart_scrollbar_size_adjust(sd);
1196 }
1197
1198 void
1199 elm_smart_scroller_page_size_get(Evas_Object *obj, Evas_Coord *x, Evas_Coord *y)
1200 {
1201    API_ENTRY return;
1202    if (x) *x = sd->page.x;
1203    if (y) *y = sd->page.y;
1204 }
1205
1206 void
1207 elm_smart_scroller_policy_set(Evas_Object *obj, Elm_Smart_Scroller_Policy hbar, Elm_Smart_Scroller_Policy vbar)
1208 {
1209    API_ENTRY return;
1210    if ((sd->hbar_flags == hbar) && (sd->vbar_flags == vbar)) return;
1211    sd->hbar_flags = hbar;
1212    sd->vbar_flags = vbar;
1213    if (sd->hbar_flags == ELM_SMART_SCROLLER_POLICY_ON)
1214      edje_object_signal_emit(sd->edje_obj, "elm,action,show_always,hbar", "elm");
1215    else if (sd->hbar_flags == ELM_SMART_SCROLLER_POLICY_OFF)
1216      edje_object_signal_emit(sd->edje_obj, "elm,action,hide,hbar", "elm");
1217    else
1218      edje_object_signal_emit(sd->edje_obj, "elm,action,show_notalways,hbar", "elm");
1219    if (sd->vbar_flags == ELM_SMART_SCROLLER_POLICY_ON)
1220      edje_object_signal_emit(sd->edje_obj, "elm,action,show_always,vbar", "elm");
1221    else if (sd->vbar_flags == ELM_SMART_SCROLLER_POLICY_OFF)
1222      edje_object_signal_emit(sd->edje_obj, "elm,action,hide,vbar", "elm");
1223    else
1224      edje_object_signal_emit(sd->edje_obj, "elm,action,show_notalways,vbar", "elm");
1225    _smart_scrollbar_size_adjust(sd);
1226 }
1227
1228 void
1229 elm_smart_scroller_policy_get(Evas_Object *obj, Elm_Smart_Scroller_Policy *hbar, Elm_Smart_Scroller_Policy *vbar)
1230 {
1231    API_ENTRY return;
1232    if (hbar) *hbar = sd->hbar_flags;
1233    if (vbar) *vbar = sd->vbar_flags;
1234 }
1235
1236 Evas_Object *
1237 elm_smart_scroller_edje_object_get(Evas_Object *obj)
1238 {
1239    API_ENTRY return NULL;
1240    return sd->edje_obj;
1241 }
1242
1243 void
1244 elm_smart_scroller_single_dir_set(Evas_Object *obj, Eina_Bool single_dir)
1245 {
1246    API_ENTRY return;
1247    sd->one_dir_at_a_time = single_dir;
1248 }
1249
1250 Eina_Bool
1251 elm_smart_scroller_single_dir_get(Evas_Object *obj)
1252 {
1253    API_ENTRY return EINA_FALSE;
1254    return sd->one_dir_at_a_time;
1255 }
1256
1257 void
1258 elm_smart_scroller_propagate_events_set(Evas_Object *obj, Eina_Bool propagation)
1259 {
1260    API_ENTRY return;
1261    sd->event_propagation = propagation;
1262
1263    evas_object_propagate_events_set(sd->edje_obj, propagation);
1264 }
1265
1266 Eina_Bool
1267 elm_smart_scroller_propagate_events_get(Evas_Object *obj)
1268 {
1269    API_ENTRY return EINA_FALSE;
1270    return sd->event_propagation;
1271 }
1272
1273 void
1274 elm_smart_scroller_object_theme_set(Evas_Object *parent, Evas_Object *obj, const char *clas, const char *group, const char *style)
1275 {
1276    API_ENTRY return;
1277    Evas_Coord mw, mh;
1278    //Does this API require parent object absolutely? if then remove this exception.
1279    double parent_scale = parent ? elm_widget_scale_get(parent) : 1;
1280    _elm_theme_object_set(parent, sd->edje_obj, clas, group, style);
1281    edje_object_scale_set(sd->edje_obj, parent_scale * _elm_config->scale);
1282    if (sd->pan_obj)
1283      edje_object_part_swallow(sd->edje_obj, "elm.swallow.content", sd->pan_obj);
1284    mw = mh = -1;
1285    elm_coords_finger_size_adjust(1, &mw, 1, &mh);
1286    if (edje_object_part_exists(sd->edje_obj, "elm.scrollbar.base"))
1287      {
1288         Evas_Object *base;
1289         base = edje_object_part_swallow_get(sd->edje_obj, "elm.scrollbar.base");
1290         if (!base)
1291           {
1292              base = evas_object_rectangle_add(evas_object_evas_get(sd->edje_obj));
1293              evas_object_color_set(base, 0, 0, 0, 0);
1294              edje_object_part_swallow(sd->edje_obj, "elm.scrollbar.base", base);
1295           }
1296         if (!_elm_config->thumbscroll_enable)
1297           evas_object_size_hint_min_set(base, mw, mh);
1298      }
1299    sd->vbar_visible = !sd->vbar_visible;
1300    sd->hbar_visible = !sd->hbar_visible;
1301    _smart_scrollbar_bar_visibility_adjust(sd);
1302 }
1303
1304 void
1305 elm_smart_scroller_hold_set(Evas_Object *obj, Eina_Bool hold)
1306 {
1307    API_ENTRY return;
1308    sd->hold = hold;
1309 }
1310
1311 void
1312 elm_smart_scroller_freeze_set(Evas_Object *obj, Eina_Bool freeze)
1313 {
1314    API_ENTRY return;
1315    sd->freeze = freeze;
1316    if (sd->freeze)
1317      {
1318         if (sd->down.onhold_animator)
1319           {
1320              ecore_animator_del(sd->down.onhold_animator);
1321              sd->down.onhold_animator = NULL;
1322           }
1323      }
1324    else
1325      bounce_eval(sd);
1326 }
1327
1328 void
1329 elm_smart_scroller_bounce_allow_set(Evas_Object *obj, Eina_Bool horiz, Eina_Bool vert)
1330 {
1331    API_ENTRY return;
1332    sd->bounce_horiz = horiz;
1333    sd->bounce_vert = vert;
1334 }
1335
1336 void
1337 elm_smart_scroller_bounce_allow_get(const Evas_Object *obj, Eina_Bool *horiz, Eina_Bool *vert)
1338 {
1339    API_ENTRY return;
1340    if (horiz) *horiz = sd->bounce_horiz;
1341    if (vert) *vert = sd->bounce_vert;
1342 }
1343
1344 void
1345 elm_smart_scroller_paging_set(Evas_Object *obj, double pagerel_h, double pagerel_v, Evas_Coord pagesize_h, Evas_Coord pagesize_v)
1346 {
1347    API_ENTRY return;
1348    sd->pagerel_h = pagerel_h;
1349    sd->pagerel_v = pagerel_v;
1350    sd->pagesize_h = pagesize_h;
1351    sd->pagesize_v = pagesize_v;
1352    _smart_page_adjust(sd);
1353 }
1354
1355 void
1356 elm_smart_scroller_paging_get(Evas_Object *obj, double *pagerel_h, double *pagerel_v, Evas_Coord *pagesize_h, Evas_Coord *pagesize_v)
1357 {
1358    API_ENTRY return;
1359    if (pagerel_h) *pagerel_h = sd->pagerel_h;
1360    if (pagerel_v) *pagerel_v = sd->pagerel_v;
1361    if (pagesize_h) *pagesize_h = sd->pagesize_h;
1362    if (pagesize_v) *pagesize_v = sd->pagesize_v;
1363 }
1364
1365 void
1366 elm_smart_scroller_current_page_get(Evas_Object *obj, int *pagenumber_h, int *pagenumber_v)
1367 {
1368    API_ENTRY return;
1369    Evas_Coord x, y;
1370    elm_smart_scroller_child_pos_get(sd->smart_obj, &x, &y);
1371    if (pagenumber_h) *pagenumber_h = (x + sd->pagesize_h - 1) / sd->pagesize_h;
1372    if (pagenumber_v) *pagenumber_v = (y + sd->pagesize_v - 1) / sd->pagesize_v;
1373 }
1374
1375 void
1376 elm_smart_scroller_last_page_get(Evas_Object *obj, int *pagenumber_h, int *pagenumber_v)
1377 {
1378    API_ENTRY return;
1379    Evas_Coord cw, ch;
1380    sd->pan_func.child_size_get(sd->pan_obj, &cw, &ch);
1381    if (pagenumber_h)
1382      {
1383         if (sd->pagesize_h > 0)
1384           *pagenumber_h = cw / sd->pagesize_h + 1;
1385         else
1386           *pagenumber_h = 0;
1387      }
1388    if (pagenumber_v)
1389      {
1390         if (sd->pagesize_v > 0)
1391           *pagenumber_v = ch / sd->pagesize_v + 1;
1392         else
1393           *pagenumber_v = 0;
1394      }
1395 }
1396
1397 void
1398 elm_smart_scroller_page_show(Evas_Object *obj, int pagenumber_h, int pagenumber_v)
1399 {
1400    API_ENTRY return;
1401    Evas_Coord x, y, w, h;
1402    elm_smart_scroller_child_viewport_size_get(sd->smart_obj, &w, &h);
1403    if (pagenumber_h) x = sd->pagesize_h * pagenumber_h;
1404    if (pagenumber_v) y = sd->pagesize_v * pagenumber_v;
1405    if (_elm_smart_scroller_child_region_show_internal(obj, &x, &y, w, h))
1406      elm_smart_scroller_child_pos_set(obj, x, y);
1407 }
1408
1409 void
1410 elm_smart_scroller_page_bring_in(Evas_Object *obj, int pagenumber_h, int pagenumber_v)
1411 {
1412    API_ENTRY return;
1413    Evas_Coord x, y, w, h;
1414    elm_smart_scroller_child_viewport_size_get(sd->smart_obj, &w, &h);
1415    if (pagenumber_h) x = sd->pagesize_h * pagenumber_h;
1416    if (pagenumber_v) y = sd->pagesize_v * pagenumber_v;
1417    if (_elm_smart_scroller_child_region_show_internal(obj, &x, &y, w, h))
1418      {
1419         _smart_scrollto_x(sd, _elm_config->bring_in_scroll_friction, x);
1420         _smart_scrollto_y(sd, _elm_config->bring_in_scroll_friction, y);
1421      }
1422 }
1423
1424 void
1425 elm_smart_scroller_region_bring_in(Evas_Object *obj, Evas_Coord x, Evas_Coord y, Evas_Coord w, Evas_Coord h)
1426 {
1427    API_ENTRY return;
1428    if (_elm_smart_scroller_child_region_show_internal(obj, &x, &y, w, h))
1429      {
1430         _smart_scrollto_x(sd, _elm_config->bring_in_scroll_friction, x);
1431         _smart_scrollto_y(sd, _elm_config->bring_in_scroll_friction, y);
1432      }
1433 }
1434
1435 void
1436 elm_smart_scroller_widget_set(Evas_Object *obj, Evas_Object *wid)
1437 {
1438    API_ENTRY return;
1439    sd->widget = wid;
1440 }
1441
1442 static void
1443 _elm_smart_scroller_wanted_region_set(Evas_Object *obj)
1444 {
1445    INTERNAL_ENTRY;
1446    Evas_Coord ww, wh, wx = sd->wx;
1447
1448    if (sd->down.now || sd->down.momentum_animator ||
1449        sd->down.bounce_x_animator || sd->down.bounce_y_animator ||
1450        sd->down.hold_animator || sd->down.onhold_animator) return;
1451
1452    /* Flip to RTL cords only if init in RTL mode */
1453    if (sd->is_mirrored)
1454      wx = _elm_smart_scroller_x_mirrored_get(obj, sd->wx);
1455
1456    if (sd->ww == -1)
1457      {
1458         elm_smart_scroller_child_viewport_size_get(obj, &ww, &wh);
1459      }
1460    else
1461      {
1462         ww = sd->ww;
1463         wh = sd->wh;
1464      }
1465
1466    elm_smart_scroller_child_region_set(obj, wx, sd->wy, ww, wh);
1467 }
1468
1469 /* local subsystem functions */
1470 static void
1471 _smart_edje_drag_v_start(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
1472 {
1473    Smart_Data *sd;
1474
1475    sd = data;
1476    _smart_scrollbar_read(sd);
1477    _smart_drag_start(sd->smart_obj);
1478    sd->freeze = EINA_TRUE;
1479 }
1480
1481 static void
1482 _smart_edje_drag_v_stop(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
1483 {
1484    Smart_Data *sd;
1485
1486    sd = data;
1487    _smart_scrollbar_read(sd);
1488    _smart_drag_stop(sd->smart_obj);
1489    sd->freeze = EINA_FALSE;
1490 }
1491
1492 static void
1493 _smart_edje_drag_v(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
1494 {
1495    Smart_Data *sd;
1496
1497    sd = data;
1498    _smart_scrollbar_read(sd);
1499 }
1500
1501 static void
1502 _smart_edje_drag_h_start(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
1503 {
1504    Smart_Data *sd;
1505
1506    sd = data;
1507    _smart_scrollbar_read(sd);
1508    _smart_drag_start(sd->smart_obj);
1509    sd->freeze = EINA_TRUE;
1510 }
1511
1512 static void
1513 _smart_edje_drag_h_stop(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
1514 {
1515    Smart_Data *sd;
1516
1517    sd = data;
1518    _smart_scrollbar_read(sd);
1519    _smart_drag_stop(sd->smart_obj);
1520    sd->freeze = EINA_FALSE;
1521 }
1522
1523 static void
1524 _smart_edje_drag_h(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
1525 {
1526    Smart_Data *sd;
1527
1528    sd = data;
1529    _smart_scrollbar_read(sd);
1530 }
1531
1532 static void
1533 _smart_child_del_hook(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
1534 {
1535    Smart_Data *sd;
1536
1537    sd = data;
1538    sd->child_obj = NULL;
1539    _smart_scrollbar_size_adjust(sd);
1540    _smart_scrollbar_reset(sd);
1541 }
1542
1543 static void
1544 _smart_pan_changed_hook(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
1545 {
1546    Evas_Coord w, h;
1547    Smart_Data *sd;
1548
1549    sd = data;
1550    sd->pan_func.child_size_get(sd->pan_obj, &w, &h);
1551    if ((w != sd->child.w) || (h != sd->child.h))
1552      {
1553         sd->child.w = w;
1554         sd->child.h = h;
1555         _smart_scrollbar_size_adjust(sd);
1556         evas_object_size_hint_min_set(sd->smart_obj, sd->child.w, sd->child.h);
1557         _elm_smart_scroller_wanted_region_set(sd->smart_obj);
1558      }
1559 }
1560
1561 static void
1562 _smart_pan_pan_changed_hook(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
1563 {
1564    Smart_Data *sd;
1565
1566    sd = data;
1567    if ((sd->down.bounce_x_animator) || (sd->down.bounce_y_animator) ||
1568        (sd->scrollto.x.animator) || (sd->scrollto.y.animator))
1569      {
1570         _smart_anim_stop(sd->smart_obj);
1571      }
1572    if (sd->scrollto.x.animator)
1573      {
1574         ecore_animator_del(sd->scrollto.x.animator);
1575         sd->scrollto.x.animator = NULL;
1576      }
1577    if (sd->scrollto.y.animator)
1578      {
1579         ecore_animator_del(sd->scrollto.y.animator);
1580         sd->scrollto.y.animator = NULL;
1581      }
1582    if (sd->down.bounce_x_animator)
1583      {
1584         ecore_animator_del(sd->down.bounce_x_animator);
1585         sd->down.bounce_x_animator = NULL;
1586         sd->bouncemex = 0;
1587      }
1588    if (sd->down.bounce_y_animator)
1589      {
1590         ecore_animator_del(sd->down.bounce_y_animator);
1591         sd->down.bounce_y_animator = NULL;
1592         sd->bouncemey = 0;
1593      }
1594    _elm_smart_scroller_wanted_region_set(sd->smart_obj);
1595 }
1596
1597 void
1598 elm_smart_scroller_gravity_set(Evas_Object *obj, double x, double y)
1599 {
1600    API_ENTRY return;
1601
1602    sd->pan_func.gravity_set(sd->pan_obj, x, y);
1603 }
1604
1605 void
1606 elm_smart_scroller_gravity_get(Evas_Object *obj, double *x, double *y)
1607 {
1608    API_ENTRY return;
1609
1610    sd->pan_func.gravity_get(sd->pan_obj, x, y);
1611 }
1612
1613 static void
1614 _smart_event_wheel(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
1615 {
1616    Evas_Event_Mouse_Wheel *ev;
1617    Smart_Data *sd;
1618    Evas_Coord x = 0, y = 0;
1619
1620    sd = data;
1621    ev = event_info;
1622    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return ;
1623    if ((evas_key_modifier_is_set(ev->modifiers, "Control")) ||
1624        (evas_key_modifier_is_set(ev->modifiers, "Alt")) ||
1625        (evas_key_modifier_is_set(ev->modifiers, "Shift")) ||
1626        (evas_key_modifier_is_set(ev->modifiers, "Meta")) ||
1627        (evas_key_modifier_is_set(ev->modifiers, "Hyper")) ||
1628        (evas_key_modifier_is_set(ev->modifiers, "Super")))
1629      return;
1630    elm_smart_scroller_child_pos_get(sd->smart_obj, &x, &y);
1631    if ((sd->down.bounce_x_animator) || (sd->down.bounce_y_animator) ||
1632        (sd->scrollto.x.animator) || (sd->scrollto.y.animator))
1633      {
1634         _smart_anim_stop(sd->smart_obj);
1635      }
1636    if (sd->scrollto.x.animator)
1637      {
1638         ecore_animator_del(sd->scrollto.x.animator);
1639         sd->scrollto.x.animator = NULL;
1640      }
1641    if (sd->scrollto.y.animator)
1642      {
1643         ecore_animator_del(sd->scrollto.y.animator);
1644         sd->scrollto.y.animator = NULL;
1645      }
1646    if (sd->down.bounce_x_animator)
1647      {
1648         ecore_animator_del(sd->down.bounce_x_animator);
1649         sd->down.bounce_x_animator = NULL;
1650         sd->bouncemex = 0;
1651      }
1652    if (sd->down.bounce_y_animator)
1653      {
1654         ecore_animator_del(sd->down.bounce_y_animator);
1655         sd->down.bounce_y_animator = NULL;
1656         sd->bouncemey = 0;
1657      }
1658    if (!ev->direction)
1659      y += ev->z * sd->step.y;
1660    else if (ev->direction == 1)
1661      x += ev->z * sd->step.x;
1662
1663    if ((!sd->hold) && (!sd->freeze))
1664      {
1665         _update_wanted_coordinates(sd, x, y);
1666         elm_smart_scroller_child_pos_set(sd->smart_obj, x, y);
1667      }
1668 }
1669
1670 static void
1671 _smart_event_mouse_down(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
1672 {
1673    Evas_Event_Mouse_Down *ev;
1674    Smart_Data *sd;
1675    Evas_Coord x = 0, y = 0;
1676
1677    sd = data;
1678    ev = event_info;
1679    //   if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return ;
1680    if (_elm_config->thumbscroll_enable)
1681      {
1682         sd->down.hold = 0;
1683         if ((sd->down.bounce_x_animator) || (sd->down.bounce_y_animator) ||
1684             (sd->down.momentum_animator) || (sd->scrollto.x.animator) ||
1685             (sd->scrollto.y.animator))
1686           {
1687              ev->event_flags |= EVAS_EVENT_FLAG_ON_SCROLL | EVAS_EVENT_FLAG_ON_HOLD;
1688              sd->down.scroll = 1;
1689              sd->down.hold = 1;
1690              _smart_anim_stop(sd->smart_obj);
1691           }
1692         if (sd->scrollto.x.animator)
1693           {
1694              ecore_animator_del(sd->scrollto.x.animator);
1695              sd->scrollto.x.animator = NULL;
1696           }
1697         if (sd->scrollto.y.animator)
1698           {
1699              ecore_animator_del(sd->scrollto.y.animator);
1700              sd->scrollto.y.animator = NULL;
1701           }
1702         if (sd->down.bounce_x_animator)
1703           {
1704              ecore_animator_del(sd->down.bounce_x_animator);
1705              sd->down.bounce_x_animator = NULL;
1706              sd->bouncemex = 0;
1707           }
1708         if (sd->down.bounce_y_animator)
1709           {
1710              ecore_animator_del(sd->down.bounce_y_animator);
1711              sd->down.bounce_y_animator = NULL;
1712              sd->bouncemey = 0;
1713           }
1714         if (sd->down.hold_animator)
1715           {
1716              ecore_animator_del(sd->down.hold_animator);
1717              sd->down.hold_animator = NULL;
1718              _smart_drag_stop(sd->smart_obj);
1719           }
1720         if (sd->down.momentum_animator)
1721           {
1722              ecore_animator_del(sd->down.momentum_animator);
1723              sd->down.momentum_animator = NULL;
1724              sd->down.bounce_x_hold = 0;
1725              sd->down.bounce_y_hold = 0;
1726              sd->down.ax = 0;
1727              sd->down.ay = 0;
1728           }
1729         if (ev->button == 1)
1730           {
1731              sd->down.hist.est_timestamp_diff =
1732                 ecore_loop_time_get() - ((double)ev->timestamp / 1000.0);
1733              sd->down.hist.tadd = 0.0;
1734              sd->down.hist.dxsum = 0.0;
1735              sd->down.hist.dysum = 0.0;
1736              sd->down.now = 1;
1737              sd->down.dragged = 0;
1738              sd->down.dir_x = 0;
1739              sd->down.dir_y = 0;
1740              sd->down.x = ev->canvas.x;
1741              sd->down.y = ev->canvas.y;
1742              elm_smart_scroller_child_pos_get(sd->smart_obj, &x, &y);
1743              sd->down.sx = x;
1744              sd->down.sy = y;
1745              sd->down.locked = 0;
1746              memset(&(sd->down.history[0]), 0, sizeof(sd->down.history[0]) * 60);
1747 #ifdef EVTIME
1748              sd->down.history[0].timestamp = ev->timestamp / 1000.0;
1749              sd->down.history[0].localtimestamp = ecore_loop_time_get();
1750 #else
1751              sd->down.history[0].timestamp = ecore_loop_time_get();
1752 #endif
1753              sd->down.history[0].x = ev->canvas.x;
1754              sd->down.history[0].y = ev->canvas.y;
1755           }
1756         sd->down.dragged_began = 0;
1757         sd->down.hold_parent = 0;
1758         sd->down.cancelled = 0;
1759         if(sd->hold || sd->freeze)
1760            sd->down.want_reset = 1;
1761         else
1762            sd->down.want_reset = 0;
1763      }
1764 }
1765
1766 static void
1767 _down_coord_eval(Smart_Data *sd, Evas_Coord *x, Evas_Coord *y)
1768 {
1769    Evas_Coord minx, miny;
1770
1771    if (sd->down.dir_x) *x = sd->down.sx - (*x - sd->down.x);
1772    else *x = sd->down.sx;
1773    if (sd->down.dir_y) *y = sd->down.sy - (*y - sd->down.y);
1774    else *y = sd->down.sy;
1775
1776    if ((sd->down.dir_x) || (sd->down.dir_y))
1777      {
1778         if (!((sd->down.dir_x) && (sd->down.dir_y)))
1779           {
1780              if (sd->down.dir_x) *y = sd->down.locked_y;
1781              else *x = sd->down.locked_x;
1782           }
1783      }
1784
1785    sd->pan_func.min_get(sd->pan_obj, &minx, &miny);
1786
1787    if (*x < minx)
1788      *x += (minx - *x) * _elm_config->thumbscroll_border_friction;
1789    else if (sd->child.w <= sd->w)
1790      *x += (sd->down.sx - *x) * _elm_config->thumbscroll_border_friction;
1791    else if ((sd->child.w - sd->w + minx) < *x)
1792      *x += (sd->child.w - sd->w + minx - *x) *
1793         _elm_config->thumbscroll_border_friction;
1794
1795    if (*y < miny)
1796      *y += (miny - *y) * _elm_config->thumbscroll_border_friction;
1797    else if (sd->child.h <= sd->h)
1798      *y += (sd->down.sy - *y) * _elm_config->thumbscroll_border_friction;
1799    else if ((sd->child.h - sd->h + miny) < *y)
1800      *y += (sd->child.h - sd->h + miny - *y) *
1801         _elm_config->thumbscroll_border_friction;
1802 }
1803
1804 static Eina_Bool
1805 _smart_hold_animator(void *data)
1806 {
1807    Smart_Data *sd = data;
1808    Evas_Coord ox = 0, oy = 0, fx = 0, fy= 0;
1809
1810    fx = sd->down.hold_x;
1811    fy = sd->down.hold_y;
1812
1813    if ((!sd->hold) && (!sd->freeze) && (_elm_config->scroll_smooth_time_interval > 0.0))
1814      {
1815         int i, count = 0; //count for the real event number we have to deal with
1816         int queue_size = 10; //for event queue size
1817         int src_index = 0, dst_index = 0;
1818         int xsum = 0, ysum=0;
1819         Evas_Coord  x=0, y=0;
1820
1821         struct {
1822              Evas_Coord x, y;
1823              double t;
1824         } pos[queue_size];
1825
1826         double tdiff, tnow;
1827         double time_interval=_elm_config->scroll_smooth_time_interval;
1828         // FIXME: assume server and client have the same "timezone"
1829         // (0 timepoint) for now. this needs to be figured out in advance
1830         // though.
1831         tdiff = sd->down.hist.est_timestamp_diff;
1832         tnow = ecore_time_get() - tdiff;
1833
1834         for(i = 0; i < queue_size; i++)
1835           {
1836              x = sd->down.history[i].x;
1837              y = sd->down.history[i].y;
1838
1839              //if there is no history value , we don't deal with it
1840              //if there is better wat to know existance of history value , I will modify this code to it
1841              if ( (x == 0) && (y == 0) )
1842                {
1843                   break;
1844                }
1845              _down_coord_eval(sd, &x, &y);
1846
1847              pos[i].x = x;
1848              pos[i].y = y;
1849              pos[i].t = tnow - sd->down.history[i].timestamp;
1850           }
1851         count = --i;
1852
1853         // we only deal with smooth scroll there is enough history
1854         for(i = 0; i < queue_size; i++)
1855           {
1856              if (src_index > count) break;
1857              if (i == 0)
1858                {
1859                   xsum = pos[i].x;
1860                   ysum = pos[i].y;
1861                   dst_index++;
1862                   continue;
1863                }
1864              while ((pos[src_index].t < time_interval *i) &&
1865                      (src_index <= count))
1866                {
1867                   src_index++;
1868                }
1869              if (src_index <= count)
1870                {
1871                   xsum += pos[src_index].x;
1872                   ysum += pos[src_index].y;
1873                   dst_index++;
1874                }
1875           }
1876         fx = xsum / dst_index;
1877         fy = ysum / dst_index;
1878      }
1879
1880    elm_smart_scroller_child_pos_get(sd->smart_obj, &ox, &oy);
1881    if (sd->down.dir_x)
1882      {
1883         if ((!sd->widget) ||
1884             (!elm_widget_drag_child_locked_x_get(sd->widget)))
1885           ox = fx;
1886      }
1887    if (sd->down.dir_y)
1888      {
1889         if ((!sd->widget) ||
1890             (!elm_widget_drag_child_locked_y_get(sd->widget)))
1891           oy = fy;
1892      }
1893
1894    elm_smart_scroller_child_pos_set(sd->smart_obj, ox, oy);
1895    return ECORE_CALLBACK_RENEW;
1896 }
1897
1898 static Eina_Bool
1899 _smart_event_post_up(void *data, Evas *e __UNUSED__)
1900 {
1901    Smart_Data *sd = data;
1902    if (sd->widget)
1903      {
1904         if (sd->down.dragged)
1905           {
1906              elm_widget_drag_lock_x_set(sd->widget, 0);
1907              elm_widget_drag_lock_y_set(sd->widget, 0);
1908           }
1909      }
1910    return EINA_TRUE;
1911 }
1912
1913 static void
1914 _smart_event_mouse_up(void *data, Evas *e, Evas_Object *obj __UNUSED__, void *event_info)
1915 {
1916    Evas_Event_Mouse_Down *ev;
1917    Smart_Data *sd;
1918    Evas_Coord x = 0, y = 0, ox = 0, oy = 0;
1919    Evas_Coord vw, vh, aw, ah;
1920
1921    sd = data;
1922    ev = event_info;
1923    sd->down.hold_parent = 0;
1924    sd->down.dx = 0;
1925    sd->down.dy = 0;
1926    //   if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return ;
1927    evas_post_event_callback_push(e, _smart_event_post_up, sd);
1928    // FIXME: respect elm_widget_scroll_hold_get of parent container
1929    if (_elm_config->thumbscroll_enable)
1930      {
1931         if (ev->button == 1)
1932           {
1933              if (sd->down.onhold_animator)
1934                {
1935                   ecore_animator_del(sd->down.onhold_animator);
1936                   sd->down.onhold_animator = NULL;
1937                }
1938              x = ev->canvas.x - sd->down.x;
1939              y = ev->canvas.y - sd->down.y;
1940              if (sd->down.dragged)
1941                {
1942                   _smart_drag_stop(sd->smart_obj);
1943                   if ((!sd->hold) && (!sd->freeze))
1944                     {
1945                        double t, at, dt;
1946                        int i;
1947                        Evas_Coord ax, ay, dx, dy, vel;
1948
1949 #ifdef EVTIME
1950                        t = ev->timestamp / 1000.0;
1951 #else
1952                        t = ecore_loop_time_get();
1953 #endif
1954                        ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
1955                        ax = ev->canvas.x;
1956                        ay = ev->canvas.y;
1957                        at = 0.0;
1958 #ifdef SCROLLDBG
1959                        printf("------ %i %i\n", ev->canvas.x, ev->canvas.y);
1960 #endif
1961                        for (i = 0; i < 60; i++)
1962                          {
1963                             dt = t - sd->down.history[i].timestamp;
1964                             if (dt > 0.2) break;
1965 #ifdef SCROLLDBG
1966                             printf("H: %i %i @ %1.3f\n",
1967                                    sd->down.history[i].x,
1968                                    sd->down.history[i].y, dt);
1969 #endif
1970                             at += dt;
1971                             ax += sd->down.history[i].x;
1972                             ay += sd->down.history[i].y;
1973                          }
1974                        ax /= (i + 1);
1975                        ay /= (i + 1);
1976                        at /= (i + 1);
1977                        at /= _elm_config->thumbscroll_sensitivity_friction;
1978                        dx = ev->canvas.x - ax;
1979                        dy = ev->canvas.y - ay;
1980                        if (at > 0)
1981                          {
1982                             vel = sqrt((dx * dx) + (dy * dy)) / at;
1983                             if ((_elm_config->thumbscroll_friction > 0.0) &&
1984                                 (vel > _elm_config->thumbscroll_momentum_threshold))
1985                               {
1986                                  int minx, miny, mx, my, px, py;
1987                                  sd->pan_func.min_get(sd->pan_obj, &minx, &miny);
1988                                  sd->pan_func.max_get(sd->pan_obj, &mx, &my);
1989                                  sd->pan_func.get(sd->pan_obj, &px, &py);
1990                                  sd->down.dx = ((double)dx / at);
1991                                  sd->down.dy = ((double)dy / at);
1992                                  if (((sd->down.dx > 0) && (sd->down.pdx > 0)) ||
1993                                      ((sd->down.dx < 0) && (sd->down.pdx < 0)))
1994                                    if (px > minx && px < mx)
1995                                      sd->down.dx += (double)sd->down.pdx * 1.5; // FIXME: * 1.5 - probably should be config
1996                                  if (((sd->down.dy > 0) && (sd->down.pdy > 0)) ||
1997                                      ((sd->down.dy < 0) && (sd->down.pdy < 0)))
1998                                    if (py > miny && py < my)
1999                                      sd->down.dy += (double)sd->down.pdy * 1.5; // FIXME: * 1.5 - probably should be config
2000                                  if (((sd->down.dx > 0) && (sd->down.pdx > 0)) ||
2001                                      ((sd->down.dx < 0) && (sd->down.pdx < 0)) ||
2002                                      ((sd->down.dy > 0) && (sd->down.pdy > 0)) ||
2003                                      ((sd->down.dy < 0) && (sd->down.pdy < 0)))
2004                                    {
2005                                       double t = ecore_loop_time_get();
2006                                       double dt = t - sd->down.anim_start;
2007
2008                                       if (dt < 0.0) dt = 0.0;
2009                                       else if (dt > _elm_config->thumbscroll_friction)
2010                                         dt = _elm_config->thumbscroll_friction;
2011                                       sd->down.extra_time = _elm_config->thumbscroll_friction - dt;
2012                                    }
2013                                  else
2014                                    sd->down.extra_time = 0.0;
2015                                  elm_smart_scroller_child_viewport_size_get(sd->smart_obj, &vw, &vh);
2016                                  aw = abs(sd->down.dx);
2017                                  if (aw  > vw*3)
2018                                    {
2019                                       if (sd->down.dx > 0) sd->down.dx = vw*3;
2020                                       else sd->down.dx = -(vw*3);
2021                                    }
2022                                  ah = abs(sd->down.dy);
2023                                  if (ah  > vh*3)
2024                                    {
2025                                       if (sd->down.dy > 0) sd->down.dy = vh*3;
2026                                       else sd->down.dy = -(vh*3);
2027                                    }
2028                                  sd->down.pdx = sd->down.dx;
2029                                  sd->down.pdy = sd->down.dy;
2030                                  ox = -sd->down.dx;
2031                                  oy = -sd->down.dy;
2032                                  if (!_smart_do_page(sd))
2033                                    {
2034                                       if ((!sd->down.momentum_animator) && (!sd->momentum_animator_disabled))
2035                                         {
2036                                            sd->down.momentum_animator = ecore_animator_add(_smart_momentum_animator, sd);
2037                                            ev->event_flags |= EVAS_EVENT_FLAG_ON_SCROLL;
2038                                            _smart_anim_start(sd->smart_obj);
2039                                         }
2040                                       sd->down.anim_start = ecore_loop_time_get();
2041                                       elm_smart_scroller_child_pos_get(sd->smart_obj, &x, &y);
2042                                       sd->down.sx = x;
2043                                       sd->down.sy = y;
2044                                       sd->down.b0x = 0;
2045                                       sd->down.b0y = 0;
2046                                    }
2047                               }
2048                          }
2049                     }
2050                   else
2051                     {
2052                        sd->down.pdx = 0;
2053                        sd->down.pdy = 0;
2054                     }
2055                   evas_event_feed_hold(e, 0, ev->timestamp, ev->data);
2056                   if (_smart_do_page(sd))
2057                     {
2058                        Evas_Coord pgx, pgy;
2059
2060                        elm_smart_scroller_child_pos_get(sd->smart_obj, &x, &y);
2061                        if ((!sd->widget) ||
2062                            (!elm_widget_drag_child_locked_x_get(sd->widget)))
2063                          {
2064                             pgx = _smart_page_x_get(sd, ox);
2065                             if (pgx != x)
2066                               {
2067                                  ev->event_flags |= EVAS_EVENT_FLAG_ON_SCROLL;
2068                                  _smart_scrollto_x(sd, _elm_config->page_scroll_friction, pgx);
2069                               }
2070                          }
2071                        if ((!sd->widget) ||
2072                            (!elm_widget_drag_child_locked_y_get(sd->widget)))
2073                          {
2074                             pgy = _smart_page_y_get(sd, oy);
2075                             if (pgy != y)
2076                               {
2077                                  ev->event_flags |= EVAS_EVENT_FLAG_ON_SCROLL;
2078                                  _smart_scrollto_y(sd, _elm_config->page_scroll_friction, pgy);
2079                               }
2080                          }
2081                     }
2082                }
2083              else
2084                {
2085                   sd->down.pdx = 0;
2086                   sd->down.pdy = 0;
2087                   if (_smart_do_page(sd))
2088                     {
2089                        Evas_Coord pgx, pgy;
2090
2091                        elm_smart_scroller_child_pos_get(sd->smart_obj, &x, &y);
2092                        if ((!sd->widget) ||
2093                            (!elm_widget_drag_child_locked_x_get(sd->widget)))
2094                          {
2095                             pgx = _smart_page_x_get(sd, ox);
2096                             if (pgx != x) _smart_scrollto_x(sd, _elm_config->page_scroll_friction, pgx);
2097                          }
2098                        if ((!sd->widget) ||
2099                            (!elm_widget_drag_child_locked_y_get(sd->widget)))
2100                          {
2101                             pgy = _smart_page_y_get(sd, oy);
2102                             if (pgy != y) _smart_scrollto_y(sd, _elm_config->page_scroll_friction, pgy);
2103                          }
2104                     }
2105                }
2106              if (sd->down.hold_animator)
2107                {
2108                   ecore_animator_del(sd->down.hold_animator);
2109                   sd->down.hold_animator = NULL;
2110                }
2111              if (sd->down.scroll)
2112                {
2113                   ev->event_flags |= EVAS_EVENT_FLAG_ON_SCROLL;
2114                   sd->down.scroll = 0;
2115                }
2116              if (sd->down.hold)
2117                {
2118                   ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
2119                   sd->down.hold = 0;
2120                }
2121              sd->down.dragged_began = 0;
2122              sd->down.dir_x = 0;
2123              sd->down.dir_y = 0;
2124              sd->down.want_dragged = 0;
2125              sd->down.dragged = 0;
2126              sd->down.now = 0;
2127              elm_smart_scroller_child_pos_get(sd->smart_obj, &x, &y);
2128              elm_smart_scroller_child_pos_set(sd->smart_obj, x, y);
2129              _update_wanted_coordinates(sd, x, y);
2130
2131              if (!_smart_do_page(sd))
2132                bounce_eval(sd);
2133           }
2134      }
2135 }
2136
2137 static Eina_Bool
2138 _smart_onhold_animator(void *data)
2139 {
2140    Smart_Data *sd;
2141    double t, td;
2142    double vx, vy;
2143    Evas_Coord x, y, ox, oy;
2144
2145    sd = data;
2146    t = ecore_loop_time_get();
2147    if (sd->down.onhold_tlast > 0.0)
2148      {
2149         td = t - sd->down.onhold_tlast;
2150         vx = sd->down.onhold_vx * td * (double)_elm_config->thumbscroll_threshold * 2.0;
2151         vy = sd->down.onhold_vy * td * (double)_elm_config->thumbscroll_threshold * 2.0;
2152         elm_smart_scroller_child_pos_get(sd->smart_obj, &ox, &oy);
2153         x = ox;
2154         y = oy;
2155
2156         if (sd->down.dir_x)
2157           {
2158              if ((!sd->widget) ||
2159                  (!elm_widget_drag_child_locked_x_get(sd->widget)))
2160                {
2161                   sd->down.onhold_vxe += vx;
2162                   x = ox + (int)sd->down.onhold_vxe;
2163                   sd->down.onhold_vxe -= (int)sd->down.onhold_vxe;
2164                }
2165           }
2166
2167         if (sd->down.dir_y)
2168           {
2169              if ((!sd->widget) ||
2170                  (!elm_widget_drag_child_locked_y_get(sd->widget)))
2171                {
2172                   sd->down.onhold_vye += vy;
2173                   y = oy + (int)sd->down.onhold_vye;
2174                   sd->down.onhold_vye -= (int)sd->down.onhold_vye;
2175                }
2176           }
2177
2178         elm_smart_scroller_child_pos_set(sd->smart_obj, x, y);
2179      }
2180    sd->down.onhold_tlast = t;
2181    return ECORE_CALLBACK_RENEW;
2182 }
2183
2184 static Eina_Bool
2185 _smart_event_post_move(void *data, Evas *e __UNUSED__)
2186 {
2187    Smart_Data *sd = data;
2188
2189    if (sd->down.want_dragged)
2190      {
2191         int start = 0;
2192
2193         if (sd->down.hold_parent)
2194           {
2195              if ((sd->down.dir_x) && !can_scroll(sd, sd->down.hdir))
2196                {
2197                   sd->down.dir_x = 0;
2198                }
2199              if ((sd->down.dir_y) && !can_scroll(sd, sd->down.vdir))
2200                {
2201                   sd->down.dir_y = 0;
2202                }
2203           }
2204         if (sd->down.dir_x)
2205           {
2206              if ((!sd->widget) ||
2207                  (!elm_widget_drag_child_locked_x_get(sd->widget)))
2208                {
2209                   sd->down.want_dragged = 0;
2210                   sd->down.dragged = 1;
2211                   if (sd->widget)
2212                     {
2213                        elm_widget_drag_lock_x_set(sd->widget, 1);
2214                     }
2215                   start = 1;
2216                }
2217              else
2218                sd->down.dir_x = 0;
2219           }
2220         if (sd->down.dir_y)
2221           {
2222              if ((!sd->widget) ||
2223                  (!elm_widget_drag_child_locked_y_get(sd->widget)))
2224                {
2225                   sd->down.want_dragged = 0;
2226                   sd->down.dragged = 1;
2227                   if (sd->widget)
2228                     {
2229                        elm_widget_drag_lock_y_set(sd->widget, 1);
2230                     }
2231                   start = 1;
2232                }
2233              else
2234                sd->down.dir_y = 0;
2235           }
2236         if ((!sd->down.dir_x) && (!sd->down.dir_y))
2237           {
2238              sd->down.cancelled = 1;
2239           }
2240         if (start) _smart_drag_start(sd->smart_obj);
2241      }
2242    return EINA_TRUE;
2243 }
2244
2245 static void
2246 _smart_event_mouse_move(void *data, Evas *e, Evas_Object *obj __UNUSED__, void *event_info)
2247 {
2248    Evas_Event_Mouse_Move *ev;
2249    Smart_Data *sd;
2250    Evas_Coord x = 0, y = 0;
2251
2252    sd = data;
2253    ev = event_info;
2254    //   if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return ;
2255    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) sd->down.hold_parent = 1;
2256    evas_post_event_callback_push(e, _smart_event_post_move, sd);
2257    // FIXME: respect elm_widget_scroll_hold_get of parent container
2258    if (_elm_config->thumbscroll_enable)
2259      {
2260         if (sd->down.now)
2261           {
2262              int dodir = 0;
2263
2264 #ifdef SCROLLDBG
2265              printf("::: %i %i\n", ev->cur.canvas.x, ev->cur.canvas.y);
2266 #endif
2267              memmove(&(sd->down.history[1]), &(sd->down.history[0]),
2268                      sizeof(sd->down.history[0]) * (60 - 1));
2269 #ifdef EVTIME
2270              sd->down.history[0].timestamp = ev->timestamp / 1000.0;
2271              sd->down.history[0].localtimestamp = ecore_loop_time_get();
2272 #else
2273              sd->down.history[0].timestamp = ecore_loop_time_get();
2274 #endif
2275              sd->down.history[0].x = ev->cur.canvas.x;
2276              sd->down.history[0].y = ev->cur.canvas.y;
2277
2278              if (!sd->down.dragged_began)
2279                {
2280                   x = ev->cur.canvas.x - sd->down.x;
2281                   y = ev->cur.canvas.y - sd->down.y;
2282
2283                   sd->down.hdir = -1;
2284                   sd->down.vdir = -1;
2285
2286                   if      (x > 0) sd->down.hdir = LEFT;
2287                   else if (x < 0) sd->down.hdir = RIGHT;
2288                   if      (y > 0) sd->down.vdir = UP;
2289                   else if (y < 0) sd->down.vdir = DOWN;
2290
2291                   if (x < 0) x = -x;
2292                   if (y < 0) y = -y;
2293
2294                   if ((sd->one_dir_at_a_time) &&
2295                       (!((sd->down.dir_x) || (sd->down.dir_y))))
2296                     {
2297                        if (x > _elm_config->thumbscroll_threshold)
2298                          {
2299                             if (x > (y * 2))
2300                               {
2301                                  sd->down.dir_x = 1;
2302                                  sd->down.dir_y = 0;
2303                                  dodir++;
2304                               }
2305                          }
2306                        if (y > _elm_config->thumbscroll_threshold)
2307                          {
2308                             if (y > (x * 2))
2309                               {
2310                                  sd->down.dir_x = 0;
2311                                  sd->down.dir_y = 1;
2312                                  dodir++;
2313                               }
2314                          }
2315                        if (!dodir)
2316                          {
2317                             sd->down.dir_x = 1;
2318                             sd->down.dir_y = 1;
2319                          }
2320                     }
2321                   else
2322                     {
2323                        //                       can_scroll(sd, LEFT);
2324                        //                       can_scroll(sd, RIGHT);
2325                        //                       can_scroll(sd, UP);
2326                        //                       can_scroll(sd, DOWN);
2327                        sd->down.dir_x = 1;
2328                        sd->down.dir_y = 1;
2329                     }
2330                }
2331              if ((!sd->hold) && (!sd->freeze))
2332                {
2333                   if ((sd->down.dragged) ||
2334                       (((x * x) + (y * y)) >
2335                        (_elm_config->thumbscroll_threshold *
2336                         _elm_config->thumbscroll_threshold)))
2337                     {
2338                        sd->down.dragged_began = 1;
2339                        if (!sd->down.dragged)
2340                          {
2341                             sd->down.want_dragged = 1;
2342                             ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
2343                             //                            evas_event_feed_hold(e, 1, ev->timestamp, ev->data);
2344                             //                            _smart_drag_start(sd->smart_obj);
2345                          }
2346                        if (sd->down.dragged)
2347                          {
2348                             ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
2349                          }
2350                        //                       ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
2351                        //                       sd->down.dragged = 1;
2352                        if (sd->down.dir_x)
2353                          x = sd->down.sx - (ev->cur.canvas.x - sd->down.x);
2354                        else
2355                          x = sd->down.sx;
2356                        if (sd->down.dir_y)
2357                          y = sd->down.sy - (ev->cur.canvas.y - sd->down.y);
2358                        else
2359                          y = sd->down.sy;
2360                        if(sd->down.want_reset)
2361                          {
2362                             sd->down.x = ev->cur.canvas.x;
2363                             sd->down.y = ev->cur.canvas.y;
2364                             sd->down.want_reset = 0;
2365                          }
2366                        if ((sd->down.dir_x) || (sd->down.dir_y))
2367                          {
2368                             if (!sd->down.locked)
2369                               {
2370                                  sd->down.locked_x = x;
2371                                  sd->down.locked_y = y;
2372                                  sd->down.locked = 1;
2373                               }
2374                             if (!((sd->down.dir_x) && (sd->down.dir_y)))
2375                               {
2376                                  if (sd->down.dir_x) y = sd->down.locked_y;
2377                                  else x = sd->down.locked_x;
2378                               }
2379                          }
2380                          {
2381                             Evas_Coord minx, miny;
2382                             sd->pan_func.min_get(sd->pan_obj, &minx, &miny);
2383                             if (y < miny)
2384                               y += (miny - y) *
2385                                  _elm_config->thumbscroll_border_friction;
2386                             else if (sd->child.h <= sd->h)
2387                               y += (sd->down.sy - y) *
2388                                  _elm_config->thumbscroll_border_friction;
2389                             else if ((sd->child.h - sd->h + miny) < y)
2390                               y += (sd->child.h - sd->h + miny - y) *
2391                                  _elm_config->thumbscroll_border_friction;
2392                             if (x < minx)
2393                               x += (minx - x) *
2394                                  _elm_config->thumbscroll_border_friction;
2395                             else if (sd->child.w <= sd->w)
2396                               x += (sd->down.sx - x) *
2397                                  _elm_config->thumbscroll_border_friction;
2398                             else if ((sd->child.w - sd->w + minx) < x)
2399                               x += (sd->child.w - sd->w + minx - x) *
2400                                  _elm_config->thumbscroll_border_friction;
2401                          }
2402
2403                        sd->down.hold_x = x;
2404                        sd->down.hold_y = y;
2405                        if (!sd->down.hold_animator)
2406                          sd->down.hold_animator =
2407                             ecore_animator_add(_smart_hold_animator, sd);
2408                        //                       printf("a %i %i\n", sd->down.hold_x, sd->down.hold_y);
2409                        //                       _smart_onhold_animator(sd);
2410                        //                       elm_smart_scroller_child_pos_set(sd->smart_obj, x, y);
2411                     }
2412                   else
2413                     {
2414                        if (sd->down.dragged_began)
2415                          {
2416                             ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
2417                             if (!sd->down.hold)
2418                               {
2419                                  sd->down.hold = 1;
2420                                  evas_event_feed_hold(e, 1, ev->timestamp, ev->data);
2421                               }
2422                          }
2423                     }
2424                }
2425              else if (!sd->freeze)
2426                {
2427                   Evas_Coord ex, ey, ew, eh;
2428                   double vx = 0.0, vy = 0.0;
2429
2430                   evas_object_geometry_get(sd->event_obj, &ex, &ey, &ew, &eh);
2431                   x = ev->cur.canvas.x - ex;
2432                   y = ev->cur.canvas.y - ey;
2433                   if (x < _elm_config->thumbscroll_threshold)
2434                     {
2435                        if (_elm_config->thumbscroll_threshold > 0.0)
2436                          vx = -(double)(_elm_config->thumbscroll_threshold - x) /
2437                             _elm_config->thumbscroll_threshold;
2438                        else
2439                          vx = -1.0;
2440                     }
2441                   else if (x > (ew - _elm_config->thumbscroll_threshold))
2442                     {
2443                        if (_elm_config->thumbscroll_threshold > 0.0)
2444                          vx = (double)(_elm_config->thumbscroll_threshold - (ew - x)) /
2445                             _elm_config->thumbscroll_threshold;
2446                        else
2447                          vx = 1.0;
2448                     }
2449                   if (y < _elm_config->thumbscroll_threshold)
2450                     {
2451                        if (_elm_config->thumbscroll_threshold > 0.0)
2452                          vy = -(double)(_elm_config->thumbscroll_threshold - y) /
2453                             _elm_config->thumbscroll_threshold;
2454                        else
2455                          vy = -1.0;
2456                     }
2457                   else if (y > (eh - _elm_config->thumbscroll_threshold))
2458                     {
2459                        if (_elm_config->thumbscroll_threshold > 0.0)
2460                          vy = (double)(_elm_config->thumbscroll_threshold - (eh - y)) /
2461                             _elm_config->thumbscroll_threshold;
2462                        else
2463                          vy = 1.0;
2464                     }
2465                   if ((vx != 0.0) || (vy != 0.0))
2466                     {
2467                        sd->down.onhold_vx = vx;
2468                        sd->down.onhold_vy = vy;
2469                        if (!sd->down.onhold_animator)
2470                          {
2471                             sd->down.onhold_vxe = 0.0;
2472                             sd->down.onhold_vye = 0.0;
2473                             sd->down.onhold_tlast = 0.0;
2474                             sd->down.onhold_animator = ecore_animator_add(_smart_onhold_animator, sd);
2475                          }
2476                        //                       printf("b %i %i\n", sd->down.hold_x, sd->down.hold_y);
2477                     }
2478                   else
2479                     {
2480                        if (sd->down.onhold_animator)
2481                          {
2482                             ecore_animator_del(sd->down.onhold_animator);
2483                             sd->down.onhold_animator = NULL;
2484                          }
2485                     }
2486                }
2487           }
2488      }
2489 }
2490
2491 static void
2492 _smart_scrollbar_read(Smart_Data *sd)
2493 {
2494    Evas_Coord x, y, mx = 0, my = 0, px, py, minx = 0, miny = 0;
2495    double vx, vy;
2496
2497    edje_object_part_drag_value_get(sd->edje_obj, "elm.dragable.vbar", NULL, &vy);
2498    edje_object_part_drag_value_get(sd->edje_obj, "elm.dragable.hbar", &vx, NULL);
2499    sd->pan_func.max_get(sd->pan_obj, &mx, &my);
2500    sd->pan_func.min_get(sd->pan_obj, &minx, &miny);
2501    x = vx * (double)mx + minx;
2502    y = vy * (double)my + miny;
2503    sd->pan_func.get(sd->pan_obj, &px, &py);
2504    sd->pan_func.set(sd->pan_obj, x, y);
2505    if ((px != x) || (py != y))
2506      edje_object_signal_emit(sd->edje_obj, "elm,action,scroll", "elm");
2507 }
2508
2509 static void
2510 _smart_scrollbar_reset(Smart_Data *sd)
2511 {
2512    Evas_Coord px = 0, py = 0, minx = 0, miny = 0;
2513
2514    edje_object_part_drag_value_set(sd->edje_obj, "elm.dragable.vbar", 0.0, 0.0);
2515    edje_object_part_drag_value_set(sd->edje_obj, "elm.dragable.hbar", 0.0, 0.0);
2516    if ((!sd->child_obj) && (!sd->extern_pan))
2517      {
2518         edje_object_part_drag_size_set(sd->edje_obj, "elm.dragable.vbar", 1.0, 1.0);
2519         edje_object_part_drag_size_set(sd->edje_obj, "elm.dragable.hbar", 1.0, 1.0);
2520      }
2521    if (sd->pan_obj)
2522      {
2523         sd->pan_func.min_get(sd->pan_obj, &minx, &miny);
2524         sd->pan_func.get(sd->pan_obj, &px, &py);
2525         sd->pan_func.set(sd->pan_obj, minx, miny);
2526      }
2527    if ((px != minx) || (py != miny))
2528      edje_object_signal_emit(sd->edje_obj, "elm,action,scroll", "elm");
2529 }
2530
2531 static int
2532 _smart_scrollbar_bar_v_visibility_adjust(Smart_Data *sd)
2533 {
2534    int scroll_v_vis_change = 0;
2535    Evas_Coord h, vw = 0, vh = 0;
2536
2537    h = sd->child.h;
2538    if (sd->pan_obj)
2539      evas_object_geometry_get(sd->pan_obj, NULL, NULL, &vw, &vh);
2540    if (sd->vbar_visible)
2541      {
2542         if (sd->vbar_flags == ELM_SMART_SCROLLER_POLICY_AUTO)
2543           {
2544              if ((sd->child_obj) || (sd->extern_pan))
2545                {
2546                   if (h <= vh)
2547                     {
2548                        scroll_v_vis_change = 1;
2549                        sd->vbar_visible = 0;
2550                     }
2551                }
2552              else
2553                {
2554                   scroll_v_vis_change = 1;
2555                   sd->vbar_visible = 0;
2556                }
2557           }
2558         else if (sd->vbar_flags == ELM_SMART_SCROLLER_POLICY_OFF)
2559           {
2560              scroll_v_vis_change = 1;
2561              sd->vbar_visible = 0;
2562           }
2563      }
2564    else
2565      {
2566         if (sd->vbar_flags == ELM_SMART_SCROLLER_POLICY_AUTO)
2567           {
2568              if ((sd->child_obj) || (sd->extern_pan))
2569                {
2570                   if (h > vh)
2571                     {
2572                        scroll_v_vis_change = 1;
2573                        sd->vbar_visible = 1;
2574                     }
2575                }
2576           }
2577         else if (sd->vbar_flags == ELM_SMART_SCROLLER_POLICY_ON)
2578           {
2579              scroll_v_vis_change = 1;
2580              sd->vbar_visible = 1;
2581           }
2582      }
2583    if (scroll_v_vis_change)
2584      {
2585         if (sd->vbar_flags != ELM_SMART_SCROLLER_POLICY_OFF)
2586           {
2587              if (sd->vbar_visible)
2588                edje_object_signal_emit(sd->edje_obj, "elm,action,show,vbar", "elm");
2589              else
2590                edje_object_signal_emit(sd->edje_obj, "elm,action,hide,vbar", "elm");
2591              edje_object_message_signal_process(sd->edje_obj);
2592              _smart_scrollbar_size_adjust(sd);
2593           }
2594         else
2595           edje_object_signal_emit(sd->edje_obj, "elm,action,hide,vbar", "elm");
2596      }
2597    return scroll_v_vis_change;
2598 }
2599
2600 static int
2601 _smart_scrollbar_bar_h_visibility_adjust(Smart_Data *sd)
2602 {
2603    int scroll_h_vis_change = 0;
2604    Evas_Coord w, vw = 0, vh = 0;
2605
2606    w = sd->child.w;
2607    if (sd->pan_obj)
2608      evas_object_geometry_get(sd->pan_obj, NULL, NULL, &vw, &vh);
2609    if (sd->hbar_visible)
2610      {
2611         if (sd->hbar_flags == ELM_SMART_SCROLLER_POLICY_AUTO)
2612           {
2613              if ((sd->child_obj) || (sd->extern_pan))
2614                {
2615                   if (w <= vw)
2616                     {
2617                        scroll_h_vis_change = 1;
2618                        sd->hbar_visible = 0;
2619                     }
2620                }
2621              else
2622                {
2623                   scroll_h_vis_change = 1;
2624                   sd->hbar_visible = 0;
2625                }
2626           }
2627         else if (sd->hbar_flags == ELM_SMART_SCROLLER_POLICY_OFF)
2628           {
2629              scroll_h_vis_change = 1;
2630              sd->hbar_visible = 0;
2631           }
2632      }
2633    else
2634      {
2635         if (sd->hbar_flags == ELM_SMART_SCROLLER_POLICY_AUTO)
2636           {
2637              if ((sd->child_obj) || (sd->extern_pan))
2638                {
2639                   if (w > vw)
2640                     {
2641                        scroll_h_vis_change = 1;
2642                        sd->hbar_visible = 1;
2643                     }
2644                }
2645           }
2646         else if (sd->hbar_flags == ELM_SMART_SCROLLER_POLICY_ON)
2647           {
2648              scroll_h_vis_change = 1;
2649              sd->hbar_visible = 1;
2650           }
2651      }
2652    if (scroll_h_vis_change)
2653      {
2654         if (sd->hbar_flags != ELM_SMART_SCROLLER_POLICY_OFF)
2655           {
2656              if (sd->hbar_visible)
2657                edje_object_signal_emit(sd->edje_obj, "elm,action,show,hbar", "elm");
2658              else
2659                edje_object_signal_emit(sd->edje_obj, "elm,action,hide,hbar", "elm");
2660              edje_object_message_signal_process(sd->edje_obj);
2661              _smart_scrollbar_size_adjust(sd);
2662           }
2663         else
2664           edje_object_signal_emit(sd->edje_obj, "elm,action,hide,hbar", "elm");
2665         _smart_scrollbar_size_adjust(sd);
2666      }
2667    return scroll_h_vis_change;
2668 }
2669
2670 static void
2671 _smart_scrollbar_bar_visibility_adjust(Smart_Data *sd)
2672 {
2673    int changed = 0;
2674
2675    changed |= _smart_scrollbar_bar_h_visibility_adjust(sd);
2676    changed |= _smart_scrollbar_bar_v_visibility_adjust(sd);
2677    if (changed)
2678      {
2679         _smart_scrollbar_bar_h_visibility_adjust(sd);
2680         _smart_scrollbar_bar_v_visibility_adjust(sd);
2681      }
2682 }
2683
2684 static void
2685 _smart_scrollbar_size_adjust(Smart_Data *sd)
2686 {
2687    if ((sd->child_obj) || (sd->extern_pan))
2688      {
2689         Evas_Coord x, y, w, h, mx = 0, my = 0, vw = 0, vh = 0, px, py, minx = 0, miny = 0;
2690         double vx, vy, size;
2691
2692         edje_object_part_geometry_get(sd->edje_obj, "elm.swallow.content",
2693                                       NULL, NULL, &vw, &vh);
2694         w = sd->child.w;
2695         if (w < 1) w = 1;
2696         size = (double)vw / (double)w;
2697         if (size > 1.0)
2698           {
2699              size = 1.0;
2700              edje_object_part_drag_value_set(sd->edje_obj, "elm.dragable.hbar", 0.0, 0.0);
2701           }
2702         edje_object_part_drag_size_set(sd->edje_obj, "elm.dragable.hbar", size, 1.0);
2703
2704         h = sd->child.h;
2705         if (h < 1) h = 1;
2706         size = (double)vh / (double)h;
2707         if (size > 1.0)
2708           {
2709              size = 1.0;
2710              edje_object_part_drag_value_set(sd->edje_obj, "elm.dragable.vbar", 0.0, 0.0);
2711           }
2712         edje_object_part_drag_size_set(sd->edje_obj, "elm.dragable.vbar", 1.0, size);
2713
2714         edje_object_part_drag_value_get(sd->edje_obj, "elm.dragable.hbar", &vx, NULL);
2715         edje_object_part_drag_value_get(sd->edje_obj, "elm.dragable.vbar", NULL, &vy);
2716         sd->pan_func.max_get(sd->pan_obj, &mx, &my);
2717         sd->pan_func.min_get(sd->pan_obj, &minx, &miny);
2718         x = vx * mx + minx;
2719         y = vy * my + miny;
2720
2721         edje_object_part_drag_step_set(sd->edje_obj, "elm.dragable.hbar", (double)sd->step.x / (double)w, 0.0);
2722         edje_object_part_drag_step_set(sd->edje_obj, "elm.dragable.vbar", 0.0, (double)sd->step.y / (double)h);
2723         if (sd->page.x > 0)
2724           edje_object_part_drag_page_set(sd->edje_obj, "elm.dragable.hbar", (double)sd->page.x / (double)w, 0.0);
2725         else
2726           edje_object_part_drag_page_set(sd->edje_obj, "elm.dragable.hbar", -((double)sd->page.x * ((double)vw / (double)w)) / 100.0, 0.0);
2727         if (sd->page.y > 0)
2728           edje_object_part_drag_page_set(sd->edje_obj, "elm.dragable.vbar", 0.0, (double)sd->page.y / (double)h);
2729         else
2730           edje_object_part_drag_page_set(sd->edje_obj, "elm.dragable.vbar", 0.0, -((double)sd->page.y * ((double)vh / (double)h)) / 100.0);
2731
2732         sd->pan_func.get(sd->pan_obj, &px, &py);
2733         if (vx != mx) x = px;
2734         if (vy != my) y = py;
2735         sd->pan_func.set(sd->pan_obj, x, y);
2736         //      if ((px != 0) || (py != 0))
2737         //        edje_object_signal_emit(sd->edje_obj, "elm,action,scroll", "elm");
2738      }
2739    else
2740      {
2741         Evas_Coord px = 0, py = 0, minx = 0, miny = 0;
2742
2743         edje_object_part_drag_size_set(sd->edje_obj, "elm.dragable.vbar", 1.0, 1.0);
2744         edje_object_part_drag_size_set(sd->edje_obj, "elm.dragable.hbar", 1.0, 1.0);
2745         sd->pan_func.min_get(sd->pan_obj, &minx, &miny);
2746         sd->pan_func.get(sd->pan_obj, &px, &py);
2747         sd->pan_func.set(sd->pan_obj, minx, miny);
2748         if ((px != minx) || (py != miny))
2749           edje_object_signal_emit(sd->edje_obj, "elm,action,scroll", "elm");
2750      }
2751    _smart_scrollbar_bar_visibility_adjust(sd);
2752 }
2753
2754 static void
2755 _smart_reconfigure(Smart_Data *sd)
2756 {
2757    evas_object_move(sd->edje_obj, sd->x, sd->y);
2758    evas_object_resize(sd->edje_obj, sd->w, sd->h);
2759    evas_object_move(sd->event_obj, sd->x, sd->y);
2760    evas_object_resize(sd->event_obj, sd->w, sd->h);
2761    _smart_scrollbar_size_adjust(sd);
2762    _smart_page_adjust(sd);
2763 }
2764
2765 static void
2766 _smart_add(Evas_Object *obj)
2767 {
2768    Smart_Data *sd;
2769    Evas_Object *o;
2770
2771    sd = calloc(1, sizeof(Smart_Data));
2772    if (!sd) return;
2773    evas_object_smart_data_set(obj, sd);
2774
2775    sd->smart_obj = obj;
2776    sd->x = 0;
2777    sd->y = 0;
2778    sd->w = 0;
2779    sd->h = 0;
2780    sd->step.x = 32;
2781    sd->step.y = 32;
2782    sd->page.x = -50;
2783    sd->page.y = -50;
2784    sd->hbar_flags = ELM_SMART_SCROLLER_POLICY_AUTO;
2785    sd->vbar_flags = ELM_SMART_SCROLLER_POLICY_AUTO;
2786    sd->hbar_visible = 1;
2787    sd->vbar_visible = 1;
2788
2789    sd->bounce_horiz = 1;
2790    sd->bounce_vert = 1;
2791
2792    sd->one_dir_at_a_time = 1;
2793    sd->momentum_animator_disabled = EINA_FALSE;
2794    sd->bounce_animator_disabled = EINA_FALSE;
2795
2796    o = edje_object_add(evas_object_evas_get(obj));
2797    evas_object_propagate_events_set(o, 0);
2798    sd->edje_obj = o;
2799    elm_smart_scroller_object_theme_set(NULL, obj, "scroller", "base", "default");
2800    edje_object_signal_callback_add(o, "drag", "elm.dragable.vbar", _smart_edje_drag_v, sd);
2801    edje_object_signal_callback_add(o, "drag,start", "elm.dragable.vbar", _smart_edje_drag_v_start, sd);
2802    edje_object_signal_callback_add(o, "drag,stop", "elm.dragable.vbar", _smart_edje_drag_v_stop, sd);
2803    edje_object_signal_callback_add(o, "drag,step", "elm.dragable.vbar", _smart_edje_drag_v, sd);
2804    edje_object_signal_callback_add(o, "drag,page", "elm.dragable.vbar", _smart_edje_drag_v, sd);
2805    edje_object_signal_callback_add(o, "drag", "elm.dragable.hbar", _smart_edje_drag_h, sd);
2806    edje_object_signal_callback_add(o, "drag,start", "elm.dragable.hbar", _smart_edje_drag_h_start, sd);
2807    edje_object_signal_callback_add(o, "drag,stop", "elm.dragable.hbar", _smart_edje_drag_h_stop, sd);
2808    edje_object_signal_callback_add(o, "drag,step", "elm.dragable.hbar", _smart_edje_drag_h, sd);
2809    edje_object_signal_callback_add(o, "drag,page", "elm.dragable.hbar", _smart_edje_drag_h, sd);
2810    evas_object_smart_member_add(o, obj);
2811
2812    o = evas_object_rectangle_add(evas_object_evas_get(obj));
2813    sd->event_obj = o;
2814    evas_object_color_set(o, 0, 0, 0, 0);
2815    evas_object_event_callback_add(o, EVAS_CALLBACK_MOUSE_WHEEL, _smart_event_wheel, sd);
2816    evas_object_event_callback_add(o, EVAS_CALLBACK_MOUSE_DOWN, _smart_event_mouse_down, sd);
2817    evas_object_event_callback_add(o, EVAS_CALLBACK_MOUSE_UP, _smart_event_mouse_up, sd);
2818    evas_object_event_callback_add(o, EVAS_CALLBACK_MOUSE_MOVE, _smart_event_mouse_move, sd);
2819    evas_object_smart_member_add(o, obj);
2820    evas_object_repeat_events_set(o, 1);
2821
2822    sd->pan_func.set = _elm_smart_pan_set;
2823    sd->pan_func.get = _elm_smart_pan_get;
2824    sd->pan_func.max_get = _elm_smart_pan_max_get;
2825    sd->pan_func.min_get = _elm_smart_pan_min_get;
2826    sd->pan_func.child_size_get = _elm_smart_pan_child_size_get;
2827
2828    _smart_scrollbar_reset(sd);
2829 }
2830
2831 static void
2832 _smart_del(Evas_Object *obj)
2833 {
2834    INTERNAL_ENTRY;
2835    elm_smart_scroller_child_set(obj, NULL);
2836    if (!sd->extern_pan) evas_object_del(sd->pan_obj);
2837    evas_object_del(sd->edje_obj);
2838    evas_object_del(sd->event_obj);
2839    if (sd->down.hold_animator) ecore_animator_del(sd->down.hold_animator);
2840    if (sd->down.onhold_animator) ecore_animator_del(sd->down.onhold_animator);
2841    if (sd->down.momentum_animator) ecore_animator_del(sd->down.momentum_animator);
2842    if (sd->down.bounce_x_animator) ecore_animator_del(sd->down.bounce_x_animator);
2843    if (sd->down.bounce_y_animator) ecore_animator_del(sd->down.bounce_y_animator);
2844    if (sd->scrollto.x.animator) ecore_animator_del(sd->scrollto.x.animator);
2845    if (sd->scrollto.y.animator) ecore_animator_del(sd->scrollto.y.animator);
2846    free(sd);
2847    evas_object_smart_data_set(obj, NULL);
2848 }
2849
2850 static void
2851 _smart_move(Evas_Object *obj, Evas_Coord x, Evas_Coord y)
2852 {
2853    INTERNAL_ENTRY;
2854    sd->x = x;
2855    sd->y = y;
2856    _smart_reconfigure(sd);
2857 }
2858
2859 static void
2860 _smart_resize(Evas_Object *obj, Evas_Coord w, Evas_Coord h)
2861 {
2862    INTERNAL_ENTRY;
2863    sd->w = w;
2864    sd->h = h;
2865    _smart_reconfigure(sd);
2866    _elm_smart_scroller_wanted_region_set(obj);
2867 }
2868
2869 static void
2870 _smart_show(Evas_Object *obj)
2871 {
2872    INTERNAL_ENTRY;
2873    evas_object_show(sd->edje_obj);
2874    evas_object_show(sd->event_obj);
2875 }
2876
2877 static void
2878 _smart_hide(Evas_Object *obj)
2879 {
2880    INTERNAL_ENTRY;
2881    evas_object_hide(sd->edje_obj);
2882    evas_object_hide(sd->event_obj);
2883 }
2884
2885 static void
2886 _smart_color_set(Evas_Object *obj, int r, int g, int b, int a)
2887 {
2888    INTERNAL_ENTRY;
2889    evas_object_color_set(sd->edje_obj, r, g, b, a);
2890 }
2891
2892 static void
2893 _smart_clip_set(Evas_Object *obj, Evas_Object *clip)
2894 {
2895    INTERNAL_ENTRY;
2896    evas_object_clip_set(sd->edje_obj, clip);
2897    evas_object_clip_set(sd->event_obj, clip);
2898 }
2899
2900 static void
2901 _smart_clip_unset(Evas_Object *obj)
2902 {
2903    INTERNAL_ENTRY;
2904    evas_object_clip_unset(sd->edje_obj);
2905    evas_object_clip_unset(sd->event_obj);
2906 }
2907
2908 /* never need to touch this */
2909
2910 static void
2911 _smart_init(void)
2912 {
2913    if (_smart) return;
2914      {
2915         static const Evas_Smart_Class sc =
2916           {
2917              SMART_NAME,
2918              EVAS_SMART_CLASS_VERSION,
2919              _smart_add,
2920              _smart_del,
2921              _smart_move,
2922              _smart_resize,
2923              _smart_show,
2924              _smart_hide,
2925              _smart_color_set,
2926              _smart_clip_set,
2927              _smart_clip_unset,
2928              NULL,
2929              NULL,
2930              NULL,
2931              NULL,
2932              NULL,
2933              NULL,
2934              NULL
2935           };
2936         _smart = evas_smart_class_new(&sc);
2937      }
2938 }