factor/library/ui/gadgets/lists.factor

95 lines
2.4 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-18 03:51:34 -05:00
models opengl math namespaces gadgets-theme
gadgets-presentations ;
2006-10-03 18:17:21 -04:00
2006-11-17 04:34:22 -05:00
TUPLE: list index hook presenter 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 ;
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-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-11-18 03:51:34 -05:00
dup list-presenter over control-value
[ [ swap call ] keep <presentation> ] map-with
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 ;
: click-list ( list -- )
hand-gadget get [ gadget-parent list? ] find-parent
dup [
over gadget-children index dup -1 =
[ 2drop ] [ swap select-index ] if
] [
2drop
] if ;
2006-11-17 04:34:22 -05:00
: list-action ( list -- )
dup list-empty? [
drop
] [
[
list-value dup secondary-operation invoke-command
2006-11-17 18:11:35 -05:00
] keep dup list-hook call
2006-11-17 04:34:22 -05:00
] if ; inline
2006-10-05 02:10:49 -04:00
list H{
{ T{ button-down } [ dup request-focus click-list ] }
{ T{ drag } [ click-list ] }
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