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