1 ! Copyright (C) 2005, 2008 Slava Pestov
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays ui.gadgets ui.gadgets.labels ui.render
4 kernel math namespaces sequences math.order math.geometry.rect
6 IN: ui.gadgets.paragraphs
9 TUPLE: word-break-gadget < label ;
11 : <word-break-gadget> ( text -- gadget )
12 word-break-gadget new-label ;
14 M: word-break-gadget draw-gadget* drop ;
16 ! A gadget that arranges its children in a word-wrap style.
17 TUPLE: paragraph < gadget margin ;
19 : <paragraph> ( margin -- gadget )
24 SYMBOL: x SYMBOL: max-x
26 SYMBOL: y SYMBOL: max-y
32 : overrun? ( width -- ? ) x get + margin get > ;
34 : zero-vars ( seq -- ) [ 0 swap set ] each ;
38 { x line-height } zero-vars ;
40 : wrap-pos ( -- pos ) x get y get 2array ; inline
44 x get max-x [ max ] change ;
47 dup line-height [ max ] change
48 y get + max-y [ max ] change ;
50 :: wrap-step ( quot child -- )
56 [ drop ] [ first overrun? [ wrap-line ] when ] if
58 [ wrap-pos quot call ] bi
64 : wrap-dim ( -- dim ) max-x get max-y get 2array ;
66 : init-wrap ( paragraph -- )
68 { x max-x y max-y line-height } zero-vars ;
70 : do-wrap ( paragraph quot -- dim )
73 [ wrap-step ] with each-child wrap-dim
76 M: paragraph pref-dim*
80 [ swap dup prefer (>>loc) ] do-wrap drop ;