2005-01-31 22:32:06 -05:00
|
|
|
! Copyright (C) 2005 Slava Pestov.
|
|
|
|
! See http://factor.sf.net/license.txt for BSD license.
|
|
|
|
IN: gadgets
|
2005-07-12 20:30:05 -04:00
|
|
|
USING: alien generic hashtables kernel lists math matrices sdl
|
2005-05-14 17:18:45 -04:00
|
|
|
sequences ;
|
2005-02-05 11:52:24 -05:00
|
|
|
|
|
|
|
: action ( gadget gesture -- quot )
|
2005-07-13 21:17:47 -04:00
|
|
|
swap gadget-gestures ?hash ;
|
2005-02-05 11:52:24 -05:00
|
|
|
|
|
|
|
: set-action ( gadget quot gesture -- )
|
2005-07-13 21:17:47 -04:00
|
|
|
pick gadget-gestures ?set-hash swap set-gadget-gestures ;
|
2005-02-01 21:47:10 -05:00
|
|
|
|
2005-03-01 22:11:08 -05:00
|
|
|
: add-actions ( alist gadget -- )
|
|
|
|
swap [ unswons set-action ] each-with ;
|
|
|
|
|
2005-02-01 21:47:10 -05:00
|
|
|
: handle-gesture* ( gesture gadget -- ? )
|
2005-07-13 21:17:47 -04:00
|
|
|
tuck gadget-gestures ?hash dup [ call f ] [ 2drop t ] ifte ;
|
2005-01-31 22:32:06 -05:00
|
|
|
|
2005-02-12 21:15:30 -05:00
|
|
|
: handle-gesture ( gesture gadget -- ? )
|
2005-01-31 22:32:06 -05:00
|
|
|
#! If a gadget's handle-gesture* generic returns t, the
|
|
|
|
#! event was not consumed and is passed on to the gadget's
|
2005-02-12 21:15:30 -05:00
|
|
|
#! parent. This word returns t if no gadget handled the
|
|
|
|
#! gesture, otherwise returns f.
|
|
|
|
[ dupd handle-gesture* ] each-parent nip ;
|
|
|
|
|
2005-03-22 21:20:58 -05:00
|
|
|
: link-action ( gadget to from -- )
|
|
|
|
#! When gadget receives 'from' gesture, send a 'to' gesture.
|
|
|
|
>r [ swap handle-gesture drop ] cons r> set-action ;
|
|
|
|
|
2005-02-12 21:15:30 -05:00
|
|
|
: user-input ( ch gadget -- ? )
|
|
|
|
[ dupd user-input* ] each-parent nip ;
|
2005-02-01 20:14:03 -05:00
|
|
|
|
2005-02-01 21:47:10 -05:00
|
|
|
! Mouse gestures are lists where the first element is one of:
|
|
|
|
SYMBOL: motion
|
2005-02-26 02:11:25 -05:00
|
|
|
SYMBOL: drag
|
2005-02-01 21:47:10 -05:00
|
|
|
SYMBOL: button-up
|
|
|
|
SYMBOL: button-down
|
2005-02-05 11:52:24 -05:00
|
|
|
|
2005-02-10 23:58:28 -05:00
|
|
|
: hierarchy-gesture ( gadget ? gesture -- ? )
|
2005-07-12 20:30:05 -04:00
|
|
|
swap [ 2drop f ] [ swap handle-gesture* drop t ] ifte ;
|
2005-02-10 23:58:28 -05:00
|
|
|
|
2005-02-05 11:52:24 -05:00
|
|
|
: mouse-enter ( point gadget -- )
|
|
|
|
#! If the old point is inside the new gadget, do not fire an
|
|
|
|
#! enter gesture, since the mouse did not enter. Otherwise,
|
|
|
|
#! fire an enter gesture and go on to the parent.
|
|
|
|
[
|
2005-07-19 04:23:33 -04:00
|
|
|
[ rectangle-loc v+ ] keep
|
2005-02-10 23:58:28 -05:00
|
|
|
2dup inside? [ mouse-enter ] hierarchy-gesture
|
2005-02-12 21:15:30 -05:00
|
|
|
] each-parent 2drop ;
|
2005-02-05 11:52:24 -05:00
|
|
|
|
|
|
|
: mouse-leave ( point gadget -- )
|
|
|
|
#! If the new point is inside the old gadget, do not fire a
|
|
|
|
#! leave gesture, since the mouse did not leave. Otherwise,
|
|
|
|
#! fire a leave gesture and go on to the parent.
|
|
|
|
[
|
2005-07-19 04:23:33 -04:00
|
|
|
[ rectangle-loc v+ ] keep
|
2005-02-10 23:58:28 -05:00
|
|
|
2dup inside? [ mouse-leave ] hierarchy-gesture
|
2005-02-12 21:15:30 -05:00
|
|
|
] each-parent 2drop ;
|
2005-02-10 23:58:28 -05:00
|
|
|
|
2005-02-11 12:45:24 -05:00
|
|
|
: lose-focus ( new old -- )
|
2005-02-10 23:58:28 -05:00
|
|
|
#! If the old focus owner is a child of the new owner, do
|
|
|
|
#! not fire a focus lost gesture, since the focus was not
|
|
|
|
#! lost. Otherwise, fire a focus lost gesture and go to the
|
|
|
|
#! parent.
|
|
|
|
[
|
|
|
|
2dup child? [ lose-focus ] hierarchy-gesture
|
2005-02-12 21:15:30 -05:00
|
|
|
] each-parent 2drop ;
|
2005-02-10 23:58:28 -05:00
|
|
|
|
|
|
|
: gain-focus ( old new -- )
|
|
|
|
#! If the old focus owner is a child of the new owner, do
|
|
|
|
#! not fire a focus gained gesture, since the focus was not
|
|
|
|
#! gained. Otherwise, fire a focus gained gesture and go on
|
|
|
|
#! to the parent.
|
|
|
|
[
|
|
|
|
2dup child? [ gain-focus ] hierarchy-gesture
|
2005-02-12 21:15:30 -05:00
|
|
|
] each-parent 2drop ;
|