]> gitweb.factorcode.org Git - factor.git/blob - extra/ui/gadgets/lists/lists.factor
Delete empty unit tests files, remove 1- and 1+, reorder IN: lines in a lot of places...
[factor.git] / extra / ui / gadgets / lists / lists.factor
1 ! Copyright (C) 2006, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors math.vectors classes.tuple math.rectangles colors
4 kernel sequences models opengl math math.order namespaces
5 ui.commands ui.gestures ui.render ui.gadgets ui.gadgets.labels
6 ui.gadgets.scrollers ui.gadgets.presentations ui.gadgets.viewports
7 ui.gadgets.packs ;
8 IN: ui.gadgets.lists
9
10 TUPLE: list < pack index presenter color hook ;
11
12 : list-theme ( list -- list )
13     selection-color >>color ; inline
14
15 : <list> ( hook presenter model -- gadget )
16     list new
17         { 0 1 } >>orientation
18         1 >>fill
19         0 >>index
20         swap >>model
21         swap >>presenter
22         swap >>hook
23         list-theme ;
24
25 : calc-bounded-index ( n list -- m )
26     control-value length 1 - min 0 max ;
27
28 : bound-index ( list -- )
29     dup index>> over calc-bounded-index >>index drop ;
30
31 : list-presentation-hook ( list -- quot )
32     hook>> [ [ list? ] find-parent ] prepend ;
33
34 : <list-presentation> ( hook elt presenter -- gadget )
35     [ call( elt -- obj ) ] [ drop ] 2bi [ >label text-theme ] dip
36     <presentation>
37     swap >>hook ; inline
38
39 : <list-items> ( list -- seq )
40     [ list-presentation-hook ]
41     [ presenter>> ]
42     [ control-value ]
43     tri [
44         [ 2dup ] dip swap <list-presentation>
45     ] map 2nip ;
46
47 M: list model-changed
48     nip
49     dup clear-gadget
50     dup <list-items> add-gadgets
51     bound-index ;
52
53 : selected-rect ( list -- rect )
54     dup index>> swap children>> ?nth ;
55
56 M: list draw-gadget*
57     origin get [
58         dup color>> gl-color
59         selected-rect [
60             rect-bounds gl-fill-rect
61         ] when*
62     ] with-translation ;
63
64 M: list focusable-child* drop t ;
65
66 : list-value ( list -- object )
67     dup index>> swap control-value ?nth ;
68
69 : scroll>selected ( list -- )
70     #! We change the rectangle's width to zero to avoid
71     #! scrolling right.
72     [ selected-rect rect-bounds { 0 1 } v* <rect> ] keep
73     scroll>rect ;
74
75 : list-empty? ( list -- ? ) control-value empty? ;
76
77 : select-index ( n list -- )
78     dup list-empty? [
79         2drop
80     ] [
81         tuck control-value length rem >>index
82         [ relayout-1 ] [ scroll>selected ] bi
83     ] if ;
84
85 : select-previous ( list -- )
86     [ index>> 1 - ] keep select-index ;
87
88 : select-next ( list -- )
89     [ index>> 1 + ] keep select-index ;
90
91 : invoke-value-action ( list -- )
92     dup list-empty? [
93         dup hook>> call( list -- )
94     ] [
95         [ index>> ] keep nth-gadget invoke-secondary
96     ] if ;
97
98 : select-gadget ( gadget list -- )
99     tuck children>> index
100     [ swap select-index ] [ drop ] if* ;
101
102 : clamp-loc ( point max -- point )
103     vmin { 0 0 } vmax ;
104
105 : select-at ( point list -- )
106     [ dim>> clamp-loc ] keep
107     [ pick-up ] keep
108     select-gadget ;
109
110 : list-page ( list vec -- )
111     [ dup selected-rect rect-bounds 2 v/n v+ over visible-dim ] dip
112     v* v+ swap select-at ;
113
114 : list-page-up ( list -- ) { 0 -1 } list-page ;
115
116 : list-page-down ( list -- ) { 0 1 } list-page ;
117
118 list "keyboard-navigation" "Lists can be navigated from the keyboard." {
119     { T{ button-down } request-focus }
120     { T{ key-down f f "UP" } select-previous }
121     { T{ key-down f f "DOWN" } select-next }
122     { T{ key-down f f "PAGE_UP" } list-page-up }
123     { T{ key-down f f "PAGE_DOWN" } list-page-down }
124     { T{ key-down f f "RET" } invoke-value-action }
125 } define-command-map