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-08-23 18:16:42 -04:00
|
|
|
USING: generic kernel math matrices namespaces sdl sequences
|
2005-08-23 20:27:42 -04:00
|
|
|
strings styles threads vectors ;
|
2005-08-23 18:16:42 -04:00
|
|
|
|
|
|
|
! A blinking caret
|
|
|
|
TUPLE: caret ;
|
|
|
|
|
|
|
|
C: caret ( -- caret )
|
|
|
|
<plain-gadget> over set-delegate
|
2005-08-23 20:27:42 -04:00
|
|
|
dup red background set-paint-prop ;
|
2005-08-23 18:16:42 -04:00
|
|
|
|
|
|
|
: toggle-visible ( gadget -- )
|
|
|
|
dup gadget-visible? not over set-gadget-visible?
|
|
|
|
relayout ;
|
|
|
|
|
|
|
|
M: caret tick* ( ms caret -- ) nip toggle-visible ;
|
|
|
|
|
2005-08-23 20:27:42 -04:00
|
|
|
: caret-block 500 ;
|
2005-08-23 18:16:42 -04:00
|
|
|
|
2005-08-23 20:27:42 -04:00
|
|
|
: add-caret ( caret parent -- )
|
|
|
|
dupd add-gadget caret-block add-timer ;
|
|
|
|
|
|
|
|
: unparent-caret ( caret -- )
|
|
|
|
dup remove-timer unparent ;
|
|
|
|
|
|
|
|
: reset-caret ( caret -- )
|
|
|
|
dup restart-timer t swap set-gadget-visible? ;
|
2005-08-23 18:16:42 -04:00
|
|
|
|
|
|
|
USE: line-editor
|
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.
|
|
|
|
|
2005-03-08 22:54:59 -05:00
|
|
|
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
|
2005-08-23 20:27:42 -04:00
|
|
|
dup editor-caret reset-caret
|
2005-07-09 18:32:31 -04:00
|
|
|
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-08-23 18:16:42 -04:00
|
|
|
dup editor-caret swap add-caret ;
|
2005-02-12 21:15:30 -05:00
|
|
|
|
|
|
|
: unfocus-editor ( editor -- )
|
2005-08-23 18:16:42 -04:00
|
|
|
editor-caret unparent-caret ;
|
2005-02-12 21:15:30 -05:00
|
|
|
|
2005-07-12 20:30:05 -04:00
|
|
|
: run-char-widths ( font str -- wlist )
|
2005-02-19 17:54:04 -05:00
|
|
|
#! List of x co-ordinates of each character.
|
2005-07-23 01:16:28 -04:00
|
|
|
>vector [ ch>string size-string drop ] map-with
|
2005-07-12 20:30:05 -04:00
|
|
|
dup 0 [ + ] accumulate swap 2 v/n v+ ;
|
2005-02-19 17:54:04 -05:00
|
|
|
|
2005-07-12 20:30:05 -04:00
|
|
|
: x>offset ( x font str -- offset )
|
2005-07-24 23:35:34 -04:00
|
|
|
dup >r run-char-widths [ <= ] find-with drop dup -1 =
|
|
|
|
[ drop r> length ] [ r> drop ] ifte ;
|
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.
|
2005-07-12 20:30:05 -04:00
|
|
|
dup [
|
|
|
|
gadget-font line-text get x>offset caret set
|
|
|
|
] with-editor ;
|
2005-02-27 16:00:55 -05:00
|
|
|
|
|
|
|
: click-editor ( editor -- )
|
2005-07-19 04:23:33 -04:00
|
|
|
dup hand relative first 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-07-27 01:46:06 -04:00
|
|
|
[[ [ "HOME" ] [ [ home ] with-editor ] ]]
|
|
|
|
[[ [ "END" ] [ [ end ] with-editor ] ]]
|
2005-03-01 22:11:08 -05:00
|
|
|
] swap add-actions ;
|
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 )
|
2005-07-19 04:23:33 -04:00
|
|
|
rectangle-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 )
|
2005-06-29 20:04:13 -04:00
|
|
|
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
|
2005-07-19 04:23:33 -04:00
|
|
|
dup editor-caret swap caret-loc swap set-rectangle-loc ;
|
2005-02-11 19:09:48 -05:00
|
|
|
|
2005-07-13 21:03:34 -04:00
|
|
|
M: editor draw-gadget* ( editor -- )
|
2005-07-17 14:48:55 -04:00
|
|
|
dup delegate draw-gadget*
|
2005-07-17 03:47:14 -04:00
|
|
|
dup editor-text draw-string ;
|