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