]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/gadgets/editors/editors.factor
Merge branch 'master' into experimental (untested!)
[factor.git] / basis / ui / gadgets / editors / editors.factor
1 ! Copyright (C) 2006, 2008 Slava Pestov
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays documents kernel math models
4 namespaces locals fry make opengl opengl.gl sequences strings
5 io.styles math.vectors sorting colors combinators assocs
6 math.order fry calendar alarms ui.clipboards ui.commands
7 ui.gadgets ui.gadgets.borders ui.gadgets.buttons
8 ui.gadgets.labels ui.gadgets.scrollers ui.gadgets.theme
9 ui.gadgets.menus ui.gadgets.wrappers ui.render ui.gestures
10 math.geometry.rect ;
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 : editor-font* ( editor -- font ) font>> open-font ;
105
106 : line-height ( editor -- n )
107     editor-font* "" string-height ;
108
109 : y>line ( y editor -- line# )
110     line-height / >fixnum ;
111
112 :: point>loc ( point editor -- loc )
113     point second editor y>line {
114         { [ dup 0 < ] [ drop { 0 0 } ] }
115         { [ dup editor model>> last-line# > ] [ drop editor model>> doc-end ] }
116         [| n |
117             n
118             point first
119             editor editor-font*
120             n editor editor-line
121             x>offset 2array
122         ]
123     } cond ;
124
125 : clicked-loc ( editor -- loc )
126     [ hand-rel ] keep point>loc ;
127
128 : click-loc ( editor model -- )
129     [ clicked-loc ] dip set-model ;
130
131 : focus-editor ( editor -- )
132     dup start-blinking
133     t >>focused?
134     relayout-1 ;
135
136 : unfocus-editor ( editor -- )
137     dup stop-blinking
138     f >>focused?
139     relayout-1 ;
140
141 : offset>x ( col# line# editor -- x )
142     [ editor-line ] keep editor-font* spin head-slice string-width ;
143
144 : loc>x ( loc editor -- x ) [ first2 swap ] dip offset>x ;
145
146 : line>y ( lines# editor -- y )
147     line-height * ;
148
149 : caret-loc ( editor -- loc )
150     [ editor-caret* ] keep
151     [ loc>x ] [ [ first ] dip line>y ] 2bi 2array ;
152
153 : caret-dim ( editor -- dim )
154     line-height 0 swap 2array ;
155
156 : scroll>caret ( editor -- )
157     dup graft-state>> second [
158         [
159             [ caret-loc ] [ caret-dim { 1 0 } v+ ] bi <rect>
160         ] keep scroll>rect
161     ] [ drop ] if ;
162
163 : draw-caret ( -- )
164     editor get [ focused?>> ] [ blink>> ] bi and [
165         editor get
166         [ caret-color>> gl-color ]
167         [
168             dup caret-loc origin get v+
169             swap caret-dim over v+
170             gl-line
171         ] bi
172     ] when ;
173
174 : line-translation ( n -- loc )
175     editor get line-height * 0.0 swap 2array ;
176
177 : translate-lines ( n -- )
178     line-translation gl-translate ;
179
180 : draw-line ( editor str -- )
181     [ font>> ] dip { 0 0 } draw-string ;
182
183 : first-visible-line ( editor -- n )
184     [
185         [ clip get rect-loc second origin get second - ] dip
186         y>line
187     ] keep model>> validate-line ;
188
189 : last-visible-line ( editor -- n )
190     [
191         [ clip get rect-extent nip second origin get second - ] dip
192         y>line
193     ] keep model>> validate-line 1+ ;
194
195 : with-editor ( editor quot -- )
196     [
197         swap
198         dup first-visible-line \ first-visible-line set
199         dup last-visible-line \ last-visible-line set
200         dup model>> document set
201         editor set
202         call
203     ] with-scope ; inline
204
205 : visible-lines ( editor -- seq )
206     [ \ first-visible-line get \ last-visible-line get ] dip
207     control-value <slice> ;
208
209 : with-editor-translation ( n quot -- )
210     [ line-translation origin get v+ ] dip with-translation ;
211     inline
212
213 : draw-lines ( -- )
214     \ first-visible-line get [
215         editor get dup color>> gl-color
216         dup visible-lines
217         [ draw-line 1 translate-lines ] with each
218     ] with-editor-translation ;
219
220 : selection-start/end ( editor -- start end )
221     [ editor-mark* ] [ editor-caret* ] bi sort-pair ;
222
223 : (draw-selection) ( x1 x2 -- )
224     over -
225     dup 0 = [ 2 + ] when
226     [ 0.0 2array ] [ editor get line-height 2array ] bi*
227     swap [ gl-fill-rect ] with-translation ;
228
229 : draw-selected-line ( start end n -- )
230     [ start/end-on-line ] keep
231     tuck [ editor get offset>x ] 2bi@
232     (draw-selection) ;
233
234 : draw-selection ( -- )
235     editor get selection-color>> gl-color
236     editor get selection-start/end
237     over first [
238         2dup '[
239             [ _ _ ] dip
240             draw-selected-line
241             1 translate-lines
242         ] each-line
243     ] with-editor-translation ;
244
245 M: editor draw-gadget*
246     [ draw-selection draw-lines draw-caret ] with-editor ;
247
248 M: editor pref-dim*
249     dup editor-font* swap control-value text-dim ;
250
251 : contents-changed ( model editor -- )
252     swap
253     over caret>> [ over validate-loc ] (change-model)
254     over mark>> [ over validate-loc ] (change-model)
255     drop relayout ;
256
257 : caret/mark-changed ( model editor -- )
258     nip [ restart-blinking ] [ scroll>caret ] bi ;
259
260 M: editor model-changed
261     {
262         { [ 2dup model>> eq? ] [ contents-changed ] }
263         { [ 2dup caret>> eq? ] [ caret/mark-changed ] }
264         { [ 2dup mark>> eq? ] [ caret/mark-changed ] }
265     } cond ;
266
267 M: editor gadget-selection?
268     selection-start/end = not ;
269
270 M: editor gadget-selection
271     [ selection-start/end ] keep model>> doc-range ;
272
273 : remove-selection ( editor -- )
274     [ selection-start/end ] keep model>> remove-doc-range ;
275
276 M: editor user-input*
277     [ selection-start/end ] keep model>> set-doc-range t ;
278
279 : editor-string ( editor -- string )
280     model>> doc-string ;
281
282 : set-editor-string ( string editor -- )
283     model>> set-doc-string ;
284
285 M: editor gadget-text* editor-string % ;
286
287 : extend-selection ( editor -- )
288     dup request-focus
289     dup restart-blinking
290     dup caret>> click-loc ;
291
292 : mouse-elt ( -- element )
293     hand-click# get {
294         { 1 T{ one-char-elt } }
295         { 2 T{ one-word-elt } }
296     } at T{ one-line-elt } or ;
297
298 : drag-direction? ( loc editor -- ? )
299     editor-mark* before? ;
300
301 : drag-selection-caret ( loc editor element -- loc )
302     [
303         [ drag-direction? ] 2keep model>>
304     ] dip prev/next-elt ? ;
305
306 : drag-selection-mark ( loc editor element -- loc )
307     [
308         [ drag-direction? not ] keep
309         [ editor-mark* ] [ model>> ] bi
310     ] dip prev/next-elt ? ;
311
312 : drag-caret&mark ( editor -- caret mark )
313     dup clicked-loc swap mouse-elt
314     [ drag-selection-caret ] 3keep
315     drag-selection-mark ;
316
317 : drag-selection ( editor -- )
318     dup drag-caret&mark
319     pick mark>> set-model
320     swap caret>> set-model ;
321
322 : editor-cut ( editor clipboard -- )
323     dupd gadget-copy remove-selection ;
324
325 : delete/backspace ( editor quot -- )
326     over gadget-selection? [
327         drop remove-selection
328     ] [
329         [ [ [ editor-caret* ] [ model>> ] bi ] dip call ]
330         [ drop model>> ]
331         2bi remove-doc-range
332     ] if ; inline
333
334 : editor-delete ( editor elt -- )
335     '[ dupd _ next-elt ] delete/backspace ;
336
337 : editor-backspace ( editor elt -- )
338     '[ over [ _ prev-elt ] dip ] delete/backspace ;
339
340 : editor-select-prev ( editor elt -- )
341     '[ _ prev-elt ] change-caret ;
342
343 : editor-prev ( editor elt -- )
344     dupd editor-select-prev mark>caret ;
345
346 : editor-select-next ( editor elt -- )
347     '[ _ next-elt ] change-caret ;
348
349 : editor-next ( editor elt -- )
350     dupd editor-select-next mark>caret ;
351
352 : editor-select ( from to editor -- )
353     tuck caret>> set-model mark>> set-model ;
354
355 : select-elt ( editor elt -- )
356     [ [ [ editor-caret* ] [ model>> ] bi ] dip prev/next-elt ] [ drop ] 2bi
357     editor-select ;
358
359 : start-of-document ( editor -- ) T{ doc-elt } editor-prev ;
360
361 : end-of-document ( editor -- ) T{ doc-elt } editor-next ;
362
363 : position-caret ( editor -- )
364     mouse-elt dup T{ one-char-elt } =
365     [ drop dup extend-selection dup mark>> click-loc ]
366     [ select-elt ] if ;
367
368 : insert-newline ( editor -- ) "\n" swap user-input* drop ;
369
370 : delete-next-character ( editor -- ) 
371     T{ char-elt } editor-delete ;
372
373 : delete-previous-character ( editor -- ) 
374     T{ char-elt } editor-backspace ;
375
376 : delete-previous-word ( editor -- ) 
377     T{ word-elt } editor-delete ;
378
379 : delete-next-word ( editor -- ) 
380     T{ word-elt } editor-backspace ;
381
382 : delete-to-start-of-line ( editor -- ) 
383     T{ one-line-elt } editor-delete ;
384
385 : delete-to-end-of-line ( editor -- ) 
386     T{ one-line-elt } editor-backspace ;
387
388 editor "general" f {
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     { T{ paste-action } paste }
407     { T{ button-up f f 2 } paste-selection }
408     { T{ copy-action } com-copy }
409     { T{ button-up } com-copy-selection }
410     { T{ cut-action } cut }
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         T{ 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         T{ char-elt } editor-next
427     ] if ;
428
429 : previous-line ( editor -- ) T{ line-elt } editor-prev ;
430
431 : next-line ( editor -- ) T{ line-elt } editor-next ;
432
433 : previous-word ( editor -- ) T{ word-elt } editor-prev ;
434
435 : next-word ( editor -- ) T{ word-elt } editor-next ;
436
437 : start-of-line ( editor -- ) T{ one-line-elt } editor-prev ;
438
439 : end-of-line ( editor -- ) T{ one-line-elt } editor-next ;
440
441 editor "caret-motion" f {
442     { T{ button-down } position-caret }
443     { T{ key-down f f "LEFT" } previous-character }
444     { T{ key-down f f "RIGHT" } next-character }
445     { T{ key-down f f "UP" } previous-line }
446     { T{ key-down f f "DOWN" } next-line }
447     { T{ key-down f { C+ } "LEFT" } previous-word }
448     { T{ key-down f { C+ } "RIGHT" } next-word }
449     { T{ key-down f f "HOME" } start-of-line }
450     { T{ key-down f f "END" } end-of-line }
451     { T{ key-down f { C+ } "HOME" } start-of-document }
452     { T{ key-down f { C+ } "END" } end-of-document }
453 } define-command-map
454
455 : select-all ( editor -- ) T{ doc-elt } select-elt ;
456
457 : select-line ( editor -- ) T{ one-line-elt } select-elt ;
458
459 : select-word ( editor -- ) T{ one-word-elt } select-elt ;
460
461 : selected-word ( editor -- string )
462     dup gadget-selection?
463     [ dup select-word ] unless
464     gadget-selection ;
465
466 : select-previous-character ( editor -- ) 
467     T{ char-elt } editor-select-prev ;
468
469 : select-next-character ( editor -- ) 
470     T{ char-elt } editor-select-next ;
471
472 : select-previous-line ( editor -- ) 
473     T{ line-elt } editor-select-prev ;
474
475 : select-next-line ( editor -- ) 
476     T{ line-elt } editor-select-next ;
477
478 : select-previous-word ( editor -- ) 
479     T{ word-elt } editor-select-prev ;
480
481 : select-next-word ( editor -- ) 
482     T{ word-elt } editor-select-next ;
483
484 : select-start-of-line ( editor -- ) 
485     T{ one-line-elt } editor-select-prev ;
486
487 : select-end-of-line ( editor -- ) 
488     T{ one-line-elt } editor-select-next ;
489
490 : select-start-of-document ( editor -- ) 
491     T{ doc-elt } editor-select-prev ;
492
493 : select-end-of-document ( editor -- ) 
494     T{ doc-elt } editor-select-next ;
495
496 editor "selection" f {
497     { T{ button-down f { S+ } 1 } extend-selection }
498     { T{ drag } drag-selection }
499     { T{ gain-focus } focus-editor }
500     { T{ lose-focus } unfocus-editor }
501     { T{ delete-action } remove-selection }
502     { T{ select-all-action } select-all }
503     { T{ key-down f { C+ } "l" } select-line }
504     { T{ key-down f { S+ } "LEFT" } select-previous-character }
505     { T{ key-down f { S+ } "RIGHT" } select-next-character }
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 { S+ C+ } "LEFT" } select-previous-word }
509     { T{ key-down f { S+ C+ } "RIGHT" } select-next-word }
510     { T{ key-down f { S+ } "HOME" } select-start-of-line }
511     { T{ key-down f { S+ } "END" } select-end-of-line }
512     { T{ key-down f { S+ C+ } "HOME" } select-start-of-document }
513     { T{ key-down f { S+ C+ } "END" } select-end-of-document }
514 } define-command-map
515
516 : editor-menu ( editor -- )
517     { cut com-copy paste } show-commands-menu ;
518
519 editor "misc" f {
520     { T{ button-down f f 3 } editor-menu }
521 } define-command-map
522
523 ! Multi-line editors
524 TUPLE: multiline-editor < editor ;
525
526 : <multiline-editor> ( -- editor )
527     multiline-editor new-editor ;
528
529 multiline-editor "general" f {
530     { T{ key-down f f "RET" } insert-newline }
531     { T{ key-down f { S+ } "RET" } insert-newline }
532     { T{ key-down f f "ENTER" } insert-newline }
533 } define-command-map
534
535 TUPLE: source-editor < multiline-editor ;
536
537 : <source-editor> ( -- editor )
538     source-editor new-editor ;
539
540 ! Fields wrap an editor and edit an external model
541 TUPLE: field < wrapper field-model editor ;
542
543 : field-theme ( gadget -- gadget )
544     gray <solid> >>boundary ; inline
545
546 : <field-border> ( gadget -- border )
547     2 <border>
548         { 1 0 } >>fill
549         field-theme ;
550
551 : <field> ( model -- gadget )
552     <editor> dup <field-border> field new-wrapper
553         swap >>editor
554         swap >>field-model ;
555
556 M: field graft*
557     [ [ field-model>> value>> ] [ editor>> ] bi set-editor-string ]
558     [ dup editor>> model>> add-connection ]
559     bi ;
560
561 M: field ungraft*
562     dup editor>> model>> remove-connection ;
563
564 M: field model-changed
565     nip [ editor>> editor-string ] [ field-model>> ] bi set-model ;