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