]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/gadgets/lists/lists.factor
Fix permission bits
[factor.git] / basis / ui / gadgets / lists / lists.factor
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 ;
8
9 IN: ui.gadgets.lists
10
11 TUPLE: list < pack index presenter color hook ;
12
13 : list-theme ( list -- list )
14     T{ rgba f 0.8 0.8 1.0 1.0 } >>color ; inline
15
16 : <list> ( hook presenter model -- gadget )
17     list new-gadget
18         { 0 1 } >>orientation
19         1 >>fill
20         0 >>index
21         swap >>model
22         swap >>presenter
23         swap >>hook
24         list-theme ;
25
26 : calc-bounded-index ( n list -- m )
27     control-value length 1- min 0 max ;
28
29 : bound-index ( list -- )
30     dup index>> over calc-bounded-index >>index drop ;
31
32 : list-presentation-hook ( list -- quot )
33     hook>> [ [ list? ] find-parent ] prepend ;
34
35 : <list-presentation> ( hook elt presenter -- gadget )
36     keep >r >label text-theme r>
37     <presentation>
38     swap >>hook ; inline
39
40 : <list-items> ( list -- seq )
41     [ list-presentation-hook ]
42     [ presenter>> ]
43     [ control-value ]
44     tri [
45         >r 2dup r> swap <list-presentation>
46     ] map 2nip ;
47
48 M: list model-changed
49     nip
50     dup clear-gadget
51     dup <list-items> add-gadgets
52     bound-index ;
53
54 : selected-rect ( list -- rect )
55     dup index>> swap children>> ?nth ;
56
57 M: list draw-gadget*
58     origin get [
59         dup color>> set-color
60         selected-rect [ rect-extent gl-fill-rect ] when*
61     ] with-translation ;
62
63 M: list focusable-child* drop t ;
64
65 : list-value ( list -- object )
66     dup index>> swap control-value ?nth ;
67
68 : scroll>selected ( list -- )
69     #! We change the rectangle's width to zero to avoid
70     #! scrolling right.
71     [ selected-rect rect-bounds { 0 1 } v* <rect> ] keep
72     scroll>rect ;
73
74 : list-empty? ( list -- ? ) control-value empty? ;
75
76 : select-index ( n list -- )
77     dup list-empty? [
78         2drop
79     ] [
80         [ control-value length rem ] keep
81         swap >>index
82         dup relayout-1
83         scroll>selected
84     ] if ;
85
86 : select-previous ( list -- )
87     dup index>> 1- swap select-index ;
88
89 : select-next ( list -- )
90     dup index>> 1+ swap select-index ;
91
92 : invoke-value-action ( list -- )
93     dup list-empty? [
94         dup hook>> call
95     ] [
96         dup index>> swap nth-gadget invoke-secondary
97     ] if ;
98
99 : select-gadget ( gadget list -- )
100     swap over children>> index
101     [ swap select-index ] [ drop ] if* ;
102
103 : clamp-loc ( point max -- point )
104     vmin { 0 0 } vmax ;
105
106 : select-at ( point list -- )
107     [ rect-dim clamp-loc ] keep
108     [ pick-up ] keep
109     select-gadget ;
110
111 : list-page ( list vec -- )
112     >r dup selected-rect rect-bounds 2 v/n v+
113     over visible-dim r> v* v+ swap select-at ;
114
115 : list-page-up ( list -- ) { 0 -1 } list-page ;
116
117 : list-page-down ( list -- ) { 0 1 } list-page ;
118
119 list "keyboard-navigation" "Lists can be navigated from the keyboard." {
120     { T{ button-down } request-focus }
121     { T{ key-down f f "UP" } select-previous }
122     { T{ key-down f f "DOWN" } select-next }
123     { T{ key-down f f "PAGE_UP" } list-page-up }
124     { T{ key-down f f "PAGE_DOWN" } list-page-down }
125     { T{ key-down f f "RET" } invoke-value-action }
126 } define-command-map