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 math.ranges models models.filter namespaces locals fry make opengl
5 opengl.gl sequences strings math.vectors math.functions sorting colors
6 colors.constants combinators assocs math.order fry calendar alarms
7 continuations ui.clipboards ui.commands ui.gadgets ui.gadgets.borders
8 ui.gadgets.buttons ui.gadgets.labels ui.gadgets.scrollers
9 ui.gadgets.theme ui.gadgets.menus ui.gadgets.wrappers ui.render
10 ui.pens.solid ui.gadgets.line-support ui.text ui.gestures
11 math.rectangles splitting unicode.categories fonts ;
12 IN: ui.gadgets.editors
14 TUPLE: editor < gadget
15 font caret-color selection-color
17 focused? blink blink-alarm ;
19 : <loc> ( -- loc ) { 0 0 } <model> ;
21 : init-editor-locs ( editor -- editor )
25 : editor-theme ( editor -- editor )
26 COLOR: red >>caret-color
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 :: point>loc ( point editor -- loc )
105 point second editor y>line {
106 { [ dup 0 < ] [ drop { 0 0 } ] }
107 { [ dup editor model>> last-line# > ] [ drop editor model>> doc-end ] }
117 : clicked-loc ( editor -- loc )
118 [ hand-rel ] keep point>loc ;
120 : click-loc ( editor model -- )
121 [ clicked-loc ] dip set-model ;
123 : focus-editor ( editor -- )
124 [ start-blinking ] [ t >>focused? relayout-1 ] bi ;
126 : unfocus-editor ( editor -- )
127 [ stop-blinking ] [ f >>focused? relayout-1 ] bi ;
129 : loc>x ( loc editor -- x )
130 [ first2 swap ] dip [ editor-line ] [ font>> ] bi swap offset>x round ;
132 : loc>point ( loc editor -- loc )
133 [ loc>x ] [ [ first ] dip line>y ] 2bi 2array ;
135 : caret-loc ( editor -- loc )
136 [ editor-caret ] keep loc>point ;
138 : caret-dim ( editor -- dim )
139 line-height 0 swap 2array ;
141 : scroll>caret ( editor -- )
142 dup graft-state>> second [
144 [ caret-loc ] [ caret-dim { 1 0 } v+ ] bi <rect>
148 : draw-caret? ( editor -- ? )
149 [ focused?>> ] [ blink>> ] bi and ;
151 : draw-caret ( editor -- )
153 [ caret-color>> gl-color ]
155 [ caret-loc ] [ caret-dim ] bi
160 : selection-start/end ( editor -- start end )
161 [ editor-mark ] [ editor-caret ] bi sort-pair ;
163 SYMBOL: selected-lines
165 TUPLE: selected-line start end first? last? ;
167 : compute-selection ( editor -- assoc )
168 [ selection-start/end [ [ first ] bi@ [a,b] ] 2keep ] keep model>>
169 '[ [ _ _ ] keep _ start/end-on-line 2array ] H{ } map>assoc ;
171 M: editor draw-line ( line index editor -- )
173 [ selected-lines get at ] dip
174 '[ first2 _ selection-color>> <selection> ] when*
175 ] keep font>> swap draw-text ;
177 M: editor draw-gadget*
178 dup compute-selection selected-lines [
179 [ draw-lines ] [ draw-caret ] bi
183 [ font>> ] [ control-value ] bi text-dim ;
186 font>> "" line-metrics ascent>> ;
188 : contents-changed ( model editor -- )
190 over caret>> [ over validate-loc ] (change-model)
191 over mark>> [ over validate-loc ] (change-model)
194 : caret/mark-changed ( model editor -- )
195 nip [ restart-blinking ] [ scroll>caret ] bi ;
197 M: editor model-changed
199 { [ 2dup model>> eq? ] [ contents-changed ] }
200 { [ 2dup caret>> eq? ] [ caret/mark-changed ] }
201 { [ 2dup mark>> eq? ] [ caret/mark-changed ] }
204 M: editor gadget-selection?
205 selection-start/end = not ;
207 M: editor gadget-selection
208 [ selection-start/end ] keep model>> doc-range ;
210 : remove-selection ( editor -- )
211 [ selection-start/end ] keep model>> remove-doc-range ;
213 M: editor user-input*
214 [ selection-start/end ] keep model>> set-doc-range t ;
216 : editor-string ( editor -- string )
219 : set-editor-string ( string editor -- )
220 model>> set-doc-string ;
222 M: editor gadget-text* editor-string % ;
224 : extend-selection ( editor -- )
227 dup caret>> click-loc ;
229 : mouse-elt ( -- element )
233 } at one-line-elt or ;
235 : drag-direction? ( loc editor -- ? )
236 editor-mark before? ;
238 : drag-selection-caret ( loc editor element -- loc )
240 [ drag-direction? ] 2keep model>>
241 ] dip prev/next-elt ? ;
243 : drag-selection-mark ( loc editor element -- loc )
245 [ drag-direction? not ] keep
246 [ editor-mark ] [ model>> ] bi
247 ] dip prev/next-elt ? ;
249 : drag-caret&mark ( editor -- caret mark )
250 dup clicked-loc swap mouse-elt
251 [ drag-selection-caret ] 3keep
252 drag-selection-mark ;
254 : drag-selection ( editor -- )
256 pick mark>> set-model
257 swap caret>> set-model ;
259 : editor-cut ( editor clipboard -- )
260 [ gadget-copy ] [ drop remove-selection ] 2bi ;
262 : delete/backspace ( editor quot -- )
263 over gadget-selection? [
264 drop remove-selection
266 [ [ [ editor-caret ] [ model>> ] bi ] dip call ]
271 : editor-delete ( editor elt -- )
272 '[ dupd _ next-elt ] delete/backspace ;
274 : editor-backspace ( editor elt -- )
275 '[ over [ _ prev-elt ] dip ] delete/backspace ;
277 : editor-select-prev ( editor elt -- )
278 '[ _ prev-elt ] change-caret ;
280 : editor-prev ( editor elt -- )
281 [ editor-select-prev ] [ drop mark>caret ] 2bi ;
283 : editor-select-next ( editor elt -- )
284 '[ _ next-elt ] change-caret ;
286 : editor-next ( editor elt -- )
287 dupd editor-select-next mark>caret ;
289 : editor-select ( from to editor -- )
290 [ mark>> set-model ] [ caret>> set-model ] bi-curry bi* ;
292 : select-elt ( editor elt -- )
293 [ [ [ editor-caret ] [ model>> ] bi ] dip prev/next-elt ] [ drop ] 2bi
296 : start-of-document ( editor -- ) doc-elt editor-prev ;
298 : end-of-document ( editor -- ) doc-elt editor-next ;
300 : position-caret ( editor -- )
301 mouse-elt dup one-char-elt =
302 [ drop dup extend-selection dup mark>> click-loc ]
305 : delete-next-character ( editor -- )
306 char-elt editor-delete ;
308 : delete-previous-character ( editor -- )
309 char-elt editor-backspace ;
311 : delete-previous-word ( editor -- )
312 word-elt editor-delete ;
314 : delete-next-word ( editor -- )
315 word-elt editor-backspace ;
317 : delete-to-start-of-line ( editor -- )
318 one-line-elt editor-delete ;
320 : delete-to-end-of-line ( editor -- )
321 one-line-elt editor-backspace ;
323 : com-undo ( editor -- )
326 : com-redo ( editor -- )
330 { undo-action com-undo }
331 { redo-action com-redo }
332 { T{ key-down f f "DELETE" } delete-next-character }
333 { T{ key-down f { S+ } "DELETE" } delete-next-character }
334 { T{ key-down f f "BACKSPACE" } delete-previous-character }
335 { T{ key-down f { S+ } "BACKSPACE" } delete-previous-character }
336 { T{ key-down f { C+ } "DELETE" } delete-previous-word }
337 { T{ key-down f { C+ } "BACKSPACE" } delete-next-word }
338 { T{ key-down f { A+ } "DELETE" } delete-to-start-of-line }
339 { T{ key-down f { A+ } "BACKSPACE" } delete-to-end-of-line }
342 : paste ( editor -- ) clipboard get paste-clipboard ;
344 : paste-selection ( editor -- ) selection get paste-clipboard ;
346 : cut ( editor -- ) clipboard get editor-cut ;
348 editor "clipboard" f {
349 { paste-action paste }
350 { copy-action com-copy }
352 { T{ button-up f f 2 } paste-selection }
353 { T{ button-up } com-copy-selection }
356 : previous-character ( editor -- )
357 dup gadget-selection? [
358 dup selection-start/end drop
359 over set-caret mark>caret
364 : next-character ( editor -- )
365 dup gadget-selection? [
366 dup selection-start/end nip
367 over set-caret mark>caret
372 : previous-word ( editor -- ) word-elt editor-prev ;
374 : next-word ( editor -- ) word-elt editor-next ;
376 : start-of-line ( editor -- ) one-line-elt editor-prev ;
378 : end-of-line ( editor -- ) one-line-elt editor-next ;
380 editor "caret-motion" f {
381 { T{ button-down } position-caret }
382 { T{ key-down f f "LEFT" } previous-character }
383 { T{ key-down f f "RIGHT" } next-character }
384 { T{ key-down f { C+ } "LEFT" } previous-word }
385 { T{ key-down f { C+ } "RIGHT" } next-word }
386 { T{ key-down f f "HOME" } start-of-line }
387 { T{ key-down f f "END" } end-of-line }
388 { T{ key-down f { C+ } "HOME" } start-of-document }
389 { T{ key-down f { C+ } "END" } end-of-document }
392 : clear-editor ( editor -- )
393 #! The with-datastack is a kludge to make it infer. Stupid.
394 model>> 1array [ clear-doc ] with-datastack drop ;
396 : select-all ( editor -- ) doc-elt select-elt ;
398 : select-line ( editor -- ) one-line-elt select-elt ;
400 : select-word ( editor -- ) one-word-elt select-elt ;
402 : selected-token ( editor -- string )
403 dup gadget-selection?
404 [ dup select-word ] unless
407 : select-previous-character ( editor -- )
408 char-elt editor-select-prev ;
410 : select-next-character ( editor -- )
411 char-elt editor-select-next ;
413 : select-previous-word ( editor -- )
414 word-elt editor-select-prev ;
416 : select-next-word ( editor -- )
417 word-elt editor-select-next ;
419 : select-start-of-line ( editor -- )
420 one-line-elt editor-select-prev ;
422 : select-end-of-line ( editor -- )
423 one-line-elt editor-select-next ;
425 : select-start-of-document ( editor -- )
426 doc-elt editor-select-prev ;
428 : select-end-of-document ( editor -- )
429 doc-elt editor-select-next ;
431 editor "selection" f {
432 { T{ button-down f { S+ } 1 } extend-selection }
433 { T{ drag } drag-selection }
434 { gain-focus focus-editor }
435 { lose-focus unfocus-editor }
436 { delete-action remove-selection }
437 { select-all-action select-all }
438 { T{ key-down f { C+ } "l" } select-line }
439 { T{ key-down f { S+ } "LEFT" } select-previous-character }
440 { T{ key-down f { S+ } "RIGHT" } select-next-character }
441 { T{ key-down f { S+ C+ } "LEFT" } select-previous-word }
442 { T{ key-down f { S+ C+ } "RIGHT" } select-next-word }
443 { T{ key-down f { S+ } "HOME" } select-start-of-line }
444 { T{ key-down f { S+ } "END" } select-end-of-line }
445 { T{ key-down f { S+ C+ } "HOME" } select-start-of-document }
446 { T{ key-down f { S+ C+ } "END" } select-end-of-document }
449 : editor-menu ( editor -- )
450 { com-undo com-redo cut com-copy paste } show-commands-menu ;
453 { T{ button-down f f 3 } editor-menu }
457 TUPLE: multiline-editor < editor ;
459 : <multiline-editor> ( -- editor )
460 multiline-editor new-editor ;
462 : previous-line ( editor -- ) line-elt editor-prev ;
464 : next-line ( editor -- ) line-elt editor-next ;
466 : select-previous-line ( editor -- )
467 line-elt editor-select-prev ;
469 : select-next-line ( editor -- )
470 line-elt editor-select-next ;
472 : insert-newline ( editor -- )
473 "\n" swap user-input* drop ;
475 : change-selection ( editor quot -- )
476 '[ gadget-selection @ ] keep user-input* drop ; inline
478 : join-lines ( string -- string' )
480 [ rest-slice [ [ blank? ] trim-head-slice ] change-each ]
481 [ but-last-slice [ [ blank? ] trim-tail-slice ] change-each ]
485 : this-line-and-next ( document line -- start end )
486 [ nip 0 swap 2array ]
487 [ [ nip 1+ ] [ 1+ swap doc-line length ] 2bi 2array ]
490 : last-line? ( document line -- ? )
491 [ last-line# ] dip = ;
493 : com-join-lines ( editor -- )
494 dup gadget-selection?
495 [ [ join-lines ] change-selection ] [
496 [ model>> ] [ editor-caret first ] bi
497 2dup last-line? [ 2drop ] [
498 [ this-line-and-next ] [ drop ] 2bi
499 [ join-lines ] change-doc-range
503 multiline-editor "multiline" f {
504 { T{ key-down f f "UP" } previous-line }
505 { T{ key-down f f "DOWN" } next-line }
506 { T{ key-down f { S+ } "UP" } select-previous-line }
507 { T{ key-down f { S+ } "DOWN" } select-next-line }
508 { T{ key-down f f "RET" } insert-newline }
509 { T{ key-down f { S+ } "RET" } insert-newline }
510 { T{ key-down f f "ENTER" } insert-newline }
511 { T{ key-down f { C+ } "j" } com-join-lines }
514 TUPLE: source-editor < multiline-editor ;
516 : <source-editor> ( -- editor )
517 source-editor new-editor ;
520 : <element-model> ( editor element -- model )
521 [ [ caret>> ] [ model>> ] bi ] dip
522 '[ _ _ elt-string ] <filter> ;
524 ! Fields wrap an editor
525 TUPLE: field < wrapper editor min-width max-width ;
527 : field-theme ( gadget -- gadget )
528 COLOR: gray <solid> >>boundary ; inline
530 : <field-border> ( gadget -- border )
535 : new-field ( class -- gadget )
536 [ <editor> dup <field-border> ] dip new-wrapper swap >>editor ; inline
538 : column-width ( editor n -- width )
539 [ editor>> font>> ] [ CHAR: \s <string> ] bi* text-width ;
543 [ dup min-width>> dup [ column-width 0 2array vmax ] [ 2drop ] if ]
544 [ dup max-width>> dup [ column-width 1/0. 2array vmin ] [ 2drop ] if ]
547 TUPLE: model-field < field field-model ;
549 : <model-field> ( model -- gadget )
550 model-field new-field swap >>field-model ;
552 M: model-field graft*
553 [ [ field-model>> value>> ] [ editor>> ] bi set-editor-string ]
554 [ dup editor>> model>> add-connection ]
557 M: model-field ungraft*
558 dup editor>> model>> remove-connection ;
560 M: model-field model-changed
561 nip [ editor>> editor-string ] [ field-model>> ] bi set-model ;
563 TUPLE: action-field < field quot ;
565 : <action-field> ( quot -- gadget )
566 action-field new-field swap >>quot ;
568 : invoke-action-field ( field -- )
569 [ editor>> editor-string ]
570 [ editor>> clear-editor ]
575 { T{ key-down f f "RET" } [ invoke-action-field ] }