1 ! Copyright (C) 2005, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays hashtables io kernel namespaces sequences
4 strings quotations math opengl combinators memoize math.vectors
5 sorting splitting assocs classes.tuple models continuations
6 destructors accessors math.rectangles fry fonts ui.pens.solid
7 ui.images ui.gadgets ui.gadgets.private ui.gadgets.borders
8 ui.gadgets.buttons ui.gadgets.labels ui.gadgets.scrollers
9 ui.gadgets.paragraphs ui.gadgets.incremental ui.gadgets.packs
10 ui.gadgets.menus ui.clipboards ui.gestures ui.traverse ui.render
11 ui.text ui.gadgets.presentations ui.gadgets.grids ui.gadgets.tracks
12 ui.gadgets.icons ui.gadgets.grid-lines ui.baseline-alignment
14 FROM: io.styles => foreground background ;
18 output current input last-line prototype scrolls?
19 selection-color caret mark selecting? ;
21 TUPLE: pane-stream pane ;
23 C: <pane-stream> pane-stream
25 M: pane-stream stream-element-type drop +character+ ;
29 : clear-selection ( pane -- pane )
30 f >>caret f >>mark ; inline
32 : prepare-last-line ( pane -- )
34 [ current>> f track-add ]
35 [ input>> [ 1 track-add ] when* ] bi
38 : init-current ( pane -- pane )
39 dup prototype>> clone >>current ; inline
41 : focus-input ( pane -- )
42 input>> [ request-focus ] when* ;
44 : next-line ( pane -- )
47 [ init-current prepare-last-line ]
50 : pane-caret&mark ( pane -- caret mark )
51 [ caret>> ] [ mark>> ] bi ; inline
53 : selected-subtree ( pane -- seq )
54 [ pane-caret&mark sort-pair ] keep gadget-subtree ;
56 M: pane gadget-selection? pane-caret&mark and ;
58 M: pane gadget-selection ( pane -- string/f )
59 selected-subtree gadget-text ;
61 : init-prototype ( pane -- pane )
62 <shelf> +baseline+ >>align >>prototype ; inline
64 : init-output ( pane -- pane )
65 <incremental> [ >>output ] [ f track-add ] bi ; inline
67 : pane-theme ( pane -- pane )
69 selection-color >>selection-color ; inline
71 : init-last-line ( pane -- pane )
72 horizontal <track> 0 >>fill +baseline+ >>align
73 [ >>last-line ] [ 1 track-add ] bi
74 dup prepare-last-line ; inline
76 M: pane selected-children
77 dup gadget-selection? [
78 [ selected-subtree leaves ]
83 : scroll-pane ( pane -- )
84 dup scrolls?>> [ scroll>bottom ] [ drop ] if ;
86 : smash-line ( current -- gadget )
88 { [ dup empty? ] [ 2drop "" <label> ] }
89 { [ dup length 1 = ] [ nip first ] }
95 [ current>> [ unparent ] [ smash-line ] bi ] [ output>> ] bi
99 : smash-pane ( pane -- gadget ) [ pane-nl ] [ output>> smash-line ] bi ;
101 : pane-write ( seq pane -- )
102 [ pane-nl ] [ current>> stream-write ]
103 bi-curry interleave ;
105 : pane-format ( seq style pane -- )
106 [ nip pane-nl ] [ current>> stream-format ]
107 bi-curry bi-curry interleave ;
109 : do-pane-stream ( pane-stream quot -- )
110 [ pane>> ] dip keep scroll-pane ; inline
112 M: pane-stream stream-nl
113 [ pane-nl ] do-pane-stream ;
115 M: pane-stream stream-write1
116 [ current>> stream-write1 ] do-pane-stream ;
118 M: pane-stream stream-write
119 [ [ string-lines ] dip pane-write ] do-pane-stream ;
121 M: pane-stream stream-format
122 [ [ string-lines ] 2dip pane-format ] do-pane-stream ;
124 M: pane-stream dispose drop ;
126 M: pane-stream stream-flush drop ;
128 M: pane-stream make-span-stream
129 swap <style-stream> <ignore-close-stream> ;
133 : new-pane ( input class -- pane )
134 [ vertical ] dip new-track
140 init-last-line ; inline
142 : <pane> ( -- pane ) f pane new-pane ;
144 GENERIC: write-gadget ( gadget stream -- )
146 M: pane-stream write-gadget ( gadget pane-stream -- )
147 pane>> current>> swap add-gadget drop ;
149 M: style-stream write-gadget
150 stream>> write-gadget ;
152 : print-gadget ( gadget stream -- )
153 [ write-gadget ] [ nip stream-nl ] 2bi ;
155 : gadget. ( gadget -- )
156 output-stream get print-gadget ;
158 : pane-clear ( pane -- )
160 [ output>> clear-incremental ]
161 [ current>> clear-gadget ]
164 : with-pane ( pane quot -- )
165 [ [ scroll>top ] [ pane-clear ] [ <pane-stream> ] tri ] dip
166 with-output-stream* ; inline
168 : make-pane ( quot -- gadget )
169 [ <pane> ] dip [ with-pane ] [ drop smash-pane ] 2bi ; inline
171 TUPLE: pane-control < pane quot ;
173 M: pane-control model-changed ( model pane-control -- )
174 [ value>> ] [ dup quot>> ] bi*
175 '[ _ call( value -- ) ] with-pane ;
177 : <pane-control> ( model quot -- pane )
178 f pane-control new-pane
185 MEMO: specified-font ( assoc -- font )
186 #! We memoize here to avoid creating lots of duplicate font objects.
187 [ monospace-font <font> ] dip
189 [ font-name swap at >>name ]
194 { bold [ t >>bold? ] }
195 { italic [ t >>italic? ] }
196 { bold-italic [ t >>bold? t >>italic? ] }
199 [ font-size swap at >>size ]
200 [ foreground swap at >>foreground ]
201 [ background swap at >>background ]
205 : apply-font-style ( style gadget -- style gadget )
206 { font-name font-style font-size foreground background }
207 pick extract-keys specified-font >>font ;
209 : apply-style ( style gadget key quot -- style gadget )
210 [ pick at ] dip when* ; inline
212 : apply-presentation-style ( style gadget -- style gadget )
213 presented [ <presentation> ] apply-style ;
215 : apply-image-style ( style gadget -- style gadget )
216 image [ nip <image-name> <icon> ] apply-style ;
218 : apply-background-style ( style gadget -- style gadget )
219 background [ <solid> >>interior ] apply-style ;
221 : style-label ( style gadget -- gadget )
223 apply-background-style
224 apply-presentation-style
228 : <styled-label> ( style text -- gadget )
229 <label> style-label ;
233 : apply-wrap-style ( style pane -- style pane )
235 2dup <paragraph> >>prototype drop
236 <paragraph> >>current
239 : apply-border-color-style ( style gadget -- style gadget )
240 border-color [ <solid> >>boundary ] apply-style ;
242 : apply-page-color-style ( style gadget -- style gadget )
243 page-color [ <solid> >>interior ] apply-style ;
245 : apply-inset-style ( style gadget -- style gadget )
246 inset [ <border> ] apply-style ;
248 : style-pane ( style pane -- pane )
250 apply-border-color-style
251 apply-page-color-style
252 apply-presentation-style
255 TUPLE: nested-pane-stream < pane-stream style parent ;
257 : new-nested-pane-stream ( style parent class -- stream )
260 swap <pane> apply-wrap-style [ >>style ] [ >>pane ] bi* ;
263 : unnest-pane-stream ( stream -- child parent )
264 [ [ style>> ] [ pane>> smash-pane ] bi style-pane ] [ parent>> ] bi ;
266 TUPLE: pane-block-stream < nested-pane-stream ;
268 M: pane-block-stream dispose
269 unnest-pane-stream write-gadget ;
271 M: pane-stream make-block-stream
272 pane-block-stream new-nested-pane-stream ;
275 : apply-table-gap-style ( style grid -- style grid )
276 table-gap [ >>gap ] apply-style ;
278 : apply-table-border-style ( style grid -- style grid )
279 table-border [ <grid-lines> >>boundary ]
282 : styled-grid ( style grid -- grid )
285 apply-table-gap-style
286 apply-table-border-style
289 TUPLE: pane-cell-stream < nested-pane-stream ;
291 M: pane-cell-stream dispose drop ;
293 M: pane-stream make-cell-stream
294 pane-cell-stream new-nested-pane-stream ;
296 M: pane-stream stream-write-table
298 swap [ [ pane>> smash-pane ] map ] map
303 M: pack dispose drop ;
305 M: paragraph dispose drop ;
307 : gadget-write ( string gadget -- )
309 [ 2drop ] [ <label> text-theme add-gadget drop ] if ;
311 M: pack stream-write gadget-write ;
313 : gadget-bl ( style stream -- )
314 swap " " <word-break-gadget> style-label add-gadget drop ;
316 M: paragraph stream-write
318 [ H{ } over gadget-bl ] [ over gadget-write ] interleave
321 : gadget-write1 ( char gadget -- )
322 [ 1string ] dip stream-write ;
324 M: pack stream-write1 gadget-write1 ;
326 M: paragraph stream-write1
328 [ H{ } swap gadget-bl drop ] [ gadget-write1 ] if ;
330 : empty-output? ( string style -- ? )
331 [ empty? ] [ image swap key? not ] bi* and ;
333 : gadget-format ( string style stream -- )
334 [ [ empty-output? ] 2keep ] dip
335 '[ _ _ swap <styled-label> _ swap add-gadget drop ] unless ;
337 M: pack stream-format
340 M: paragraph stream-format
341 over { presented image } [ swap key? ] with any? [
345 [ gadget-bl ] [ gadget-format ] bi-curry bi-curry
349 : caret>mark ( pane -- )
350 dup caret>> >>mark relayout-1 ;
352 GENERIC: sloppy-pick-up* ( loc gadget -- n )
354 M: pack sloppy-pick-up* ( loc gadget -- n )
355 [ orientation>> ] [ children>> ] bi
356 [ loc>> ] (fast-children-on) ;
358 M: gadget sloppy-pick-up*
359 children>> [ contains-point? ] with find-last drop ;
364 : wet-and-sloppy ( loc gadget n -- newloc newgadget )
365 swap nth-gadget [ loc>> v- ] keep ;
367 : sloppy-pick-up ( loc gadget -- path )
368 2dup sloppy-pick-up* dup
369 [ [ wet-and-sloppy sloppy-pick-up ] keep prefix ]
373 : move-caret ( pane loc -- )
374 over screen-loc v- over sloppy-pick-up >>caret
377 : begin-selection ( pane -- )
379 dup hand-loc get move-caret
383 : extend-selection ( pane -- )
387 hand-loc get move-caret
389 dup hand-clicked get child? [
391 [ hand-clicked set-global ]
392 [ hand-click-loc get move-caret ]
397 ] [ dup caret>> gadget-at-path scroll>gadget ] bi
400 : end-selection ( pane -- )
401 dup selecting?>> hand-moved? or
402 [ f >>selecting? ] dip
403 [ [ com-copy-selection ] [ request-focus ] bi ]
404 [ [ relayout-1 ] [ focus-input ] bi ]
407 : select-to-caret ( pane -- )
409 [ dup mark>> [ dup caret>mark ] unless hand-loc get move-caret ]
410 [ com-copy-selection ]
414 : pane-menu ( pane -- ) { com-copy } show-commands-menu ;
419 { T{ button-down } [ begin-selection ] }
420 { T{ button-down f { S+ } 1 } [ select-to-caret ] }
421 { T{ button-up f { S+ } 1 } [ end-selection ] }
422 { T{ button-up } [ end-selection ] }
423 { T{ drag { # 1 } } [ extend-selection ] }
424 { copy-action [ com-copy ] }
425 { T{ button-down f f 3 } [ pane-menu ] }