factor/library/ui/editors.factor

103 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
2005-04-02 02:39:33 -05:00
USING: generic kernel line-editor lists math namespaces sdl
sequences strings ;
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 -- )
2005-03-03 20:43:55 -05:00
hand
2005-02-27 16:00:55 -05:00
2dup relative shape-x pick set-caret-x
request-focus ;
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 )
0 0 0 0 <plain-rect> <gadget>
2005-03-06 19:46:29 -05:00
dup red background set-paint-prop ;
2005-02-27 16:00:55 -05:00
C: editor ( text -- )
0 0 0 0 <line> <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
: offset>x ( offset str -- x )
string-head font get swap 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-02-19 17:54:04 -05:00
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-02-27 16:00:55 -05:00
M: editor user-input* ( ch field -- ? )
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-02-12 21:15:30 -05:00
M: editor layout* ( field -- )
dup [ editor-text shape-size ] keep resize-gadget
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
M: editor draw-shape ( label -- )
2005-02-26 02:11:25 -05:00
dup [ editor-text draw-shape ] with-trans ;
2005-02-11 19:11:31 -05:00
2005-02-27 16:00:55 -05:00
: <field> ( text -- field )
#! A field is just a stand-alone editor with a border.
2005-03-01 18:55:25 -05:00
<editor> line-border ;