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
|
|
|
|
|
|
|
M: list model-changed
|
|
|
|
dup clear-gadget
|
2006-10-04 17:21:37 -04:00
|
|
|
dup control-value over list-presenter map
|
2006-10-03 18:17:21 -04:00
|
|
|
swap add-gadgets ;
|
|
|
|
|
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 )
|
|
|
|
dup control-value empty? [
|
|
|
|
drop f
|
2006-10-03 18:17:21 -04:00
|
|
|
] [
|
2006-10-04 17:21:37 -04:00
|
|
|
dup list-index swap control-value nth
|
2006-10-03 18:17:21 -04:00
|
|
|
] if ;
|
|
|
|
|
2006-10-04 17:21:37 -04:00
|
|
|
: scroll>selected ( list -- )
|
|
|
|
dup selected-rect swap scroll>rect ;
|
2006-10-03 18:17:21 -04:00
|
|
|
|
|
|
|
: select-index ( n list -- )
|
|
|
|
dup control-value empty? [
|
|
|
|
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 -- )
|
|
|
|
dup list-value swap list-action call ;
|
|
|
|
|
2006-10-03 18:17:21 -04:00
|
|
|
\ list H{
|
|
|
|
{ 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
|