factor/library/ui/gadgets/grids.factor

75 lines
2.1 KiB
Factor
Raw Normal View History

2006-06-07 21:59:59 -04:00
! Copyright (C) 2006 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
2006-06-07 23:51:28 -04:00
IN: gadgets-grids
USING: arrays gadgets kernel math namespaces sequences words ;
2006-06-07 21:59:59 -04:00
2006-06-07 23:51:28 -04:00
TUPLE: grid children gap ;
2006-06-07 21:59:59 -04:00
: collapse-grid concat [ ] subset ;
: set-grid-children* ( children grid -- )
[ set-grid-children ] 2keep
>r collapse-grid r> add-gadgets ;
C: grid ( children -- grid )
2006-06-07 23:51:28 -04:00
dup delegate>gadget
[ set-grid-children* ] keep
0 over set-grid-gap ;
2006-06-07 21:59:59 -04:00
: grid-child ( grid i j -- gadget ) rot grid-children nth nth ;
: grid-add ( gadget grid i j -- )
>r >r over [ over add-gadget ] when* r> r>
3dup grid-child unparent rot grid-children nth set-nth ;
: grid-remove ( grid i j -- )
>r >r >r f r> r> r> grid-add ;
2006-06-07 23:51:28 -04:00
: pref-dim-grid ( -- dims )
grid get grid-children
[ [ [ pref-dim ] [ { 0 0 0 } ] if* ] map ] map ;
2006-06-07 21:59:59 -04:00
2006-06-07 23:51:28 -04:00
: compute-grid ( -- horiz vert )
pref-dim-grid
dup flip [ max-dim first ] map swap [ max-dim second ] map ;
2006-06-07 21:59:59 -04:00
2006-06-07 23:51:28 -04:00
: with-grid ( grid quot -- | quot: horiz vert -- )
[ >r grid set compute-grid r> call ] with-scope ; inline
2006-06-07 21:59:59 -04:00
2006-06-07 23:51:28 -04:00
: +gap+ + grid get grid-gap + ;
M: grid pref-dim* ( grid -- dim )
[ [ 0 [ +gap+ ] reduce ] 2apply 0 3array ] with-grid ;
2006-06-07 21:59:59 -04:00
: pair-up ( horiz vert -- dims )
[ swap [ swap 0 3array ] map-with ] map-with ;
2006-06-07 23:51:28 -04:00
: do-grid ( dims quot -- )
swap grid get grid-children [
2006-06-07 21:59:59 -04:00
[ dup [ pick call ] [ 2drop ] if ] 2each
] 2each drop ; inline
2006-06-07 23:51:28 -04:00
: position-grid ( horiz vert -- )
[ 0 [ +gap+ ] accumulate ] 2apply
2006-06-07 21:59:59 -04:00
pair-up [ set-rect-loc ] do-grid ;
2006-06-07 23:51:28 -04:00
: resize-grid ( horiz vert -- )
2006-06-07 21:59:59 -04:00
pair-up [ set-gadget-dim ] do-grid ;
2006-06-07 23:51:28 -04:00
: grid-layout ( horiz vert -- )
2dup position-grid resize-grid ;
2006-06-07 21:59:59 -04:00
M: grid layout* ( frame -- dim )
2006-06-07 23:51:28 -04:00
[ grid-layout ] with-grid ;
2006-06-07 23:04:37 -04:00
: grid-add-spec ( { quot setter loc } -- )
first3 >r >r call
grid get 2dup r> dup [ execute ] [ 3drop ] if
r> execute grid-add ;
: build-grid ( grid specs -- )
#! Specs is an array of triples { quot setter loc }.
#! The setter has stack effect ( new gadget -- ),
#! the loc is @center, @top, etc.
[ swap grid set [ grid-add-spec ] each ] with-scope ;