factor/library/ui/gadgets/lists.factor

97 lines
2.5 KiB
Factor
Raw Normal View History

2006-10-03 18:17:21 -04:00
! Copyright (C) 2006 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
IN: gadgets-lists
USING: gadgets gadgets-labels gadgets-scrolling kernel sequences
2006-11-19 21:13:37 -05:00
generic models opengl math namespaces gadgets-theme
2006-11-18 03:51:34 -05:00
gadgets-presentations ;
2006-10-03 18:17:21 -04:00
2006-11-19 21:13:37 -05:00
TUPLE: list index presenter color hook ;
2006-10-03 18:17:21 -04:00
2006-10-04 17:21:37 -04:00
: list-theme ( list -- )
{ 0.8 0.8 1.0 1.0 } swap set-list-color ;
2006-11-17 04:34:22 -05:00
C: list ( hook presenter model -- gadget )
[ swap <pile> delegate>control ] keep
2006-10-04 17:21:37 -04:00
[ set-list-presenter ] keep
2006-11-17 04:34:22 -05:00
[ set-list-hook ] keep
2006-10-03 18:17:21 -04:00
0 over set-list-index
2006-11-19 21:13:37 -05:00
1 over set-pack-fill
2006-10-04 17:21:37 -04:00
dup list-theme ;
2006-10-03 18:17:21 -04:00
2006-10-05 02:10:49 -04:00
: bound-index ( list -- )
dup list-index over control-value length 1- max 0 min
swap set-list-index ;
2006-11-19 21:13:37 -05:00
: list-presentation-hook ( list -- quot )
list-hook [ [ [ list? ] is? ] find-parent ] swap append ;
: <list-presentation> ( hook presenter elt -- gadget )
[ swap call ] keep <presentation>
[ set-presentation-hook ] keep
[ text-theme ] keep ;
: <list-items> ( list -- seq )
dup list-presentation-hook
over list-presenter
rot control-value [
>r 2dup r> <list-presentation>
] map 2nip ;
2006-10-03 18:17:21 -04:00
M: list model-changed
dup clear-gadget
2006-11-19 21:13:37 -05:00
dup <list-items> over add-gadgets
2006-10-05 02:10:49 -04:00
bound-index ;
2006-10-03 18:17:21 -04:00
2006-10-04 17:21:37 -04:00
: selected-rect ( list -- rect )
dup list-index swap gadget-children 2dup bounds-check?
[ nth ] [ 2drop f ] if ;
2006-10-03 18:17:21 -04:00
M: list draw-gadget*
2006-10-13 21:45:24 -04:00
origin get [
dup list-color gl-color
selected-rect [ rect-extent gl-fill-rect ] when*
] with-translation ;
2006-10-04 17:21:37 -04:00
M: list focusable-child* drop t ;
: list-value ( list -- object )
2006-10-06 20:27:40 -04:00
dup list-index swap control-value ?nth ;
2006-10-03 18:17:21 -04:00
2006-10-04 17:21:37 -04:00
: scroll>selected ( list -- )
2006-10-07 02:17:32 -04:00
#! We change the rectangle's width to zero to avoid
#! scrolling right.
[ selected-rect rect-bounds { 0 1 } v* <rect> ] keep
scroll>rect ;
2006-10-03 18:17:21 -04:00
2006-10-06 20:27:40 -04:00
: list-empty? ( list -- ? ) control-value empty? ;
2006-10-03 18:17:21 -04:00
: select-index ( n list -- )
2006-10-06 20:27:40 -04:00
dup list-empty? [
2006-10-03 18:17:21 -04:00
2drop
] [
[ control-value length rem ] keep
[ set-list-index ] keep
2006-10-04 17:21:37 -04:00
[ relayout-1 ] keep
scroll>selected
2006-10-03 18:17:21 -04:00
] if ;
: select-prev ( list -- )
dup list-index 1- swap select-index ;
: select-next ( list -- )
dup list-index 1+ swap select-index ;
2006-11-17 04:34:22 -05:00
: list-action ( list -- )
dup list-empty? [
drop
] [
2006-11-19 21:13:37 -05:00
dup list-index swap nth-gadget invoke-secondary
2006-11-17 04:34:22 -05:00
] if ; inline
2006-10-05 02:10:49 -04:00
list H{
2006-11-19 21:13:37 -05:00
{ T{ button-down } [ request-focus ] }
2006-10-03 18:17:21 -04:00
{ T{ key-down f f "UP" } [ select-prev ] }
{ T{ key-down f f "DOWN" } [ select-next ] }
2006-11-17 04:34:22 -05:00
{ T{ key-down f f "RETURN" } [ list-action ] }
2006-10-03 18:17:21 -04:00
} set-gestures