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