factor/library/ui/hierarchy.factor

78 lines
2.2 KiB
Factor
Raw Normal View History

2005-03-07 22:11:36 -05:00
! Copyright (C) 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
IN: gadgets
USING: gadgets-layouts generic hashtables kernel lists math
namespaces sequences vectors ;
2005-03-07 22:11:36 -05:00
2005-07-09 16:08:50 -04:00
: remove-gadget ( gadget parent -- )
2005-09-14 00:37:50 -04:00
f pick set-gadget-parent
[ gadget-children delete ] keep
relayout ;
2005-03-07 22:11:36 -05:00
: unparent ( gadget -- )
[
dup gadget-parent dup
2005-09-14 00:37:50 -04:00
[ 2dup remove-gadget ] when 2drop
] when* ;
2005-03-07 22:11:36 -05:00
2005-07-17 00:21:10 -04:00
: (clear-gadget) ( gadget -- )
dup gadget-children [ f swap set-gadget-parent ] each
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
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
2005-07-16 22:16:18 -04:00
[ gadget-children ?push ] keep set-gadget-children ;
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 ;
: (parents-down) ( list gadget -- list )
[ [ swons ] keep gadget-parent (parents-down) ] when* ;
: parents-down ( gadget -- list )
#! A list of all parents of the gadget, the last element
#! is the gadget itself.
f swap (parents-down) ;
: parents-up ( gadget -- list )
2005-07-13 21:03:34 -04:00
#! A list of all parents of the gadget, the first element
#! is the gadget itself.
dup [ dup gadget-parent parents-up cons ] when ;
2005-03-07 22:11:36 -05:00
: each-parent ( gadget quot -- ? )
>r parents-up r> all? ; inline
2005-03-07 22:11:36 -05:00
2005-07-20 16:03:03 -04:00
: find-parent ( gadget quot -- ? )
>r parents-up r> find nip ; inline
2005-07-20 16:03:03 -04:00
: screen-loc ( gadget -- point )
#! The position of the gadget on the screen.
parents-up @{ 0 0 0 }@ [ rect-loc v+ ] reduce ;
2005-09-01 01:20:43 -04:00
: gadget-point ( gadget vector -- point )
#! @{ 0 0 0 }@ - top left corner
#! @{ 1/2 1/2 0 }@ - middle
#! @{ 1 1 0 }@ - bottom right corner
2005-09-01 01:20:43 -04:00
>r dup screen-loc swap rect-dim r> v* v+ ;
: relative ( g1 g2 -- g2-g1 ) screen-loc swap screen-loc v- ;
2005-03-07 22:11:36 -05:00
: child? ( parent child -- ? ) parents-down memq? ;
GENERIC: focusable-child* ( gadget -- gadget/t )
M: gadget focusable-child* drop t ;
: focusable-child ( gadget -- gadget )
dup focusable-child*
dup t = [ drop ] [ nip focusable-child ] ifte ;