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