2009-01-29 17:44:18 -05:00
|
|
|
! Copyright (C) 2005, 2009 Slava Pestov.
|
2007-09-20 18:09:08 -04:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2008-04-21 00:09:00 -04:00
|
|
|
USING: accessors arrays ui.gadgets ui.gadgets.viewports
|
|
|
|
ui.gadgets.frames ui.gadgets.grids ui.gadgets.theme
|
|
|
|
ui.gadgets.sliders ui.gestures kernel math namespaces sequences
|
2008-11-19 22:58:45 -05:00
|
|
|
models models.range models.compose combinators math.vectors
|
|
|
|
classes.tuple math.geometry.rect combinators.short-circuit ;
|
2007-09-20 18:09:08 -04:00
|
|
|
IN: ui.gadgets.scrollers
|
|
|
|
|
2008-07-10 21:32:17 -04:00
|
|
|
TUPLE: scroller < frame viewport x y follows ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: find-scroller ( gadget -- scroller/f )
|
2008-08-23 00:20:49 -04:00
|
|
|
[ scroller? ] find-parent ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-06-08 16:32:55 -04:00
|
|
|
: scroll-up-page ( scroller -- ) y>> -1 swap slide-by-page ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-06-08 16:32:55 -04:00
|
|
|
: scroll-down-page ( scroller -- ) y>> 1 swap slide-by-page ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-06-08 16:32:55 -04:00
|
|
|
: scroll-up-line ( scroller -- ) y>> -1 swap slide-by-line ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-06-08 16:32:55 -04:00
|
|
|
: scroll-down-line ( scroller -- ) y>> 1 swap slide-by-line ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: do-mouse-scroll ( scroller -- )
|
2008-11-19 22:58:45 -05:00
|
|
|
scroll-direction get-global
|
|
|
|
[ first swap x>> slide-by-line ]
|
|
|
|
[ second swap y>> slide-by-line ]
|
|
|
|
2bi ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
scroller H{
|
2009-01-28 01:30:57 -05:00
|
|
|
{ mouse-scroll [ do-mouse-scroll ] }
|
2007-09-20 18:09:08 -04:00
|
|
|
} set-gestures
|
|
|
|
|
|
|
|
: <scroller-model> ( -- model )
|
|
|
|
0 0 0 0 <range> 0 0 0 0 <range> 2array <compose> ;
|
|
|
|
|
2008-07-10 21:32:17 -04:00
|
|
|
: new-scroller ( gadget class -- scroller )
|
2008-09-27 15:36:04 -04:00
|
|
|
new-frame
|
|
|
|
t >>root?
|
|
|
|
<scroller-model> >>model
|
2008-07-14 18:48:59 -04:00
|
|
|
|
2009-01-13 20:09:47 -05:00
|
|
|
dup model>> dependencies>>
|
|
|
|
[ first <x-slider> [ >>x ] [ @bottom grid-add ] bi ]
|
|
|
|
[ second <y-slider> [ >>y ] [ @right grid-add ] bi ] bi
|
2008-09-27 15:36:04 -04:00
|
|
|
|
2009-01-13 20:09:47 -05:00
|
|
|
tuck model>> <viewport> [ >>viewport ] [ @center grid-add ] bi
|
|
|
|
|
|
|
|
faint-boundary ; inline
|
2008-07-14 18:48:59 -04:00
|
|
|
|
|
|
|
: <scroller> ( gadget -- scroller ) scroller new-scroller ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: scroll ( value scroller -- )
|
|
|
|
[
|
2008-11-19 22:58:45 -05:00
|
|
|
viewport>> [ rect-dim { 0 0 } ] [ viewport-dim ] bi
|
|
|
|
4array flip
|
2007-09-20 18:09:08 -04:00
|
|
|
] keep
|
|
|
|
2dup control-value = [ 2drop ] [ set-control-value ] if ;
|
|
|
|
|
2008-11-19 18:13:39 -05:00
|
|
|
: rect-min ( rect dim -- rect' )
|
|
|
|
[ [ loc>> ] [ dim>> ] bi ] dip vmin <rect> ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: (scroll>rect) ( rect scroller -- )
|
2009-01-29 17:44:18 -05:00
|
|
|
[ [ loc>> ] [ dim>> { 1 1 } v+ ] bi <rect> ] dip
|
2008-11-19 22:58:45 -05:00
|
|
|
{
|
|
|
|
[ scroller-value vneg offset-rect viewport-gap offset-rect ]
|
|
|
|
[ viewport>> dim>> rect-min ]
|
|
|
|
[ viewport>> 2rect-extent [ v- { 0 0 } vmin ] [ v- { 0 0 } vmax ] 2bi* v+ ]
|
|
|
|
[ scroller-value v+ ]
|
|
|
|
[ scroll ]
|
|
|
|
} cleave ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: relative-scroll-rect ( rect gadget scroller -- newrect )
|
2008-07-11 16:19:54 -04:00
|
|
|
viewport>> gadget-child relative-loc offset-rect ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-11-10 03:10:18 -05:00
|
|
|
: find-scroller* ( gadget -- scroller/f )
|
|
|
|
dup find-scroller
|
2008-11-10 06:08:30 -05:00
|
|
|
{ [ nip ] [ viewport>> gadget-child swap child? ] [ nip ] }
|
2008-11-10 03:10:18 -05:00
|
|
|
2&& ;
|
2007-11-14 16:35:17 -05:00
|
|
|
|
|
|
|
: scroll>rect ( rect gadget -- )
|
|
|
|
dup find-scroller* dup [
|
2007-09-20 18:09:08 -04:00
|
|
|
[ relative-scroll-rect ] keep
|
2008-09-27 15:36:04 -04:00
|
|
|
swap >>follows
|
2007-09-20 18:09:08 -04:00
|
|
|
relayout
|
2008-11-19 22:58:45 -05:00
|
|
|
] [ 3drop ] if ;
|
|
|
|
|
|
|
|
: (update-scroller) ( scroller -- )
|
|
|
|
[ scroller-value ] keep scroll ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2007-11-22 01:40:17 -05:00
|
|
|
: (scroll>gadget) ( gadget scroller -- )
|
2008-11-19 22:58:45 -05:00
|
|
|
2dup swap child? [
|
2009-01-29 17:44:18 -05:00
|
|
|
[ [ [ { 0 0 } ] dip pref-dim <rect> ] keep ] dip
|
2008-11-19 22:58:45 -05:00
|
|
|
[ relative-scroll-rect ] keep
|
|
|
|
(scroll>rect)
|
|
|
|
] [ f >>follows (update-scroller) drop ] if ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: scroll>gadget ( gadget -- )
|
2007-11-14 16:35:17 -05:00
|
|
|
dup find-scroller* dup [
|
2008-09-27 15:36:04 -04:00
|
|
|
swap >>follows
|
2007-09-20 18:09:08 -04:00
|
|
|
relayout
|
|
|
|
] [
|
|
|
|
2drop
|
|
|
|
] if ;
|
|
|
|
|
|
|
|
: (scroll>bottom) ( scroller -- )
|
2008-11-19 22:58:45 -05:00
|
|
|
[ viewport>> viewport-dim { 0 1 } v* ] keep scroll ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: scroll>bottom ( gadget -- )
|
2008-09-27 15:36:04 -04:00
|
|
|
find-scroller [ t >>follows relayout-1 ] when* ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: scroll>top ( gadget -- )
|
|
|
|
<zero-rect> swap scroll>rect ;
|
|
|
|
|
2008-07-11 16:19:54 -04:00
|
|
|
GENERIC: update-scroller ( scroller follows -- )
|
|
|
|
|
|
|
|
M: t update-scroller drop (scroll>bottom) ;
|
|
|
|
|
|
|
|
M: gadget update-scroller swap (scroll>gadget) ;
|
|
|
|
|
|
|
|
M: rect update-scroller swap (scroll>rect) ;
|
|
|
|
|
2008-11-19 22:58:45 -05:00
|
|
|
M: f update-scroller drop (update-scroller) ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
M: scroller layout*
|
2008-11-19 22:58:45 -05:00
|
|
|
[ call-next-method ] [
|
|
|
|
dup follows>>
|
|
|
|
[ update-scroller ] [ >>follows drop ] 2bi
|
|
|
|
] bi ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
M: scroller focusable-child*
|
2008-08-31 02:42:30 -04:00
|
|
|
viewport>> ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
M: scroller model-changed
|
2008-11-19 22:58:45 -05:00
|
|
|
f >>follows 2drop ;
|
2008-04-21 00:09:00 -04:00
|
|
|
|
2008-11-19 18:47:12 -05:00
|
|
|
TUPLE: limited-scroller < scroller
|
|
|
|
{ min-dim initial: { 0 0 } }
|
|
|
|
{ max-dim initial: { 1/0. 1/0. } } ;
|
2008-04-21 00:09:00 -04:00
|
|
|
|
2008-11-19 18:47:12 -05:00
|
|
|
: <limited-scroller> ( gadget -- scroller )
|
|
|
|
limited-scroller new-scroller ;
|
2008-04-21 00:09:00 -04:00
|
|
|
|
|
|
|
M: limited-scroller pref-dim*
|
2008-11-19 18:47:12 -05:00
|
|
|
[ call-next-method ] [ min-dim>> vmax ] [ max-dim>> vmin ] tri ;
|