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