factor/library/ui/fields.factor

108 lines
3.1 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-02-12 21:15:30 -05:00
USING: generic kernel lists math namespaces sdl line-editor
strings ;
2005-02-11 19:09:48 -05:00
2005-02-12 21:15:30 -05:00
TUPLE: editor line caret delegate ;
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
: <caret> ( -- caret )
0 0 0 0 <plain-rect> <gadget>
dup red background set-paint-property ;
2005-02-11 19:11:31 -05:00
C: editor ( text -- )
2005-02-12 21:15:30 -05:00
0 0 0 0 <line> <gadget> over set-editor-delegate
2005-02-11 19:11:31 -05:00
[ <line-editor> swap set-editor-line ] keep
2005-02-12 21:15:30 -05:00
[ <caret> swap set-editor-caret ] keep
2005-02-11 19:11:31 -05:00
[ set-editor-text ] keep ;
2005-02-12 21:15:30 -05:00
: focus-editor ( editor -- )
dup editor-caret over add-gadget
dup blue foreground set-paint-property relayout ;
: unfocus-editor ( editor -- )
dup editor-caret unparent
dup black foreground set-paint-property relayout ;
2005-02-19 17:54:04 -05:00
: offset>x ( offset str -- x )
str-head font get swap size-string drop ;
: run-char-widths ( str -- wlist )
#! List of x co-ordinates of each character.
0 swap str>list
[ ch>str shape-w [ + dup ] keep 2 /i - ] map nip ;
: (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
: 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 )
0 swap shape-h ;
M: editor layout* ( field -- )
2005-02-19 17:54:04 -05:00
dup [ editor-text dup shape-w swap shape-h ] 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 -- )
dup [ editor-text draw-shape ] with-translation ;
2005-02-11 19:11:31 -05:00
2005-02-19 19:57:26 -05:00
TUPLE: field active? editor delegate ;
2005-02-12 21:15:30 -05:00
2005-02-19 19:57:26 -05:00
: with-editor ( editor quot -- )
swap [ editor-line swap bind ] keep relayout ; inline
2005-02-12 21:15:30 -05:00
2005-02-19 19:57:26 -05:00
: set-caret-x ( x editor -- )
2005-02-19 17:54:04 -05:00
#! Move the caret to a clicked location.
2005-02-19 19:57:26 -05:00
[ line-text get x>offset caret set ] with-editor ;
: click-editor ( editor -- )
my-hand
2dup relative-pos shape-x pick set-caret-x
request-focus ;
: field-border ( gadget -- border )
bevel-border dup f bevel-up? set-paint-property ;
2005-02-19 17:54:04 -05:00
2005-02-19 19:57:26 -05:00
M: field user-input* ( ch field -- ? )
2005-02-19 21:49:37 -05:00
field-editor [ insert-char ] with-editor t ;
2005-02-12 21:15:30 -05:00
: field-gestures ( -- hash )
2005-02-11 19:11:31 -05:00
{{
2005-02-12 21:15:30 -05:00
[[ [ gain-focus ] [ field-editor focus-editor ] ]]
[[ [ lose-focus ] [ field-editor unfocus-editor ] ]]
2005-02-19 19:57:26 -05:00
[[ [ button-down 1 ] [ field-editor click-editor ] ]]
[[ [ "BACKSPACE" ] [ field-editor [ backspace ] with-editor ] ]]
[[ [ "LEFT" ] [ field-editor [ left ] with-editor ] ]]
[[ [ "RIGHT" ] [ field-editor [ right ] with-editor ] ]]
[[ [ "CTRL" "k" ] [ field-editor [ line-clear ] with-editor ] ]]
2005-02-12 21:15:30 -05:00
}} ;
C: field ( text -- field )
#! Note that we want the editor's parent to be the field,
#! not the border.
[ f field-border swap set-field-delegate ] keep
[ >r <editor> dup r> set-field-editor ] keep
[ add-gadget ] keep
[ field-gestures swap set-gadget-gestures ] keep ;