factor/library/ui/gestures.factor

72 lines
2.2 KiB
Factor
Raw Normal View History

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-02-05 11:52:24 -05:00
USING: alien generic hashtables kernel lists math sdl-event ;
: action ( gadget gesture -- quot )
swap gadget-gestures hash ;
: set-action ( gadget quot gesture -- )
rot gadget-gestures set-hash ;
2005-02-01 21:47:10 -05:00
: handle-gesture* ( gesture gadget -- ? )
tuck gadget-gestures hash* dup [
cdr call f
] [
2drop t
] ifte ;
2005-01-31 22:32:06 -05:00
: handle-gesture ( gesture gadget -- )
#! If a gadget's handle-gesture* generic returns t, the
#! event was not consumed and is passed on to the gadget's
#! parent.
2005-02-05 11:52:24 -05:00
[ dupd handle-gesture* ] each-parent drop ;
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
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 -- ? )
swap [
2drop f
] [
swap handle-gesture* drop t
] ifte ;
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.
[
[ shape-pos + ] keep
2005-02-10 23:58:28 -05:00
2dup inside? [ mouse-enter ] hierarchy-gesture
2005-02-05 11:52:24 -05:00
] each-parent drop ;
: 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.
[
[ shape-pos + ] keep
2005-02-10 23:58:28 -05:00
2dup inside? [ mouse-leave ] hierarchy-gesture
] each-parent drop ;
: lose-focus ( old new -- )
#! 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
] each-parent drop ;
: 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-05 11:52:24 -05:00
] each-parent drop ;