1 ! Copyright (C) 2005, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs classes combinators destructors
4 documents.private fonts io io.styles kernel math math.rectangles
5 math.vectors models namespaces sequences sets sorting splitting
6 strings ui.baseline-alignment ui.clipboards ui.gadgets
7 ui.gadgets.borders ui.gadgets.grid-lines ui.gadgets.grids
8 ui.gadgets.icons ui.gadgets.incremental ui.gadgets.labels
9 ui.gadgets.menus ui.gadgets.packs ui.gadgets.paragraphs
10 ui.gadgets.presentations ui.gadgets.private ui.gadgets.scrollers
11 ui.gadgets.tracks ui.gestures ui.images ui.pens.solid ui.render
12 ui.theme ui.traverse unicode ;
13 FROM: io.styles => foreground background ;
14 FROM: ui.gadgets.wrappers => <wrapper> ;
18 output current input last-line prototype scrolls?
19 selection-color caret mark selecting? ;
21 TUPLE: pane-stream pane parent ;
22 INSTANCE: pane-stream output-stream
24 : <pane-stream> ( pane -- pane-stream )
27 M: pane-stream stream-element-type drop +character+ ;
33 : clear-selection ( pane -- pane )
34 f >>caret f >>mark ; inline
36 : prepare-last-line ( pane -- )
38 [ current>> f track-add ]
39 [ input>> [ 1 track-add ] when* ] bi
42 : init-current ( pane -- pane )
43 dup prototype>> clone >>current ; inline
45 : focus-input ( pane -- )
46 input>> [ request-focus ] when* ;
48 : next-line ( pane -- )
50 [ init-current prepare-last-line ]
53 : pane-caret&mark ( pane -- caret mark )
54 [ caret>> ] [ mark>> ] bi ; inline
56 : selected-subtree ( pane -- seq )
57 [ pane-caret&mark sort-pair ] keep gadget-subtree ;
59 M: pane gadget-selection? pane-caret&mark and ;
61 M: pane gadget-selection selected-subtree gadget-text ;
63 : init-prototype ( pane -- pane )
64 <shelf> +baseline+ >>align >>prototype ; inline
66 : init-output ( pane -- pane )
67 <incremental> [ >>output ] [ f track-add ] bi ; inline
69 : pane-theme ( pane -- pane )
71 selection-color >>selection-color ; inline
73 : init-last-line ( pane -- pane )
74 horizontal <track> 0 >>fill +baseline+ >>align
75 [ >>last-line ] [ 1 track-add ] bi
76 dup prepare-last-line ; inline
78 M: pane selected-children
79 dup gadget-selection? [
80 [ selected-subtree leaves ]
85 : scroll-pane ( pane -- )
86 dup scrolls?>> [ scroll>bottom ] [ drop ] if ;
88 GENERIC: pane-label ( pane -- label )
90 M: pane pane-label drop "" <label> ;
92 : smash-line ( pane current -- gadget )
94 { [ dup empty? ] [ 2drop pane-label ] }
95 { [ dup length 1 = ] [ 2nip first ] }
101 [ dup current>> [ unparent ] [ smash-line ] bi ] [ output>> ] bi
105 GENERIC: smash-pane ( pane -- gadget )
108 [ pane-nl ] [ dup output>> smash-line ] bi ;
110 GENERIC: pane-line ( str style gadget -- )
112 : pane-format ( lines style pane -- )
113 [ nip pane-nl ] [ current>> pane-line ]
114 bi-curry bi-curry interleave ;
116 : pane-write ( lines pane -- )
117 H{ } swap pane-format ;
119 : pane-write1 ( char pane -- )
120 [ 1string H{ } ] dip current>> pane-line ;
122 :: do-pane-stream ( pane-stream quot -- )
123 pane-stream pane>> :> pane
124 pane find-scroller :> scroller
127 [ range-value second ]
128 [ range-page-value second + ]
129 [ range-max-value second >= ]
131 ] [ f ] if* :> bottom?
133 pane scrolls?>> bottom? and scroller and [
135 [ model>> range-value first ]
136 [ model>> range-max-value second 2array ]
137 [ set-scroll-position ]
141 M: pane-stream stream-nl
142 [ pane-nl ] do-pane-stream ;
144 M: pane-stream stream-write1
145 [ pane-write1 ] do-pane-stream ;
147 : split-pane ( str quot: ( str -- ) -- )
150 [ 3639 over last-grapheme-from cut-slice ] [ f ] if
151 swap "" like ?split-lines @ dup
154 M: pane-stream stream-write
155 [ '[ _ pane-write ] split-pane ] do-pane-stream ;
157 M: pane-stream stream-format
158 [ '[ _ _ pane-format ] split-pane ] do-pane-stream ;
160 M: pane-stream dispose
162 [ pane>> smash-pane ] dip write-gadget
165 ! M: pane-stream dispose drop ;
167 M: pane-stream stream-flush drop ;
169 M: pane-stream make-span-stream
170 swap <style-stream> <ignore-close-stream> ;
174 : new-pane ( input class -- pane )
175 [ vertical ] dip new-track
181 init-last-line ; inline
183 : <pane> ( -- pane ) f pane new-pane ;
185 GENERIC: gadget-alt-text ( gadget -- string )
187 M: object gadget-alt-text
188 class-of name>> "( " " )" surround ;
190 GENERIC: write-gadget ( gadget stream -- )
192 M: object write-gadget
193 [ gadget-alt-text ] dip stream-write ;
195 M: filter-writer write-gadget
196 stream>> write-gadget ;
198 M: pane-stream write-gadget
199 [ current>> swap add-gadget drop ] do-pane-stream ;
201 : print-gadget ( gadget stream -- )
202 [ write-gadget ] [ nip stream-nl ] 2bi ;
204 : gadget. ( gadget -- )
205 output-stream get print-gadget ;
207 : clear-pane ( pane -- )
209 [ output>> clear-incremental ]
210 [ current>> clear-gadget ]
213 : with-pane ( pane quot -- )
215 [ [ scroll>top ] [ clear-pane ] [ <pane-stream> ] tri ] dip
217 ] dip scroll-pane ; inline
219 : make-pane ( quot -- gadget )
220 [ <pane> ] dip '[ _ with-pane ] keep smash-pane ; inline
222 TUPLE: pane-control < pane quot ;
224 M: pane-control model-changed
225 [ value>> ] [ dup quot>> ] bi*
226 '[ _ call( value -- ) ] with-pane ;
228 : <pane-control> ( model quot -- pane )
229 f pane-control new-pane
237 MEMO:: specified-font ( name style size foreground background -- font )
238 ! We memoize here to avoid creating lots of duplicate font objects.
240 name [ >>name ] when*
244 { bold [ t >>bold? ] }
245 { italic [ t >>italic? ] }
246 { bold-italic [ t >>bold? t >>italic? ] }
248 size [ >>size ] when*
249 foreground [ >>foreground ] when*
250 background [ >>background ] when* ;
252 : apply-font-style ( style gadget -- style gadget )
259 } cleave specified-font >>font ;
261 : apply-style ( style gadget key quot -- style gadget )
262 [ pick at ] dip when* ; inline
264 : apply-presentation-style ( style gadget -- style gadget )
265 presented [ <presentation> ] apply-style ;
267 : apply-image-style ( style gadget -- style gadget )
268 image-style [ nip <image-name> <icon> ] apply-style ;
270 : apply-background-style ( style gadget -- style gadget )
271 background [ <solid> >>interior ] apply-style ;
273 : apply-character-style ( style gadget -- gadget )
275 apply-background-style
277 apply-presentation-style
282 : apply-wrap-style ( style pane -- style pane )
284 2dup <paragraph> >>prototype drop
285 <paragraph> >>current
288 : apply-border-color-style ( style gadget -- style gadget )
289 border-color [ <solid> >>boundary ] apply-style ;
291 : apply-page-color-style ( style gadget -- style gadget )
292 page-color [ <solid> >>interior ] apply-style ;
294 : apply-inset-style ( style gadget -- style gadget )
295 inset [ <border> ] apply-style ;
297 : apply-paragraph-style ( style pane -- pane )
299 apply-border-color-style
300 apply-page-color-style
301 apply-presentation-style
304 : remove-paragraph-styles ( style -- style' )
307 wrap-margin border-color page-color inset presented
311 TUPLE: styled-pane < pane style ;
313 : <styled-pane> ( style -- pane )
314 f styled-pane new-pane apply-wrap-style swap >>style ;
316 M: styled-pane smash-pane
317 [ style>> ] [ call-next-method apply-paragraph-style ] bi ;
319 : <styled-pane-stream> ( style pane-stream -- styled-stream )
323 [ remove-paragraph-styles <style-stream> ] tri* ;
325 : make-styled-pane ( style quot -- gadget )
326 [ <styled-pane> ] dip '[ _ with-pane ] keep smash-pane ; inline
328 M: pane-stream make-block-stream
329 <styled-pane-stream> ;
333 : apply-table-gap-style ( style grid -- style grid )
334 table-gap [ >>gap ] apply-style ;
336 : apply-table-border-style ( style grid -- style grid )
337 table-border [ <grid-lines> >>boundary ] apply-style ;
339 : <styled-grid> ( style grid -- grid )
342 apply-table-gap-style
343 apply-table-border-style
344 apply-paragraph-style ;
346 M: pane-stream make-cell-stream
347 drop f <styled-pane-stream> ;
349 M: pane-stream stream-write-table
351 swap [ [ stream>> pane>> smash-pane ] map ] map
357 : pane-bl ( style gadget -- )
358 swap " " <word-break-gadget> apply-character-style add-gadget drop ;
360 : <styled-label> ( style text -- gadget )
363 apply-background-style
365 apply-presentation-style
368 M: styled-pane pane-label style>> "" <styled-label> ;
370 : pane-text ( string style gadget -- )
371 [ swap <styled-label> ] [ swap add-gadget drop ] bi* ;
373 M: pack pane-line pane-text ;
375 M: paragraph pane-line
376 { presented image-style } pick '[ _ key? ] any? [
380 [ pane-bl ] [ pane-text ] bi-curry bi-curry
384 : caret>mark ( pane -- )
385 dup caret>> >>mark relayout-1 ;
387 GENERIC: sloppy-pick-up* ( loc gadget -- n )
389 M: pack sloppy-pick-up*
390 [ orientation>> ] [ children>> ] bi
391 [ loc>> ] (fast-children-on) ;
393 M: gadget sloppy-pick-up*
394 children>> [ contains-point? ] with find-last drop ;
399 : wet-and-sloppy ( loc gadget n -- newloc newgadget )
400 swap nth-gadget [ loc>> v- ] keep ;
402 : sloppy-pick-up ( loc gadget -- path )
403 2dup sloppy-pick-up* dup
404 [ [ wet-and-sloppy sloppy-pick-up ] keep prefix ]
408 : move-caret ( pane loc -- )
409 over screen-loc v- over sloppy-pick-up >>caret
412 : begin-selection ( pane -- )
414 dup hand-loc get-global move-caret
418 : extend-selection ( pane -- )
422 hand-loc get-global move-caret
424 dup hand-clicked get-global child? [
426 [ hand-clicked set-global ]
427 [ hand-click-loc get-global move-caret ]
432 ] [ dup caret>> gadget-at-path scroll>gadget ] bi
435 : end-selection ( pane -- )
436 dup selecting?>> hand-moved? or
437 [ f >>selecting? ] dip
438 [ [ com-copy-selection ] [ request-focus ] bi ]
439 [ [ relayout-1 ] [ focus-input ] bi ]
442 : select-to-caret ( pane -- )
444 [ dup mark>> [ dup caret>mark ] unless hand-loc get-global move-caret ]
445 [ com-copy-selection ]
449 : pane-menu ( pane -- ) { com-copy } show-commands-menu ;
454 { T{ button-down } [ begin-selection ] }
455 { T{ button-down f { S+ } 1 } [ select-to-caret ] }
456 { T{ button-up f { S+ } 1 } [ end-selection ] }
457 { T{ button-up } [ end-selection ] }
458 { T{ drag { # 1 } } [ extend-selection ] }
459 { copy-action [ com-copy ] }
460 { T{ button-down f f 3 } [ pane-menu ] }
463 GENERIC: content-gadget ( object -- gadget/f )
465 M: object content-gadget drop f ;