1 ! Copyright (C) 2006 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
4 USING: gadgets gadgets-labels gadgets-scrolling kernel sequences
5 generic models opengl math namespaces gadgets-theme
6 gadgets-presentations ;
8 TUPLE: list index presenter color hook ;
10 : list-theme ( list -- )
11 { 0.8 0.8 1.0 1.0 } swap set-list-color ;
13 C: list ( hook presenter model -- gadget )
14 [ swap <pile> delegate>control ] keep
15 [ set-list-presenter ] keep
16 [ set-list-hook ] keep
21 : bound-index ( list -- )
22 dup list-index over control-value length 1- max 0 min
25 : list-presentation-hook ( list -- quot )
26 list-hook [ [ [ list? ] is? ] find-parent ] swap append ;
28 : <list-presentation> ( hook presenter elt -- gadget )
29 [ swap call ] keep <presentation>
30 [ set-presentation-hook ] keep
33 : <list-items> ( list -- seq )
34 dup list-presentation-hook
37 >r 2dup r> <list-presentation>
42 dup <list-items> over add-gadgets
45 : selected-rect ( list -- rect )
46 dup list-index swap gadget-children 2dup bounds-check?
47 [ nth ] [ 2drop f ] if ;
51 dup list-color gl-color
52 selected-rect [ rect-extent gl-fill-rect ] when*
55 M: list focusable-child* drop t ;
57 : list-value ( list -- object )
58 dup list-index swap control-value ?nth ;
60 : scroll>selected ( list -- )
61 #! We change the rectangle's width to zero to avoid
63 [ selected-rect rect-bounds { 0 1 } v* <rect> ] keep
66 : list-empty? ( list -- ? ) control-value empty? ;
68 : select-index ( n list -- )
72 [ control-value length rem ] keep
73 [ set-list-index ] keep
78 : select-prev ( list -- )
79 dup list-index 1- swap select-index ;
81 : select-next ( list -- )
82 dup list-index 1+ swap select-index ;
84 : list-action ( list -- )
88 dup list-index swap nth-gadget invoke-secondary
92 { "Request focus" T{ button-down } [ request-focus ] }
93 { "Select previous value" T{ key-down f f "UP" } [ select-prev ] }
94 { "Select next value" T{ key-down f f "DOWN" } [ select-next ] }
95 { "Invoke value action" T{ key-down f f "RETURN" } [ list-action ] }