factor/library/ui/editors.factor

99 lines
2.8 KiB
Factor
Raw Normal View History

2005-02-11 19:09:48 -05:00
! Copyright (C) 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
IN: gadgets
USING: generic kernel line-editor lists math matrices namespaces
sdl sequences strings styles vectors ;
2005-02-11 19:09:48 -05:00
2005-02-27 16:00:55 -05:00
! An editor gadget wraps a line editor object and passes
! gestures to the line editor.
TUPLE: editor line caret ;
2005-02-11 19:09:48 -05:00
2005-07-09 18:32:31 -04:00
: with-editor ( editor quot -- )
#! Execute a quotation in the line editor scope, then
#! update the display.
swap [ editor-line swap bind ] keep
dup relayout scroll>bottom ; inline
2005-02-11 19:09:48 -05:00
: editor-text ( editor -- text )
editor-line [ line-text get ] bind ;
2005-02-11 19:11:31 -05:00
: set-editor-text ( text editor -- )
2005-07-09 18:32:31 -04:00
[ set-line-text ] with-editor ;
2005-02-11 19:11:31 -05:00
2005-02-12 21:15:30 -05:00
: focus-editor ( editor -- )
2005-03-01 18:55:25 -05:00
dup editor-caret swap add-gadget ;
2005-02-12 21:15:30 -05:00
: unfocus-editor ( editor -- )
2005-03-01 18:55:25 -05:00
editor-caret unparent ;
2005-02-12 21:15:30 -05:00
: run-char-widths ( font str -- wlist )
2005-02-19 17:54:04 -05:00
#! List of x co-ordinates of each character.
>list [ ch>string size-string drop ] map-with
dup 0 [ + ] accumulate swap 2 v/n v+ ;
2005-02-19 17:54:04 -05:00
: (x>offset) ( n x wlist -- offset )
dup [
uncons >r over >
[ r> 2drop ] [ >r 1 + r> r> (x>offset) ] ifte
2005-02-19 17:54:04 -05:00
] [
2drop
] ifte ;
: x>offset ( x font str -- offset )
run-char-widths 0 -rot (x>offset) ;
2005-02-12 21:15:30 -05:00
2005-02-27 16:00:55 -05:00
: set-caret-x ( x editor -- )
#! Move the caret to a clicked location.
dup [
gadget-font line-text get x>offset caret set
] with-editor ;
2005-02-27 16:00:55 -05:00
: click-editor ( editor -- )
dup hand relative shape-x over set-caret-x request-focus ;
2005-02-27 16:00:55 -05:00
2005-02-27 16:51:12 -05:00
: editor-actions ( editor -- )
2005-03-01 22:11:08 -05:00
[
2005-02-27 16:00:55 -05:00
[[ [ gain-focus ] [ focus-editor ] ]]
[[ [ lose-focus ] [ unfocus-editor ] ]]
[[ [ button-down 1 ] [ click-editor ] ]]
[[ [ "BACKSPACE" ] [ [ backspace ] with-editor ] ]]
[[ [ "LEFT" ] [ [ left ] with-editor ] ]]
[[ [ "RIGHT" ] [ [ right ] with-editor ] ]]
[[ [ "CTRL" "k" ] [ [ line-clear ] with-editor ] ]]
2005-03-01 22:11:08 -05:00
] swap add-actions ;
2005-02-27 16:00:55 -05:00
: <caret> ( -- caret )
2005-06-23 03:15:44 -04:00
<plain-gadget> dup red background set-paint-prop ;
2005-02-27 16:00:55 -05:00
C: editor ( text -- )
2005-07-13 21:03:34 -04:00
<gadget> over set-delegate
2005-02-27 16:00:55 -05:00
[ <line-editor> swap set-editor-line ] keep
[ <caret> swap set-editor-caret ] keep
[ set-editor-text ] keep
2005-02-27 16:51:12 -05:00
dup editor-actions ;
2005-02-27 16:00:55 -05:00
2005-06-27 03:47:22 -04:00
: offset>x ( gadget offset str -- x )
head >r gadget-font r> size-string drop ;
2005-02-27 16:00:55 -05:00
2005-07-08 01:32:29 -04:00
: caret-loc ( editor -- x y )
dup editor-line [ caret get line-text get ] bind offset>x
0 0 3vector ;
2005-02-12 21:15:30 -05:00
2005-07-08 01:32:29 -04:00
: caret-dim ( editor -- w h )
shape-dim { 0 1 1 } v* { 1 0 0 } v+ ;
2005-02-12 21:15:30 -05:00
2005-04-30 17:17:10 -04:00
M: editor user-input* ( ch editor -- ? )
2005-07-09 18:32:31 -04:00
[ insert-char ] with-editor t ;
2005-02-27 16:00:55 -05:00
2005-06-28 23:50:23 -04:00
M: editor pref-dim ( editor -- dim )
dup editor-text label-size { 1 0 0 } v+ ;
2005-04-30 17:17:10 -04:00
M: editor layout* ( editor -- )
2005-07-08 01:32:29 -04:00
dup editor-caret over caret-dim swap set-gadget-dim
dup editor-caret swap caret-loc swap set-shape-loc ;
2005-02-11 19:09:48 -05:00
2005-07-13 21:03:34 -04:00
M: editor draw-gadget* ( editor -- )
dup delegate draw-gadget*
dup editor-text draw-string ;