1 ! Copyright (C) 2005, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs classes combinators destructors fonts
4 fry io io.styles kernel math.rectangles math.vectors memoize
5 models namespaces sequences sorting splitting strings
6 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
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 ;
22 INSTANCE: pane-stream output-stream
24 C: <pane-stream> pane-stream
26 M: pane-stream stream-element-type drop +character+ ;
30 : clear-selection ( pane -- pane )
31 f >>caret f >>mark ; inline
33 : prepare-last-line ( pane -- )
35 [ current>> f track-add ]
36 [ input>> [ 1 track-add ] when* ] bi
39 : init-current ( pane -- pane )
40 dup prototype>> clone >>current ; inline
42 : focus-input ( pane -- )
43 input>> [ request-focus ] when* ;
45 : next-line ( pane -- )
48 [ init-current prepare-last-line ]
51 : pane-caret&mark ( pane -- caret mark )
52 [ caret>> ] [ mark>> ] bi ; inline
54 : selected-subtree ( pane -- seq )
55 [ pane-caret&mark sort-pair ] keep gadget-subtree ;
57 M: pane gadget-selection? pane-caret&mark and ;
59 M: pane gadget-selection ( pane -- string/f )
60 selected-subtree gadget-text ;
62 : init-prototype ( pane -- pane )
63 <shelf> +baseline+ >>align >>prototype ; inline
65 : init-output ( pane -- pane )
66 <incremental> [ >>output ] [ f track-add ] bi ; inline
68 : pane-theme ( pane -- pane )
70 selection-color >>selection-color ; inline
72 : init-last-line ( pane -- pane )
73 horizontal <track> 0 >>fill +baseline+ >>align
74 [ >>last-line ] [ 1 track-add ] bi
75 dup prepare-last-line ; inline
77 M: pane selected-children
78 dup gadget-selection? [
79 [ selected-subtree leaves ]
84 : scroll-pane ( pane -- )
85 dup scrolls?>> [ scroll>bottom ] [ drop ] if ;
87 : smash-line ( current -- gadget )
89 { [ dup empty? ] [ 2drop "" <label> ] }
90 { [ dup length 1 = ] [ nip first ] }
96 [ current>> [ unparent ] [ smash-line ] bi ] [ output>> ] bi
100 : smash-pane ( pane -- gadget ) [ pane-nl ] [ output>> smash-line ] bi ;
102 : pane-write ( seq pane -- )
103 [ pane-nl ] [ current>> stream-write ]
104 bi-curry interleave ;
106 : pane-format ( seq style pane -- )
107 [ nip pane-nl ] [ current>> stream-format ]
108 bi-curry bi-curry interleave ;
110 : do-pane-stream ( pane-stream quot -- )
111 [ pane>> ] dip keep scroll-pane ; inline
113 M: pane-stream stream-nl
114 [ pane-nl ] do-pane-stream ;
116 M: pane-stream stream-write1
117 [ current>> stream-write1 ] do-pane-stream ;
119 M: pane-stream stream-write
120 [ [ string-lines ] dip pane-write ] do-pane-stream ;
122 M: pane-stream stream-format
123 [ [ string-lines ] 2dip pane-format ] do-pane-stream ;
125 M: pane-stream dispose drop ;
127 M: pane-stream stream-flush drop ;
129 M: pane-stream make-span-stream
130 swap <style-stream> <ignore-close-stream> ;
134 : new-pane ( input class -- pane )
135 [ vertical ] dip new-track
141 init-last-line ; inline
143 : <pane> ( -- pane ) f pane new-pane ;
145 GENERIC: gadget-alt-text ( gadget -- string )
147 M: object gadget-alt-text
148 class-of name>> "( " " )" surround ;
150 GENERIC: write-gadget ( gadget stream -- )
152 M: object write-gadget
153 [ gadget-alt-text ] dip stream-write ;
155 M: filter-writer write-gadget
156 stream>> write-gadget ;
158 M: pane-stream write-gadget ( gadget pane-stream -- )
159 pane>> current>> swap add-gadget drop ;
161 M: style-stream write-gadget
162 stream>> write-gadget ;
164 : print-gadget ( gadget stream -- )
165 [ write-gadget ] [ nip stream-nl ] 2bi ;
167 : gadget. ( gadget -- )
168 output-stream get print-gadget ;
170 : pane-clear ( pane -- )
172 [ output>> clear-incremental ]
173 [ current>> clear-gadget ]
176 : with-pane ( pane quot -- )
177 [ [ scroll>top ] [ pane-clear ] [ <pane-stream> ] tri ] dip
178 with-output-stream* ; inline
180 : make-pane ( quot -- gadget )
181 [ <pane> ] dip [ with-pane ] [ drop smash-pane ] 2bi ; inline
183 TUPLE: pane-control < pane quot ;
185 M: pane-control model-changed ( model pane-control -- )
186 [ value>> ] [ dup quot>> ] bi*
187 '[ _ call( value -- ) ] with-pane ;
189 : <pane-control> ( model quot -- pane )
190 f pane-control new-pane
197 MEMO: specified-font ( assoc -- font )
198 #! We memoize here to avoid creating lots of duplicate font objects.
199 [ monospace-font <font> ] dip
201 [ font-name of >>name ]
206 { bold [ t >>bold? ] }
207 { italic [ t >>italic? ] }
208 { bold-italic [ t >>bold? t >>italic? ] }
211 [ font-size of >>size ]
212 [ foreground of >>foreground ]
213 [ background of >>background ]
217 : apply-font-style ( style gadget -- style gadget )
218 { font-name font-style font-size foreground background }
219 pick extract-keys specified-font >>font ;
221 : apply-style ( style gadget key quot -- style gadget )
222 [ pick at ] dip when* ; inline
224 : apply-presentation-style ( style gadget -- style gadget )
225 presented [ <presentation> ] apply-style ;
227 : apply-image-style ( style gadget -- style gadget )
228 image [ nip <image-name> <icon> ] apply-style ;
230 : apply-background-style ( style gadget -- style gadget )
231 background [ <solid> >>interior ] apply-style ;
233 : style-label ( style gadget -- gadget )
235 apply-background-style
236 apply-presentation-style
240 : <styled-label> ( style text -- gadget )
241 <label> style-label ;
245 : apply-wrap-style ( style pane -- style pane )
247 2dup <paragraph> >>prototype drop
248 <paragraph> >>current
251 : apply-border-color-style ( style gadget -- style gadget )
252 border-color [ <solid> >>boundary ] apply-style ;
254 : apply-page-color-style ( style gadget -- style gadget )
255 page-color [ <solid> >>interior ] apply-style ;
257 : apply-inset-style ( style gadget -- style gadget )
258 inset [ <border> ] apply-style ;
260 : style-pane ( style pane -- pane )
262 apply-border-color-style
263 apply-page-color-style
264 apply-presentation-style
267 TUPLE: nested-pane-stream < pane-stream style parent ;
269 : new-nested-pane-stream ( style parent class -- stream )
272 swap <pane> apply-wrap-style [ >>style ] [ >>pane ] bi* ;
275 : unnest-pane-stream ( stream -- child parent )
276 [ [ style>> ] [ pane>> smash-pane ] bi style-pane ] [ parent>> ] bi ;
278 TUPLE: pane-block-stream < nested-pane-stream ;
280 M: pane-block-stream dispose
281 unnest-pane-stream write-gadget ;
283 M: pane-stream make-block-stream
284 pane-block-stream new-nested-pane-stream ;
287 : apply-table-gap-style ( style grid -- style grid )
288 table-gap [ >>gap ] apply-style ;
290 : apply-table-border-style ( style grid -- style grid )
291 table-border [ <grid-lines> >>boundary ]
294 : styled-grid ( style grid -- grid )
297 apply-table-gap-style
298 apply-table-border-style
301 TUPLE: pane-cell-stream < nested-pane-stream ;
303 M: pane-cell-stream dispose drop ;
305 M: pane-stream make-cell-stream
306 pane-cell-stream new-nested-pane-stream ;
308 M: pane-stream stream-write-table
310 swap [ [ pane>> smash-pane ] map ] map
315 M: pack dispose drop ;
317 M: paragraph dispose drop ;
319 : gadget-write ( string gadget -- )
321 [ 2drop ] [ <label> text-theme add-gadget drop ] if ;
323 M: pack stream-write gadget-write ;
325 : gadget-bl ( style stream -- )
326 swap " " <word-break-gadget> style-label add-gadget drop ;
328 M: paragraph stream-write
330 [ H{ } over gadget-bl ] [ over gadget-write ] interleave
333 : gadget-write1 ( char gadget -- )
334 [ 1string ] dip stream-write ;
336 M: pack stream-write1 gadget-write1 ;
338 M: paragraph stream-write1
340 [ H{ } swap gadget-bl drop ] [ gadget-write1 ] if ;
342 : empty-output? ( string style -- ? )
343 [ empty? ] [ image swap key? not ] bi* and ;
345 : gadget-format ( string style stream -- )
346 [ [ empty-output? ] 2keep ] dip
347 '[ _ _ swap <styled-label> _ swap add-gadget drop ] unless ;
349 M: pack stream-format
352 M: paragraph stream-format
353 over { presented image } [ swap key? ] with any? [
357 [ gadget-bl ] [ gadget-format ] bi-curry bi-curry
361 : caret>mark ( pane -- )
362 dup caret>> >>mark relayout-1 ;
364 GENERIC: sloppy-pick-up* ( loc gadget -- n )
366 M: pack sloppy-pick-up* ( loc gadget -- n )
367 [ orientation>> ] [ children>> ] bi
368 [ loc>> ] (fast-children-on) ;
370 M: gadget sloppy-pick-up*
371 children>> [ contains-point? ] with find-last drop ;
376 : wet-and-sloppy ( loc gadget n -- newloc newgadget )
377 swap nth-gadget [ loc>> v- ] keep ;
379 : sloppy-pick-up ( loc gadget -- path )
380 2dup sloppy-pick-up* dup
381 [ [ wet-and-sloppy sloppy-pick-up ] keep prefix ]
385 : move-caret ( pane loc -- )
386 over screen-loc v- over sloppy-pick-up >>caret
389 : begin-selection ( pane -- )
391 dup hand-loc get-global move-caret
395 : extend-selection ( pane -- )
399 hand-loc get-global move-caret
401 dup hand-clicked get-global child? [
403 [ hand-clicked set-global ]
404 [ hand-click-loc get-global move-caret ]
409 ] [ dup caret>> gadget-at-path scroll>gadget ] bi
412 : end-selection ( pane -- )
413 dup selecting?>> hand-moved? or
414 [ f >>selecting? ] dip
415 [ [ com-copy-selection ] [ request-focus ] bi ]
416 [ [ relayout-1 ] [ focus-input ] bi ]
419 : select-to-caret ( pane -- )
421 [ dup mark>> [ dup caret>mark ] unless hand-loc get-global move-caret ]
422 [ com-copy-selection ]
426 : pane-menu ( pane -- ) { com-copy } show-commands-menu ;
431 { T{ button-down } [ begin-selection ] }
432 { T{ button-down f { S+ } 1 } [ select-to-caret ] }
433 { T{ button-up f { S+ } 1 } [ end-selection ] }
434 { T{ button-up } [ end-selection ] }
435 { T{ drag { # 1 } } [ extend-selection ] }
436 { copy-action [ com-copy ] }
437 { T{ button-down f f 3 } [ pane-menu ] }
440 GENERIC: content-gadget ( object -- gadget/f )
441 M: object content-gadget drop f ;
443 M: string content-gadget
444 '[ _ write ] make-pane <scroller>
445 { 450 100 } >>pref-dim