]> 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.theme ui.gadgets.menus ui.gadgets.wrappers ui.render
10 ui.pens.solid ui.gadgets.line-support ui.text ui.gestures
11 math.rectangles splitting unicode.categories fonts grouping ;
12 IN: ui.gadgets.editors
13
14 TUPLE: editor < gadget
15 font caret-color selection-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     selection-color >>selection-color
28     monospace-font >>font ; inline
29
30 : new-editor ( class -- editor )
31     new-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 ] 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-empty-selection ( line pair editor -- )
174     editor font>> :> font
175     pair first font line offset>x 0 2array [
176         editor selection-color>> gl-color
177         1 font font-metrics height>> 2array gl-fill-rect
178     ] with-translation ;
179
180 : draw-unselected-line ( line editor -- )
181     font>> swap draw-text ;
182
183 : draw-selected-line ( line pair editor -- )
184     over all-equal? [
185         [ nip draw-unselected-line ] [ draw-empty-selection ] 3bi
186     ] [
187         [ [ first2 ] [ selection-color>> ] bi* <selection> ] keep
188         draw-unselected-line
189     ] if ;
190
191 M: editor draw-line ( line index editor -- )
192     [ selected-lines get at ] dip over
193     [ draw-selected-line ] [ nip draw-unselected-line ] if ;
194
195 M: editor draw-gadget*
196     dup compute-selection selected-lines [
197         [ draw-lines ] [ draw-caret ] bi
198     ] with-variable ;
199
200 M: editor pref-dim*
201     [ font>> ] [ control-value ] bi text-dim ;
202
203 M: editor baseline
204     font>> font-metrics ascent>> ;
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 : paste ( editor -- ) clipboard get paste-clipboard ;
361
362 : paste-selection ( editor -- ) selection get paste-clipboard ;
363
364 : cut ( editor -- ) clipboard get editor-cut ;
365
366 editor "clipboard" f {
367     { paste-action paste }
368     { copy-action com-copy }
369     { cut-action 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     { com-undo com-redo cut com-copy paste } show-commands-menu ;
469
470 editor "misc" f {
471     { T{ button-down f f 3 } editor-menu }
472 } define-command-map
473
474 ! Multi-line editors
475 TUPLE: multiline-editor < editor ;
476
477 : <multiline-editor> ( -- editor )
478     multiline-editor new-editor ;
479
480 : previous-line ( editor -- ) line-elt editor-prev ;
481
482 : next-line ( editor -- ) line-elt editor-next ;
483
484 : select-previous-line ( editor -- ) 
485     line-elt editor-select-prev ;
486
487 : select-next-line ( editor -- ) 
488     line-elt editor-select-next ;
489
490 : insert-newline ( editor -- )
491     "\n" swap user-input* drop ;
492
493 : change-selection ( editor quot -- )
494     '[ gadget-selection @ ] keep user-input* drop ; inline
495
496 : join-lines ( string -- string' )
497     "\n" split
498     [ rest-slice [ [ blank? ] trim-head-slice ] change-each ]
499     [ but-last-slice [ [ blank? ] trim-tail-slice ] change-each ]
500     [ " " join ]
501     tri ;
502
503 : this-line-and-next ( document line -- start end )
504     [ nip 0 swap 2array ]
505     [ [ nip 1+ ] [ 1+ swap doc-line length ] 2bi 2array ]
506     2bi ;
507
508 : last-line? ( document line -- ? )
509     [ last-line# ] dip = ;
510
511 : com-join-lines ( editor -- )
512     dup gadget-selection?
513     [ [ join-lines ] change-selection ] [
514         [ model>> ] [ editor-caret first ] bi
515         2dup last-line? [ 2drop ] [
516             [ this-line-and-next ] [ drop ] 2bi
517             [ join-lines ] change-doc-range
518         ] if
519     ] if ;
520
521 multiline-editor "multiline" f {
522     { T{ key-down f f "UP" } previous-line }
523     { T{ key-down f f "DOWN" } next-line }
524     { T{ key-down f { S+ } "UP" } select-previous-line }
525     { T{ key-down f { S+ } "DOWN" } select-next-line }
526     { T{ key-down f f "RET" } insert-newline }
527     { T{ key-down f { S+ } "RET" } insert-newline }
528     { T{ key-down f f "ENTER" } insert-newline }
529     { T{ key-down f { C+ } "j" } com-join-lines }
530 } define-command-map
531
532 TUPLE: source-editor < multiline-editor ;
533
534 : <source-editor> ( -- editor )
535     source-editor new-editor ;
536
537 ! A useful model
538 : <element-model> ( editor element -- model )
539     [ [ caret>> ] [ model>> ] bi ] dip
540     '[ _ _ elt-string ] <filter> ;
541
542 ! Fields wrap an editor
543 TUPLE: field < wrapper editor min-width max-width ;
544
545 : field-theme ( gadget -- gadget )
546     COLOR: gray <solid> >>boundary ; inline
547
548 : <field-border> ( gadget -- border )
549     { 2 2 } <border>
550         { 1 0 } >>fill
551         field-theme ;
552
553 : new-field ( class -- gadget )
554     [ <editor> dup <field-border> ] dip new-wrapper swap >>editor ; inline
555
556 : column-width ( editor n -- width )
557     [ editor>> font>> ] [ CHAR: \s <string> ] bi* text-width ;
558
559 M: field pref-dim*
560     [ call-next-method ]
561     [ dup min-width>> dup [ column-width 0 2array vmax ] [ 2drop ] if ]
562     [ dup max-width>> dup [ column-width 1/0. 2array vmin ] [ 2drop ] if ]
563     tri ;
564
565 TUPLE: model-field < field field-model ;
566
567 : <model-field> ( model -- gadget )
568     model-field new-field swap >>field-model ;
569
570 M: model-field graft*
571     [ [ field-model>> value>> ] [ editor>> ] bi set-editor-string ]
572     [ dup editor>> model>> add-connection ]
573     bi ;
574
575 M: model-field ungraft*
576     dup editor>> model>> remove-connection ;
577
578 M: model-field model-changed
579     nip [ editor>> editor-string ] [ field-model>> ] bi set-model ;
580
581 TUPLE: action-field < field quot ;
582
583 : <action-field> ( quot -- gadget )
584     action-field new-field swap >>quot ;
585
586 : invoke-action-field ( field -- )
587     [ editor>> editor-string ]
588     [ editor>> clear-editor ]
589     [ quot>> ]
590     tri call ;
591
592 action-field H{
593     { T{ key-down f f "RET" } [ invoke-action-field ] }
594 } set-gestures