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: field active? editor delegate ;
|
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 ;
|
|
|
|
|
|
|
|
: offset>x ( offset editor -- x )
|
|
|
|
editor-line [ line-text get ] bind str-head
|
|
|
|
font get swap
|
|
|
|
size-string drop ;
|
|
|
|
|
|
|
|
: caret-pos ( editor -- x y )
|
|
|
|
dup editor-line [ caret get ] bind swap offset>x 0 ;
|
|
|
|
|
|
|
|
: caret-size ( editor -- w h )
|
|
|
|
0 swap shape-h ;
|
|
|
|
|
|
|
|
M: editor layout* ( field -- )
|
|
|
|
dup [ editor-text dup shape-w swap shape-h ] keep resize-gadget
|
|
|
|
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
|
|
|
|
|
|
|
: field-border ( gadget -- border )
|
|
|
|
bevel-border dup f bevel-up? set-paint-property ;
|
|
|
|
|
2005-02-12 21:15:30 -05:00
|
|
|
: with-field-editor ( field quot -- )
|
|
|
|
swap field-editor [ editor-line swap bind ] keep relayout ;
|
|
|
|
|
|
|
|
M: field user-input* ( ch field -- ? )
|
|
|
|
[ insert-char ] with-field-editor f ;
|
|
|
|
|
|
|
|
: click-field ( field -- )
|
|
|
|
my-hand request-focus ;
|
|
|
|
|
|
|
|
: 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 ] ]]
|
|
|
|
[[ [ button-down 1 ] [ click-field ] ]]
|
|
|
|
[[ [ "BACKSPACE" ] [ [ backspace ] with-field-editor ] ]]
|
|
|
|
[[ [ "LEFT" ] [ [ left ] with-field-editor ] ]]
|
|
|
|
[[ [ "RIGHT" ] [ [ right ] with-field-editor ] ]]
|
|
|
|
[[ [ "CTRL" "k" ] [ [ line-clear ] with-field-editor ] ]]
|
|
|
|
}} ;
|
|
|
|
|
|
|
|
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 ;
|