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