factor/basis/ui/gadgets/paragraphs/paragraphs.factor

81 lines
1.8 KiB
Factor
Raw Normal View History

! Copyright (C) 2005, 2008 Slava Pestov
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
USING: accessors arrays ui.gadgets ui.gadgets.labels ui.render
kernel math namespaces sequences math.order math.geometry.rect
locals ;
2007-09-20 18:09:08 -04:00
IN: ui.gadgets.paragraphs
! A word break gadget
TUPLE: word-break-gadget < label ;
2007-09-20 18:09:08 -04:00
: <word-break-gadget> ( text -- gadget )
word-break-gadget new-label ;
2007-09-20 18:09:08 -04:00
M: word-break-gadget draw-gadget* drop ;
! A gadget that arranges its children in a word-wrap style.
TUPLE: paragraph < gadget margin ;
2007-09-20 18:09:08 -04:00
: <paragraph> ( margin -- gadget )
paragraph new-gadget
{ 1 0 } >>orientation
swap >>margin ;
2007-09-20 18:09:08 -04:00
SYMBOL: x SYMBOL: max-x
SYMBOL: y SYMBOL: max-y
SYMBOL: line-height
SYMBOL: margin
: overrun? ( width -- ? ) x get + margin get > ;
2008-06-08 16:32:55 -04:00
: zero-vars ( seq -- ) [ 0 swap set ] each ;
2007-09-20 18:09:08 -04:00
: wrap-line ( -- )
line-height get y +@
{ x line-height } zero-vars ;
: wrap-pos ( -- pos ) x get y get 2array ; inline
: advance-x ( x -- )
x +@
x get max-x [ max ] change ;
: advance-y ( y -- )
dup line-height [ max ] change
y get + max-y [ max ] change ;
:: wrap-step ( quot child -- )
child pref-dim
[
child
[
word-break-gadget?
[ drop ] [ first overrun? [ wrap-line ] when ] if
]
[ wrap-pos quot call ] bi
]
[ first advance-x ]
[ second advance-y ]
tri ; inline
2007-09-20 18:09:08 -04:00
: wrap-dim ( -- dim ) max-x get max-y get 2array ;
: init-wrap ( paragraph -- )
2008-08-31 02:42:30 -04:00
margin>> margin set
2007-09-20 18:09:08 -04:00
{ x max-x y max-y line-height } zero-vars ;
: do-wrap ( paragraph quot -- dim )
[
swap dup init-wrap
2008-01-09 17:36:30 -05:00
[ wrap-step ] with each-child wrap-dim
2007-09-20 18:09:08 -04:00
] with-scope ; inline
M: paragraph pref-dim*
[ 2drop ] do-wrap ;
M: paragraph layout*
2008-09-01 23:44:54 -04:00
[ swap dup prefer (>>loc) ] do-wrap drop ;