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