elementary / els_scroller - removed a useless line
[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, ret;
162    elm_smart_scroller_child_viewport_size_get(obj, &w, NULL);
163    sd->pan_func.child_size_get(sd->pan_obj, &cw, &ch);
164    ret = (cw - (x + w));
165    return (ret >= 0) ? ret : 0;
166 }
167
168 void
169 elm_smart_scroller_mirrored_set(Evas_Object *obj, Eina_Bool mirrored)
170 {
171    API_ENTRY return;
172    Evas_Coord wx;
173    if (sd->is_mirrored == mirrored)
174      return;
175
176    sd->is_mirrored = mirrored;
177    edje_object_mirrored_set(sd->edje_obj, mirrored);
178
179    if (sd->is_mirrored)
180      wx = _elm_smart_scroller_x_mirrored_get(sd->smart_obj, sd->wx);
181    else
182      wx = sd->wx;
183
184    elm_smart_scroller_child_pos_set(sd->smart_obj, wx, sd->wy);
185 }
186
187 void
188 elm_smart_scroller_child_set(Evas_Object *obj, Evas_Object *child)
189 {
190    Evas_Coord w, h;
191    Evas_Object *o;
192
193    API_ENTRY return;
194    if (sd->child_obj)
195      {
196         _elm_smart_pan_child_set(sd->pan_obj, NULL);
197         evas_object_event_callback_del_full(sd->child_obj, EVAS_CALLBACK_DEL, _smart_child_del_hook, sd);
198      }
199
200    sd->child_obj = child;
201    sd->wx = sd->wy = 0;
202    /* (-1) means want viewports size */
203    sd->ww = sd->wh = -1;
204    if (!child) return;
205
206    if (!sd->pan_obj)
207      {
208         o = _elm_smart_pan_add(evas_object_evas_get(obj));
209         sd->pan_obj = o;
210         evas_object_smart_callback_add(o, "changed", _smart_pan_changed_hook, sd);
211         evas_object_smart_callback_add(o, "pan_changed", _smart_pan_pan_changed_hook, sd);
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    x = nx;
1064    if (x < minx) x = minx;
1065    else if ((x + w) > cw) x = cw - w;
1066    y = ny;
1067    if (y < miny) y = miny;
1068    else if ((y + h) > ch) y = ch - h;
1069    elm_smart_scroller_child_pos_set(obj, x, y);
1070 }
1071
1072 /* Set should be used for calculated positions, for example, when we move
1073  * because of an animation or because this is the correct position after
1074  * constraints. */
1075 void
1076 elm_smart_scroller_child_region_set(Evas_Object *obj, Evas_Coord x, Evas_Coord y, Evas_Coord w, Evas_Coord h)
1077 {
1078    _elm_smart_scroller_child_region_show_internal(obj, x, y, w, h, EINA_TRUE);
1079 }
1080
1081 /* Set should be used for setting the wanted position, for example a user scroll
1082  * or moving the cursor in an entry. */
1083 void
1084 elm_smart_scroller_child_region_show(Evas_Object *obj, Evas_Coord x, Evas_Coord y, Evas_Coord w, Evas_Coord h)
1085 {
1086    _elm_smart_scroller_child_region_show_internal(obj, x, y, w, h, EINA_FALSE);
1087 }
1088
1089 void
1090 elm_smart_scroller_child_viewport_size_get(Evas_Object *obj, Evas_Coord *w, Evas_Coord *h)
1091 {
1092    API_ENTRY return;
1093    edje_object_calc_force(sd->edje_obj);
1094    evas_object_geometry_get(sd->pan_obj, NULL, NULL, w, h);
1095 }
1096
1097 void
1098 elm_smart_scroller_step_size_set(Evas_Object *obj, Evas_Coord x, Evas_Coord y)
1099 {
1100    API_ENTRY return;
1101    if (x < 1) x = 1;
1102    if (y < 1) y = 1;
1103    sd->step.x = x;
1104    sd->step.y = y;
1105    _smart_scrollbar_size_adjust(sd);
1106 }
1107
1108 void
1109 elm_smart_scroller_step_size_get(Evas_Object *obj, Evas_Coord *x, Evas_Coord *y)
1110 {
1111    API_ENTRY return;
1112    if (x) *x = sd->step.x;
1113    if (y) *y = sd->step.y;
1114 }
1115
1116 void
1117 elm_smart_scroller_page_size_set(Evas_Object *obj, Evas_Coord x, Evas_Coord y)
1118 {
1119    API_ENTRY return;
1120    sd->page.x = x;
1121    sd->page.y = y;
1122    _smart_scrollbar_size_adjust(sd);
1123 }
1124
1125 void
1126 elm_smart_scroller_page_size_get(Evas_Object *obj, Evas_Coord *x, Evas_Coord *y)
1127 {
1128    API_ENTRY return;
1129    if (x) *x = sd->page.x;
1130    if (y) *y = sd->page.y;
1131 }
1132
1133 void
1134 elm_smart_scroller_policy_set(Evas_Object *obj, Elm_Smart_Scroller_Policy hbar, Elm_Smart_Scroller_Policy vbar)
1135 {
1136    API_ENTRY return;
1137    if ((sd->hbar_flags == hbar) && (sd->vbar_flags == vbar)) return;
1138    sd->hbar_flags = hbar;
1139    sd->vbar_flags = vbar;
1140    if (sd->hbar_flags == ELM_SMART_SCROLLER_POLICY_ON)
1141      edje_object_signal_emit(sd->edje_obj, "elm,action,show_always,hbar", "elm");
1142    else if (sd->hbar_flags == ELM_SMART_SCROLLER_POLICY_OFF)
1143      edje_object_signal_emit(sd->edje_obj, "elm,action,hide,hbar", "elm");
1144    else
1145      edje_object_signal_emit(sd->edje_obj, "elm,action,show_notalways,hbar", "elm");
1146    if (sd->vbar_flags == ELM_SMART_SCROLLER_POLICY_ON)
1147      edje_object_signal_emit(sd->edje_obj, "elm,action,show_always,vbar", "elm");
1148    else if (sd->vbar_flags == ELM_SMART_SCROLLER_POLICY_OFF)
1149      edje_object_signal_emit(sd->edje_obj, "elm,action,hide,vbar", "elm");
1150    else
1151      edje_object_signal_emit(sd->edje_obj, "elm,action,show_notalways,vbar", "elm");
1152    _smart_scrollbar_size_adjust(sd);
1153 }
1154
1155 void
1156 elm_smart_scroller_policy_get(Evas_Object *obj, Elm_Smart_Scroller_Policy *hbar, Elm_Smart_Scroller_Policy *vbar)
1157 {
1158    API_ENTRY return;
1159    if (hbar) *hbar = sd->hbar_flags;
1160    if (vbar) *vbar = sd->vbar_flags;
1161 }
1162
1163 Evas_Object *
1164 elm_smart_scroller_edje_object_get(Evas_Object *obj)
1165 {
1166    API_ENTRY return NULL;
1167    return sd->edje_obj;
1168 }
1169
1170 void
1171 elm_smart_scroller_single_dir_set(Evas_Object *obj, Eina_Bool single_dir)
1172 {
1173    API_ENTRY return;
1174    sd->one_dir_at_a_time = single_dir;
1175 }
1176
1177 Eina_Bool
1178 elm_smart_scroller_single_dir_get(Evas_Object *obj)
1179 {
1180    API_ENTRY return EINA_FALSE;
1181    return sd->one_dir_at_a_time;
1182 }
1183
1184 void
1185 elm_smart_scroller_object_theme_set(Evas_Object *parent, Evas_Object *obj, const char *clas, const char *group, const char *style)
1186 {
1187    API_ENTRY return;
1188    Evas_Coord mw, mh;
1189    _elm_theme_object_set(parent, sd->edje_obj, clas, group, style);
1190    edje_object_scale_set(sd->edje_obj, elm_widget_scale_get(parent) * _elm_config->scale);
1191    if (sd->pan_obj)
1192      edje_object_part_swallow(sd->edje_obj, "elm.swallow.content", sd->pan_obj);
1193    mw = mh = -1;
1194    elm_coords_finger_size_adjust(1, &mw, 1, &mh);
1195    if (edje_object_part_exists(sd->edje_obj, "elm.scrollbar.base"))
1196      {
1197         Evas_Object *base;
1198         base = edje_object_part_swallow_get(sd->edje_obj, "elm.scrollbar.base");
1199         if (!base)
1200           {
1201              base = evas_object_rectangle_add(evas_object_evas_get(sd->edje_obj));
1202              evas_object_color_set(base, 0, 0, 0, 0);
1203              edje_object_part_swallow(sd->edje_obj, "elm.scrollbar.base", base);
1204           }
1205         if (!_elm_config->thumbscroll_enable)
1206            evas_object_size_hint_min_set(base, mw, mh);
1207      }
1208    sd->vbar_visible = !sd->vbar_visible;
1209    sd->hbar_visible = !sd->hbar_visible;
1210    _smart_scrollbar_bar_visibility_adjust(sd);
1211 }
1212
1213 void
1214 elm_smart_scroller_hold_set(Evas_Object *obj, Eina_Bool hold)
1215 {
1216    API_ENTRY return;
1217    sd->hold = hold;
1218 }
1219
1220 void
1221 elm_smart_scroller_freeze_set(Evas_Object *obj, Eina_Bool freeze)
1222 {
1223    API_ENTRY return;
1224    sd->freeze = freeze;
1225    if (sd->freeze)
1226      {
1227         if (sd->down.onhold_animator)
1228           {
1229              ecore_animator_del(sd->down.onhold_animator);
1230              sd->down.onhold_animator = NULL;
1231           }
1232      }
1233 }
1234
1235 void
1236 elm_smart_scroller_bounce_allow_set(Evas_Object *obj, Eina_Bool horiz, Eina_Bool vert)
1237 {
1238    API_ENTRY return;
1239    sd->bounce_horiz = horiz;
1240    sd->bounce_vert = vert;
1241 }
1242
1243 void
1244 elm_smart_scroller_bounce_allow_get(const Evas_Object *obj, Eina_Bool *horiz, Eina_Bool *vert)
1245 {
1246    API_ENTRY return;
1247    *horiz = sd->bounce_horiz;
1248    *vert = sd->bounce_vert;
1249 }
1250
1251 void
1252 elm_smart_scroller_paging_set(Evas_Object *obj, double pagerel_h, double pagerel_v, Evas_Coord pagesize_h, Evas_Coord pagesize_v)
1253 {
1254    API_ENTRY return;
1255    sd->pagerel_h = pagerel_h;
1256    sd->pagerel_v = pagerel_v;
1257    sd->pagesize_h = pagesize_h;
1258    sd->pagesize_v = pagesize_v;
1259    _smart_page_adjust(sd);
1260 }
1261
1262 void
1263 elm_smart_scroller_paging_get(Evas_Object *obj, double *pagerel_h, double *pagerel_v, Evas_Coord *pagesize_h, Evas_Coord *pagesize_v)
1264 {
1265    API_ENTRY return;
1266    if(pagerel_h) *pagerel_h = sd->pagerel_h;
1267    if(pagerel_v) *pagerel_v = sd->pagerel_v;
1268    if(pagesize_h) *pagesize_h = sd->pagesize_h;
1269    if(pagesize_v) *pagesize_v = sd->pagesize_v;
1270 }
1271
1272 void
1273 elm_smart_scroller_region_bring_in(Evas_Object *obj, Evas_Coord x, Evas_Coord y, Evas_Coord w, Evas_Coord h)
1274 {
1275    Evas_Coord mx = 0, my = 0, cw = 0, ch = 0, px = 0, py = 0, nx, ny, minx = 0, miny = 0;
1276
1277    API_ENTRY return;
1278    sd->pan_func.max_get(sd->pan_obj, &mx, &my);
1279    sd->pan_func.min_get(sd->pan_obj, &minx, &miny);
1280    sd->pan_func.child_size_get(sd->pan_obj, &cw, &ch);
1281    sd->pan_func.get(sd->pan_obj, &px, &py);
1282
1283    nx = px;
1284    if ((x < px) && ((x + w) < (px + (cw - mx)))) nx = x;
1285    else if ((x > px) && ((x + w) > (px + (cw - mx)))) nx = x + w - (cw - mx);
1286    ny = py;
1287    if ((y < py) && ((y + h) < (py + (ch - my)))) ny = y;
1288    else if ((y > py) && ((y + h) > (py + (ch - my)))) ny = y + h - (ch - my);
1289    sd->wx = x;
1290    sd->wy = y;
1291    sd->ww = w;
1292    sd->wh = h;
1293    if ((nx == px) && (ny == py)) return;
1294    if ((sd->down.bounce_x_animator) || (sd->down.bounce_y_animator) ||
1295        (sd->scrollto.x.animator) || (sd->scrollto.y.animator))
1296      {
1297         _smart_anim_stop(sd->smart_obj);
1298      }
1299    if (sd->scrollto.x.animator)
1300      {
1301         ecore_animator_del(sd->scrollto.x.animator);
1302         sd->scrollto.x.animator = NULL;
1303      }
1304    if (sd->scrollto.y.animator)
1305      {
1306         ecore_animator_del(sd->scrollto.y.animator);
1307         sd->scrollto.y.animator = NULL;
1308      }
1309    if (sd->down.bounce_x_animator)
1310      {
1311         ecore_animator_del(sd->down.bounce_x_animator);
1312         sd->down.bounce_x_animator = NULL;
1313         sd->bouncemex = 0;
1314      }
1315    if (sd->down.bounce_y_animator)
1316      {
1317         ecore_animator_del(sd->down.bounce_y_animator);
1318         sd->down.bounce_y_animator = NULL;
1319         sd->bouncemey = 0;
1320      }
1321    if (sd->down.hold_animator)
1322      {
1323         ecore_animator_del(sd->down.hold_animator);
1324         sd->down.hold_animator = NULL;
1325         _smart_drag_stop(sd->smart_obj);
1326      }
1327    if (sd->down.momentum_animator)
1328      {
1329         ecore_animator_del(sd->down.momentum_animator);
1330         sd->down.momentum_animator = NULL;
1331         sd->down.bounce_x_hold = 0;
1332         sd->down.bounce_y_hold = 0;
1333         sd->down.ax = 0;
1334         sd->down.ay = 0;
1335         sd->down.pdx = 0;
1336         sd->down.pdy = 0;
1337      }
1338    x = nx;
1339    if (x < minx) x = minx;
1340    else if ((x + w) > cw) x = cw - w;
1341    _smart_scrollto_x(sd, _elm_config->bring_in_scroll_friction, x);
1342    y = ny;
1343    if (y < miny) y = miny;
1344    else if ((y + h) > ch) y = ch - h;
1345    _smart_scrollto_y(sd, _elm_config->bring_in_scroll_friction, y);
1346 }
1347
1348 void
1349 elm_smart_scroller_widget_set(Evas_Object *obj, Evas_Object *wid)
1350 {
1351    API_ENTRY return;
1352    sd->widget = wid;
1353 }
1354
1355 static void
1356 _elm_smart_scroller_wanted_region_set(Evas_Object *obj)
1357 {
1358    INTERNAL_ENTRY;
1359    Evas_Coord ww, wh, wx = sd->wx;
1360
1361    /* Flip to RTL cords only if init in RTL mode */
1362    if(sd->is_mirrored)
1363      wx = _elm_smart_scroller_x_mirrored_get(obj, sd->wx);
1364
1365    if (sd->ww == -1)
1366      {
1367         elm_smart_scroller_child_viewport_size_get(obj, &ww, &wh);
1368      }
1369    else
1370      {
1371         ww = sd->ww;
1372         wh = sd->wh;
1373      }
1374
1375    elm_smart_scroller_child_region_set(obj, wx, sd->wy, ww, wh);
1376 }
1377
1378 /* local subsystem functions */
1379 static void
1380 _smart_edje_drag_v_start(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
1381 {
1382    Smart_Data *sd;
1383
1384    sd = data;
1385    _smart_scrollbar_read(sd);
1386    _smart_drag_start(sd->smart_obj);
1387    sd->freeze = EINA_TRUE;
1388 }
1389
1390 static void
1391 _smart_edje_drag_v_stop(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
1392 {
1393    Smart_Data *sd;
1394
1395    sd = data;
1396    _smart_scrollbar_read(sd);
1397    _smart_drag_stop(sd->smart_obj);
1398    sd->freeze = EINA_FALSE;
1399 }
1400
1401 static void
1402 _smart_edje_drag_v(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
1403 {
1404    Smart_Data *sd;
1405
1406    sd = data;
1407    _smart_scrollbar_read(sd);
1408 }
1409
1410 static void
1411 _smart_edje_drag_h_start(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
1412 {
1413    Smart_Data *sd;
1414
1415    sd = data;
1416    _smart_scrollbar_read(sd);
1417    _smart_drag_start(sd->smart_obj);
1418    sd->freeze = EINA_TRUE;
1419 }
1420
1421 static void
1422 _smart_edje_drag_h_stop(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
1423 {
1424    Smart_Data *sd;
1425
1426    sd = data;
1427    _smart_scrollbar_read(sd);
1428    _smart_drag_stop(sd->smart_obj);
1429    sd->freeze = EINA_FALSE;
1430 }
1431
1432 static void
1433 _smart_edje_drag_h(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
1434 {
1435    Smart_Data *sd;
1436
1437    sd = data;
1438    _smart_scrollbar_read(sd);
1439 }
1440
1441 static void
1442 _smart_child_del_hook(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
1443 {
1444    Smart_Data *sd;
1445
1446    sd = data;
1447    sd->child_obj = NULL;
1448    _smart_scrollbar_size_adjust(sd);
1449    _smart_scrollbar_reset(sd);
1450 }
1451
1452 static void
1453 _smart_pan_changed_hook(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
1454 {
1455    Evas_Coord w, h;
1456    Smart_Data *sd;
1457
1458    sd = data;
1459    sd->pan_func.child_size_get(sd->pan_obj, &w, &h);
1460    if ((w != sd->child.w) || (h != sd->child.h))
1461      {
1462         sd->child.w = w;
1463         sd->child.h = h;
1464         _smart_scrollbar_size_adjust(sd);
1465         evas_object_size_hint_min_set(sd->smart_obj, sd->child.w, sd->child.h);
1466         _elm_smart_scroller_wanted_region_set(sd->smart_obj);
1467      }
1468 }
1469
1470 static void
1471 _smart_pan_pan_changed_hook(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
1472 {
1473    Smart_Data *sd;
1474
1475    sd = data;
1476    if ((sd->down.bounce_x_animator) || (sd->down.bounce_y_animator) ||
1477        (sd->scrollto.x.animator) || (sd->scrollto.y.animator))
1478      {
1479         _smart_anim_stop(sd->smart_obj);
1480      }
1481    if (sd->scrollto.x.animator)
1482      {
1483         ecore_animator_del(sd->scrollto.x.animator);
1484         sd->scrollto.x.animator = NULL;
1485      }
1486    if (sd->scrollto.y.animator)
1487      {
1488         ecore_animator_del(sd->scrollto.y.animator);
1489         sd->scrollto.y.animator = NULL;
1490      }
1491    if (sd->down.bounce_x_animator)
1492      {
1493         ecore_animator_del(sd->down.bounce_x_animator);
1494         sd->down.bounce_x_animator = NULL;
1495         sd->bouncemex = 0;
1496      }
1497    if (sd->down.bounce_y_animator)
1498      {
1499         ecore_animator_del(sd->down.bounce_y_animator);
1500         sd->down.bounce_y_animator = NULL;
1501         sd->bouncemey = 0;
1502      }
1503    _elm_smart_scroller_wanted_region_set(sd->smart_obj);
1504 }
1505
1506 static void
1507 _smart_event_wheel(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
1508 {
1509    Evas_Event_Mouse_Wheel *ev;
1510    Smart_Data *sd;
1511    Evas_Coord x = 0, y = 0;
1512
1513    sd = data;
1514    ev = event_info;
1515    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return ;
1516    if ((evas_key_modifier_is_set(ev->modifiers, "Control")) ||
1517        (evas_key_modifier_is_set(ev->modifiers, "Alt")) ||
1518        (evas_key_modifier_is_set(ev->modifiers, "Shift")) ||
1519        (evas_key_modifier_is_set(ev->modifiers, "Meta")) ||
1520        (evas_key_modifier_is_set(ev->modifiers, "Hyper")) ||
1521        (evas_key_modifier_is_set(ev->modifiers, "Super")))
1522      return;
1523    elm_smart_scroller_child_pos_get(sd->smart_obj, &x, &y);
1524    if ((sd->down.bounce_x_animator) || (sd->down.bounce_y_animator) ||
1525        (sd->scrollto.x.animator) || (sd->scrollto.y.animator))
1526      {
1527         _smart_anim_stop(sd->smart_obj);
1528      }
1529    if (sd->scrollto.x.animator)
1530      {
1531         ecore_animator_del(sd->scrollto.x.animator);
1532         sd->scrollto.x.animator = NULL;
1533      }
1534    if (sd->scrollto.y.animator)
1535      {
1536         ecore_animator_del(sd->scrollto.y.animator);
1537         sd->scrollto.y.animator = NULL;
1538      }
1539    if (sd->down.bounce_x_animator)
1540      {
1541         ecore_animator_del(sd->down.bounce_x_animator);
1542         sd->down.bounce_x_animator = NULL;
1543         sd->bouncemex = 0;
1544      }
1545    if (sd->down.bounce_y_animator)
1546      {
1547         ecore_animator_del(sd->down.bounce_y_animator);
1548         sd->down.bounce_y_animator = NULL;
1549         sd->bouncemey = 0;
1550      }
1551    if (!ev->direction)
1552      y += ev->z * sd->step.y;
1553    else if (ev->direction == 1)
1554      x += ev->z * sd->step.x;
1555
1556    if ((!sd->hold) && (!sd->freeze))
1557      {
1558         _update_wanted_coordinates(sd, x, y);
1559         elm_smart_scroller_child_pos_set(sd->smart_obj, x, y);
1560      }
1561 }
1562
1563 static void
1564 _smart_event_mouse_down(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
1565 {
1566    Evas_Event_Mouse_Down *ev;
1567    Smart_Data *sd;
1568    Evas_Coord x = 0, y = 0;
1569
1570    sd = data;
1571    ev = event_info;
1572 //   if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return ;
1573    if (_elm_config->thumbscroll_enable)
1574      {
1575         sd->down.hold = 0;
1576         if ((sd->down.bounce_x_animator) || (sd->down.bounce_y_animator) ||
1577             (sd->down.momentum_animator) || (sd->scrollto.x.animator) ||
1578             (sd->scrollto.y.animator))
1579           {
1580              ev->event_flags |= EVAS_EVENT_FLAG_ON_SCROLL | EVAS_EVENT_FLAG_ON_HOLD;
1581              sd->down.scroll = 1;
1582              sd->down.hold = 1;
1583              _smart_anim_stop(sd->smart_obj);
1584           }
1585         if (sd->scrollto.x.animator)
1586           {
1587              ecore_animator_del(sd->scrollto.x.animator);
1588              sd->scrollto.x.animator = NULL;
1589           }
1590         if (sd->scrollto.y.animator)
1591           {
1592              ecore_animator_del(sd->scrollto.y.animator);
1593              sd->scrollto.y.animator = NULL;
1594           }
1595         if (sd->down.bounce_x_animator)
1596           {
1597              ecore_animator_del(sd->down.bounce_x_animator);
1598              sd->down.bounce_x_animator = NULL;
1599              sd->bouncemex = 0;
1600           }
1601         if (sd->down.bounce_y_animator)
1602           {
1603              ecore_animator_del(sd->down.bounce_y_animator);
1604              sd->down.bounce_y_animator = NULL;
1605              sd->bouncemey = 0;
1606           }
1607         if (sd->down.hold_animator)
1608           {
1609              ecore_animator_del(sd->down.hold_animator);
1610              sd->down.hold_animator = NULL;
1611              _smart_drag_stop(sd->smart_obj);
1612           }
1613         if (sd->down.momentum_animator)
1614           {
1615              ecore_animator_del(sd->down.momentum_animator);
1616              sd->down.momentum_animator = NULL;
1617              sd->down.bounce_x_hold = 0;
1618              sd->down.bounce_y_hold = 0;
1619              sd->down.ax = 0;
1620              sd->down.ay = 0;
1621           }
1622         if (ev->button == 1)
1623           {
1624              sd->down.now = 1;
1625              sd->down.dragged = 0;
1626              sd->down.dir_x = 0;
1627              sd->down.dir_y = 0;
1628              sd->down.x = ev->canvas.x;
1629              sd->down.y = ev->canvas.y;
1630              elm_smart_scroller_child_pos_get(sd->smart_obj, &x, &y);
1631              sd->down.sx = x;
1632              sd->down.sy = y;
1633              sd->down.locked = 0;
1634              memset(&(sd->down.history[0]), 0, sizeof(sd->down.history[0]) * 20);
1635 #ifdef EVTIME
1636              sd->down.history[0].timestamp = ev->timestamp / 1000.0;
1637 #else
1638              sd->down.history[0].timestamp = ecore_loop_time_get();
1639 #endif
1640              sd->down.history[0].x = ev->canvas.x;
1641              sd->down.history[0].y = ev->canvas.y;
1642           }
1643         sd->down.dragged_began = 0;
1644         sd->down.hold_parent = 0;
1645         sd->down.cancelled = 0;
1646      }
1647 }
1648
1649 static Eina_Bool
1650 _smart_hold_animator(void *data)
1651 {
1652    Smart_Data *sd = data;
1653    Evas_Coord ox, oy;
1654    
1655    elm_smart_scroller_child_pos_get(sd->smart_obj, &ox, &oy);
1656    if (sd->down.dir_x)
1657      {
1658         if ((!sd->widget) || 
1659             (!elm_widget_drag_child_locked_x_get(sd->widget)))
1660           {
1661              ox = sd->down.hold_x;
1662           }
1663      }
1664    if (sd->down.dir_y)
1665      {
1666         if ((!sd->widget) || 
1667             (!elm_widget_drag_child_locked_y_get(sd->widget)))
1668           {
1669              oy = sd->down.hold_y;
1670           }
1671      }
1672    elm_smart_scroller_child_pos_set(sd->smart_obj, ox, oy);
1673    return ECORE_CALLBACK_RENEW;
1674 }
1675
1676 static Eina_Bool
1677 _smart_event_post_up(void *data, Evas *e __UNUSED__)
1678 {
1679    Smart_Data *sd = data;
1680    if (sd->widget)
1681      {
1682         if (sd->down.dragged)
1683           {
1684              elm_widget_drag_lock_x_set(sd->widget, 0);
1685              elm_widget_drag_lock_y_set(sd->widget, 0);
1686           }
1687      }
1688    return EINA_TRUE;
1689 }
1690
1691 static void
1692 _smart_event_mouse_up(void *data, Evas *e, Evas_Object *obj __UNUSED__, void *event_info)
1693 {
1694    Evas_Event_Mouse_Down *ev;
1695    Smart_Data *sd;
1696    Evas_Coord x = 0, y = 0, ox = 0, oy = 0;
1697
1698    sd = data;
1699    ev = event_info;
1700    sd->down.hold_parent = 0;
1701 //   if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return ;
1702    evas_post_event_callback_push(e, _smart_event_post_up, sd);
1703    // FIXME: respect elm_widget_scroll_hold_get of parent container
1704    if (_elm_config->thumbscroll_enable)
1705      {
1706         if (ev->button == 1)
1707           {
1708              if (sd->down.onhold_animator)
1709                {
1710                   ecore_animator_del(sd->down.onhold_animator);
1711                   sd->down.onhold_animator = NULL;
1712                }
1713              x = ev->canvas.x - sd->down.x;
1714              y = ev->canvas.y - sd->down.y;
1715              if (sd->down.dragged)
1716                {
1717                   _smart_drag_stop(sd->smart_obj);
1718                   if ((!sd->hold) && (!sd->freeze))
1719                     {
1720                        double t, at, dt;
1721                        int i;
1722                        Evas_Coord ax, ay, dx, dy, vel;
1723
1724 #ifdef EVTIME
1725                        t = ev->timestamp / 1000.0;
1726 #else
1727                        t = ecore_loop_time_get();
1728 #endif
1729                        ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
1730                        ax = ev->canvas.x;
1731                        ay = ev->canvas.y;
1732                        at = 0.0;
1733 #ifdef SCROLLDBG
1734                        printf("------ %i %i\n", ev->canvas.x, ev->canvas.y);
1735 #endif
1736                        for (i = 0; i < 20; i++)
1737                          {
1738                             dt = t - sd->down.history[i].timestamp;
1739                             if (dt > 0.2) break;
1740 #ifdef SCROLLDBG
1741                             printf("H: %i %i @ %1.3f\n",
1742                                    sd->down.history[i].x,
1743                                    sd->down.history[i].y, dt);
1744 #endif
1745                             at += dt;
1746                             ax += sd->down.history[i].x;
1747                             ay += sd->down.history[i].y;
1748                          }
1749                        ax /= (i + 1);
1750                        ay /= (i + 1);
1751                        at /= (i + 1);
1752                        at *= 4.0;
1753                        dx = ev->canvas.x - ax;
1754                        dy = ev->canvas.y - ay;
1755                        if (at > 0)
1756                          {
1757                             vel = sqrt((dx * dx) + (dy * dy)) / at;
1758                             if ((_elm_config->thumbscroll_friction > 0.0) &&
1759                                 (vel > _elm_config->thumbscroll_momentum_threshold) &&
1760                                 (!sd->freeze))
1761                               {
1762                                  sd->down.dx = ((double)dx / at);
1763                                  sd->down.dy = ((double)dy / at);
1764                                  if (((sd->down.dx > 0) && (sd->down.pdx > 0)) ||
1765                                      ((sd->down.dx < 0) && (sd->down.pdx < 0)))
1766                                    sd->down.dx += (double)sd->down.pdx * 1.5; // FIXME: * 1.5 - probably should be config
1767                                  if (((sd->down.dy > 0) && (sd->down.pdy > 0)) ||
1768                                      ((sd->down.dy < 0) && (sd->down.pdy < 0)))
1769                                    sd->down.dy += (double)sd->down.pdy * 1.5; // FIXME: * 1.5 - probably should be config
1770                                  if (((sd->down.dx > 0) && (sd->down.pdx > 0)) ||
1771                                      ((sd->down.dx < 0) && (sd->down.pdx < 0)) ||
1772                                      ((sd->down.dy > 0) && (sd->down.pdy > 0)) ||
1773                                      ((sd->down.dy < 0) && (sd->down.pdy < 0)))
1774                                   {
1775                                     double t = ecore_loop_time_get();
1776                                     double dt = t - sd->down.anim_start;
1777                                     
1778                                     if (dt < 0.0) dt = 0.0;
1779                                     else if (dt > _elm_config->thumbscroll_friction)
1780                                       dt = _elm_config->thumbscroll_friction;
1781                                     sd->down.extra_time = _elm_config->thumbscroll_friction - dt;
1782                                   }
1783                                  else
1784                                   sd->down.extra_time = 0.0;
1785                                  sd->down.pdx = sd->down.dx;
1786                                  sd->down.pdy = sd->down.dy;
1787                                  ox = -sd->down.dx;
1788                                  oy = -sd->down.dy;
1789                                  if (!_smart_do_page(sd))
1790                                    {
1791                                       if (!sd->down.momentum_animator && !sd->momentum_animator_disabled)
1792                                         {
1793                                            sd->down.momentum_animator = ecore_animator_add(_smart_momentum_animator, sd);
1794                                            ev->event_flags |= EVAS_EVENT_FLAG_ON_SCROLL;
1795                                            _smart_anim_start(sd->smart_obj);
1796                                         }
1797                                       sd->down.anim_start = ecore_loop_time_get();
1798                                       elm_smart_scroller_child_pos_get(sd->smart_obj, &x, &y);
1799                                       sd->down.sx = x;
1800                                       sd->down.sy = y;
1801                                       sd->down.b0x = 0;
1802                                       sd->down.b0y = 0;
1803                                    }
1804                               }
1805                          }
1806                        if (sd->down.hold_animator)
1807                          {
1808                             ecore_animator_del(sd->down.hold_animator);
1809                             sd->down.hold_animator = NULL;
1810                          }
1811                     }
1812                   else
1813                     {
1814                        sd->down.pdx = 0;
1815                        sd->down.pdy = 0;
1816                     }
1817                   evas_event_feed_hold(e, 0, ev->timestamp, ev->data);
1818                   if (_smart_do_page(sd))
1819                     {
1820                        Evas_Coord pgx, pgy;
1821
1822                        elm_smart_scroller_child_pos_get(sd->smart_obj, &x, &y);
1823                        if ((!sd->widget) || 
1824                            (!elm_widget_drag_child_locked_x_get(sd->widget)))
1825                          {
1826                             pgx = _smart_page_x_get(sd, ox);
1827                             if (pgx != x) 
1828                               {
1829                                  ev->event_flags |= EVAS_EVENT_FLAG_ON_SCROLL;
1830                                  _smart_scrollto_x(sd, _elm_config->page_scroll_friction, pgx);
1831                               }
1832                          }
1833                        if ((!sd->widget) || 
1834                            (!elm_widget_drag_child_locked_y_get(sd->widget)))
1835                          {
1836                             pgy = _smart_page_y_get(sd, oy);
1837                             if (pgy != y) 
1838                               {
1839                                  ev->event_flags |= EVAS_EVENT_FLAG_ON_SCROLL;
1840                                  _smart_scrollto_y(sd, _elm_config->page_scroll_friction, pgy);
1841                               }
1842                          }
1843                     }
1844                }
1845              else
1846                {
1847                   sd->down.pdx = 0;
1848                   sd->down.pdy = 0;
1849                   if (_smart_do_page(sd))
1850                     {
1851                        Evas_Coord pgx, pgy;
1852
1853                        elm_smart_scroller_child_pos_get(sd->smart_obj, &x, &y);
1854                        if ((!sd->widget) || 
1855                            (!elm_widget_drag_child_locked_x_get(sd->widget)))
1856                          {
1857                             pgx = _smart_page_x_get(sd, ox);
1858                             if (pgx != x) _smart_scrollto_x(sd, _elm_config->page_scroll_friction, pgx);
1859                          }
1860                        if ((!sd->widget) || 
1861                            (!elm_widget_drag_child_locked_y_get(sd->widget)))
1862                          {
1863                             pgy = _smart_page_y_get(sd, oy);
1864                             if (pgy != y) _smart_scrollto_y(sd, _elm_config->page_scroll_friction, pgy);
1865                          }
1866                     }
1867                   if (sd->down.hold_animator)
1868                     {
1869                        ecore_animator_del(sd->down.hold_animator);
1870                        sd->down.hold_animator = NULL;
1871                     }
1872                }
1873              if (sd->down.scroll)
1874                {
1875                   ev->event_flags |= EVAS_EVENT_FLAG_ON_SCROLL;
1876                   sd->down.scroll = 0;
1877                }
1878              if (sd->down.hold)
1879                {
1880                   ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
1881                   sd->down.hold = 0;
1882                }
1883              sd->down.dragged_began = 0;
1884              sd->down.dir_x = 0;
1885              sd->down.dir_y = 0;
1886              sd->down.want_dragged = 0;
1887              sd->down.dragged = 0;
1888              sd->down.now = 0;
1889              elm_smart_scroller_child_pos_get(sd->smart_obj, &x, &y);
1890              elm_smart_scroller_child_pos_set(sd->smart_obj, x, y);
1891              _update_wanted_coordinates(sd, x, y);
1892
1893              if (!_smart_do_page(sd))
1894                bounce_eval(sd);
1895           }
1896      }
1897 }
1898
1899 static Eina_Bool
1900 _smart_onhold_animator(void *data)
1901 {
1902    Smart_Data *sd;
1903    double t, td;
1904    double vx, vy;
1905    Evas_Coord x, y, ox, oy;
1906
1907    sd = data;
1908    t = ecore_loop_time_get();
1909    if (sd->down.onhold_tlast > 0.0)
1910      {
1911         td = t - sd->down.onhold_tlast;
1912         vx = sd->down.onhold_vx * td * (double)_elm_config->thumbscroll_threshold * 2.0;
1913         vy = sd->down.onhold_vy * td * (double)_elm_config->thumbscroll_threshold * 2.0;
1914         elm_smart_scroller_child_pos_get(sd->smart_obj, &ox, &oy);
1915         x = ox;
1916         y = oy;
1917         
1918         if (sd->down.dir_x)
1919           {
1920              if ((!sd->widget) || 
1921                  (!elm_widget_drag_child_locked_x_get(sd->widget)))
1922                {
1923                   sd->down.onhold_vxe += vx;
1924                   x = ox + (int)sd->down.onhold_vxe;
1925                   sd->down.onhold_vxe -= (int)sd->down.onhold_vxe;
1926                }
1927           }
1928         
1929         if (sd->down.dir_y)
1930           {
1931              if ((!sd->widget) || 
1932                  (!elm_widget_drag_child_locked_y_get(sd->widget)))
1933                {
1934                   sd->down.onhold_vye += vy;
1935                   y = oy + (int)sd->down.onhold_vye;
1936                   sd->down.onhold_vye -= (int)sd->down.onhold_vye;
1937                }
1938           }
1939         
1940         elm_smart_scroller_child_pos_set(sd->smart_obj, x, y);
1941      }
1942    sd->down.onhold_tlast = t;
1943    return ECORE_CALLBACK_RENEW;
1944 }
1945
1946 static Eina_Bool
1947 _smart_event_post_move(void *data, Evas *e __UNUSED__)
1948 {
1949    Smart_Data *sd = data;
1950
1951    if (sd->down.want_dragged)
1952      {
1953         int start = 0;
1954
1955         if (sd->down.hold_parent)
1956           {
1957              if ((sd->down.dir_x) && !can_scroll(sd, sd->down.hdir))
1958                {
1959                   sd->down.dir_x = 0;
1960                }
1961              if ((sd->down.dir_y) && !can_scroll(sd, sd->down.vdir))
1962                {
1963                   sd->down.dir_y = 0;
1964                }
1965           }
1966         if (sd->down.dir_x)
1967           {
1968              if ((!sd->widget) || 
1969                  (!elm_widget_drag_child_locked_x_get(sd->widget)))
1970                {
1971                   sd->down.want_dragged = 0;
1972                   sd->down.dragged = 1;
1973                   if (sd->widget)
1974                     {
1975                        elm_widget_drag_lock_x_set(sd->widget, 1);
1976                     }
1977                   start = 1;
1978                }
1979              else
1980                sd->down.dir_x = 0;
1981           }
1982         if (sd->down.dir_y)
1983           {
1984              if ((!sd->widget) || 
1985                  (!elm_widget_drag_child_locked_y_get(sd->widget)))
1986                {
1987                   sd->down.want_dragged = 0;
1988                   sd->down.dragged = 1;
1989                   if (sd->widget)
1990                     {
1991                        elm_widget_drag_lock_y_set(sd->widget, 1);
1992                     }
1993                   start = 1;
1994                }
1995              else
1996                sd->down.dir_y = 0;
1997           }
1998         if ((!sd->down.dir_x) && (!sd->down.dir_y))
1999           {
2000              sd->down.cancelled = 1;
2001           }
2002         if (start) _smart_drag_start(sd->smart_obj);
2003      }
2004    return EINA_TRUE;
2005 }
2006
2007 static void
2008 _smart_event_mouse_move(void *data, Evas *e, Evas_Object *obj __UNUSED__, void *event_info)
2009 {
2010    Evas_Event_Mouse_Move *ev;
2011    Smart_Data *sd;
2012    Evas_Coord x = 0, y = 0;
2013
2014    sd = data;
2015    ev = event_info;
2016 //   if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return ;
2017    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) sd->down.hold_parent = 1;
2018    evas_post_event_callback_push(e, _smart_event_post_move, sd);
2019    // FIXME: respect elm_widget_scroll_hold_get of parent container
2020    if (_elm_config->thumbscroll_enable)
2021      {
2022         if (sd->down.now)
2023           {
2024              int dodir = 0;
2025
2026 #ifdef SCROLLDBG
2027              printf("::: %i %i\n", ev->cur.canvas.x, ev->cur.canvas.y);
2028 #endif
2029              memmove(&(sd->down.history[1]), &(sd->down.history[0]),
2030                      sizeof(sd->down.history[0]) * 19);
2031 #ifdef EVTIME
2032              sd->down.history[0].timestamp = ev->timestamp / 1000.0;
2033 #else
2034              sd->down.history[0].timestamp = ecore_loop_time_get();
2035 #endif
2036              sd->down.history[0].x = ev->cur.canvas.x;
2037              sd->down.history[0].y = ev->cur.canvas.y;
2038
2039              if (!sd->down.dragged_began)
2040                {
2041                   x = ev->cur.canvas.x - sd->down.x;
2042                   y = ev->cur.canvas.y - sd->down.y;
2043                   
2044                   sd->down.hdir = -1;
2045                   sd->down.vdir = -1;
2046                     
2047                   if      (x > 0) sd->down.hdir = LEFT;
2048                   else if (x < 0) sd->down.hdir = RIGHT;
2049                   if      (y > 0) sd->down.vdir = UP;
2050                   else if (y < 0) sd->down.vdir = DOWN;
2051                   
2052                   if (x < 0) x = -x;
2053                   if (y < 0) y = -y;
2054              
2055                   if ((sd->one_dir_at_a_time) &&
2056                       (!((sd->down.dir_x) || (sd->down.dir_y))))
2057                     {
2058                        if (x > _elm_config->thumbscroll_threshold)
2059                          {
2060                             if (x > (y * 2))
2061                               {
2062                                  sd->down.dir_x = 1;
2063                                  sd->down.dir_y = 0;
2064                                  dodir++;
2065                               }
2066                          }
2067                        if (y > _elm_config->thumbscroll_threshold)
2068                          {
2069                             if (y > (x * 2))
2070                               {
2071                                  sd->down.dir_x = 0;
2072                                  sd->down.dir_y = 1;
2073                                  dodir++;
2074                               }
2075                          }
2076                        if (!dodir)
2077                          {
2078                             sd->down.dir_x = 1;
2079                             sd->down.dir_y = 1;
2080                          }
2081                     }
2082                   else
2083                     {
2084 //                       can_scroll(sd, LEFT);
2085 //                       can_scroll(sd, RIGHT);
2086 //                       can_scroll(sd, UP);
2087 //                       can_scroll(sd, DOWN);
2088                        sd->down.dir_x = 1;
2089                        sd->down.dir_y = 1;
2090                     }
2091                }
2092              if ((!sd->hold) && (!sd->freeze))
2093                {
2094                   if ((sd->down.dragged) ||
2095                       (((x * x) + (y * y)) >
2096                        (_elm_config->thumbscroll_threshold *
2097                         _elm_config->thumbscroll_threshold)))
2098                     {
2099                        sd->down.dragged_began = 1;
2100                        if (!sd->down.dragged)
2101                          {
2102                             sd->down.want_dragged = 1;
2103                             ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
2104 //                            evas_event_feed_hold(e, 1, ev->timestamp, ev->data);
2105 //                            _smart_drag_start(sd->smart_obj);
2106                          }
2107                        if (sd->down.dragged)
2108                          {
2109                             ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
2110                          }
2111 //                       ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
2112 //                       sd->down.dragged = 1;
2113                        if (sd->down.dir_x)
2114                          x = sd->down.sx - (ev->cur.canvas.x - sd->down.x);
2115                        else
2116                          x = sd->down.sx;
2117                        if (sd->down.dir_y)
2118                          y = sd->down.sy - (ev->cur.canvas.y - sd->down.y);
2119                        else
2120                          y = sd->down.sy;
2121                        if ((sd->down.dir_x) || (sd->down.dir_y))
2122                          {
2123                             if (!sd->down.locked)
2124                               {
2125                                  sd->down.locked_x = x;
2126                                  sd->down.locked_y = y;
2127                                  sd->down.locked = 1;
2128                               }
2129                             if (!((sd->down.dir_x) && (sd->down.dir_y)))
2130                               {
2131                                  if (sd->down.dir_x) y = sd->down.locked_y;
2132                                  else x = sd->down.locked_x;
2133                               }
2134                          }
2135                        if (_elm_config->thumbscroll_border_friction > 0.0)
2136                          {
2137                             Evas_Coord minx, miny;
2138                             sd->pan_func.min_get(sd->pan_obj, &minx, &miny);
2139                             if (y < miny)
2140                                y += (miny - y) *
2141                                   _elm_config->thumbscroll_border_friction;
2142                             else if (sd->child.h <= sd->h)
2143                                y += (sd->down.sy - y) *
2144                                   _elm_config->thumbscroll_border_friction;
2145                             else if ((sd->child.h - sd->h + miny) < y)
2146                                y += (sd->child.h - sd->h + miny - y) *
2147                                   _elm_config->thumbscroll_border_friction;
2148                             if (x < minx)
2149                                x += (minx - x) *
2150                                   _elm_config->thumbscroll_border_friction;
2151                             else if (sd->child.w <= sd->w)
2152                                x += (sd->down.sx - x) *
2153                                   _elm_config->thumbscroll_border_friction;
2154                             else if ((sd->child.w - sd->w + minx) < x)
2155                                x += (sd->child.w - sd->w + minx - x) *
2156                                   _elm_config->thumbscroll_border_friction;
2157                          }
2158
2159                        sd->down.hold_x = x;
2160                        sd->down.hold_y = y;
2161                        if (!sd->down.hold_animator)
2162                          sd->down.hold_animator = 
2163                             ecore_animator_add(_smart_hold_animator, sd);
2164 //                       printf("a %i %i\n", sd->down.hold_x, sd->down.hold_y);
2165 //                       _smart_onhold_animator(sd);
2166 //                       elm_smart_scroller_child_pos_set(sd->smart_obj, x, y);
2167                     }
2168                   else
2169                     {
2170                        if (sd->down.dragged_began)
2171                          {
2172                             ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
2173                             if (!sd->down.hold)
2174                               {
2175                                  sd->down.hold = 1;
2176                                  evas_event_feed_hold(e, 1, ev->timestamp, ev->data);
2177                               }
2178                          }
2179                     }
2180                }
2181              else if (!sd->freeze)
2182                {
2183                   Evas_Coord ex, ey, ew, eh;
2184                   double vx = 0.0, vy = 0.0;
2185
2186                   evas_object_geometry_get(sd->event_obj, &ex, &ey, &ew, &eh);
2187                   x = ev->cur.canvas.x - ex;
2188                   y = ev->cur.canvas.y - ey;
2189                   if (x < _elm_config->thumbscroll_threshold)
2190                     {
2191                        if (_elm_config->thumbscroll_threshold > 0.0)
2192                          vx = -(double)(_elm_config->thumbscroll_threshold - x) /
2193                          _elm_config->thumbscroll_threshold;
2194                        else
2195                          vx = -1.0;
2196                     }
2197                   else if (x > (ew - _elm_config->thumbscroll_threshold))
2198                     {
2199                        if (_elm_config->thumbscroll_threshold > 0.0)
2200                          vx = (double)(_elm_config->thumbscroll_threshold - (ew - x)) /
2201                          _elm_config->thumbscroll_threshold;
2202                        else
2203                          vx = 1.0;
2204                     }
2205                   if (y < _elm_config->thumbscroll_threshold)
2206                     {
2207                        if (_elm_config->thumbscroll_threshold > 0.0)
2208                          vy = -(double)(_elm_config->thumbscroll_threshold - y) /
2209                          _elm_config->thumbscroll_threshold;
2210                        else
2211                          vy = -1.0;
2212                     }
2213                   else if (y > (eh - _elm_config->thumbscroll_threshold))
2214                     {
2215                        if (_elm_config->thumbscroll_threshold > 0.0)
2216                          vy = (double)(_elm_config->thumbscroll_threshold - (eh - y)) /
2217                          _elm_config->thumbscroll_threshold;
2218                        else
2219                          vy = 1.0;
2220                     }
2221                   if ((vx != 0.0) || (vy != 0.0))
2222                     {
2223                        sd->down.onhold_vx = vx;
2224                        sd->down.onhold_vy = vy;
2225                        if (!sd->down.onhold_animator)
2226                          {
2227                             sd->down.onhold_vxe = 0.0;
2228                             sd->down.onhold_vye = 0.0;
2229                             sd->down.onhold_tlast = 0.0;
2230                             sd->down.onhold_animator = ecore_animator_add(_smart_onhold_animator, sd);
2231                          }
2232 //                       printf("b %i %i\n", sd->down.hold_x, sd->down.hold_y);
2233                     }
2234                   else
2235                     {
2236                        if (sd->down.onhold_animator)
2237                          {
2238                             ecore_animator_del(sd->down.onhold_animator);
2239                             sd->down.onhold_animator = NULL;
2240                          }
2241                     }
2242                }
2243           }
2244      }
2245 }
2246
2247 static void
2248 _smart_scrollbar_read(Smart_Data *sd)
2249 {
2250    Evas_Coord x, y, mx = 0, my = 0, px, py, minx = 0, miny = 0;
2251    double vx, vy;
2252
2253    edje_object_part_drag_value_get(sd->edje_obj, "elm.dragable.vbar", NULL, &vy);
2254    edje_object_part_drag_value_get(sd->edje_obj, "elm.dragable.hbar", &vx, NULL);
2255    sd->pan_func.max_get(sd->pan_obj, &mx, &my);
2256    sd->pan_func.min_get(sd->pan_obj, &minx, &miny);
2257    x = vx * (double)mx + minx;
2258    y = vy * (double)my + miny;
2259    sd->pan_func.get(sd->pan_obj, &px, &py);
2260    sd->pan_func.set(sd->pan_obj, x, y);
2261    if ((px != x) || (py != y))
2262      edje_object_signal_emit(sd->edje_obj, "elm,action,scroll", "elm");
2263 }
2264
2265 static void
2266 _smart_scrollbar_reset(Smart_Data *sd)
2267 {
2268    Evas_Coord px = 0, py = 0, minx = 0, miny = 0;
2269
2270    edje_object_part_drag_value_set(sd->edje_obj, "elm.dragable.vbar", 0.0, 0.0);
2271    edje_object_part_drag_value_set(sd->edje_obj, "elm.dragable.hbar", 0.0, 0.0);
2272    if ((!sd->child_obj) && (!sd->extern_pan))
2273      {
2274         edje_object_part_drag_size_set(sd->edje_obj, "elm.dragable.vbar", 1.0, 1.0);
2275         edje_object_part_drag_size_set(sd->edje_obj, "elm.dragable.hbar", 1.0, 1.0);
2276      }
2277    sd->pan_func.min_get(sd->pan_obj, &minx, &miny);
2278    sd->pan_func.get(sd->pan_obj, &px, &py);
2279    sd->pan_func.set(sd->pan_obj, minx, miny);
2280    if ((px != minx) || (py != miny))
2281      edje_object_signal_emit(sd->edje_obj, "elm,action,scroll", "elm");
2282 }
2283
2284 static int
2285 _smart_scrollbar_bar_v_visibility_adjust(Smart_Data *sd)
2286 {
2287    int scroll_v_vis_change = 0;
2288    Evas_Coord h, vw, vh;
2289
2290    h = sd->child.h;
2291    evas_object_geometry_get(sd->pan_obj, NULL, NULL, &vw, &vh);
2292    if (sd->vbar_visible)
2293      {
2294         if (sd->vbar_flags == ELM_SMART_SCROLLER_POLICY_AUTO)
2295           {
2296              if ((sd->child_obj) || (sd->extern_pan))
2297                {
2298                   if (h <= vh)
2299                     {
2300                        scroll_v_vis_change = 1;
2301                        sd->vbar_visible = 0;
2302                     }
2303                }
2304              else
2305                {
2306                   scroll_v_vis_change = 1;
2307                   sd->vbar_visible = 0;
2308                }
2309           }
2310         else if (sd->vbar_flags == ELM_SMART_SCROLLER_POLICY_OFF)
2311           {
2312              scroll_v_vis_change = 1;
2313              sd->vbar_visible = 0;
2314           }
2315      }
2316    else
2317      {
2318         if (sd->vbar_flags == ELM_SMART_SCROLLER_POLICY_AUTO)
2319           {
2320              if ((sd->child_obj) || (sd->extern_pan))
2321                {
2322                   if (h > vh)
2323                     {
2324                        scroll_v_vis_change = 1;
2325                        sd->vbar_visible = 1;
2326                     }
2327                }
2328           }
2329         else if (sd->vbar_flags == ELM_SMART_SCROLLER_POLICY_ON)
2330           {
2331              scroll_v_vis_change = 1;
2332              sd->vbar_visible = 1;
2333           }
2334      }
2335    if (scroll_v_vis_change)
2336      {
2337         if (sd->vbar_flags != ELM_SMART_SCROLLER_POLICY_OFF)
2338           {
2339              if (sd->vbar_visible)
2340                edje_object_signal_emit(sd->edje_obj, "elm,action,show,vbar", "elm");
2341              else
2342                edje_object_signal_emit(sd->edje_obj, "elm,action,hide,vbar", "elm");
2343              edje_object_message_signal_process(sd->edje_obj);
2344              _smart_scrollbar_size_adjust(sd);
2345           }
2346         else
2347           edje_object_signal_emit(sd->edje_obj, "elm,action,hide,vbar", "elm");
2348      }
2349    return scroll_v_vis_change;
2350 }
2351
2352 static int
2353 _smart_scrollbar_bar_h_visibility_adjust(Smart_Data *sd)
2354 {
2355    int scroll_h_vis_change = 0;
2356    Evas_Coord w, vw, vh;
2357
2358    w = sd->child.w;
2359    evas_object_geometry_get(sd->pan_obj, NULL, NULL, &vw, &vh);
2360    if (sd->hbar_visible)
2361      {
2362         if (sd->hbar_flags == ELM_SMART_SCROLLER_POLICY_AUTO)
2363           {
2364              if ((sd->child_obj) || (sd->extern_pan))
2365                {
2366                   if (w <= vw)
2367                     {
2368                        scroll_h_vis_change = 1;
2369                        sd->hbar_visible = 0;
2370                     }
2371                }
2372              else
2373                {
2374                   scroll_h_vis_change = 1;
2375                   sd->hbar_visible = 0;
2376                }
2377           }
2378         else if (sd->hbar_flags == ELM_SMART_SCROLLER_POLICY_OFF)
2379           {
2380              scroll_h_vis_change = 1;
2381              sd->hbar_visible = 0;
2382           }
2383      }
2384    else
2385      {
2386         if (sd->hbar_flags == ELM_SMART_SCROLLER_POLICY_AUTO)
2387           {
2388              if ((sd->child_obj) || (sd->extern_pan))
2389                {
2390                   if (w > vw)
2391                     {
2392                        scroll_h_vis_change = 1;
2393                        sd->hbar_visible = 1;
2394                     }
2395                }
2396           }
2397         else if (sd->hbar_flags == ELM_SMART_SCROLLER_POLICY_ON)
2398           {
2399              scroll_h_vis_change = 1;
2400              sd->hbar_visible = 1;
2401           }
2402      }
2403    if (scroll_h_vis_change)
2404      {
2405         if (sd->hbar_flags != ELM_SMART_SCROLLER_POLICY_OFF)
2406           {
2407              if (sd->hbar_visible)
2408                edje_object_signal_emit(sd->edje_obj, "elm,action,show,hbar", "elm");
2409              else
2410                edje_object_signal_emit(sd->edje_obj, "elm,action,hide,hbar", "elm");
2411              edje_object_message_signal_process(sd->edje_obj);
2412              _smart_scrollbar_size_adjust(sd);
2413           }
2414         else
2415           edje_object_signal_emit(sd->edje_obj, "elm,action,hide,hbar", "elm");
2416         _smart_scrollbar_size_adjust(sd);
2417      }
2418    return scroll_h_vis_change;
2419 }
2420
2421 static void
2422 _smart_scrollbar_bar_visibility_adjust(Smart_Data *sd)
2423 {
2424    int changed = 0;
2425
2426    changed |= _smart_scrollbar_bar_h_visibility_adjust(sd);
2427    changed |= _smart_scrollbar_bar_v_visibility_adjust(sd);
2428    if (changed)
2429      {
2430         _smart_scrollbar_bar_h_visibility_adjust(sd);
2431         _smart_scrollbar_bar_v_visibility_adjust(sd);
2432      }
2433 }
2434
2435 static void
2436 _smart_scrollbar_size_adjust(Smart_Data *sd)
2437 {
2438    if ((sd->child_obj) || (sd->extern_pan))
2439      {
2440         Evas_Coord x, y, w, h, mx = 0, my = 0, vw = 0, vh = 0, px, py, minx = 0, miny = 0;
2441         double vx, vy, size;
2442
2443         edje_object_part_geometry_get(sd->edje_obj, "elm.swallow.content",
2444                                       NULL, NULL, &vw, &vh);
2445         w = sd->child.w;
2446         if (w < 1) w = 1;
2447         size = (double)vw / (double)w;
2448         if (size > 1.0)
2449           {
2450              size = 1.0;
2451              edje_object_part_drag_value_set(sd->edje_obj, "elm.dragable.hbar", 0.0, 0.0);
2452           }
2453         edje_object_part_drag_size_set(sd->edje_obj, "elm.dragable.hbar", size, 1.0);
2454
2455         h = sd->child.h;
2456         if (h < 1) h = 1;
2457         size = (double)vh / (double)h;
2458         if (size > 1.0)
2459           {
2460              size = 1.0;
2461              edje_object_part_drag_value_set(sd->edje_obj, "elm.dragable.vbar", 0.0, 0.0);
2462           }
2463         edje_object_part_drag_size_set(sd->edje_obj, "elm.dragable.vbar", 1.0, size);
2464
2465         edje_object_part_drag_value_get(sd->edje_obj, "elm.dragable.hbar", &vx, NULL);
2466         edje_object_part_drag_value_get(sd->edje_obj, "elm.dragable.vbar", NULL, &vy);
2467         sd->pan_func.max_get(sd->pan_obj, &mx, &my);
2468         sd->pan_func.min_get(sd->pan_obj, &minx, &miny);
2469         x = vx * mx + minx;
2470         y = vy * my + miny;
2471
2472         edje_object_part_drag_step_set(sd->edje_obj, "elm.dragable.hbar", (double)sd->step.x / (double)w, 0.0);
2473         edje_object_part_drag_step_set(sd->edje_obj, "elm.dragable.vbar", 0.0, (double)sd->step.y / (double)h);
2474         if (sd->page.x > 0)
2475           edje_object_part_drag_page_set(sd->edje_obj, "elm.dragable.hbar", (double)sd->page.x / (double)w, 0.0);
2476         else
2477           edje_object_part_drag_page_set(sd->edje_obj, "elm.dragable.hbar", -((double)sd->page.x * ((double)vw / (double)w)) / 100.0, 0.0);
2478         if (sd->page.y > 0)
2479           edje_object_part_drag_page_set(sd->edje_obj, "elm.dragable.vbar", 0.0, (double)sd->page.y / (double)h);
2480         else
2481           edje_object_part_drag_page_set(sd->edje_obj, "elm.dragable.vbar", 0.0, -((double)sd->page.y * ((double)vh / (double)h)) / 100.0);
2482
2483         sd->pan_func.get(sd->pan_obj, &px, &py);
2484         if (vx != mx) x = px;
2485         if (vy != my) y = py;
2486         sd->pan_func.set(sd->pan_obj, x, y);
2487 //      if ((px != 0) || (py != 0))
2488 //        edje_object_signal_emit(sd->edje_obj, "elm,action,scroll", "elm");
2489      }
2490    else
2491      {
2492         Evas_Coord px = 0, py = 0, minx = 0, miny = 0;
2493
2494         edje_object_part_drag_size_set(sd->edje_obj, "elm.dragable.vbar", 1.0, 1.0);
2495         edje_object_part_drag_size_set(sd->edje_obj, "elm.dragable.hbar", 1.0, 1.0);
2496         sd->pan_func.min_get(sd->pan_obj, &minx, &miny);
2497         sd->pan_func.get(sd->pan_obj, &px, &py);
2498         sd->pan_func.set(sd->pan_obj, minx, miny);
2499         if ((px != minx) || (py != miny))
2500           edje_object_signal_emit(sd->edje_obj, "elm,action,scroll", "elm");
2501      }
2502    _smart_scrollbar_bar_visibility_adjust(sd);
2503 }
2504
2505 static void
2506 _smart_reconfigure(Smart_Data *sd)
2507 {
2508    evas_object_move(sd->edje_obj, sd->x, sd->y);
2509    evas_object_resize(sd->edje_obj, sd->w, sd->h);
2510    evas_object_move(sd->event_obj, sd->x, sd->y);
2511    evas_object_resize(sd->event_obj, sd->w, sd->h);
2512    _smart_scrollbar_size_adjust(sd);
2513 }
2514
2515 static void
2516 _smart_add(Evas_Object *obj)
2517 {
2518    Smart_Data *sd;
2519    Evas_Object *o;
2520
2521    sd = calloc(1, sizeof(Smart_Data));
2522    if (!sd) return;
2523    evas_object_smart_data_set(obj, sd);
2524
2525    sd->smart_obj = obj;
2526    sd->x = 0;
2527    sd->y = 0;
2528    sd->w = 0;
2529    sd->h = 0;
2530    sd->step.x = 32;
2531    sd->step.y = 32;
2532    sd->page.x = -50;
2533    sd->page.y = -50;
2534    sd->hbar_flags = ELM_SMART_SCROLLER_POLICY_AUTO;
2535    sd->vbar_flags = ELM_SMART_SCROLLER_POLICY_AUTO;
2536    sd->hbar_visible = 1;
2537    sd->vbar_visible = 1;
2538
2539    sd->bounce_horiz = 1;
2540    sd->bounce_vert = 1;
2541
2542    sd->one_dir_at_a_time = 1;
2543    sd->momentum_animator_disabled = 0;
2544    sd->bounce_animator_disabled = 0;
2545    
2546    o = edje_object_add(evas_object_evas_get(obj));
2547    evas_object_propagate_events_set(o, 0);
2548    sd->edje_obj = o;
2549    // FIXME: null parent obj ... :(
2550    elm_smart_scroller_object_theme_set(NULL, obj, "scroller", "base", "default");
2551    edje_object_signal_callback_add(o, "drag", "elm.dragable.vbar", _smart_edje_drag_v, sd);
2552    edje_object_signal_callback_add(o, "drag,start", "elm.dragable.vbar", _smart_edje_drag_v_start, sd);
2553    edje_object_signal_callback_add(o, "drag,stop", "elm.dragable.vbar", _smart_edje_drag_v_stop, sd);
2554    edje_object_signal_callback_add(o, "drag,step", "elm.dragable.vbar", _smart_edje_drag_v, sd);
2555    edje_object_signal_callback_add(o, "drag,page", "elm.dragable.vbar", _smart_edje_drag_v, sd);
2556    edje_object_signal_callback_add(o, "drag", "elm.dragable.hbar", _smart_edje_drag_h, sd);
2557    edje_object_signal_callback_add(o, "drag,start", "elm.dragable.hbar", _smart_edje_drag_h_start, sd);
2558    edje_object_signal_callback_add(o, "drag,stop", "elm.dragable.hbar", _smart_edje_drag_h_stop, sd);
2559    edje_object_signal_callback_add(o, "drag,step", "elm.dragable.hbar", _smart_edje_drag_h, sd);
2560    edje_object_signal_callback_add(o, "drag,page", "elm.dragable.hbar", _smart_edje_drag_h, sd);
2561    evas_object_smart_member_add(o, obj);
2562
2563    o = evas_object_rectangle_add(evas_object_evas_get(obj));
2564    sd->event_obj = o;
2565    evas_object_color_set(o, 0, 0, 0, 0);
2566    evas_object_event_callback_add(o, EVAS_CALLBACK_MOUSE_WHEEL, _smart_event_wheel, sd);
2567    evas_object_event_callback_add(o, EVAS_CALLBACK_MOUSE_DOWN, _smart_event_mouse_down, sd);
2568    evas_object_event_callback_add(o, EVAS_CALLBACK_MOUSE_UP, _smart_event_mouse_up, sd);
2569    evas_object_event_callback_add(o, EVAS_CALLBACK_MOUSE_MOVE, _smart_event_mouse_move, sd);
2570    evas_object_smart_member_add(o, obj);
2571    evas_object_repeat_events_set(o, 1);
2572
2573    sd->pan_func.set = _elm_smart_pan_set;
2574    sd->pan_func.get = _elm_smart_pan_get;
2575    sd->pan_func.max_get = _elm_smart_pan_max_get;
2576    sd->pan_func.min_get = _elm_smart_pan_min_get;
2577    sd->pan_func.child_size_get = _elm_smart_pan_child_size_get;
2578
2579    _smart_scrollbar_reset(sd);
2580 }
2581
2582 static void
2583 _smart_del(Evas_Object *obj)
2584 {
2585    INTERNAL_ENTRY;
2586    elm_smart_scroller_child_set(obj, NULL);
2587    if (!sd->extern_pan) evas_object_del(sd->pan_obj);
2588    evas_object_del(sd->edje_obj);
2589    evas_object_del(sd->event_obj);
2590    if (sd->down.hold_animator) ecore_animator_del(sd->down.hold_animator);
2591    if (sd->down.onhold_animator) ecore_animator_del(sd->down.onhold_animator);
2592    if (sd->down.momentum_animator) ecore_animator_del(sd->down.momentum_animator);
2593    if (sd->down.bounce_x_animator) ecore_animator_del(sd->down.bounce_x_animator);
2594    if (sd->down.bounce_y_animator) ecore_animator_del(sd->down.bounce_y_animator);
2595    if (sd->scrollto.x.animator) ecore_animator_del(sd->scrollto.x.animator);
2596    if (sd->scrollto.y.animator) ecore_animator_del(sd->scrollto.y.animator);
2597    free(sd);
2598    evas_object_smart_data_set(obj, NULL);
2599 }
2600
2601 static void
2602 _smart_move(Evas_Object *obj, Evas_Coord x, Evas_Coord y)
2603 {
2604    INTERNAL_ENTRY;
2605    sd->x = x;
2606    sd->y = y;
2607    _smart_reconfigure(sd);
2608 }
2609
2610 static void
2611 _smart_resize(Evas_Object *obj, Evas_Coord w, Evas_Coord h)
2612 {
2613    INTERNAL_ENTRY;
2614    sd->w = w;
2615    sd->h = h;
2616    _smart_reconfigure(sd);
2617    _elm_smart_scroller_wanted_region_set(obj);
2618 }
2619
2620 static void
2621 _smart_show(Evas_Object *obj)
2622 {
2623    INTERNAL_ENTRY;
2624    evas_object_show(sd->edje_obj);
2625    evas_object_show(sd->event_obj);
2626 }
2627
2628 static void
2629 _smart_hide(Evas_Object *obj)
2630 {
2631    INTERNAL_ENTRY;
2632    evas_object_hide(sd->edje_obj);
2633    evas_object_hide(sd->event_obj);
2634 }
2635
2636 static void
2637 _smart_color_set(Evas_Object *obj, int r, int g, int b, int a)
2638 {
2639    INTERNAL_ENTRY;
2640    evas_object_color_set(sd->edje_obj, r, g, b, a);
2641 }
2642
2643 static void
2644 _smart_clip_set(Evas_Object *obj, Evas_Object *clip)
2645 {
2646    INTERNAL_ENTRY;
2647    evas_object_clip_set(sd->edje_obj, clip);
2648    evas_object_clip_set(sd->event_obj, clip);
2649 }
2650
2651 static void
2652 _smart_clip_unset(Evas_Object *obj)
2653 {
2654    INTERNAL_ENTRY;
2655    evas_object_clip_unset(sd->edje_obj);
2656    evas_object_clip_unset(sd->event_obj);
2657 }
2658
2659 /* never need to touch this */
2660
2661 static void
2662 _smart_init(void)
2663 {
2664    if (_smart) return;
2665      {
2666         static const Evas_Smart_Class sc =
2667           {
2668              SMART_NAME,
2669                EVAS_SMART_CLASS_VERSION,
2670                _smart_add,
2671                _smart_del,
2672                _smart_move,
2673                _smart_resize,
2674                _smart_show,
2675                _smart_hide,
2676                _smart_color_set,
2677                _smart_clip_set,
2678                _smart_clip_unset,
2679                NULL,
2680                NULL,
2681                NULL,
2682                NULL,
2683                NULL,
2684                NULL,
2685                NULL
2686           };
2687         _smart = evas_smart_class_new(&sc);
2688      }
2689 }
2690