factor/library/ui/hierarchy.factor

117 lines
2.9 KiB
Factor
Raw Normal View History

! Copyright (C) 2005, 2006 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
2005-03-07 22:11:36 -05:00
IN: gadgets
USING: generic hashtables inference kernel math namespaces
2006-08-15 04:57:12 -04:00
sequences vectors words parser ;
2005-03-07 22:11:36 -05:00
GENERIC: graft* ( gadget -- )
M: gadget graft* drop ;
: graft ( gadget -- )
t over set-gadget-grafted?
dup graft*
2006-07-01 16:07:10 -04:00
[ graft ] each-child ;
GENERIC: ungraft* ( gadget -- )
M: gadget ungraft* drop ;
: ungraft ( gadget -- )
dup gadget-grafted? [
2006-07-05 18:47:42 -04:00
dup [ ungraft ] each-child
dup ungraft*
f over set-gadget-grafted?
] when drop ;
: (unparent) ( gadget -- )
dup ungraft
dup forget-pref-dim f swap set-gadget-parent ;
2005-03-07 22:11:36 -05:00
: unparent ( gadget -- )
[
dup gadget-parent dup [
over (unparent)
[ gadget-children delete ] keep relayout
] [
2drop
] if
] when* ;
2005-03-07 22:11:36 -05:00
2005-07-17 00:21:10 -04:00
: (clear-gadget) ( gadget -- )
2006-05-24 03:23:45 -04:00
dup [ (unparent) ] each-child f swap set-gadget-children ;
2005-07-17 00:21:10 -04:00
: clear-gadget ( gadget -- )
2005-07-17 00:21:10 -04:00
dup (clear-gadget) relayout ;
2005-07-16 22:16:18 -04:00
: ((add-gadget)) ( gadget box -- )
[ gadget-children ?push ] keep set-gadget-children ;
2005-07-09 16:08:50 -04:00
: (add-gadget) ( gadget box -- )
2005-03-07 22:11:36 -05:00
over unparent
dup pick set-gadget-parent
[ ((add-gadget)) ] 2keep
gadget-grafted? [ graft ] [ drop ] if ;
2005-07-09 16:08:50 -04:00
: add-gadget ( gadget parent -- )
#! Add a gadget to a parent gadget.
[ (add-gadget) ] keep relayout ;
2005-03-07 22:11:36 -05:00
2005-08-26 21:42:43 -04:00
: add-gadgets ( seq parent -- )
#! Add all gadgets in a sequence to a parent gadget.
swap [ over (add-gadget) ] each relayout ;
2006-08-15 04:57:12 -04:00
: add-spec ( quot spec -- )
dup first %
dup second [ [ dup gadget get ] % , ] when*
dup third %
[ gadget get ] %
fourth ,
% ;
: (build-spec) ( quot spec -- quot )
[ [ add-spec ] each-with ] [ ] make ;
: build-spec ( spec quot -- )
swap (build-spec) call ;
2006-08-15 04:57:12 -04:00
\ build-spec 2 0 <effect> "infer-effect" set-word-prop
\ build-spec [
pop-literal pop-literal nip (build-spec) infer-quot-value
] "infer" set-word-prop
: (parents) ( gadget -- )
[ dup , gadget-parent (parents) ] when* ;
2005-10-09 21:27:14 -04:00
: parents ( gadget -- vector )
2005-07-13 21:03:34 -04:00
#! A list of all parents of the gadget, the first element
#! is the gadget itself.
[ (parents) ] { } make ;
2005-03-07 22:11:36 -05:00
: each-parent ( gadget quot -- ? )
2005-10-09 21:27:14 -04:00
>r parents r> all? ; inline
2005-03-07 22:11:36 -05:00
: find-parent ( gadget quot -- gadget )
2005-10-09 21:27:14 -04:00
>r parents r> find nip ; inline
2005-07-20 16:03:03 -04:00
: screen-loc ( gadget -- point )
#! The position of the gadget on the screen.
2006-06-23 00:06:53 -04:00
parents { 0 0 } [ rect-loc v+ ] reduce ;
: relative-loc ( g1 point -- point-g1 ) swap screen-loc v- ;
2005-03-07 22:11:36 -05:00
2005-10-09 21:27:14 -04:00
: child? ( parent child -- ? ) parents memq? ;
GENERIC: focusable-child* ( gadget -- gadget/t )
M: gadget focusable-child* drop t ;
: focusable-child ( gadget -- gadget )
dup focusable-child*
2006-03-24 22:58:03 -05:00
dup t eq? [ drop ] [ nip focusable-child ] if ;
2005-09-27 00:44:38 -04:00
: make-pile ( children -- pack ) <pile> [ add-gadgets ] keep ;
: make-shelf ( children -- pack ) <shelf> [ add-gadgets ] keep ;