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

73 lines
1.7 KiB
Factor
Raw Normal View History

2007-09-20 18:09:08 -04:00
! Copyright (C) 2005, 2007 Slava Pestov
! See http://factorcode.org/license.txt for BSD license.
2008-08-29 19:44:19 -04:00
USING: accessors arrays ui.gadgets ui.gadgets.labels ui.render kernel math
2008-07-11 19:34:43 -04:00
namespaces sequences math.order math.geometry.rect ;
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 -- )
dup pref-dim [
over word-break-gadget? [
dup first overrun? [ wrap-line ] when
] unless drop wrap-pos rot call
] keep first2 advance-y advance-x ; inline
: 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 ;