]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/gadgets/editors/editors.factor
ui.gadgets.editors: simplify private word.
[factor.git] / basis / ui / gadgets / editors / editors.factor
1 ! Copyright (C) 2006, 2009 Slava Pestov
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors timers arrays assocs calendar colors.constants
4 combinators combinators.short-circuit documents
5 documents.elements fry grouping kernel locals make math
6 math.functions math.order math.ranges math.rectangles
7 math.vectors models models.arrow namespaces opengl sequences
8 sorting splitting ui.baseline-alignment ui.clipboards
9 ui.commands ui.gadgets ui.gadgets.borders
10 ui.gadgets.line-support ui.gadgets.menus ui.gadgets.scrollers
11 ui.gestures ui.pens.solid ui.render ui.text unicode.categories ;
12 EXCLUDE: fonts => selection ;
13 IN: ui.gadgets.editors
14
15 TUPLE: editor < line-gadget
16 caret-color
17 caret mark
18 focused? blink blink-timer ;
19
20 <PRIVATE
21
22 : <loc> ( -- loc ) { 0 0 } <model> ;
23
24 : init-editor-locs ( editor -- editor )
25     <loc> >>caret
26     <loc> >>mark ; inline
27
28 : editor-theme ( editor -- editor )
29     COLOR: red >>caret-color
30     monospace-font >>font ; inline
31
32 PRIVATE>
33
34 : new-editor ( class -- editor )
35     new-line-gadget
36         <document> >>model
37         init-editor-locs
38         editor-theme ; inline
39
40 : <editor> ( -- editor )
41     editor new-editor ;
42
43 <PRIVATE
44
45 : activate-editor-model ( editor model -- )
46     [ add-connection ]
47     [ nip activate-model ]
48     [ swap model>> add-loc ] 2tri ;
49
50 : deactivate-editor-model ( editor model -- )
51     [ remove-connection ]
52     [ nip deactivate-model ]
53     [ swap model>> remove-loc ] 2tri ;
54
55 : blink-caret ( editor -- )
56     [ not ] change-blink relayout-1 ;
57
58 SYMBOL: blink-interval
59
60 750 milliseconds blink-interval set-global
61
62 : stop-blinking ( editor -- )
63     blink-timer>> [ stop-timer ] when* ;
64
65 : start-blinking ( editor -- )
66     t >>blink
67     blink-timer>> [ restart-timer ] when* ;
68
69 : restart-blinking ( editor -- )
70     dup focused?>> [
71         [ start-blinking ]
72         [ relayout-1 ]
73         bi
74     ] [ drop ] if ;
75
76 PRIVATE>
77
78 M: editor graft*
79     [ dup caret>> activate-editor-model ]
80     [ dup mark>> activate-editor-model ]
81     [
82         [
83             '[ _ blink-caret ] blink-interval get dup <timer>
84         ] keep blink-timer<<
85     ] tri ;
86
87 M: editor ungraft*
88     [ [ stop-blinking ] [ f >>blink-timer drop ] bi ]
89     [ dup caret>> deactivate-editor-model ]
90     [ dup mark>> deactivate-editor-model ] tri ;
91
92 : editor-caret ( editor -- loc ) caret>> value>> ;
93
94 : editor-mark ( editor -- loc ) mark>> value>> ;
95
96 : set-caret ( loc editor -- )
97     [ model>> validate-loc ] [ caret>> ] bi set-model ;
98
99 : change-caret ( editor quot -- )
100     [ [ [ editor-caret ] [ model>> ] bi ] dip call ] [ drop ] 2bi
101     set-caret ; inline
102
103 : mark>caret ( editor -- )
104     [ editor-caret ] [ mark>> ] bi set-model ;
105
106 : change-caret&mark ( editor quot -- )
107     [ change-caret ] [ drop mark>caret ] 2bi ; inline
108
109 : editor-line ( n editor -- str ) control-value nth ;
110
111 :: point>loc ( point editor -- loc )
112     point second editor y>line {
113         { [ dup 0 < ] [ drop { 0 0 } ] }
114         { [ dup editor model>> last-line# > ] [ drop editor model>> doc-end ] }
115         [| n |
116             n
117             point first
118             editor font>>
119             n editor editor-line
120             x>offset 2array
121         ]
122     } cond ;
123
124 : clicked-loc ( editor -- loc )
125     [ hand-rel ] keep point>loc ;
126
127 : click-loc ( editor model -- )
128     [ clicked-loc ] dip set-model ;
129
130 : focus-editor ( editor -- )
131     [ start-blinking ] [ t >>focused? relayout-1 ] bi ;
132
133 : unfocus-editor ( editor -- )
134     [ stop-blinking ] [ f >>focused? relayout-1 ] bi ;
135
136 : loc>x ( loc editor -- x )
137     [ first2 swap ] dip [ editor-line ] [ font>> ] bi swap offset>x round ;
138
139 : loc>point ( loc editor -- loc )
140     [ loc>x ] [ [ first ] dip line>y ceiling ] 2bi 2array ;
141
142 : caret-loc ( editor -- loc )
143     [ editor-caret ] keep loc>point ;
144
145 : caret-dim ( editor -- dim )
146     [ 0 ] dip line-height 2array ;
147
148 : scroll>caret ( editor -- )
149     dup graft-state>> second [
150         [
151             [ caret-loc ] [ caret-dim { 2 1 } v+ ] bi <rect>
152         ] keep scroll>rect
153     ] [ drop ] if ;
154
155 <PRIVATE
156
157 : draw-caret? ( editor -- ? )
158     { [ focused?>> ] [ blink>> ] } 1&& ;
159
160 : draw-caret ( editor -- )
161     dup draw-caret? [
162         [ caret-color>> gl-color ]
163         [
164             [ caret-loc ] [ caret-dim ] bi
165             over v+ gl-line
166         ] bi
167     ] [ drop ] if ;
168
169 : selection-start/end ( editor -- start end )
170     [ editor-mark ] [ editor-caret ] bi sort-pair ;
171
172 SYMBOL: selected-lines
173
174 TUPLE: selected-line start end first? last? ;
175
176 : compute-selection ( editor -- assoc )
177     dup gadget-selection? [
178         [ selection-start/end [ [ first ] bi@ [a,b] ] [ ] 2bi ]
179         [ model>> ] bi
180         '[ [ _ _ ] [ _ start/end-on-line ] bi 2array ] H{ } map>assoc
181     ] [ drop f ] if ;
182
183 :: draw-selection ( line pair editor -- )
184     pair [ editor font>> line offset>x ] map :> pair
185     editor selection-color>> gl-color
186     pair first 0 2array
187     pair second pair first - round 1 max editor line-height 2array
188     gl-fill-rect ;
189
190 : draw-unselected-line ( line editor -- )
191     font>> swap draw-text ;
192
193 : draw-selected-line ( line pair editor -- )
194     over all-equal? [
195         [ nip draw-unselected-line ] [ draw-selection ] 3bi
196     ] [
197         [ draw-selection ]
198         [
199             [ [ first2 ] [ selection-color>> ] bi* <selection> ]
200             [ draw-unselected-line ] bi
201         ] 3bi
202     ] if ;
203
204 PRIVATE>
205
206 M: editor draw-line ( line index editor -- )
207     [ selected-lines get at ] dip over
208     [ draw-selected-line ] [ nip draw-unselected-line ] if ;
209
210 M: editor draw-gadget*
211     dup compute-selection selected-lines [
212         [ draw-lines ] [ draw-caret ] bi
213     ] with-variable ;
214
215 M: editor pref-dim*
216     ! Add some space for the caret.
217     [ font>> ] [ control-value ] bi text-dim { 1 0 } v+ ;
218
219 M: editor baseline font>> font-metrics ascent>> ;
220
221 M: editor cap-height font>> font-metrics cap-height>> ;
222
223 <PRIVATE
224
225 : contents-changed ( model editor -- )
226     [ [ nip caret>> ] [ drop ] 2bi '[ _ validate-loc ] (change-model) ]
227     [ [ nip mark>> ] [ drop ] 2bi '[ _ validate-loc ] (change-model) ]
228     [ nip relayout ] 2tri ;
229
230 : caret/mark-changed ( editor -- )
231     [ restart-blinking ] keep scroll>caret ;
232
233 PRIVATE>
234
235 M: editor model-changed
236     {
237         { [ 2dup model>> eq? ] [ contents-changed ] }
238         { [ 2dup caret>> eq? ] [ nip caret/mark-changed ] }
239         { [ 2dup mark>> eq? ] [ nip caret/mark-changed ] }
240     } cond ;
241
242 M: editor gadget-selection?
243     selection-start/end = not ;
244
245 M: editor gadget-selection
246     [ selection-start/end ] [ model>> ] bi doc-range ;
247
248 : remove-selection ( editor -- )
249     [ selection-start/end ] [ model>> ] bi remove-doc-range ;
250
251 M: editor user-input*
252     [ selection-start/end ] [ model>> ] bi set-doc-range t ;
253
254 : editor-string ( editor -- string )
255     model>> doc-string ;
256
257 : set-editor-string ( string editor -- )
258     model>> set-doc-string ;
259
260 M: editor gadget-text* editor-string % ;
261
262 : extend-selection ( editor -- )
263     [ request-focus ]
264     [ restart-blinking ]
265     [ dup caret>> click-loc ] tri ;
266
267 : mouse-elt ( -- element )
268     hand-click# get {
269         { 1 one-char-elt }
270         { 2 one-word-elt }
271     } at one-line-elt or ;
272
273 : drag-direction? ( loc editor -- ? )
274     editor-mark before? ;
275
276 : drag-selection-caret ( loc editor element -- loc )
277     [
278         [ drag-direction? ] [ model>> ] 2bi
279     ] dip prev/next-elt ? ;
280
281 : drag-selection-mark ( loc editor element -- loc )
282     [
283         [ drag-direction? not ]
284         [ editor-mark ]
285         [ model>> ] tri
286     ] dip prev/next-elt ? ;
287
288 : drag-caret&mark ( editor -- caret mark )
289     [ clicked-loc ] [ mouse-elt ] bi
290     [ drag-selection-caret ]
291     [ drag-selection-mark ] 3bi ;
292
293 : drag-selection ( editor -- )
294     [ drag-caret&mark ]
295     [ mark>> set-model ]
296     [ caret>> set-model ] tri ;
297
298 : editor-cut ( editor clipboard -- )
299     [ gadget-copy ] [ drop remove-selection ] 2bi ;
300
301 : delete/backspace ( editor quot -- )
302     over gadget-selection? [
303         drop remove-selection
304     ] [
305         [ [ [ editor-caret ] [ model>> ] bi ] dip call ]
306         [ drop model>> ]
307         2bi remove-doc-range
308     ] if ; inline
309
310 : editor-delete ( editor elt -- )
311     '[ dupd _ next-elt ] delete/backspace ;
312
313 : editor-backspace ( editor elt -- )
314     '[ over [ _ prev-elt ] dip ] delete/backspace ;
315
316 : editor-select-prev ( editor elt -- )
317     '[ _ prev-elt ] change-caret ;
318
319 : editor-prev ( editor elt -- )
320     [ editor-select-prev ] [ drop mark>caret ] 2bi ;
321
322 : editor-select-next ( editor elt -- )
323     '[ _ next-elt ] change-caret ;
324
325 : editor-next ( editor elt -- )
326     dupd editor-select-next mark>caret ;
327
328 : editor-select ( from to editor -- )
329     [ mark>> set-model ] [ caret>> set-model ] bi-curry bi* ;
330
331 : select-elt ( editor elt -- )
332     [ [ [ editor-caret ] [ model>> ] bi ] dip prev/next-elt ] [ drop ] 2bi
333     editor-select ;
334
335 : start-of-document ( editor -- ) doc-elt editor-prev ;
336
337 : end-of-document ( editor -- ) doc-elt editor-next ;
338
339 : position-caret ( editor -- )
340     mouse-elt dup one-char-elt =
341     [ drop dup extend-selection dup mark>> click-loc ]
342     [ select-elt ] if ;
343
344 : delete-next-character ( editor -- ) 
345     char-elt editor-delete ;
346
347 : delete-previous-character ( editor -- ) 
348     char-elt editor-backspace ;
349
350 : delete-previous-word ( editor -- ) 
351     word-elt editor-delete ;
352
353 : delete-next-word ( editor -- ) 
354     word-elt editor-backspace ;
355
356 : delete-to-start-of-line ( editor -- ) 
357     one-line-elt editor-delete ;
358
359 : delete-to-end-of-line ( editor -- ) 
360     one-line-elt editor-backspace ;
361
362 : com-undo ( editor -- ) model>> undo ;
363
364 : com-redo ( editor -- ) model>> redo ;
365
366 editor "editing" f {
367     { undo-action com-undo }
368     { redo-action com-redo }
369     { T{ key-down f f "DELETE" } delete-next-character }
370     { T{ key-down f f "BACKSPACE" } delete-previous-character }
371     { T{ key-down f { S+ } "BACKSPACE" } delete-previous-character }
372     { T{ key-down f { C+ } "DELETE" } delete-previous-word }
373     { T{ key-down f { C+ } "BACKSPACE" } delete-next-word }
374     { T{ key-down f { A+ } "DELETE" } delete-to-start-of-line }
375     { T{ key-down f { A+ } "BACKSPACE" } delete-to-end-of-line }
376 } define-command-map
377
378 : com-paste ( editor -- ) clipboard get paste-clipboard ;
379
380 : paste-selection ( editor -- ) selection get paste-clipboard ;
381
382 : com-cut ( editor -- ) clipboard get editor-cut ;
383
384 editor "clipboard" f {
385     { cut-action com-cut }
386     { copy-action com-copy }
387     { paste-action com-paste }
388     { T{ button-up } com-copy-selection }
389     { T{ button-up f f 2 } paste-selection }
390 } define-command-map
391
392 : previous-character ( editor -- )
393     dup gadget-selection? [
394         dup selection-start/end drop
395         over set-caret mark>caret
396     ] [
397         char-elt editor-prev
398     ] if ;
399
400 : next-character ( editor -- )
401     dup gadget-selection? [
402         dup selection-start/end nip
403         over set-caret mark>caret
404     ] [
405         char-elt editor-next
406     ] if ;
407
408 : previous-word ( editor -- ) word-elt editor-prev ;
409
410 : next-word ( editor -- ) word-elt editor-next ;
411
412 : start-of-line ( editor -- ) one-line-elt editor-prev ;
413
414 : end-of-line ( editor -- ) one-line-elt editor-next ;
415
416 editor "caret-motion" f {
417     { T{ button-down } position-caret }
418     { T{ key-down f f "LEFT" } previous-character }
419     { T{ key-down f f "RIGHT" } next-character }
420     { T{ key-down f { C+ } "LEFT" } previous-word }
421     { T{ key-down f { C+ } "RIGHT" } next-word }
422     { T{ key-down f f "HOME" } start-of-line }
423     { T{ key-down f f "END" } end-of-line }
424     { T{ key-down f { C+ } "HOME" } start-of-document }
425     { T{ key-down f { C+ } "END" } end-of-document }
426 } define-command-map
427
428 : clear-editor ( editor -- )
429     model>> clear-doc ;
430
431 : select-all ( editor -- ) doc-elt select-elt ;
432
433 : select-line ( editor -- ) one-line-elt select-elt ;
434
435 : select-word ( editor -- ) one-word-elt select-elt ;
436
437 : selected-token ( editor -- string )
438     dup gadget-selection?
439     [ dup select-word ] unless
440     gadget-selection ;
441
442 : select-previous-character ( editor -- ) 
443     char-elt editor-select-prev ;
444
445 : select-next-character ( editor -- ) 
446     char-elt editor-select-next ;
447
448 : select-previous-word ( editor -- ) 
449     word-elt editor-select-prev ;
450
451 : select-next-word ( editor -- ) 
452     word-elt editor-select-next ;
453
454 : select-start-of-line ( editor -- ) 
455     one-line-elt editor-select-prev ;
456
457 : select-end-of-line ( editor -- ) 
458     one-line-elt editor-select-next ;
459
460 : select-start-of-document ( editor -- ) 
461     doc-elt editor-select-prev ;
462
463 : select-end-of-document ( editor -- ) 
464     doc-elt editor-select-next ;
465
466 editor "selection" f {
467     { T{ button-down f { S+ } 1 } extend-selection }
468     { T{ button-up f { S+ } 1 } com-copy-selection }
469     { T{ drag { # 1 } } drag-selection }
470     { gain-focus focus-editor }
471     { lose-focus unfocus-editor }
472     { delete-action remove-selection }
473     { select-all-action select-all }
474     { T{ key-down f { C+ } "l" } select-line }
475     { T{ key-down f { S+ } "LEFT" } select-previous-character }
476     { T{ key-down f { S+ } "RIGHT" } select-next-character }
477     { T{ key-down f { S+ C+ } "LEFT" } select-previous-word }
478     { T{ key-down f { S+ C+ } "RIGHT" } select-next-word }
479     { T{ key-down f { S+ } "HOME" } select-start-of-line }
480     { T{ key-down f { S+ } "END" } select-end-of-line }
481     { T{ key-down f { S+ C+ } "HOME" } select-start-of-document }
482     { T{ key-down f { S+ C+ } "END" } select-end-of-document }
483 } define-command-map
484
485 : editor-menu ( editor -- )
486     {
487         com-undo
488         com-redo
489         ----
490         com-cut
491         com-copy
492         com-paste
493     } show-commands-menu ;
494
495 editor "misc" f {
496     { T{ button-down f f 3 } editor-menu }
497 } define-command-map
498
499 ! Multi-line editors
500 TUPLE: multiline-editor < editor ;
501
502 : <multiline-editor> ( -- editor )
503     multiline-editor new-editor ;
504
505 : previous-line ( editor -- ) line-elt editor-prev ;
506
507 : next-line ( editor -- ) line-elt editor-next ;
508
509 <PRIVATE
510
511 : page-elt ( editor -- editor element ) dup visible-lines 1 - <page-elt> ;
512
513 PRIVATE>
514
515 : previous-page ( editor -- ) page-elt editor-prev ;
516
517 : next-page ( editor -- ) page-elt editor-next ;
518
519 : select-previous-line ( editor -- ) line-elt editor-select-prev ;
520
521 : select-next-line ( editor -- ) line-elt editor-select-next ;
522
523 : select-previous-page ( editor -- ) page-elt editor-select-prev ;
524
525 : select-next-page ( editor -- ) page-elt editor-select-next ;
526
527 : insert-newline ( editor -- )
528     "\n" swap user-input* drop ;
529
530 : change-selection ( editor quot -- )
531     '[ gadget-selection @ ] [ user-input* drop ] bi ; inline
532
533 <PRIVATE
534
535 : join-lines ( string -- string' )
536     "\n" split
537     [ rest-slice [ [ blank? ] trim-head-slice ] map! drop ]
538     [ but-last-slice [ [ blank? ] trim-tail-slice ] map! drop ]
539     [ " " join ]
540     tri ;
541
542 : last-line? ( document line -- ? )
543     [ last-line# ] dip = ;
544
545 : prev-line-and-this ( document line -- start end )
546     swap
547     [ drop 1 - 0 2array ]
548     [ [ drop ] [ doc-line length ] 2bi 2array ]
549     2bi ;
550
551 : join-with-prev ( document line -- )
552     [ prev-line-and-this ] [ drop ] 2bi
553     [ join-lines ] change-doc-range ;
554
555 : this-line-and-next ( document line -- start end )
556     swap
557     [ drop 0 2array ]
558     [ [ 1 + ] dip [ drop ] [ doc-line length ] 2bi 2array ]
559     2bi ;
560
561 : join-with-next ( document line -- )
562     [ this-line-and-next ] [ drop ] 2bi
563     [ join-lines ] change-doc-range ;
564
565 PRIVATE>
566
567 : com-join-lines ( editor -- )
568     dup gadget-selection?
569     [ [ join-lines ] change-selection ] [
570         [ model>> ] [ editor-caret first ] bi {
571             { [ over last-line# 0 = ] [ 2drop ] }
572             { [ 2dup last-line? ] [ join-with-prev ] }
573             [ join-with-next ]
574         } cond
575     ] if ;
576
577 multiline-editor "multiline" f {
578     { T{ key-down f f "UP" } previous-line }
579     { T{ key-down f f "DOWN" } next-line }
580     { T{ key-down f { S+ } "UP" } select-previous-line }
581     { T{ key-down f { S+ } "DOWN" } select-next-line }
582     { T{ key-down f f "PAGE_UP" } previous-page }
583     { T{ key-down f f "PAGE_DOWN" } next-page }
584     { T{ key-down f { S+ } "PAGE_UP" } select-previous-page }
585     { T{ key-down f { S+ } "PAGE_DOWN" } select-next-page }
586     { T{ key-down f f "RET" } insert-newline }
587     { T{ key-down f { S+ } "RET" } insert-newline }
588     { T{ key-down f f "ENTER" } insert-newline }
589     { T{ key-down f { C+ } "j" } com-join-lines }
590 } define-command-map
591
592 TUPLE: source-editor < multiline-editor ;
593
594 : <source-editor> ( -- editor )
595     source-editor new-editor ;
596
597 ! A useful model
598 : <element-model> ( editor element -- model )
599     [ [ caret>> ] [ model>> ] bi ] dip
600     '[ _ _ elt-string ] <arrow> ;
601
602 ! Fields wrap an editor
603 TUPLE: field < border editor min-cols max-cols ;
604
605 <PRIVATE
606
607 : field-theme ( gadget -- gadget )
608     { 2 2 } >>size
609     { 1 0 } >>fill
610     COLOR: gray <solid> >>boundary ; inline
611
612 : <field-border> ( gadget -- border )
613     border new-border field-theme ;
614
615 PRIVATE>
616
617 : new-field ( class -- gadget )
618     [ <editor> ] dip new-border
619         dup gadget-child >>editor
620         field-theme ; inline
621
622 ! For line-gadget-width
623 M: field font>> editor>> font>> ;
624
625 M: field pref-dim*
626     [ ]
627     [ editor>> pref-dim ]
628     [ [ line-gadget-width ] [ drop second ] 2bi 2array ]
629     tri border-pref-dim ;
630
631 TUPLE: model-field < field field-model ;
632
633 : <model-field> ( model -- gadget )
634     model-field new-field
635         swap >>field-model ;
636
637 M: model-field graft*
638     [ [ field-model>> value>> ] [ editor>> ] bi set-editor-string ]
639     [ dup editor>> model>> add-connection ]
640     bi ;
641
642 M: model-field ungraft*
643     dup editor>> model>> remove-connection ;
644
645 M: model-field model-changed
646     nip [ editor>> editor-string ] [ field-model>> ] bi set-model ;
647
648 TUPLE: action-field < field quot ;
649
650 : <action-field> ( quot -- gadget )
651     action-field new-field
652         swap >>quot ;
653
654 : invoke-action-field ( field -- )
655     [ editor>> editor-string ]
656     [ editor>> clear-editor ]
657     [ quot>> ]
658     tri call( string -- ) ;
659
660 action-field H{
661     { T{ key-down f f "RET" } [ invoke-action-field ] }
662 } set-gestures