1 ! Copyright (C) 2006, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors ui.commands ui.gestures ui.render ui.gadgets
4 ui.gadgets.labels ui.gadgets.scrollers
5 kernel sequences models opengl math math.order namespaces
6 ui.gadgets.presentations ui.gadgets.viewports ui.gadgets.packs
7 math.vectors classes.tuple math.geometry.rect colors ;
11 TUPLE: list < pack index presenter color hook ;
13 : list-theme ( list -- list )
14 T{ rgba f 0.8 0.8 1.0 1.0 } >>color ; inline
16 : <list> ( hook presenter model -- gadget )
26 : calc-bounded-index ( n list -- m )
27 control-value length 1- min 0 max ;
29 : bound-index ( list -- )
30 dup index>> over calc-bounded-index >>index drop ;
32 : list-presentation-hook ( list -- quot )
33 hook>> [ [ list? ] find-parent ] prepend ;
35 : <list-presentation> ( hook elt presenter -- gadget )
36 keep [ >label text-theme ] dip
40 : <list-items> ( list -- seq )
41 [ list-presentation-hook ]
45 [ 2dup ] dip swap <list-presentation>
51 dup <list-items> add-gadgets
54 : selected-rect ( list -- rect )
55 dup index>> swap children>> ?nth ;
67 M: list focusable-child* drop t ;
69 : list-value ( list -- object )
70 dup index>> swap control-value ?nth ;
72 : scroll>selected ( list -- )
73 #! We change the rectangle's width to zero to avoid
75 [ selected-rect rect-bounds { 0 1 } v* <rect> ] keep
78 : list-empty? ( list -- ? ) control-value empty? ;
80 : select-index ( n list -- )
84 [ control-value length rem ] keep
90 : select-previous ( list -- )
91 dup index>> 1- swap select-index ;
93 : select-next ( list -- )
94 dup index>> 1+ swap select-index ;
96 : invoke-value-action ( list -- )
100 dup index>> swap nth-gadget invoke-secondary
103 : select-gadget ( gadget list -- )
104 tuck children>> index
105 [ swap select-index ] [ drop ] if* ;
107 : clamp-loc ( point max -- point )
110 : select-at ( point list -- )
111 [ rect-dim clamp-loc ] keep
115 : list-page ( list vec -- )
116 [ dup selected-rect rect-bounds 2 v/n v+ over visible-dim ] dip
117 v* v+ swap select-at ;
119 : list-page-up ( list -- ) { 0 -1 } list-page ;
121 : list-page-down ( list -- ) { 0 1 } list-page ;
123 list "keyboard-navigation" "Lists can be navigated from the keyboard." {
124 { T{ button-down } request-focus }
125 { T{ key-down f f "UP" } select-previous }
126 { T{ key-down f f "DOWN" } select-next }
127 { T{ key-down f f "PAGE_UP" } list-page-up }
128 { T{ key-down f f "PAGE_DOWN" } list-page-down }
129 { T{ key-down f f "RET" } invoke-value-action }