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