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