]> gitweb.factorcode.org Git - factor.git/blob - library/ui/gadgets/lists.factor
Menus
[factor.git] / library / ui / gadgets / lists.factor
1 ! Copyright (C) 2006 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 IN: gadgets-lists
4 USING: gadgets gadgets-scrolling kernel sequences models opengl
5 math ;
6
7 TUPLE: list index presenter action color ;
8
9 : list-theme ( list -- )
10     { 0.8 0.8 1.0 1.0 } swap set-list-color ;
11
12 C: list ( model presenter action -- gadget )
13     [ set-list-action ] keep
14     [ set-list-presenter ] keep
15     dup rot <pile> 1 over set-pack-fill delegate>control
16     0 over set-list-index
17     dup list-theme ;
18
19 : bound-index ( list -- )
20     dup list-index over control-value length 1- max 0 min
21     swap set-list-index ;
22
23 M: list model-changed
24     dup clear-gadget
25     dup control-value over list-presenter map over add-gadgets
26     bound-index ;
27
28 : selected-rect ( list -- rect )
29     dup list-index swap gadget-children 2dup bounds-check?
30     [ nth ] [ 2drop f ] if ;
31
32 M: list draw-gadget*
33     dup list-color gl-color
34     selected-rect [
35         rect-bounds swap [ gl-fill-rect ] with-translation
36     ] when* ;
37
38 M: list focusable-child* drop t ;
39
40 : list-value ( list -- object )
41     dup list-index swap control-value ?nth ;
42
43 : scroll>selected ( list -- )
44     #! We change the rectangle's width to zero to avoid
45     #! scrolling right.
46     [ selected-rect rect-bounds { 0 1 } v* <rect> ] keep
47     scroll>rect ;
48
49 : list-empty? ( list -- ? ) control-value empty? ;
50
51 : select-index ( n list -- )
52     dup list-empty? [
53         2drop
54     ] [
55         [ control-value length rem ] keep
56         [ set-list-index ] keep
57         [ relayout-1 ] keep
58         scroll>selected
59     ] if ;
60
61 : select-prev ( list -- )
62     dup list-index 1- swap select-index ;
63
64 : select-next ( list -- )
65     dup list-index 1+ swap select-index ;
66
67 : call-action ( list -- )
68     dup list-empty? [
69         dup list-value over list-action call
70     ] unless drop ;
71
72 list H{
73     { T{ button-down } [ request-focus ] }
74     { T{ key-down f f "UP" } [ select-prev ] }
75     { T{ key-down f f "DOWN" } [ select-next ] }
76     { T{ key-down f f "RETURN" } [ call-action ] }
77 } set-gestures