factor/library/ui/text/editor.factor

240 lines
6.4 KiB
Factor
Raw Normal View History

2006-07-19 02:27:57 -04:00
! Copyright (C) 2006 Slava Pestov
! See http://factorcode.org/license.txt for BSD license.
IN: gadgets-text
USING: arrays errors freetype gadgets gadgets-borders
2006-10-03 18:17:21 -04:00
gadgets-buttons gadgets-frames gadgets-labels
gadgets-scrolling gadgets-theme io kernel math models namespaces
opengl sequences strings styles ;
2006-07-19 02:27:57 -04:00
TUPLE: editor
font color caret-color selection-color
caret mark
focused? ;
2006-07-25 00:14:59 -04:00
TUPLE: loc-monitor editor ;
: <loc> ( editor -- loc )
<loc-monitor> { 0 0 } <model> [ add-connection ] keep ;
: init-editor-locs ( editor -- )
dup <loc> over set-editor-caret
dup <loc> swap set-editor-mark ;
2006-07-19 02:27:57 -04:00
2006-08-15 04:57:12 -04:00
C: editor ( -- editor )
2006-10-03 18:17:21 -04:00
dup <document> <gadget> delegate>control
2006-07-25 00:14:59 -04:00
dup init-editor-locs
2006-07-19 02:27:57 -04:00
dup editor-theme ;
: activate-editor-model ( editor model -- )
dup activate-model swap control-model add-loc ;
2006-07-19 02:27:57 -04:00
: deactivate-editor-model ( editor model -- )
dup deactivate-model swap control-model remove-loc ;
2006-07-19 02:27:57 -04:00
M: editor graft*
dup dup editor-caret activate-editor-model
dup dup editor-mark activate-editor-model
dup control-self swap control-model add-connection ;
2006-07-19 02:27:57 -04:00
M: editor ungraft*
dup dup editor-caret deactivate-editor-model
dup dup editor-mark deactivate-editor-model
dup control-self swap control-model remove-connection ;
2006-07-19 02:27:57 -04:00
M: editor model-changed
2006-07-25 00:14:59 -04:00
control-self dup control-model
over editor-caret [ over validate-loc ] (change-model)
over editor-mark [ over validate-loc ] (change-model)
drop relayout ;
2006-07-19 02:27:57 -04:00
: editor-caret* editor-caret model-value ;
: editor-mark* editor-mark model-value ;
2006-08-15 16:29:35 -04:00
: change-caret ( editor quot -- )
over >r >r dup editor-caret* swap control-model r> call r>
[ control-model validate-loc ] keep
2006-08-31 21:58:15 -04:00
editor-caret set-model* ; inline
2006-07-19 02:27:57 -04:00
: mark>caret ( editor -- )
2006-08-31 21:58:15 -04:00
dup editor-caret* swap editor-mark set-model* ;
2006-07-19 02:27:57 -04:00
: change-caret&mark ( editor quot -- )
over >r change-caret r> mark>caret ; inline
2006-10-03 18:17:21 -04:00
: editor-line ( n editor -- str ) control-value nth ;
2006-07-19 02:27:57 -04:00
: editor-font* ( editor -- font ) editor-font lookup-font ;
: line-height ( editor -- n )
editor-font* font-height ;
: run-char-widths ( str editor -- wlist )
#! List of x co-ordinates of each character.
editor-font* swap >array [ char-width ] map-with
dup 0 [ + ] accumulate nip swap 2 v/n v+ ;
2006-07-19 02:27:57 -04:00
: x>offset ( x line# editor -- col# )
[ editor-line ] keep
over >r run-char-widths [ <= ] find-with drop dup -1 =
[ drop r> length ] [ r> drop ] if ;
: y>line ( y editor -- line# )
[ line-height / >fixnum ] keep control-model validate-line ;
2006-07-19 02:27:57 -04:00
: point>loc ( point editor -- loc )
over second over y>line [
>r >r first r> r> swap x>offset
] keep swap 2array ;
: click-loc ( editor model -- )
2006-08-31 21:58:15 -04:00
>r [ hand-rel ] keep point>loc r> set-model* ;
2006-07-19 02:27:57 -04:00
: focus-editor ( editor -- )
t over set-editor-focused? relayout-1 ;
: unfocus-editor ( editor -- )
f over set-editor-focused? relayout-1 ;
: (offset>x) ( font col# str -- x )
swap head-slice string-width ;
2006-07-19 02:27:57 -04:00
: offset>x ( col# line# editor -- x )
[ editor-line ] keep editor-font* -rot (offset>x) ;
: loc>x ( loc editor -- x ) >r first2 swap r> offset>x ;
2006-08-03 16:25:20 -04:00
: line>y ( lines# editor -- y )
line-height * ;
: caret-loc ( editor -- loc )
[ editor-caret* ] keep 2dup loc>x
rot first rot line>y 2array ;
: caret-dim ( editor -- dim )
line-height 0 swap 2array ;
2006-07-19 02:27:57 -04:00
2006-08-03 20:05:54 -04:00
: scroll>caret ( editor -- )
2006-09-23 15:54:37 -04:00
dup gadget-grafted? [
dup caret-loc over caret-dim { 1 0 } v+ <rect>
over scroll>rect
2006-09-23 15:54:37 -04:00
] when drop ;
2006-08-03 20:05:54 -04:00
M: loc-monitor model-changed
2006-10-05 17:15:41 -04:00
loc-monitor-editor control-self
dup relayout-1 scroll>caret ;
2006-08-03 16:25:20 -04:00
: draw-caret ( -- )
2006-08-03 22:01:24 -04:00
editor get editor-focused? [
editor get
dup editor-caret-color gl-color
2006-10-11 16:39:53 -04:00
dup caret-loc origin get v+
swap caret-dim over v+ gl-line
2006-08-03 22:01:24 -04:00
] when ;
2006-08-03 16:25:20 -04:00
2006-10-11 16:39:53 -04:00
: line-translation ( n -- loc )
editor get line-height * 0.0 swap 2array ;
2006-09-15 22:43:00 -04:00
: translate-lines ( n -- )
2006-10-11 16:39:53 -04:00
line-translation gl-translate ;
2006-07-19 02:27:57 -04:00
2006-08-03 16:25:20 -04:00
: draw-line ( editor str -- )
2006-10-11 16:39:53 -04:00
>r dup editor-color gl-color editor-font r>
{ 0 0 } draw-string ;
2006-07-19 02:27:57 -04:00
2006-09-15 22:43:00 -04:00
: first-visible-line ( editor -- n )
clip get rect-loc second origin get second -
swap y>line ;
: last-visible-line ( editor -- n )
clip get rect-extent nip second origin get second -
swap y>line 1+ ;
2006-07-19 02:27:57 -04:00
: with-editor ( editor quot -- )
[
2006-09-15 22:43:00 -04:00
swap
dup first-visible-line \ first-visible-line set
dup last-visible-line \ last-visible-line set
dup control-model document set
editor set
call
2006-07-19 02:27:57 -04:00
] with-scope ; inline
2006-09-15 22:43:00 -04:00
: visible-lines ( editor -- seq )
\ first-visible-line get
\ last-visible-line get
2006-10-03 18:17:21 -04:00
rot control-value <slice> ;
2006-09-15 22:43:00 -04:00
2006-10-11 16:39:53 -04:00
: with-editor-translation ( n quot -- )
>r line-translation origin get v+ r> with-translation ;
inline
2006-08-15 05:24:30 -04:00
: draw-lines ( -- )
2006-10-11 16:39:53 -04:00
\ first-visible-line get [
2006-09-15 22:43:00 -04:00
editor get dup visible-lines
[ draw-line 1 translate-lines ] each-with
2006-10-11 16:39:53 -04:00
] with-editor-translation ;
2006-07-19 02:27:57 -04:00
: selection-start/end ( editor -- start end )
dup editor-mark* swap editor-caret*
2dup <=> 0 > [ swap ] when ;
: (draw-selection) ( x1 x2 -- )
2dup = [ 2 + ] when
0.0 swap editor get line-height glRectd ;
: draw-selected-line ( start end n -- )
[ start/end-on-line ] keep tuck
>r >r editor get offset>x r> r>
editor get offset>x
(draw-selection) ;
: draw-selection ( -- )
2006-10-11 16:39:53 -04:00
editor get editor-selection-color gl-color
editor get selection-start/end
over first [
2006-07-19 02:27:57 -04:00
2dup [
2006-08-03 22:06:55 -04:00
>r 2dup r> draw-selected-line
2006-09-15 22:43:00 -04:00
1 translate-lines
2006-07-19 02:27:57 -04:00
] each-line 2drop
2006-10-11 16:39:53 -04:00
] with-editor-translation ;
2006-07-19 02:27:57 -04:00
M: editor draw-gadget*
2006-08-24 04:08:21 -04:00
[ draw-selection draw-lines draw-caret ] with-editor ;
2006-07-19 02:27:57 -04:00
: editor-height ( editor -- n )
2006-10-03 18:17:21 -04:00
[ control-value length ] keep line>y ;
2006-07-19 02:27:57 -04:00
: editor-width ( editor -- n )
2006-10-03 18:17:21 -04:00
0 swap dup editor-font* swap control-value
2006-07-19 02:27:57 -04:00
[ string-width max ] each-with ;
M: editor pref-dim*
2006-07-19 02:27:57 -04:00
dup editor-width swap editor-height 2array ;
M: editor gadget-selection?
2006-07-19 02:27:57 -04:00
selection-start/end = not ;
M: editor gadget-selection
[ selection-start/end ] keep control-model doc-range ;
2006-07-19 02:27:57 -04:00
: remove-editor-selection ( editor -- )
[ selection-start/end ] keep control-model
2006-07-19 02:27:57 -04:00
remove-doc-range ;
M: editor user-input*
[ selection-start/end ] keep control-model set-doc-range t ;
: editor-text ( editor -- str )
control-model doc-text ;
: set-editor-text ( str editor -- )
control-model set-doc-text ;
! Editors support the stream output protocol
M: editor stream-write1 >r ch>string r> stream-write ;
M: editor stream-write control-self user-input ;
M: editor stream-close drop ;