factor/library/ui/hierarchy.factor

105 lines
2.7 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
2006-06-02 15:12:57 -04:00
USING: generic hashtables kernel math namespaces sequences
vectors words ;
2005-03-07 22:11:36 -05:00
GENERIC: add-notify* ( gadget -- )
M: gadget add-notify* drop ;
: add-notify ( gadget -- )
dup [ add-notify ] each-child add-notify* ;
GENERIC: remove-notify* ( gadget -- )
M: gadget remove-notify* drop ;
: remove-notify ( gadget -- )
dup [ remove-notify* ] each-child remove-notify* ;
: (unparent) ( gadget -- )
dup remove-notify
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
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
[ gadget-children ?push ] 2keep swapd set-gadget-children
add-notify ;
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 ;
: add-spec ( { quot setter post loc } quot -- )
2006-06-29 03:54:30 -04:00
[
over first %
over second [ [ dup gadget get ] % , ] when*
over third %
[ gadget get ] %
swap fourth ,
%
] [ ] make call ;
2005-10-09 21:27:14 -04:00
: (parents) ( gadget vector -- )
over
[ 2dup push >r gadget-parent r> (parents) ] [ 2drop ] if ;
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.
V{ } clone [ (parents) ] keep ;
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 ;
2005-09-01 01:20:43 -04:00
: gadget-point ( gadget vector -- point )
2006-06-23 00:06:53 -04:00
#! { 0 0 } - top left corner
#! { 1/2 1/2 } - middle
#! { 1 1 } - bottom right corner
2005-09-01 01:20:43 -04:00
>r dup screen-loc swap rect-dim r> v* v+ ;
: 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 ;