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