1 ! Copyright (C) 2005, 2007 Slava Pestov
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays ui.gadgets ui.gadgets.labels ui.render kernel math
4 namespaces sequences math.order ;
5 IN: ui.gadgets.paragraphs
8 TUPLE: word-break-gadget ;
10 : <word-break-gadget> ( gadget -- gadget )
11 { set-delegate } word-break-gadget construct ;
13 M: word-break-gadget draw-gadget* drop ;
15 ! A gadget that arranges its children in a word-wrap style.
16 TUPLE: paragraph margin ;
18 : <paragraph> ( margin -- gadget )
19 paragraph construct-gadget
20 { 1 0 } over set-gadget-orientation
21 [ set-paragraph-margin ] keep ;
23 SYMBOL: x SYMBOL: max-x
25 SYMBOL: y SYMBOL: max-y
31 : overrun? ( width -- ? ) x get + margin get > ;
33 : zero-vars [ 0 swap set ] each ;
37 { x line-height } zero-vars ;
39 : wrap-pos ( -- pos ) x get y get 2array ; inline
43 x get max-x [ max ] change ;
46 dup line-height [ max ] change
47 y get + max-y [ max ] change ;
49 : wrap-step ( quot child -- )
51 over word-break-gadget? [
52 dup first overrun? [ wrap-line ] when
53 ] unless drop wrap-pos rot call
54 ] keep first2 advance-y advance-x ; inline
56 : wrap-dim ( -- dim ) max-x get max-y get 2array ;
58 : init-wrap ( paragraph -- )
59 paragraph-margin margin set
60 { x max-x y max-y line-height } zero-vars ;
62 : do-wrap ( paragraph quot -- dim )
65 [ wrap-step ] with each-child wrap-dim
68 M: paragraph pref-dim*
72 [ swap dup prefer set-rect-loc ] do-wrap drop ;