]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/gadgets/editors/editors.factor
Add font-metrics hook
[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 ;
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     [ selection-start/end [ [ first ] bi@ [a,b] ] 2keep ] keep model>>
169     '[ [ _ _ ] keep _ start/end-on-line 2array ] H{ } map>assoc ;
170
171 M: editor draw-line ( line index editor -- )
172     [
173         [ selected-lines get at ] dip
174         '[ first2 _ selection-color>> <selection> ] when*
175     ] keep font>> swap draw-text ;
176
177 M: editor draw-gadget*
178     dup compute-selection selected-lines [
179         [ draw-lines ] [ draw-caret ] bi
180     ] with-variable ;
181
182 M: editor pref-dim*
183     [ font>> ] [ control-value ] bi text-dim ;
184
185 M: editor baseline
186     font>> font-metrics ascent>> ;
187
188 : contents-changed ( model editor -- )
189     swap
190     over caret>> [ over validate-loc ] (change-model)
191     over mark>> [ over validate-loc ] (change-model)
192     drop relayout ;
193
194 : caret/mark-changed ( model editor -- )
195     nip [ restart-blinking ] [ scroll>caret ] bi ;
196
197 M: editor model-changed
198     {
199         { [ 2dup model>> eq? ] [ contents-changed ] }
200         { [ 2dup caret>> eq? ] [ caret/mark-changed ] }
201         { [ 2dup mark>> eq? ] [ caret/mark-changed ] }
202     } cond ;
203
204 M: editor gadget-selection?
205     selection-start/end = not ;
206
207 M: editor gadget-selection
208     [ selection-start/end ] keep model>> doc-range ;
209
210 : remove-selection ( editor -- )
211     [ selection-start/end ] keep model>> remove-doc-range ;
212
213 M: editor user-input*
214     [ selection-start/end ] keep model>> set-doc-range t ;
215
216 : editor-string ( editor -- string )
217     model>> doc-string ;
218
219 : set-editor-string ( string editor -- )
220     model>> set-doc-string ;
221
222 M: editor gadget-text* editor-string % ;
223
224 : extend-selection ( editor -- )
225     dup request-focus
226     dup restart-blinking
227     dup caret>> click-loc ;
228
229 : mouse-elt ( -- element )
230     hand-click# get {
231         { 1 one-char-elt }
232         { 2 one-word-elt }
233     } at one-line-elt or ;
234
235 : drag-direction? ( loc editor -- ? )
236     editor-mark before? ;
237
238 : drag-selection-caret ( loc editor element -- loc )
239     [
240         [ drag-direction? ] 2keep model>>
241     ] dip prev/next-elt ? ;
242
243 : drag-selection-mark ( loc editor element -- loc )
244     [
245         [ drag-direction? not ] keep
246         [ editor-mark ] [ model>> ] bi
247     ] dip prev/next-elt ? ;
248
249 : drag-caret&mark ( editor -- caret mark )
250     dup clicked-loc swap mouse-elt
251     [ drag-selection-caret ] 3keep
252     drag-selection-mark ;
253
254 : drag-selection ( editor -- )
255     dup drag-caret&mark
256     pick mark>> set-model
257     swap caret>> set-model ;
258
259 : editor-cut ( editor clipboard -- )
260     [ gadget-copy ] [ drop remove-selection ] 2bi ;
261
262 : delete/backspace ( editor quot -- )
263     over gadget-selection? [
264         drop remove-selection
265     ] [
266         [ [ [ editor-caret ] [ model>> ] bi ] dip call ]
267         [ drop model>> ]
268         2bi remove-doc-range
269     ] if ; inline
270
271 : editor-delete ( editor elt -- )
272     '[ dupd _ next-elt ] delete/backspace ;
273
274 : editor-backspace ( editor elt -- )
275     '[ over [ _ prev-elt ] dip ] delete/backspace ;
276
277 : editor-select-prev ( editor elt -- )
278     '[ _ prev-elt ] change-caret ;
279
280 : editor-prev ( editor elt -- )
281     [ editor-select-prev ] [ drop mark>caret ] 2bi ;
282
283 : editor-select-next ( editor elt -- )
284     '[ _ next-elt ] change-caret ;
285
286 : editor-next ( editor elt -- )
287     dupd editor-select-next mark>caret ;
288
289 : editor-select ( from to editor -- )
290     [ mark>> set-model ] [ caret>> set-model ] bi-curry bi* ;
291
292 : select-elt ( editor elt -- )
293     [ [ [ editor-caret ] [ model>> ] bi ] dip prev/next-elt ] [ drop ] 2bi
294     editor-select ;
295
296 : start-of-document ( editor -- ) doc-elt editor-prev ;
297
298 : end-of-document ( editor -- ) doc-elt editor-next ;
299
300 : position-caret ( editor -- )
301     mouse-elt dup one-char-elt =
302     [ drop dup extend-selection dup mark>> click-loc ]
303     [ select-elt ] if ;
304
305 : delete-next-character ( editor -- ) 
306     char-elt editor-delete ;
307
308 : delete-previous-character ( editor -- ) 
309     char-elt editor-backspace ;
310
311 : delete-previous-word ( editor -- ) 
312     word-elt editor-delete ;
313
314 : delete-next-word ( editor -- ) 
315     word-elt editor-backspace ;
316
317 : delete-to-start-of-line ( editor -- ) 
318     one-line-elt editor-delete ;
319
320 : delete-to-end-of-line ( editor -- ) 
321     one-line-elt editor-backspace ;
322
323 : com-undo ( editor -- )
324     model>> undo ;
325
326 : com-redo ( editor -- )
327     model>> redo ;
328
329 editor "editing" f {
330     { undo-action com-undo }
331     { redo-action com-redo }
332     { T{ key-down f f "DELETE" } delete-next-character }
333     { T{ key-down f { S+ } "DELETE" } delete-next-character }
334     { T{ key-down f f "BACKSPACE" } delete-previous-character }
335     { T{ key-down f { S+ } "BACKSPACE" } delete-previous-character }
336     { T{ key-down f { C+ } "DELETE" } delete-previous-word }
337     { T{ key-down f { C+ } "BACKSPACE" } delete-next-word }
338     { T{ key-down f { A+ } "DELETE" } delete-to-start-of-line }
339     { T{ key-down f { A+ } "BACKSPACE" } delete-to-end-of-line }
340 } define-command-map
341
342 : paste ( editor -- ) clipboard get paste-clipboard ;
343
344 : paste-selection ( editor -- ) selection get paste-clipboard ;
345
346 : cut ( editor -- ) clipboard get editor-cut ;
347
348 editor "clipboard" f {
349     { paste-action paste }
350     { copy-action com-copy }
351     { cut-action cut }
352     { T{ button-up f f 2 } paste-selection }
353     { T{ button-up } com-copy-selection }
354 } define-command-map
355
356 : previous-character ( editor -- )
357     dup gadget-selection? [
358         dup selection-start/end drop
359         over set-caret mark>caret
360     ] [
361         char-elt editor-prev
362     ] if ;
363
364 : next-character ( editor -- )
365     dup gadget-selection? [
366         dup selection-start/end nip
367         over set-caret mark>caret
368     ] [
369         char-elt editor-next
370     ] if ;
371
372 : previous-word ( editor -- ) word-elt editor-prev ;
373
374 : next-word ( editor -- ) word-elt editor-next ;
375
376 : start-of-line ( editor -- ) one-line-elt editor-prev ;
377
378 : end-of-line ( editor -- ) one-line-elt editor-next ;
379
380 editor "caret-motion" f {
381     { T{ button-down } position-caret }
382     { T{ key-down f f "LEFT" } previous-character }
383     { T{ key-down f f "RIGHT" } next-character }
384     { T{ key-down f { C+ } "LEFT" } previous-word }
385     { T{ key-down f { C+ } "RIGHT" } next-word }
386     { T{ key-down f f "HOME" } start-of-line }
387     { T{ key-down f f "END" } end-of-line }
388     { T{ key-down f { C+ } "HOME" } start-of-document }
389     { T{ key-down f { C+ } "END" } end-of-document }
390 } define-command-map
391
392 : clear-editor ( editor -- )
393     #! The with-datastack is a kludge to make it infer. Stupid.
394     model>> 1array [ clear-doc ] with-datastack drop ;
395
396 : select-all ( editor -- ) doc-elt select-elt ;
397
398 : select-line ( editor -- ) one-line-elt select-elt ;
399
400 : select-word ( editor -- ) one-word-elt select-elt ;
401
402 : selected-token ( editor -- string )
403     dup gadget-selection?
404     [ dup select-word ] unless
405     gadget-selection ;
406
407 : select-previous-character ( editor -- ) 
408     char-elt editor-select-prev ;
409
410 : select-next-character ( editor -- ) 
411     char-elt editor-select-next ;
412
413 : select-previous-word ( editor -- ) 
414     word-elt editor-select-prev ;
415
416 : select-next-word ( editor -- ) 
417     word-elt editor-select-next ;
418
419 : select-start-of-line ( editor -- ) 
420     one-line-elt editor-select-prev ;
421
422 : select-end-of-line ( editor -- ) 
423     one-line-elt editor-select-next ;
424
425 : select-start-of-document ( editor -- ) 
426     doc-elt editor-select-prev ;
427
428 : select-end-of-document ( editor -- ) 
429     doc-elt editor-select-next ;
430
431 editor "selection" f {
432     { T{ button-down f { S+ } 1 } extend-selection }
433     { T{ drag } drag-selection }
434     { gain-focus focus-editor }
435     { lose-focus unfocus-editor }
436     { delete-action remove-selection }
437     { select-all-action select-all }
438     { T{ key-down f { C+ } "l" } select-line }
439     { T{ key-down f { S+ } "LEFT" } select-previous-character }
440     { T{ key-down f { S+ } "RIGHT" } select-next-character }
441     { T{ key-down f { S+ C+ } "LEFT" } select-previous-word }
442     { T{ key-down f { S+ C+ } "RIGHT" } select-next-word }
443     { T{ key-down f { S+ } "HOME" } select-start-of-line }
444     { T{ key-down f { S+ } "END" } select-end-of-line }
445     { T{ key-down f { S+ C+ } "HOME" } select-start-of-document }
446     { T{ key-down f { S+ C+ } "END" } select-end-of-document }
447 } define-command-map
448
449 : editor-menu ( editor -- )
450     { com-undo com-redo cut com-copy paste } show-commands-menu ;
451
452 editor "misc" f {
453     { T{ button-down f f 3 } editor-menu }
454 } define-command-map
455
456 ! Multi-line editors
457 TUPLE: multiline-editor < editor ;
458
459 : <multiline-editor> ( -- editor )
460     multiline-editor new-editor ;
461
462 : previous-line ( editor -- ) line-elt editor-prev ;
463
464 : next-line ( editor -- ) line-elt editor-next ;
465
466 : select-previous-line ( editor -- ) 
467     line-elt editor-select-prev ;
468
469 : select-next-line ( editor -- ) 
470     line-elt editor-select-next ;
471
472 : insert-newline ( editor -- )
473     "\n" swap user-input* drop ;
474
475 : change-selection ( editor quot -- )
476     '[ gadget-selection @ ] keep user-input* drop ; inline
477
478 : join-lines ( string -- string' )
479     "\n" split
480     [ rest-slice [ [ blank? ] trim-head-slice ] change-each ]
481     [ but-last-slice [ [ blank? ] trim-tail-slice ] change-each ]
482     [ " " join ]
483     tri ;
484
485 : this-line-and-next ( document line -- start end )
486     [ nip 0 swap 2array ]
487     [ [ nip 1+ ] [ 1+ swap doc-line length ] 2bi 2array ]
488     2bi ;
489
490 : last-line? ( document line -- ? )
491     [ last-line# ] dip = ;
492
493 : com-join-lines ( editor -- )
494     dup gadget-selection?
495     [ [ join-lines ] change-selection ] [
496         [ model>> ] [ editor-caret first ] bi
497         2dup last-line? [ 2drop ] [
498             [ this-line-and-next ] [ drop ] 2bi
499             [ join-lines ] change-doc-range
500         ] if
501     ] if ;
502
503 multiline-editor "multiline" f {
504     { T{ key-down f f "UP" } previous-line }
505     { T{ key-down f f "DOWN" } next-line }
506     { T{ key-down f { S+ } "UP" } select-previous-line }
507     { T{ key-down f { S+ } "DOWN" } select-next-line }
508     { T{ key-down f f "RET" } insert-newline }
509     { T{ key-down f { S+ } "RET" } insert-newline }
510     { T{ key-down f f "ENTER" } insert-newline }
511     { T{ key-down f { C+ } "j" } com-join-lines }
512 } define-command-map
513
514 TUPLE: source-editor < multiline-editor ;
515
516 : <source-editor> ( -- editor )
517     source-editor new-editor ;
518
519 ! A useful model
520 : <element-model> ( editor element -- model )
521     [ [ caret>> ] [ model>> ] bi ] dip
522     '[ _ _ elt-string ] <filter> ;
523
524 ! Fields wrap an editor
525 TUPLE: field < wrapper editor min-width max-width ;
526
527 : field-theme ( gadget -- gadget )
528     COLOR: gray <solid> >>boundary ; inline
529
530 : <field-border> ( gadget -- border )
531     { 2 2 } <border>
532         { 1 0 } >>fill
533         field-theme ;
534
535 : new-field ( class -- gadget )
536     [ <editor> dup <field-border> ] dip new-wrapper swap >>editor ; inline
537
538 : column-width ( editor n -- width )
539     [ editor>> font>> ] [ CHAR: \s <string> ] bi* text-width ;
540
541 M: field pref-dim*
542     [ call-next-method ]
543     [ dup min-width>> dup [ column-width 0 2array vmax ] [ 2drop ] if ]
544     [ dup max-width>> dup [ column-width 1/0. 2array vmin ] [ 2drop ] if ]
545     tri ;
546
547 TUPLE: model-field < field field-model ;
548
549 : <model-field> ( model -- gadget )
550     model-field new-field swap >>field-model ;
551
552 M: model-field graft*
553     [ [ field-model>> value>> ] [ editor>> ] bi set-editor-string ]
554     [ dup editor>> model>> add-connection ]
555     bi ;
556
557 M: model-field ungraft*
558     dup editor>> model>> remove-connection ;
559
560 M: model-field model-changed
561     nip [ editor>> editor-string ] [ field-model>> ] bi set-model ;
562
563 TUPLE: action-field < field quot ;
564
565 : <action-field> ( quot -- gadget )
566     action-field new-field swap >>quot ;
567
568 : invoke-action-field ( field -- )
569     [ editor>> editor-string ]
570     [ editor>> clear-editor ]
571     [ quot>> ]
572     tri call ;
573
574 action-field H{
575     { T{ key-down f f "RET" } [ invoke-action-field ] }
576 } set-gestures