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
: editor-text ( editor -- text )
editor-line [ line-text get ] bind ;
2005-02-11 19:11:31 -05:00
: set-editor-text ( text editor -- )
2005-02-11 19:35:50 -05:00
editor-line [ set-line-text ] bind ;
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
2005-02-27 16:00:55 -05:00
: with-editor ( editor quot -- )
#! Execute a quotation in the line editor scope, then
#! update the display.
swap [ editor-line swap bind ] keep relayout ; inline
2005-02-19 17:54:04 -05:00
: run-char-widths ( str -- wlist )
#! List of x co-ordinates of each character.
2005-04-02 02:39:33 -05:00
0 swap >list
[ ch>string shape-w [ + dup ] keep 2 /i - ] map nip ;
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
] [
2drop
] ifte ;
: x>offset ( x str -- offset )
0 -rot run-char-widths (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.
[ line-text get x>offset caret set ] with-editor ;
: 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-04-30 17:17:10 -04:00
<empty-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-02-12 21:15:30 -05:00
: caret-pos ( editor -- x y )
2005-06-27 03:47:22 -04:00
dup editor-line [ caret get line-text get ] bind offset>x 0 ;
2005-02-12 21:15:30 -05:00
: caret-size ( editor -- w h )
2005-03-03 22:45:23 -05:00
1 swap shape-h ;
2005-02-12 21:15:30 -05:00
2005-04-30 17:17:10 -04:00
M: editor user-input* ( ch editor -- ? )
2005-03-10 17:57:22 -05:00
[ [ insert-char ] with-editor ] keep
2005-03-11 21:41:46 -05:00
scroll>bottom 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-02-12 21:15:30 -05:00
dup editor-caret over caret-size rot resize-gadget
dup editor-caret swap caret-pos rot move-gadget ;
2005-02-11 19:09:48 -05:00
2005-04-30 17:17:10 -04:00
M: editor draw-shape ( editor -- )
2005-06-27 03:47:22 -04:00
[ dup gadget-font swap editor-text ] keep
[ draw-string ] with-trans ;