1 ! Copyright (C) 2006, 2009 Slava Pestov
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays documents documents.elements kernel math
4 models models.filter namespaces locals fry make opengl opengl.gl
5 sequences strings math.vectors sorting colors combinators assocs
6 math.order fry calendar alarms continuations ui.clipboards ui.commands
7 ui.gadgets ui.gadgets.borders ui.gadgets.buttons ui.gadgets.labels
8 ui.gadgets.scrollers ui.gadgets.theme ui.gadgets.menus
9 ui.gadgets.wrappers ui.render ui.text ui.gestures math.geometry.rect
10 splitting unicode.categories fonts ;
11 IN: ui.gadgets.editors
13 TUPLE: editor < gadget
14 font color caret-color selection-color
16 focused? blink blink-alarm ;
18 : <loc> ( -- loc ) { 0 0 } <model> ;
20 : init-editor-locs ( editor -- editor )
24 : editor-theme ( editor -- editor )
27 selection-color >>selection-color
28 monospace-font >>font ; inline
30 : new-editor ( class -- editor )
36 : <editor> ( -- editor )
39 : activate-editor-model ( editor model -- )
42 swap model>> add-loc ;
44 : deactivate-editor-model ( editor model -- )
45 2dup remove-connection
47 swap model>> remove-loc ;
49 : blink-caret ( editor -- )
50 [ not ] change-blink relayout-1 ;
52 SYMBOL: blink-interval
54 750 milliseconds blink-interval set-global
56 : stop-blinking ( editor -- )
57 [ [ cancel-alarm ] when* f ] change-blink-alarm drop ;
59 : start-blinking ( editor -- )
62 dup '[ _ blink-caret ] blink-interval get every
66 : restart-blinking ( editor -- )
75 dup caret>> activate-editor-model
76 dup mark>> activate-editor-model ;
81 dup caret>> deactivate-editor-model
82 dup mark>> deactivate-editor-model ;
84 : editor-caret ( editor -- loc ) caret>> value>> ;
86 : editor-mark ( editor -- loc ) mark>> value>> ;
88 : set-caret ( loc editor -- )
89 [ model>> validate-loc ] keep
92 : change-caret ( editor quot -- )
93 [ [ [ editor-caret ] [ model>> ] bi ] dip call ] [ drop ] 2bi
96 : mark>caret ( editor -- )
97 [ editor-caret ] [ mark>> ] bi set-model ;
99 : change-caret&mark ( editor quot -- )
100 [ change-caret ] [ drop mark>caret ] 2bi ; inline
102 : editor-line ( n editor -- str ) control-value nth ;
104 : line-height ( editor -- n )
105 font>> "" text-height ;
107 : y>line ( y editor -- line# )
110 :: point>loc ( point editor -- loc )
111 point second editor y>line {
112 { [ dup 0 < ] [ drop { 0 0 } ] }
113 { [ dup editor model>> last-line# > ] [ drop editor model>> doc-end ] }
123 : clicked-loc ( editor -- loc )
124 [ hand-rel ] keep point>loc ;
126 : click-loc ( editor model -- )
127 [ clicked-loc ] dip set-model ;
129 : focus-editor ( editor -- )
130 [ start-blinking ] [ t >>focused? relayout-1 ] bi ;
132 : unfocus-editor ( editor -- )
133 [ stop-blinking ] [ f >>focused? relayout-1 ] bi ;
135 : loc>x ( loc editor -- x )
136 [ first2 swap ] dip [ editor-line ] [ font>> ] bi swap offset>x ;
138 : line>y ( lines# editor -- y )
141 : loc>point ( loc editor -- loc )
142 [ loc>x ] [ [ first ] dip line>y ] 2bi 2array ;
144 : caret-loc ( editor -- loc )
145 [ editor-caret ] keep loc>point ;
147 : caret-dim ( editor -- dim )
148 line-height 0 swap 2array ;
150 : scroll>caret ( editor -- )
151 dup graft-state>> second [
153 [ caret-loc ] [ caret-dim { 1 0 } v+ ] bi <rect>
158 editor get [ focused?>> ] [ blink>> ] bi and [
160 [ caret-color>> gl-color ]
162 dup caret-loc origin get v+
163 swap caret-dim over v+
168 : line-translation ( n -- loc )
169 editor get line-height * 0.0 swap 2array ;
171 : translate-lines ( n -- )
172 line-translation gl-translate ;
174 : draw-line ( editor str -- )
175 [ font>> ] dip { 0 0 } draw-text ;
177 : first-visible-line ( editor -- n )
179 [ clip get rect-loc second origin get second - ] dip
181 ] keep model>> validate-line ;
183 : last-visible-line ( editor -- n )
185 [ clip get rect-extent nip second origin get second - ] dip
187 ] keep model>> validate-line 1+ ;
189 : with-editor ( editor quot -- )
192 dup first-visible-line \ first-visible-line set
193 dup last-visible-line \ last-visible-line set
194 dup model>> document set
197 ] with-scope ; inline
199 : visible-lines ( editor -- seq )
200 [ \ first-visible-line get \ last-visible-line get ] dip
201 control-value <slice> ;
203 : with-editor-translation ( n quot -- )
204 [ line-translation origin get v+ ] dip with-translation ;
208 \ first-visible-line get [
209 editor get dup color>> gl-color
211 [ draw-line 1 translate-lines ] with each
212 ] with-editor-translation ;
214 : selection-start/end ( editor -- start end )
215 [ editor-mark ] [ editor-caret ] bi sort-pair ;
217 : (draw-selection) ( x1 x2 -- )
220 [ 0.0 2array ] [ editor get line-height 2array ] bi*
221 swap [ gl-fill-rect ] with-translation ;
223 : draw-selected-line ( start end n -- )
224 [ start/end-on-line ] keep
225 tuck [ swap 2array editor get loc>x ] 2bi@
228 : draw-selection ( -- )
229 editor get selection-color>> gl-color
230 editor get selection-start/end
237 ] with-editor-translation ;
239 M: editor draw-gadget*
240 [ draw-selection draw-lines draw-caret ] with-editor ;
243 [ font>> ] [ control-value ] bi text-dim ;
245 : contents-changed ( model editor -- )
247 over caret>> [ over validate-loc ] (change-model)
248 over mark>> [ over validate-loc ] (change-model)
251 : caret/mark-changed ( model editor -- )
252 nip [ restart-blinking ] [ scroll>caret ] bi ;
254 M: editor model-changed
256 { [ 2dup model>> eq? ] [ contents-changed ] }
257 { [ 2dup caret>> eq? ] [ caret/mark-changed ] }
258 { [ 2dup mark>> eq? ] [ caret/mark-changed ] }
261 M: editor gadget-selection?
262 selection-start/end = not ;
264 M: editor gadget-selection
265 [ selection-start/end ] keep model>> doc-range ;
267 : remove-selection ( editor -- )
268 [ selection-start/end ] keep model>> remove-doc-range ;
270 M: editor user-input*
271 [ selection-start/end ] keep model>> set-doc-range t ;
273 : editor-string ( editor -- string )
276 : set-editor-string ( string editor -- )
277 model>> set-doc-string ;
279 M: editor gadget-text* editor-string % ;
281 : extend-selection ( editor -- )
284 dup caret>> click-loc ;
286 : mouse-elt ( -- element )
290 } at one-line-elt or ;
292 : drag-direction? ( loc editor -- ? )
293 editor-mark before? ;
295 : drag-selection-caret ( loc editor element -- loc )
297 [ drag-direction? ] 2keep model>>
298 ] dip prev/next-elt ? ;
300 : drag-selection-mark ( loc editor element -- loc )
302 [ drag-direction? not ] keep
303 [ editor-mark ] [ model>> ] bi
304 ] dip prev/next-elt ? ;
306 : drag-caret&mark ( editor -- caret mark )
307 dup clicked-loc swap mouse-elt
308 [ drag-selection-caret ] 3keep
309 drag-selection-mark ;
311 : drag-selection ( editor -- )
313 pick mark>> set-model
314 swap caret>> set-model ;
316 : editor-cut ( editor clipboard -- )
317 dupd gadget-copy remove-selection ;
319 : delete/backspace ( editor quot -- )
320 over gadget-selection? [
321 drop remove-selection
323 [ [ [ editor-caret ] [ model>> ] bi ] dip call ]
328 : editor-delete ( editor elt -- )
329 '[ dupd _ next-elt ] delete/backspace ;
331 : editor-backspace ( editor elt -- )
332 '[ over [ _ prev-elt ] dip ] delete/backspace ;
334 : editor-select-prev ( editor elt -- )
335 '[ _ prev-elt ] change-caret ;
337 : editor-prev ( editor elt -- )
338 dupd editor-select-prev mark>caret ;
340 : editor-select-next ( editor elt -- )
341 '[ _ next-elt ] change-caret ;
343 : editor-next ( editor elt -- )
344 dupd editor-select-next mark>caret ;
346 : editor-select ( from to editor -- )
347 tuck [ mark>> set-model ] [ caret>> set-model ] 2bi* ;
349 : select-elt ( editor elt -- )
350 [ [ [ editor-caret ] [ model>> ] bi ] dip prev/next-elt ] [ drop ] 2bi
353 : start-of-document ( editor -- ) doc-elt editor-prev ;
355 : end-of-document ( editor -- ) doc-elt editor-next ;
357 : position-caret ( editor -- )
358 mouse-elt dup one-char-elt =
359 [ drop dup extend-selection dup mark>> click-loc ]
362 : delete-next-character ( editor -- )
363 char-elt editor-delete ;
365 : delete-previous-character ( editor -- )
366 char-elt editor-backspace ;
368 : delete-previous-word ( editor -- )
369 word-elt editor-delete ;
371 : delete-next-word ( editor -- )
372 word-elt editor-backspace ;
374 : delete-to-start-of-line ( editor -- )
375 one-line-elt editor-delete ;
377 : delete-to-end-of-line ( editor -- )
378 one-line-elt editor-backspace ;
380 : com-undo ( editor -- )
383 : com-redo ( editor -- )
387 { undo-action com-undo }
388 { redo-action com-redo }
389 { T{ key-down f f "DELETE" } delete-next-character }
390 { T{ key-down f { S+ } "DELETE" } delete-next-character }
391 { T{ key-down f f "BACKSPACE" } delete-previous-character }
392 { T{ key-down f { S+ } "BACKSPACE" } delete-previous-character }
393 { T{ key-down f { C+ } "DELETE" } delete-previous-word }
394 { T{ key-down f { C+ } "BACKSPACE" } delete-next-word }
395 { T{ key-down f { A+ } "DELETE" } delete-to-start-of-line }
396 { T{ key-down f { A+ } "BACKSPACE" } delete-to-end-of-line }
399 : paste ( editor -- ) clipboard get paste-clipboard ;
401 : paste-selection ( editor -- ) selection get paste-clipboard ;
403 : cut ( editor -- ) clipboard get editor-cut ;
405 editor "clipboard" f {
406 { paste-action paste }
407 { copy-action com-copy }
409 { T{ button-up f f 2 } paste-selection }
410 { T{ button-up } com-copy-selection }
413 : previous-character ( editor -- )
414 dup gadget-selection? [
415 dup selection-start/end drop
416 over set-caret mark>caret
421 : next-character ( editor -- )
422 dup gadget-selection? [
423 dup selection-start/end nip
424 over set-caret mark>caret
429 : previous-word ( editor -- ) word-elt editor-prev ;
431 : next-word ( editor -- ) word-elt editor-next ;
433 : start-of-line ( editor -- ) one-line-elt editor-prev ;
435 : end-of-line ( editor -- ) one-line-elt editor-next ;
437 editor "caret-motion" f {
438 { T{ button-down } position-caret }
439 { T{ key-down f f "LEFT" } previous-character }
440 { T{ key-down f f "RIGHT" } next-character }
441 { T{ key-down f { C+ } "LEFT" } previous-word }
442 { T{ key-down f { C+ } "RIGHT" } next-word }
443 { T{ key-down f f "HOME" } start-of-line }
444 { T{ key-down f f "END" } end-of-line }
445 { T{ key-down f { C+ } "HOME" } start-of-document }
446 { T{ key-down f { C+ } "END" } end-of-document }
449 : clear-editor ( editor -- )
450 #! The with-datastack is a kludge to make it infer. Stupid.
451 model>> 1array [ clear-doc ] with-datastack drop ;
453 : select-all ( editor -- ) doc-elt select-elt ;
455 : select-line ( editor -- ) one-line-elt select-elt ;
457 : select-word ( editor -- ) one-word-elt select-elt ;
459 : selected-token ( editor -- string )
460 dup gadget-selection?
461 [ dup select-word ] unless
464 : select-previous-character ( editor -- )
465 char-elt editor-select-prev ;
467 : select-next-character ( editor -- )
468 char-elt editor-select-next ;
470 : select-previous-word ( editor -- )
471 word-elt editor-select-prev ;
473 : select-next-word ( editor -- )
474 word-elt editor-select-next ;
476 : select-start-of-line ( editor -- )
477 one-line-elt editor-select-prev ;
479 : select-end-of-line ( editor -- )
480 one-line-elt editor-select-next ;
482 : select-start-of-document ( editor -- )
483 doc-elt editor-select-prev ;
485 : select-end-of-document ( editor -- )
486 doc-elt editor-select-next ;
488 editor "selection" f {
489 { T{ button-down f { S+ } 1 } extend-selection }
490 { T{ drag } drag-selection }
491 { gain-focus focus-editor }
492 { lose-focus unfocus-editor }
493 { delete-action remove-selection }
494 { select-all-action select-all }
495 { T{ key-down f { C+ } "l" } select-line }
496 { T{ key-down f { S+ } "LEFT" } select-previous-character }
497 { T{ key-down f { S+ } "RIGHT" } select-next-character }
498 { T{ key-down f { S+ C+ } "LEFT" } select-previous-word }
499 { T{ key-down f { S+ C+ } "RIGHT" } select-next-word }
500 { T{ key-down f { S+ } "HOME" } select-start-of-line }
501 { T{ key-down f { S+ } "END" } select-end-of-line }
502 { T{ key-down f { S+ C+ } "HOME" } select-start-of-document }
503 { T{ key-down f { S+ C+ } "END" } select-end-of-document }
506 : editor-menu ( editor -- )
507 { cut com-copy paste } show-commands-menu ;
510 { T{ button-down f f 3 } editor-menu }
514 TUPLE: multiline-editor < editor ;
516 : <multiline-editor> ( -- editor )
517 multiline-editor new-editor ;
519 : previous-line ( editor -- ) line-elt editor-prev ;
521 : next-line ( editor -- ) line-elt editor-next ;
523 : select-previous-line ( editor -- )
524 line-elt editor-select-prev ;
526 : select-next-line ( editor -- )
527 line-elt editor-select-next ;
529 : insert-newline ( editor -- )
530 "\n" swap user-input* drop ;
532 : change-selection ( editor quot -- )
533 '[ gadget-selection @ ] keep user-input* drop ; inline
535 : join-lines ( string -- string' )
537 [ rest-slice [ [ blank? ] trim-head-slice ] change-each ]
538 [ but-last-slice [ [ blank? ] trim-tail-slice ] change-each ]
542 : this-line-and-next ( document line -- start end )
543 [ nip 0 swap 2array ]
544 [ [ nip 1+ ] [ 1+ swap doc-line length ] 2bi 2array ]
547 : last-line? ( document line -- ? )
548 [ last-line# ] dip = ;
550 : com-join-lines ( editor -- )
551 dup gadget-selection?
552 [ [ join-lines ] change-selection ] [
553 [ model>> ] [ editor-caret first ] bi
554 2dup last-line? [ 2drop ] [
555 [ this-line-and-next ] [ drop ] 2bi
556 [ join-lines ] change-doc-range
560 multiline-editor "multiline" f {
561 { T{ key-down f f "UP" } previous-line }
562 { T{ key-down f f "DOWN" } next-line }
563 { T{ key-down f { S+ } "UP" } select-previous-line }
564 { T{ key-down f { S+ } "DOWN" } select-next-line }
565 { T{ key-down f f "RET" } insert-newline }
566 { T{ key-down f { S+ } "RET" } insert-newline }
567 { T{ key-down f f "ENTER" } insert-newline }
568 { T{ key-down f { C+ } "j" } com-join-lines }
571 TUPLE: source-editor < multiline-editor ;
573 : <source-editor> ( -- editor )
574 source-editor new-editor ;
577 : <element-model> ( editor element -- model )
578 [ [ caret>> ] [ model>> ] bi ] dip
579 '[ _ _ elt-string ] <filter> ;
581 ! Fields wrap an editor
582 TUPLE: field < wrapper editor min-width max-width ;
584 : field-theme ( gadget -- gadget )
585 gray <solid> >>boundary ; inline
587 : <field-border> ( gadget -- border )
592 : new-field ( class -- gadget )
593 [ <editor> dup <field-border> ] dip new-wrapper swap >>editor ; inline
595 : column-width ( editor n -- width )
596 [ editor>> font>> ] [ CHAR: \s <string> ] bi* text-width ;
600 [ dup min-width>> dup [ column-width 0 2array vmax ] [ 2drop ] if ]
601 [ dup max-width>> dup [ column-width 1/0. 2array vmin ] [ 2drop ] if ]
604 TUPLE: model-field < field field-model ;
606 : <model-field> ( model -- gadget )
607 model-field new-field swap >>field-model ;
609 M: model-field graft*
610 [ [ field-model>> value>> ] [ editor>> ] bi set-editor-string ]
611 [ dup editor>> model>> add-connection ]
614 M: model-field ungraft*
615 dup editor>> model>> remove-connection ;
617 M: model-field model-changed
618 nip [ editor>> editor-string ] [ field-model>> ] bi set-model ;
620 TUPLE: action-field < field quot ;
622 : <action-field> ( quot -- gadget )
623 action-field new-field swap >>quot ;
625 : invoke-action-field ( field -- )
626 [ editor>> editor-string ]
627 [ editor>> clear-editor ]
632 { T{ key-down f f "RET" } [ invoke-action-field ] }