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