factor/library/ui/gadgets/lists.factor

78 lines
2.0 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
2006-10-04 17:21:37 -04:00
USING: gadgets gadgets-scrolling kernel sequences models opengl
math ;
2006-10-03 18:17:21 -04:00
2006-10-04 17:21:37 -04:00
TUPLE: list index presenter action color ;
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 ;
C: list ( model presenter action -- gadget )
[ set-list-action ] keep
[ set-list-presenter ] keep
dup rot <pile> 1 over set-pack-fill delegate>control
2006-10-03 18:17:21 -04:00
0 over set-list-index
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-10-03 18:17:21 -04:00
M: list model-changed
dup clear-gadget
2006-10-05 02:10:49 -04:00
dup control-value over list-presenter map over add-gadgets
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*
dup list-color gl-color
2006-10-04 17:21:37 -04:00
selected-rect [
rect-bounds swap [ gl-fill-rect ] with-translation
] when* ;
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-10-04 17:21:37 -04:00
: call-action ( list -- )
2006-10-06 20:27:40 -04:00
dup list-empty? [
dup list-value over list-action call
] unless drop ;
2006-10-04 17:21:37 -04:00
2006-10-05 02:10:49 -04:00
list H{
2006-10-03 18:17:21 -04:00
{ T{ button-down } [ request-focus ] }
{ T{ key-down f f "UP" } [ select-prev ] }
{ T{ key-down f f "DOWN" } [ select-next ] }
2006-10-04 17:21:37 -04:00
{ T{ key-down f f "RETURN" } [ call-action ] }
2006-10-03 18:17:21 -04:00
} set-gestures