]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/gadgets/grids/grids.factor
basis: removing unnecessary method stack effects.
[factor.git] / basis / ui / gadgets / grids / grids.factor
1 ! Copyright (C) 2006, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays fry kernel make math math.order
4 math.rectangles math.vectors sequences strings.tables
5 ui.baseline-alignment ui.gadgets ;
6 IN: ui.gadgets.grids
7
8 TUPLE: grid < gadget
9 grid
10 { gap initial: { 0 0 } }
11 { fill? initial: t } ;
12
13 : new-grid ( children class -- grid )
14     new
15         swap [ >>grid ] [ concat add-gadgets ] bi ; inline
16
17 : <grid> ( children -- grid )
18     grid new-grid ;
19
20 <PRIVATE
21
22 : grid@ ( grid pair -- col# row )
23     swap [ first2 ] [ grid>> ] bi* nth ;
24
25 PRIVATE>
26
27 : grid-child ( grid pair -- gadget ) grid@ nth ;
28
29 : grid-add ( grid child pair -- grid )
30     [ nip grid-child unparent ]
31     [ drop add-gadget ]
32     [ swapd grid@ set-nth ] 3tri ;
33
34 : grid-remove ( grid pair -- grid ) [ <gadget> ] dip grid-add ;
35
36 <PRIVATE
37
38 TUPLE: grid-cell pref-dim baseline cap-height ;
39
40 : <grid-cell> ( gadget -- cell )
41     [ pref-dim ] [ baseline ] [ cap-height ] tri grid-cell boa ;
42
43 M: grid-cell baseline baseline>> ;
44
45 M: grid-cell cap-height cap-height>> ;
46
47 TUPLE: grid-layout grid gap fill? row-heights column-widths ;
48
49 : iterate-cell-dims ( cells quot -- seq )
50     '[ [ pref-dim>> @ ] [ max ] map-reduce ] map ; inline
51
52 : row-heights ( grid-layout -- heights )
53     [ grid>> ] [ fill?>> ] bi
54     [ [ second ] iterate-cell-dims ]
55     [ [ dup [ pref-dim>> ] map measure-height ] map ]
56     if ;
57
58 : column-widths ( grid-layout -- widths )
59     grid>> flip [ first ] iterate-cell-dims ;
60
61 : <grid-layout> ( grid -- grid-layout )
62     grid-layout new
63         swap
64         [ grid>> [ [ <grid-cell> ] map ] map >>grid ]
65         [ fill?>> >>fill? ]
66         [ gap>> >>gap ]
67         tri
68         dup row-heights >>row-heights
69         dup column-widths >>column-widths ;
70
71 : accumulate-cell-dims ( seq gap -- n ns )
72     dup '[ + _ + ] accumulate ;
73
74 : accumulate-cell-xs ( grid-layout -- x xs )
75     [ column-widths>> ] [ gap>> first ] bi
76     accumulate-cell-dims ;
77
78 : accumulate-cell-ys ( grid-layout -- y ys )
79     [ row-heights>> ] [ gap>> second ] bi
80     accumulate-cell-dims ;
81
82 : grid-pref-dim ( grid-layout -- dim )
83     [ accumulate-cell-xs drop ]
84     [ accumulate-cell-ys drop ]
85     bi 2array ;
86
87 M: grid pref-dim* <grid-layout> grid-pref-dim ;
88
89 : (compute-cell-locs) ( grid-layout -- locs )
90     [ accumulate-cell-xs nip ]
91     [ accumulate-cell-ys nip ]
92     bi cartesian-product flip ;
93
94 : adjust-for-baseline ( row-locs row-cells -- row-locs' )
95     align-baselines [ 0 swap 2array v+ ] 2map ;
96
97 : cell-locs ( grid-layout -- locs )
98     dup fill?>>
99     [ (compute-cell-locs) ] [
100         [ (compute-cell-locs) ] [ grid>> ] bi
101         [ adjust-for-baseline ] 2map
102     ] if ;
103
104 : cell-dims ( grid-layout -- dims )
105     dup fill?>>
106     [ [ column-widths>> ] [ row-heights>> ] bi cartesian-product flip ]
107     [ grid>> [ [ pref-dim>> ] map ] map ]
108     if ;
109
110 : layout-grid ( children grid-layout -- )
111     [ cell-locs ] [ cell-dims ] bi
112     [ [ <rect> swap set-rect-bounds ] 3each ] 3each ;
113
114 M: grid layout* [ grid>> ] [ <grid-layout> ] bi layout-grid ;
115
116 M: grid children-on
117     dup children>> empty? [ 2drop f ] [
118         [ { 0 1 } ] dip
119         [ grid>> ] [ dim>> ] bi
120         '[ _ [ loc>> vmin ] reduce ] fast-children-on
121         concat
122     ] if ;
123
124 M: grid gadget-text*
125     grid>>
126     [ [ gadget-text ] map ] map format-table
127     [ CHAR: \n , ] [ % ] interleave ;
128
129 PRIVATE>