Elementary els_scroller: Fixed mouse_wheel scrolling.
[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 = 0, py = 0, 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         elm_smart_scroller_child_pos_set(sd->smart_obj, x, y);
1533      }
1534 }
1535
1536 static void
1537 _smart_event_mouse_down(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
1538 {
1539    Evas_Event_Mouse_Down *ev;
1540    Smart_Data *sd;
1541    Evas_Coord x = 0, y = 0;
1542
1543    sd = data;
1544    ev = event_info;
1545 //   if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return ;
1546    if (_elm_config->thumbscroll_enable)
1547      {
1548         sd->down.hold = 0;
1549         if ((sd->down.bounce_x_animator) || (sd->down.bounce_y_animator) ||
1550             (sd->down.momentum_animator) || (sd->scrollto.x.animator) ||
1551             (sd->scrollto.y.animator))
1552           {
1553              ev->event_flags |= EVAS_EVENT_FLAG_ON_SCROLL | EVAS_EVENT_FLAG_ON_HOLD;
1554              sd->down.scroll = 1;
1555              sd->down.hold = 1;
1556              _smart_anim_stop(sd->smart_obj);
1557           }
1558         if (sd->scrollto.x.animator)
1559           {
1560              ecore_animator_del(sd->scrollto.x.animator);
1561              sd->scrollto.x.animator = NULL;
1562           }
1563         if (sd->scrollto.y.animator)
1564           {
1565              ecore_animator_del(sd->scrollto.y.animator);
1566              sd->scrollto.y.animator = NULL;
1567           }
1568         if (sd->down.bounce_x_animator)
1569           {
1570              ecore_animator_del(sd->down.bounce_x_animator);
1571              sd->down.bounce_x_animator = NULL;
1572              sd->bouncemex = 0;
1573           }
1574         if (sd->down.bounce_y_animator)
1575           {
1576              ecore_animator_del(sd->down.bounce_y_animator);
1577              sd->down.bounce_y_animator = NULL;
1578              sd->bouncemey = 0;
1579           }
1580         if (sd->down.hold_animator)
1581           {
1582              ecore_animator_del(sd->down.hold_animator);
1583              sd->down.hold_animator = NULL;
1584              _smart_drag_stop(sd->smart_obj);
1585           }
1586         if (sd->down.momentum_animator)
1587           {
1588              ecore_animator_del(sd->down.momentum_animator);
1589              sd->down.momentum_animator = NULL;
1590              sd->down.bounce_x_hold = 0;
1591              sd->down.bounce_y_hold = 0;
1592              sd->down.ax = 0;
1593              sd->down.ay = 0;
1594           }
1595         if (ev->button == 1)
1596           {
1597              sd->down.now = 1;
1598              sd->down.dragged = 0;
1599              sd->down.dir_x = 0;
1600              sd->down.dir_y = 0;
1601              sd->down.x = ev->canvas.x;
1602              sd->down.y = ev->canvas.y;
1603              elm_smart_scroller_child_pos_get(sd->smart_obj, &x, &y);
1604              sd->down.sx = x;
1605              sd->down.sy = y;
1606              sd->down.locked = 0;
1607              memset(&(sd->down.history[0]), 0, sizeof(sd->down.history[0]) * 20);
1608 #ifdef EVTIME
1609              sd->down.history[0].timestamp = ev->timestamp / 1000.0;
1610 #else
1611              sd->down.history[0].timestamp = ecore_loop_time_get();
1612 #endif
1613              sd->down.history[0].x = ev->canvas.x;
1614              sd->down.history[0].y = ev->canvas.y;
1615           }
1616         sd->down.dragged_began = 0;
1617         sd->down.hold_parent = 0;
1618         sd->down.cancelled = 0;
1619      }
1620 }
1621
1622 static Eina_Bool
1623 _smart_hold_animator(void *data)
1624 {
1625    Smart_Data *sd = data;
1626    Evas_Coord ox, oy;
1627    
1628    elm_smart_scroller_child_pos_get(sd->smart_obj, &ox, &oy);
1629    if (sd->down.dir_x)
1630      {
1631         if ((!sd->widget) || 
1632             (!elm_widget_drag_child_locked_x_get(sd->widget)))
1633           {
1634              ox = sd->down.hold_x;
1635           }
1636      }
1637    if (sd->down.dir_y)
1638      {
1639         if ((!sd->widget) || 
1640             (!elm_widget_drag_child_locked_y_get(sd->widget)))
1641           {
1642              oy = sd->down.hold_y;
1643           }
1644      }
1645    elm_smart_scroller_child_pos_set(sd->smart_obj, ox, oy);
1646    return ECORE_CALLBACK_RENEW;
1647 }
1648
1649 static Eina_Bool
1650 _smart_event_post_up(void *data, Evas *e __UNUSED__)
1651 {
1652    Smart_Data *sd = data;
1653    if (sd->widget)
1654      {
1655         if (sd->down.dragged)
1656           {
1657              elm_widget_drag_lock_x_set(sd->widget, 0);
1658              elm_widget_drag_lock_y_set(sd->widget, 0);
1659           }
1660      }
1661    return EINA_TRUE;
1662 }
1663
1664 static void
1665 _smart_event_mouse_up(void *data, Evas *e, Evas_Object *obj __UNUSED__, void *event_info)
1666 {
1667    Evas_Event_Mouse_Down *ev;
1668    Smart_Data *sd;
1669    Evas_Coord x = 0, y = 0, ox = 0, oy = 0;
1670
1671    sd = data;
1672    ev = event_info;
1673    sd->down.hold_parent = 0;
1674 //   if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return ;
1675    evas_post_event_callback_push(e, _smart_event_post_up, sd);
1676    // FIXME: respect elm_widget_scroll_hold_get of parent container
1677    if (_elm_config->thumbscroll_enable)
1678      {
1679         if (ev->button == 1)
1680           {
1681              if (sd->down.onhold_animator)
1682                {
1683                   ecore_animator_del(sd->down.onhold_animator);
1684                   sd->down.onhold_animator = NULL;
1685                }
1686              x = ev->canvas.x - sd->down.x;
1687              y = ev->canvas.y - sd->down.y;
1688              if (sd->down.dragged)
1689                {
1690                   _smart_drag_stop(sd->smart_obj);
1691                   if ((!sd->hold) && (!sd->freeze))
1692                     {
1693                        double t, at, dt;
1694                        int i;
1695                        Evas_Coord ax, ay, dx, dy, vel;
1696
1697 #ifdef EVTIME
1698                        t = ev->timestamp / 1000.0;
1699 #else
1700                        t = ecore_loop_time_get();
1701 #endif
1702                        ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
1703                        ax = ev->canvas.x;
1704                        ay = ev->canvas.y;
1705                        at = 0.0;
1706 #ifdef SCROLLDBG
1707                        printf("------ %i %i\n", ev->canvas.x, ev->canvas.y);
1708 #endif
1709                        for (i = 0; i < 20; i++)
1710                          {
1711                             dt = t - sd->down.history[i].timestamp;
1712                             if (dt > 0.2) break;
1713 #ifdef SCROLLDBG
1714                             printf("H: %i %i @ %1.3f\n",
1715                                    sd->down.history[i].x,
1716                                    sd->down.history[i].y, dt);
1717 #endif
1718                             at += dt;
1719                             ax += sd->down.history[i].x;
1720                             ay += sd->down.history[i].y;
1721                          }
1722                        ax /= (i + 1);
1723                        ay /= (i + 1);
1724                        at /= (i + 1);
1725                        at *= 4.0;
1726                        dx = ev->canvas.x - ax;
1727                        dy = ev->canvas.y - ay;
1728                        if (at > 0)
1729                          {
1730                             vel = sqrt((dx * dx) + (dy * dy)) / at;
1731                             if ((_elm_config->thumbscroll_friction > 0.0) &&
1732                                 (vel > _elm_config->thumbscroll_momentum_threshold) &&
1733                                 (!sd->freeze))
1734                               {
1735                                  sd->down.dx = ((double)dx / at);
1736                                  sd->down.dy = ((double)dy / at);
1737                                  if (((sd->down.dx > 0) && (sd->down.pdx > 0)) ||
1738                                      ((sd->down.dx < 0) && (sd->down.pdx < 0)))
1739                                    sd->down.dx += (double)sd->down.pdx * 1.5; // FIXME: * 1.5 - probably should be config
1740                                  if (((sd->down.dy > 0) && (sd->down.pdy > 0)) ||
1741                                      ((sd->down.dy < 0) && (sd->down.pdy < 0)))
1742                                    sd->down.dy += (double)sd->down.pdy * 1.5; // FIXME: * 1.5 - probably should be config
1743                                  if (((sd->down.dx > 0) && (sd->down.pdx > 0)) ||
1744                                      ((sd->down.dx < 0) && (sd->down.pdx < 0)) ||
1745                                      ((sd->down.dy > 0) && (sd->down.pdy > 0)) ||
1746                                      ((sd->down.dy < 0) && (sd->down.pdy < 0)))
1747                                   {
1748                                     double t = ecore_loop_time_get();
1749                                     double dt = t - sd->down.anim_start;
1750                                     
1751                                     if (dt < 0.0) dt = 0.0;
1752                                     else if (dt > _elm_config->thumbscroll_friction)
1753                                       dt = _elm_config->thumbscroll_friction;
1754                                     sd->down.extra_time = _elm_config->thumbscroll_friction - dt;
1755                                   }
1756                                  else
1757                                   sd->down.extra_time = 0.0;
1758                                  sd->down.pdx = sd->down.dx;
1759                                  sd->down.pdy = sd->down.dy;
1760                                  ox = -sd->down.dx;
1761                                  oy = -sd->down.dy;
1762                                  if (!_smart_do_page(sd))
1763                                    {
1764                                       if (!sd->down.momentum_animator && !sd->momentum_animator_disabled)
1765                                         {
1766                                            sd->down.momentum_animator = ecore_animator_add(_smart_momentum_animator, sd);
1767                                            ev->event_flags |= EVAS_EVENT_FLAG_ON_SCROLL;
1768                                            _smart_anim_start(sd->smart_obj);
1769                                         }
1770                                       sd->down.anim_start = ecore_loop_time_get();
1771                                       elm_smart_scroller_child_pos_get(sd->smart_obj, &x, &y);
1772                                       sd->down.sx = x;
1773                                       sd->down.sy = y;
1774                                       sd->down.b0x = 0;
1775                                       sd->down.b0y = 0;
1776                                    }
1777                               }
1778                          }
1779                        if (sd->down.hold_animator)
1780                          {
1781                             ecore_animator_del(sd->down.hold_animator);
1782                             sd->down.hold_animator = NULL;
1783                          }
1784                     }
1785                   else
1786                     {
1787                        sd->down.pdx = 0;
1788                        sd->down.pdy = 0;
1789                     }
1790                   evas_event_feed_hold(e, 0, ev->timestamp, ev->data);
1791                   if (_smart_do_page(sd))
1792                     {
1793                        Evas_Coord pgx, pgy;
1794
1795                        elm_smart_scroller_child_pos_get(sd->smart_obj, &x, &y);
1796                        if ((!sd->widget) || 
1797                            (!elm_widget_drag_child_locked_x_get(sd->widget)))
1798                          {
1799                             pgx = _smart_page_x_get(sd, ox);
1800                             if (pgx != x) 
1801                               {
1802                                  ev->event_flags |= EVAS_EVENT_FLAG_ON_SCROLL;
1803                                  _smart_scrollto_x(sd, _elm_config->page_scroll_friction, pgx);
1804                               }
1805                          }
1806                        if ((!sd->widget) || 
1807                            (!elm_widget_drag_child_locked_y_get(sd->widget)))
1808                          {
1809                             pgy = _smart_page_y_get(sd, oy);
1810                             if (pgy != y) 
1811                               {
1812                                  ev->event_flags |= EVAS_EVENT_FLAG_ON_SCROLL;
1813                                  _smart_scrollto_y(sd, _elm_config->page_scroll_friction, pgy);
1814                               }
1815                          }
1816                     }
1817                }
1818              else
1819                {
1820                   sd->down.pdx = 0;
1821                   sd->down.pdy = 0;
1822                   if (_smart_do_page(sd))
1823                     {
1824                        Evas_Coord pgx, pgy;
1825
1826                        elm_smart_scroller_child_pos_get(sd->smart_obj, &x, &y);
1827                        if ((!sd->widget) || 
1828                            (!elm_widget_drag_child_locked_x_get(sd->widget)))
1829                          {
1830                             pgx = _smart_page_x_get(sd, ox);
1831                             if (pgx != x) _smart_scrollto_x(sd, _elm_config->page_scroll_friction, pgx);
1832                          }
1833                        if ((!sd->widget) || 
1834                            (!elm_widget_drag_child_locked_y_get(sd->widget)))
1835                          {
1836                             pgy = _smart_page_y_get(sd, oy);
1837                             if (pgy != y) _smart_scrollto_y(sd, _elm_config->page_scroll_friction, pgy);
1838                          }
1839                     }
1840                   if (sd->down.hold_animator)
1841                     {
1842                        ecore_animator_del(sd->down.hold_animator);
1843                        sd->down.hold_animator = NULL;
1844                     }
1845                }
1846              if (sd->down.scroll)
1847                {
1848                   ev->event_flags |= EVAS_EVENT_FLAG_ON_SCROLL;
1849                   sd->down.scroll = 0;
1850                }
1851              if (sd->down.hold)
1852                {
1853                   ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
1854                   sd->down.hold = 0;
1855                }
1856              sd->down.dragged_began = 0;
1857              sd->down.dir_x = 0;
1858              sd->down.dir_y = 0;
1859              sd->down.want_dragged = 0;
1860              sd->down.dragged = 0;
1861              sd->down.now = 0;
1862              elm_smart_scroller_child_pos_get(sd->smart_obj, &x, &y);
1863              elm_smart_scroller_child_pos_set(sd->smart_obj, x, y);
1864              _update_wanted_coordinates(sd, x, y);
1865
1866              if (!_smart_do_page(sd))
1867                bounce_eval(sd);
1868           }
1869      }
1870 }
1871
1872 static Eina_Bool
1873 _smart_onhold_animator(void *data)
1874 {
1875    Smart_Data *sd;
1876    double t, td;
1877    double vx, vy;
1878    Evas_Coord x, y, ox, oy;
1879
1880    sd = data;
1881    t = ecore_loop_time_get();
1882    if (sd->down.onhold_tlast > 0.0)
1883      {
1884         td = t - sd->down.onhold_tlast;
1885         vx = sd->down.onhold_vx * td * (double)_elm_config->thumbscroll_threshold * 2.0;
1886         vy = sd->down.onhold_vy * td * (double)_elm_config->thumbscroll_threshold * 2.0;
1887         elm_smart_scroller_child_pos_get(sd->smart_obj, &ox, &oy);
1888         x = ox;
1889         y = oy;
1890         
1891         if (sd->down.dir_x)
1892           {
1893              if ((!sd->widget) || 
1894                  (!elm_widget_drag_child_locked_x_get(sd->widget)))
1895                {
1896                   sd->down.onhold_vxe += vx;
1897                   x = ox + (int)sd->down.onhold_vxe;
1898                   sd->down.onhold_vxe -= (int)sd->down.onhold_vxe;
1899                }
1900           }
1901         
1902         if (sd->down.dir_y)
1903           {
1904              if ((!sd->widget) || 
1905                  (!elm_widget_drag_child_locked_y_get(sd->widget)))
1906                {
1907                   sd->down.onhold_vye += vy;
1908                   y = oy + (int)sd->down.onhold_vye;
1909                   sd->down.onhold_vye -= (int)sd->down.onhold_vye;
1910                }
1911           }
1912         
1913         elm_smart_scroller_child_pos_set(sd->smart_obj, x, y);
1914      }
1915    sd->down.onhold_tlast = t;
1916    return ECORE_CALLBACK_RENEW;
1917 }
1918
1919 static Eina_Bool
1920 _smart_event_post_move(void *data, Evas *e __UNUSED__)
1921 {
1922    Smart_Data *sd = data;
1923
1924    if (sd->down.want_dragged)
1925      {
1926         int start = 0;
1927
1928         if (sd->down.hold_parent)
1929           {
1930              if ((sd->down.dir_x) && !can_scroll(sd, sd->down.hdir))
1931                {
1932                   sd->down.dir_x = 0;
1933                }
1934              if ((sd->down.dir_y) && !can_scroll(sd, sd->down.vdir))
1935                {
1936                   sd->down.dir_y = 0;
1937                }
1938           }
1939         if (sd->down.dir_x)
1940           {
1941              if ((!sd->widget) || 
1942                  (!elm_widget_drag_child_locked_x_get(sd->widget)))
1943                {
1944                   sd->down.want_dragged = 0;
1945                   sd->down.dragged = 1;
1946                   if (sd->widget)
1947                     {
1948                        elm_widget_drag_lock_x_set(sd->widget, 1);
1949                     }
1950                   start = 1;
1951                }
1952              else
1953                sd->down.dir_x = 0;
1954           }
1955         if (sd->down.dir_y)
1956           {
1957              if ((!sd->widget) || 
1958                  (!elm_widget_drag_child_locked_y_get(sd->widget)))
1959                {
1960                   sd->down.want_dragged = 0;
1961                   sd->down.dragged = 1;
1962                   if (sd->widget)
1963                     {
1964                        elm_widget_drag_lock_y_set(sd->widget, 1);
1965                     }
1966                   start = 1;
1967                }
1968              else
1969                sd->down.dir_y = 0;
1970           }
1971         if ((!sd->down.dir_x) && (!sd->down.dir_y))
1972           {
1973              sd->down.cancelled = 1;
1974           }
1975         if (start) _smart_drag_start(sd->smart_obj);
1976      }
1977    return EINA_TRUE;
1978 }
1979
1980 static void
1981 _smart_event_mouse_move(void *data, Evas *e, Evas_Object *obj __UNUSED__, void *event_info)
1982 {
1983    Evas_Event_Mouse_Move *ev;
1984    Smart_Data *sd;
1985    Evas_Coord x = 0, y = 0;
1986
1987    sd = data;
1988    ev = event_info;
1989 //   if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return ;
1990    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) sd->down.hold_parent = 1;
1991    evas_post_event_callback_push(e, _smart_event_post_move, sd);
1992    // FIXME: respect elm_widget_scroll_hold_get of parent container
1993    if (_elm_config->thumbscroll_enable)
1994      {
1995         if (sd->down.now)
1996           {
1997              int dodir = 0;
1998
1999 #ifdef SCROLLDBG
2000              printf("::: %i %i\n", ev->cur.canvas.x, ev->cur.canvas.y);
2001 #endif
2002              memmove(&(sd->down.history[1]), &(sd->down.history[0]),
2003                      sizeof(sd->down.history[0]) * 19);
2004 #ifdef EVTIME
2005              sd->down.history[0].timestamp = ev->timestamp / 1000.0;
2006 #else
2007              sd->down.history[0].timestamp = ecore_loop_time_get();
2008 #endif
2009              sd->down.history[0].x = ev->cur.canvas.x;
2010              sd->down.history[0].y = ev->cur.canvas.y;
2011
2012              if (!sd->down.dragged_began)
2013                {
2014                   x = ev->cur.canvas.x - sd->down.x;
2015                   y = ev->cur.canvas.y - sd->down.y;
2016                   
2017                   sd->down.hdir = -1;
2018                   sd->down.vdir = -1;
2019                     
2020                   if      (x > 0) sd->down.hdir = LEFT;
2021                   else if (x < 0) sd->down.hdir = RIGHT;
2022                   if      (y > 0) sd->down.vdir = UP;
2023                   else if (y < 0) sd->down.vdir = DOWN;
2024                   
2025                   if (x < 0) x = -x;
2026                   if (y < 0) y = -y;
2027              
2028                   if ((sd->one_dir_at_a_time) &&
2029                       (!((sd->down.dir_x) || (sd->down.dir_y))))
2030                     {
2031                        if (x > _elm_config->thumbscroll_threshold)
2032                          {
2033                             if (x > (y * 2))
2034                               {
2035                                  sd->down.dir_x = 1;
2036                                  sd->down.dir_y = 0;
2037                                  dodir++;
2038                               }
2039                          }
2040                        if (y > _elm_config->thumbscroll_threshold)
2041                          {
2042                             if (y > (x * 2))
2043                               {
2044                                  sd->down.dir_x = 0;
2045                                  sd->down.dir_y = 1;
2046                                  dodir++;
2047                               }
2048                          }
2049                        if (!dodir)
2050                          {
2051                             sd->down.dir_x = 1;
2052                             sd->down.dir_y = 1;
2053                          }
2054                     }
2055                   else
2056                     {
2057 //                       can_scroll(sd, LEFT);
2058 //                       can_scroll(sd, RIGHT);
2059 //                       can_scroll(sd, UP);
2060 //                       can_scroll(sd, DOWN);
2061                        sd->down.dir_x = 1;
2062                        sd->down.dir_y = 1;
2063                     }
2064                }
2065              if ((!sd->hold) && (!sd->freeze))
2066                {
2067                   if ((sd->down.dragged) ||
2068                       (((x * x) + (y * y)) >
2069                        (_elm_config->thumbscroll_threshold *
2070                         _elm_config->thumbscroll_threshold)))
2071                     {
2072                        sd->down.dragged_began = 1;
2073                        if (!sd->down.dragged)
2074                          {
2075                             sd->down.want_dragged = 1;
2076                             ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
2077 //                            evas_event_feed_hold(e, 1, ev->timestamp, ev->data);
2078 //                            _smart_drag_start(sd->smart_obj);
2079                          }
2080                        if (sd->down.dragged)
2081                          {
2082                             ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
2083                          }
2084 //                       ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
2085 //                       sd->down.dragged = 1;
2086                        if (sd->down.dir_x)
2087                          x = sd->down.sx - (ev->cur.canvas.x - sd->down.x);
2088                        else
2089                          x = sd->down.sx;
2090                        if (sd->down.dir_y)
2091                          y = sd->down.sy - (ev->cur.canvas.y - sd->down.y);
2092                        else
2093                          y = sd->down.sy;
2094                        if ((sd->down.dir_x) || (sd->down.dir_y))
2095                          {
2096                             if (!sd->down.locked)
2097                               {
2098                                  sd->down.locked_x = x;
2099                                  sd->down.locked_y = y;
2100                                  sd->down.locked = 1;
2101                               }
2102                             if (!((sd->down.dir_x) && (sd->down.dir_y)))
2103                               {
2104                                  if (sd->down.dir_x) y = sd->down.locked_y;
2105                                  else x = sd->down.locked_x;
2106                               }
2107                          }
2108                        if (_elm_config->thumbscroll_border_friction > 0.0)
2109                          {
2110                             Evas_Coord minx, miny;
2111                             sd->pan_func.min_get(sd->pan_obj, &minx, &miny);
2112                             if (y < miny)
2113                                y += (miny - y) *
2114                                   _elm_config->thumbscroll_border_friction;
2115                             else if (sd->child.h <= sd->h)
2116                                y += (sd->down.sy - y) *
2117                                   _elm_config->thumbscroll_border_friction;
2118                             else if ((sd->child.h - sd->h + miny) < y)
2119                                y += (sd->child.h - sd->h + miny - y) *
2120                                   _elm_config->thumbscroll_border_friction;
2121                             if (x < minx)
2122                                x += (minx - x) *
2123                                   _elm_config->thumbscroll_border_friction;
2124                             else if (sd->child.w <= sd->w)
2125                                x += (sd->down.sx - x) *
2126                                   _elm_config->thumbscroll_border_friction;
2127                             else if ((sd->child.w - sd->w + minx) < x)
2128                                x += (sd->child.w - sd->w + minx - x) *
2129                                   _elm_config->thumbscroll_border_friction;
2130                          }
2131
2132                        sd->down.hold_x = x;
2133                        sd->down.hold_y = y;
2134                        if (!sd->down.hold_animator)
2135                          sd->down.hold_animator = 
2136                             ecore_animator_add(_smart_hold_animator, sd);
2137 //                       printf("a %i %i\n", sd->down.hold_x, sd->down.hold_y);
2138 //                       _smart_onhold_animator(sd);
2139 //                       elm_smart_scroller_child_pos_set(sd->smart_obj, x, y);
2140                     }
2141                   else
2142                     {
2143                        if (sd->down.dragged_began)
2144                          {
2145                             ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
2146                             if (!sd->down.hold)
2147                               {
2148                                  sd->down.hold = 1;
2149                                  evas_event_feed_hold(e, 1, ev->timestamp, ev->data);
2150                               }
2151                          }
2152                     }
2153                }
2154              else if (!sd->freeze)
2155                {
2156                   Evas_Coord ex, ey, ew, eh;
2157                   double vx = 0.0, vy = 0.0;
2158
2159                   evas_object_geometry_get(sd->event_obj, &ex, &ey, &ew, &eh);
2160                   x = ev->cur.canvas.x - ex;
2161                   y = ev->cur.canvas.y - ey;
2162                   if (x < _elm_config->thumbscroll_threshold)
2163                     {
2164                        if (_elm_config->thumbscroll_threshold > 0.0)
2165                          vx = -(double)(_elm_config->thumbscroll_threshold - x) /
2166                          _elm_config->thumbscroll_threshold;
2167                        else
2168                          vx = -1.0;
2169                     }
2170                   else if (x > (ew - _elm_config->thumbscroll_threshold))
2171                     {
2172                        if (_elm_config->thumbscroll_threshold > 0.0)
2173                          vx = (double)(_elm_config->thumbscroll_threshold - (ew - x)) /
2174                          _elm_config->thumbscroll_threshold;
2175                        else
2176                          vx = 1.0;
2177                     }
2178                   if (y < _elm_config->thumbscroll_threshold)
2179                     {
2180                        if (_elm_config->thumbscroll_threshold > 0.0)
2181                          vy = -(double)(_elm_config->thumbscroll_threshold - y) /
2182                          _elm_config->thumbscroll_threshold;
2183                        else
2184                          vy = -1.0;
2185                     }
2186                   else if (y > (eh - _elm_config->thumbscroll_threshold))
2187                     {
2188                        if (_elm_config->thumbscroll_threshold > 0.0)
2189                          vy = (double)(_elm_config->thumbscroll_threshold - (eh - y)) /
2190                          _elm_config->thumbscroll_threshold;
2191                        else
2192                          vy = 1.0;
2193                     }
2194                   if ((vx != 0.0) || (vy != 0.0))
2195                     {
2196                        sd->down.onhold_vx = vx;
2197                        sd->down.onhold_vy = vy;
2198                        if (!sd->down.onhold_animator)
2199                          {
2200                             sd->down.onhold_vxe = 0.0;
2201                             sd->down.onhold_vye = 0.0;
2202                             sd->down.onhold_tlast = 0.0;
2203                             sd->down.onhold_animator = ecore_animator_add(_smart_onhold_animator, sd);
2204                          }
2205 //                       printf("b %i %i\n", sd->down.hold_x, sd->down.hold_y);
2206                     }
2207                   else
2208                     {
2209                        if (sd->down.onhold_animator)
2210                          {
2211                             ecore_animator_del(sd->down.onhold_animator);
2212                             sd->down.onhold_animator = NULL;
2213                          }
2214                     }
2215                }
2216           }
2217      }
2218 }
2219
2220 static void
2221 _smart_scrollbar_read(Smart_Data *sd)
2222 {
2223    Evas_Coord x, y, mx = 0, my = 0, px, py, minx = 0, miny = 0;
2224    double vx, vy;
2225
2226    edje_object_part_drag_value_get(sd->edje_obj, "elm.dragable.vbar", NULL, &vy);
2227    edje_object_part_drag_value_get(sd->edje_obj, "elm.dragable.hbar", &vx, NULL);
2228    sd->pan_func.max_get(sd->pan_obj, &mx, &my);
2229    sd->pan_func.min_get(sd->pan_obj, &minx, &miny);
2230    x = vx * (double)mx + minx;
2231    y = vy * (double)my + miny;
2232    sd->pan_func.get(sd->pan_obj, &px, &py);
2233    sd->pan_func.set(sd->pan_obj, x, y);
2234    if ((px != x) || (py != y))
2235      edje_object_signal_emit(sd->edje_obj, "elm,action,scroll", "elm");
2236 }
2237
2238 static void
2239 _smart_scrollbar_reset(Smart_Data *sd)
2240 {
2241    Evas_Coord px = 0, py = 0, minx = 0, miny = 0;
2242
2243    edje_object_part_drag_value_set(sd->edje_obj, "elm.dragable.vbar", 0.0, 0.0);
2244    edje_object_part_drag_value_set(sd->edje_obj, "elm.dragable.hbar", 0.0, 0.0);
2245    if ((!sd->child_obj) && (!sd->extern_pan))
2246      {
2247         edje_object_part_drag_size_set(sd->edje_obj, "elm.dragable.vbar", 1.0, 1.0);
2248         edje_object_part_drag_size_set(sd->edje_obj, "elm.dragable.hbar", 1.0, 1.0);
2249      }
2250    sd->pan_func.min_get(sd->pan_obj, &minx, &miny);
2251    sd->pan_func.get(sd->pan_obj, &px, &py);
2252    sd->pan_func.set(sd->pan_obj, minx, miny);
2253    if ((px != minx) || (py != miny))
2254      edje_object_signal_emit(sd->edje_obj, "elm,action,scroll", "elm");
2255 }
2256
2257 static int
2258 _smart_scrollbar_bar_v_visibility_adjust(Smart_Data *sd)
2259 {
2260    int scroll_v_vis_change = 0;
2261    Evas_Coord h, vw, vh;
2262
2263    h = sd->child.h;
2264    evas_object_geometry_get(sd->pan_obj, NULL, NULL, &vw, &vh);
2265    if (sd->vbar_visible)
2266      {
2267         if (sd->vbar_flags == ELM_SMART_SCROLLER_POLICY_AUTO)
2268           {
2269              if ((sd->child_obj) || (sd->extern_pan))
2270                {
2271                   if (h <= vh)
2272                     {
2273                        scroll_v_vis_change = 1;
2274                        sd->vbar_visible = 0;
2275                     }
2276                }
2277              else
2278                {
2279                   scroll_v_vis_change = 1;
2280                   sd->vbar_visible = 0;
2281                }
2282           }
2283         else if (sd->vbar_flags == ELM_SMART_SCROLLER_POLICY_OFF)
2284           {
2285              scroll_v_vis_change = 1;
2286              sd->vbar_visible = 0;
2287           }
2288      }
2289    else
2290      {
2291         if (sd->vbar_flags == ELM_SMART_SCROLLER_POLICY_AUTO)
2292           {
2293              if ((sd->child_obj) || (sd->extern_pan))
2294                {
2295                   if (h > vh)
2296                     {
2297                        scroll_v_vis_change = 1;
2298                        sd->vbar_visible = 1;
2299                     }
2300                }
2301           }
2302         else if (sd->vbar_flags == ELM_SMART_SCROLLER_POLICY_ON)
2303           {
2304              scroll_v_vis_change = 1;
2305              sd->vbar_visible = 1;
2306           }
2307      }
2308    if (scroll_v_vis_change)
2309      {
2310         if (sd->vbar_flags != ELM_SMART_SCROLLER_POLICY_OFF)
2311           {
2312              if (sd->vbar_visible)
2313                edje_object_signal_emit(sd->edje_obj, "elm,action,show,vbar", "elm");
2314              else
2315                edje_object_signal_emit(sd->edje_obj, "elm,action,hide,vbar", "elm");
2316              edje_object_message_signal_process(sd->edje_obj);
2317              _smart_scrollbar_size_adjust(sd);
2318           }
2319         else
2320           edje_object_signal_emit(sd->edje_obj, "elm,action,hide,vbar", "elm");
2321      }
2322    return scroll_v_vis_change;
2323 }
2324
2325 static int
2326 _smart_scrollbar_bar_h_visibility_adjust(Smart_Data *sd)
2327 {
2328    int scroll_h_vis_change = 0;
2329    Evas_Coord w, vw, vh;
2330
2331    w = sd->child.w;
2332    evas_object_geometry_get(sd->pan_obj, NULL, NULL, &vw, &vh);
2333    if (sd->hbar_visible)
2334      {
2335         if (sd->hbar_flags == ELM_SMART_SCROLLER_POLICY_AUTO)
2336           {
2337              if ((sd->child_obj) || (sd->extern_pan))
2338                {
2339                   if (w <= vw)
2340                     {
2341                        scroll_h_vis_change = 1;
2342                        sd->hbar_visible = 0;
2343                     }
2344                }
2345              else
2346                {
2347                   scroll_h_vis_change = 1;
2348                   sd->hbar_visible = 0;
2349                }
2350           }
2351         else if (sd->hbar_flags == ELM_SMART_SCROLLER_POLICY_OFF)
2352           {
2353              scroll_h_vis_change = 1;
2354              sd->hbar_visible = 0;
2355           }
2356      }
2357    else
2358      {
2359         if (sd->hbar_flags == ELM_SMART_SCROLLER_POLICY_AUTO)
2360           {
2361              if ((sd->child_obj) || (sd->extern_pan))
2362                {
2363                   if (w > vw)
2364                     {
2365                        scroll_h_vis_change = 1;
2366                        sd->hbar_visible = 1;
2367                     }
2368                }
2369           }
2370         else if (sd->hbar_flags == ELM_SMART_SCROLLER_POLICY_ON)
2371           {
2372              scroll_h_vis_change = 1;
2373              sd->hbar_visible = 1;
2374           }
2375      }
2376    if (scroll_h_vis_change)
2377      {
2378         if (sd->hbar_flags != ELM_SMART_SCROLLER_POLICY_OFF)
2379           {
2380              if (sd->hbar_visible)
2381                edje_object_signal_emit(sd->edje_obj, "elm,action,show,hbar", "elm");
2382              else
2383                edje_object_signal_emit(sd->edje_obj, "elm,action,hide,hbar", "elm");
2384              edje_object_message_signal_process(sd->edje_obj);
2385              _smart_scrollbar_size_adjust(sd);
2386           }
2387         else
2388           edje_object_signal_emit(sd->edje_obj, "elm,action,hide,hbar", "elm");
2389         _smart_scrollbar_size_adjust(sd);
2390      }
2391    return scroll_h_vis_change;
2392 }
2393
2394 static void
2395 _smart_scrollbar_bar_visibility_adjust(Smart_Data *sd)
2396 {
2397    int changed = 0;
2398
2399    changed |= _smart_scrollbar_bar_h_visibility_adjust(sd);
2400    changed |= _smart_scrollbar_bar_v_visibility_adjust(sd);
2401    if (changed)
2402      {
2403         _smart_scrollbar_bar_h_visibility_adjust(sd);
2404         _smart_scrollbar_bar_v_visibility_adjust(sd);
2405      }
2406 }
2407
2408 static void
2409 _smart_scrollbar_size_adjust(Smart_Data *sd)
2410 {
2411    if ((sd->child_obj) || (sd->extern_pan))
2412      {
2413         Evas_Coord x, y, w, h, mx = 0, my = 0, vw = 0, vh = 0, px, py, minx = 0, miny = 0;
2414         double vx, vy, size;
2415
2416         edje_object_part_geometry_get(sd->edje_obj, "elm.swallow.content",
2417                                       NULL, NULL, &vw, &vh);
2418         w = sd->child.w;
2419         if (w < 1) w = 1;
2420         size = (double)vw / (double)w;
2421         if (size > 1.0)
2422           {
2423              size = 1.0;
2424              edje_object_part_drag_value_set(sd->edje_obj, "elm.dragable.hbar", 0.0, 0.0);
2425           }
2426         edje_object_part_drag_size_set(sd->edje_obj, "elm.dragable.hbar", size, 1.0);
2427
2428         h = sd->child.h;
2429         if (h < 1) h = 1;
2430         size = (double)vh / (double)h;
2431         if (size > 1.0)
2432           {
2433              size = 1.0;
2434              edje_object_part_drag_value_set(sd->edje_obj, "elm.dragable.vbar", 0.0, 0.0);
2435           }
2436         edje_object_part_drag_size_set(sd->edje_obj, "elm.dragable.vbar", 1.0, size);
2437
2438         edje_object_part_drag_value_get(sd->edje_obj, "elm.dragable.hbar", &vx, NULL);
2439         edje_object_part_drag_value_get(sd->edje_obj, "elm.dragable.vbar", NULL, &vy);
2440         sd->pan_func.max_get(sd->pan_obj, &mx, &my);
2441         sd->pan_func.min_get(sd->pan_obj, &minx, &miny);
2442         x = vx * mx + minx;
2443         y = vy * my + miny;
2444
2445         edje_object_part_drag_step_set(sd->edje_obj, "elm.dragable.hbar", (double)sd->step.x / (double)w, 0.0);
2446         edje_object_part_drag_step_set(sd->edje_obj, "elm.dragable.vbar", 0.0, (double)sd->step.y / (double)h);
2447         if (sd->page.x > 0)
2448           edje_object_part_drag_page_set(sd->edje_obj, "elm.dragable.hbar", (double)sd->page.x / (double)w, 0.0);
2449         else
2450           edje_object_part_drag_page_set(sd->edje_obj, "elm.dragable.hbar", -((double)sd->page.x * ((double)vw / (double)w)) / 100.0, 0.0);
2451         if (sd->page.y > 0)
2452           edje_object_part_drag_page_set(sd->edje_obj, "elm.dragable.vbar", 0.0, (double)sd->page.y / (double)h);
2453         else
2454           edje_object_part_drag_page_set(sd->edje_obj, "elm.dragable.vbar", 0.0, -((double)sd->page.y * ((double)vh / (double)h)) / 100.0);
2455
2456         sd->pan_func.get(sd->pan_obj, &px, &py);
2457         if (vx != mx) x = px;
2458         if (vy != my) y = py;
2459         sd->pan_func.set(sd->pan_obj, x, y);
2460 //      if ((px != 0) || (py != 0))
2461 //        edje_object_signal_emit(sd->edje_obj, "elm,action,scroll", "elm");
2462      }
2463    else
2464      {
2465         Evas_Coord px = 0, py = 0, minx = 0, miny = 0;
2466
2467         edje_object_part_drag_size_set(sd->edje_obj, "elm.dragable.vbar", 1.0, 1.0);
2468         edje_object_part_drag_size_set(sd->edje_obj, "elm.dragable.hbar", 1.0, 1.0);
2469         sd->pan_func.min_get(sd->pan_obj, &minx, &miny);
2470         sd->pan_func.get(sd->pan_obj, &px, &py);
2471         sd->pan_func.set(sd->pan_obj, minx, miny);
2472         if ((px != minx) || (py != miny))
2473           edje_object_signal_emit(sd->edje_obj, "elm,action,scroll", "elm");
2474      }
2475    _smart_scrollbar_bar_visibility_adjust(sd);
2476 }
2477
2478 static void
2479 _smart_reconfigure(Smart_Data *sd)
2480 {
2481    evas_object_move(sd->edje_obj, sd->x, sd->y);
2482    evas_object_resize(sd->edje_obj, sd->w, sd->h);
2483    evas_object_move(sd->event_obj, sd->x, sd->y);
2484    evas_object_resize(sd->event_obj, sd->w, sd->h);
2485    _smart_scrollbar_size_adjust(sd);
2486 }
2487
2488 static void
2489 _smart_add(Evas_Object *obj)
2490 {
2491    Smart_Data *sd;
2492    Evas_Object *o;
2493
2494    sd = calloc(1, sizeof(Smart_Data));
2495    if (!sd) return;
2496    evas_object_smart_data_set(obj, sd);
2497
2498    sd->smart_obj = obj;
2499    sd->x = 0;
2500    sd->y = 0;
2501    sd->w = 0;
2502    sd->h = 0;
2503    sd->step.x = 32;
2504    sd->step.y = 32;
2505    sd->page.x = -50;
2506    sd->page.y = -50;
2507    sd->hbar_flags = ELM_SMART_SCROLLER_POLICY_AUTO;
2508    sd->vbar_flags = ELM_SMART_SCROLLER_POLICY_AUTO;
2509    sd->hbar_visible = 1;
2510    sd->vbar_visible = 1;
2511
2512    sd->bounce_horiz = 1;
2513    sd->bounce_vert = 1;
2514
2515    sd->one_dir_at_a_time = 1;
2516    sd->momentum_animator_disabled = 0;
2517    sd->bounce_animator_disabled = 0;
2518    
2519    o = edje_object_add(evas_object_evas_get(obj));
2520    evas_object_propagate_events_set(o, 0);
2521    sd->edje_obj = o;
2522    // FIXME: null parent obj ... :(
2523    elm_smart_scroller_object_theme_set(NULL, obj, "scroller", "base", "default");
2524    edje_object_signal_callback_add(o, "drag", "elm.dragable.vbar", _smart_edje_drag_v, sd);
2525    edje_object_signal_callback_add(o, "drag,start", "elm.dragable.vbar", _smart_edje_drag_v_start, sd);
2526    edje_object_signal_callback_add(o, "drag,stop", "elm.dragable.vbar", _smart_edje_drag_v_stop, sd);
2527    edje_object_signal_callback_add(o, "drag,step", "elm.dragable.vbar", _smart_edje_drag_v, sd);
2528    edje_object_signal_callback_add(o, "drag,page", "elm.dragable.vbar", _smart_edje_drag_v, sd);
2529    edje_object_signal_callback_add(o, "drag", "elm.dragable.hbar", _smart_edje_drag_h, sd);
2530    edje_object_signal_callback_add(o, "drag,start", "elm.dragable.hbar", _smart_edje_drag_h_start, sd);
2531    edje_object_signal_callback_add(o, "drag,stop", "elm.dragable.hbar", _smart_edje_drag_h_stop, sd);
2532    edje_object_signal_callback_add(o, "drag,step", "elm.dragable.hbar", _smart_edje_drag_h, sd);
2533    edje_object_signal_callback_add(o, "drag,page", "elm.dragable.hbar", _smart_edje_drag_h, sd);
2534    evas_object_smart_member_add(o, obj);
2535
2536    o = evas_object_rectangle_add(evas_object_evas_get(obj));
2537    sd->event_obj = o;
2538    evas_object_color_set(o, 0, 0, 0, 0);
2539    evas_object_event_callback_add(o, EVAS_CALLBACK_MOUSE_WHEEL, _smart_event_wheel, sd);
2540    evas_object_event_callback_add(o, EVAS_CALLBACK_MOUSE_DOWN, _smart_event_mouse_down, sd);
2541    evas_object_event_callback_add(o, EVAS_CALLBACK_MOUSE_UP, _smart_event_mouse_up, sd);
2542    evas_object_event_callback_add(o, EVAS_CALLBACK_MOUSE_MOVE, _smart_event_mouse_move, sd);
2543    evas_object_smart_member_add(o, obj);
2544    evas_object_repeat_events_set(o, 1);
2545
2546    sd->pan_func.set = _elm_smart_pan_set;
2547    sd->pan_func.get = _elm_smart_pan_get;
2548    sd->pan_func.max_get = _elm_smart_pan_max_get;
2549    sd->pan_func.min_get = _elm_smart_pan_min_get;
2550    sd->pan_func.child_size_get = _elm_smart_pan_child_size_get;
2551
2552    _smart_scrollbar_reset(sd);
2553 }
2554
2555 static void
2556 _smart_del(Evas_Object *obj)
2557 {
2558    INTERNAL_ENTRY;
2559    elm_smart_scroller_child_set(obj, NULL);
2560    if (!sd->extern_pan) evas_object_del(sd->pan_obj);
2561    evas_object_del(sd->edje_obj);
2562    evas_object_del(sd->event_obj);
2563    if (sd->down.hold_animator) ecore_animator_del(sd->down.hold_animator);
2564    if (sd->down.onhold_animator) ecore_animator_del(sd->down.onhold_animator);
2565    if (sd->down.momentum_animator) ecore_animator_del(sd->down.momentum_animator);
2566    if (sd->down.bounce_x_animator) ecore_animator_del(sd->down.bounce_x_animator);
2567    if (sd->down.bounce_y_animator) ecore_animator_del(sd->down.bounce_y_animator);
2568    if (sd->scrollto.x.animator) ecore_animator_del(sd->scrollto.x.animator);
2569    if (sd->scrollto.y.animator) ecore_animator_del(sd->scrollto.y.animator);
2570    free(sd);
2571    evas_object_smart_data_set(obj, NULL);
2572 }
2573
2574 static void
2575 _smart_move(Evas_Object *obj, Evas_Coord x, Evas_Coord y)
2576 {
2577    INTERNAL_ENTRY;
2578    sd->x = x;
2579    sd->y = y;
2580    _smart_reconfigure(sd);
2581 }
2582
2583 static void
2584 _smart_resize(Evas_Object *obj, Evas_Coord w, Evas_Coord h)
2585 {
2586    INTERNAL_ENTRY;
2587    Evas_Coord ww, wh, wx = sd->wx;
2588    sd->w = w;
2589    sd->h = h;
2590    _smart_reconfigure(sd);
2591
2592    /* Flip to RTL cords only if init in RTL mode */
2593    if(sd->is_mirrored)
2594      wx = _elm_smart_scroller_x_mirrored_get(obj, sd->wx);
2595
2596    if (sd->ww == -1)
2597      {
2598         elm_smart_scroller_child_viewport_size_get(obj, &ww, &wh);
2599      }
2600    else
2601      {
2602         ww = sd->ww;
2603         wh = sd->wh;
2604      }
2605
2606    elm_smart_scroller_child_region_set(obj, wx, sd->wy, ww, wh);
2607 }
2608
2609 static void
2610 _smart_show(Evas_Object *obj)
2611 {
2612    INTERNAL_ENTRY;
2613    evas_object_show(sd->edje_obj);
2614    evas_object_show(sd->event_obj);
2615 }
2616
2617 static void
2618 _smart_hide(Evas_Object *obj)
2619 {
2620    INTERNAL_ENTRY;
2621    evas_object_hide(sd->edje_obj);
2622    evas_object_hide(sd->event_obj);
2623 }
2624
2625 static void
2626 _smart_color_set(Evas_Object *obj, int r, int g, int b, int a)
2627 {
2628    INTERNAL_ENTRY;
2629    evas_object_color_set(sd->edje_obj, r, g, b, a);
2630 }
2631
2632 static void
2633 _smart_clip_set(Evas_Object *obj, Evas_Object *clip)
2634 {
2635    INTERNAL_ENTRY;
2636    evas_object_clip_set(sd->edje_obj, clip);
2637    evas_object_clip_set(sd->event_obj, clip);
2638 }
2639
2640 static void
2641 _smart_clip_unset(Evas_Object *obj)
2642 {
2643    INTERNAL_ENTRY;
2644    evas_object_clip_unset(sd->edje_obj);
2645    evas_object_clip_unset(sd->event_obj);
2646 }
2647
2648 /* never need to touch this */
2649
2650 static void
2651 _smart_init(void)
2652 {
2653    if (_smart) return;
2654      {
2655         static const Evas_Smart_Class sc =
2656           {
2657              SMART_NAME,
2658                EVAS_SMART_CLASS_VERSION,
2659                _smart_add,
2660                _smart_del,
2661                _smart_move,
2662                _smart_resize,
2663                _smart_show,
2664                _smart_hide,
2665                _smart_color_set,
2666                _smart_clip_set,
2667                _smart_clip_unset,
2668                NULL,
2669                NULL,
2670                NULL,
2671                NULL,
2672                NULL,
2673                NULL,
2674                NULL
2675           };
2676         _smart = evas_smart_class_new(&sc);
2677      }
2678 }
2679