2006-03-24 19:26:06 -05:00
|
|
|
! Copyright (C) 2005, 2006 Slava Pestov.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2005-01-31 22:32:06 -05:00
|
|
|
IN: gadgets
|
2006-03-24 19:26:06 -05:00
|
|
|
USING: gadgets-labels gadgets-layouts hashtables kernel math
|
|
|
|
namespaces queues sequences threads ;
|
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
|
|
|
|
2005-10-07 20:26:21 -04:00
|
|
|
: init-gestures ( gadget -- gestures )
|
|
|
|
dup gadget-gestures
|
2005-10-29 23:25:38 -04:00
|
|
|
[ ] [ H{ } clone dup rot set-gadget-gestures ] ?if ;
|
2005-10-07 20:26:21 -04:00
|
|
|
|
2005-02-05 11:52:24 -05:00
|
|
|
: set-action ( gadget quot gesture -- )
|
2005-10-07 20:26:21 -04:00
|
|
|
rot init-gestures set-hash ;
|
2005-02-01 21:47:10 -05:00
|
|
|
|
2005-10-07 20:26:21 -04:00
|
|
|
: add-actions ( gadget hash -- )
|
|
|
|
dup [ >r init-gestures r> hash-update ] [ 2drop ] if ;
|
2005-03-01 22:11:08 -05:00
|
|
|
|
2005-02-01 21:47:10 -05:00
|
|
|
: handle-gesture* ( gesture gadget -- ? )
|
2005-09-24 15:21:17 -04:00
|
|
|
tuck gadget-gestures ?hash dup [ call f ] [ 2drop t ] if ;
|
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 ;
|
|
|
|
|
2006-03-22 17:22:05 -05:00
|
|
|
: user-input ( str gadget -- )
|
|
|
|
[ dupd user-input* ] each-parent 2drop ;
|
2005-02-01 20:14:03 -05:00
|
|
|
|
2006-05-15 01:01:47 -04:00
|
|
|
! Mouse gestures are arrays where the first element is one of:
|
2005-02-01 21:47:10 -05:00
|
|
|
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-12-17 00:12:32 -05:00
|
|
|
SYMBOL: wheel-up
|
|
|
|
SYMBOL: wheel-down
|
2005-07-20 18:04:29 -04:00
|
|
|
SYMBOL: mouse-enter
|
|
|
|
SYMBOL: mouse-leave
|
2005-02-05 11:52:24 -05:00
|
|
|
|
2005-07-20 18:04:29 -04:00
|
|
|
SYMBOL: lose-focus
|
|
|
|
SYMBOL: gain-focus
|
2006-03-24 19:26:06 -05:00
|
|
|
|
|
|
|
! Hand state
|
|
|
|
|
|
|
|
! Note that these are only really useful inside an event
|
|
|
|
! handler, and that the locations hand-loc and hand-click-loc
|
|
|
|
! are in the co-ordinate system of the world which contains
|
|
|
|
! the gadget in question.
|
|
|
|
SYMBOL: hand-gadget
|
|
|
|
SYMBOL: hand-loc
|
|
|
|
{ 0 0 0 } hand-loc set-global
|
|
|
|
|
|
|
|
SYMBOL: hand-clicked
|
|
|
|
SYMBOL: hand-click-loc
|
|
|
|
|
|
|
|
SYMBOL: hand-buttons
|
|
|
|
V{ } clone hand-buttons set-global
|
|
|
|
|
2006-03-25 17:01:39 -05:00
|
|
|
: button-gesture ( button gesture -- )
|
2006-03-24 19:26:06 -05:00
|
|
|
#! Send a gesture like [ button-down 2 ]; if nobody
|
|
|
|
#! handles it, send [ button-down ].
|
|
|
|
swap hand-clicked get-global 3dup >r add r> handle-gesture
|
|
|
|
[ nip handle-gesture drop ] [ 3drop ] if ;
|
|
|
|
|
|
|
|
: drag-gesture ( -- )
|
|
|
|
#! Send a gesture like [ drag 2 ]; if nobody handles it,
|
|
|
|
#! send [ drag ].
|
|
|
|
hand-buttons get-global first [ drag ] button-gesture ;
|
|
|
|
|
|
|
|
: fire-motion ( -- )
|
|
|
|
#! Fire a motion gesture to the gadget underneath the hand,
|
|
|
|
#! and if a mouse button is down, fire a drag gesture to the
|
|
|
|
#! gadget that was clicked.
|
|
|
|
[ motion ] hand-gadget get-global handle-gesture drop
|
|
|
|
hand-buttons get-global empty? [ drag-gesture ] unless ;
|
|
|
|
|
|
|
|
: each-gesture ( gesture seq -- )
|
|
|
|
[ handle-gesture* drop ] each-with ;
|
|
|
|
|
|
|
|
: hand-gestures ( new old -- )
|
2006-05-14 23:25:34 -04:00
|
|
|
drop-prefix <reversed>
|
2006-03-24 19:26:06 -05:00
|
|
|
[ mouse-leave ] swap each-gesture
|
|
|
|
fire-motion
|
|
|
|
[ mouse-enter ] swap each-gesture ;
|
|
|
|
|
|
|
|
: focus-gestures ( new old -- )
|
2006-05-14 23:25:34 -04:00
|
|
|
drop-prefix <reversed>
|
2006-03-24 19:26:06 -05:00
|
|
|
[ lose-focus ] swap each-gesture
|
|
|
|
[ gain-focus ] swap each-gesture ;
|
|
|
|
|
|
|
|
: request-focus* ( gadget world -- )
|
|
|
|
dup focused-ancestors >r
|
|
|
|
[ set-world-focus ] keep
|
|
|
|
focused-ancestors r> focus-gestures ;
|
|
|
|
|
|
|
|
: request-focus ( gadget -- )
|
|
|
|
dup focusable-child swap find-world request-focus* ;
|
|
|
|
|
|
|
|
: modifier ( mod modifiers -- seq )
|
|
|
|
[ second swap bitand 0 > ] subset-with
|
|
|
|
[ first ] map ;
|
|
|
|
|
|
|
|
: drag-loc ( -- loc )
|
|
|
|
hand-loc get-global hand-click-loc get-global v- ;
|
|
|
|
|
|
|
|
: hand-click-rel ( gadget -- loc )
|
|
|
|
hand-click-loc get-global relative-loc ;
|
|
|
|
|
|
|
|
: relevant-help ( seq -- help )
|
|
|
|
[ gadget-help ] map [ ] find nip ;
|
|
|
|
|
|
|
|
: show-message ( string/f -- )
|
|
|
|
#! Show a message in the status bar.
|
|
|
|
world-status [ set-label-text* ] [ drop ] if* ;
|
|
|
|
|
|
|
|
: update-help ( -- )
|
|
|
|
#! Update mouse-over help message.
|
|
|
|
hand-gadget get-global parents [ relevant-help ] keep
|
|
|
|
dup empty? [ 2drop ] [ peek show-message ] if ;
|
|
|
|
|
|
|
|
: under-hand ( -- seq )
|
|
|
|
#! A sequence whose first element is the world and last is
|
|
|
|
#! the current gadget, with all parents in between.
|
2006-05-14 23:25:34 -04:00
|
|
|
hand-gadget get-global parents <reversed> ;
|
2006-03-24 19:26:06 -05:00
|
|
|
|
|
|
|
: move-hand ( loc world -- )
|
|
|
|
under-hand >r over hand-loc set-global
|
|
|
|
pick-up hand-gadget set-global
|
|
|
|
under-hand r> hand-gestures update-help ;
|
|
|
|
|
|
|
|
: update-clicked ( loc world -- )
|
|
|
|
move-hand
|
|
|
|
hand-gadget get-global hand-clicked set-global
|
|
|
|
hand-loc get-global hand-click-loc set-global ;
|
|
|
|
|
|
|
|
: send-button-down ( button# loc world -- )
|
|
|
|
update-clicked
|
|
|
|
dup hand-buttons get-global push
|
|
|
|
[ button-down ] button-gesture ;
|
|
|
|
|
2006-03-25 17:01:39 -05:00
|
|
|
: send-button-up ( button# loc world -- )
|
2006-03-24 19:26:06 -05:00
|
|
|
move-hand
|
2006-03-25 17:41:40 -05:00
|
|
|
dup hand-buttons get-global delete
|
|
|
|
[ button-up ] button-gesture ;
|
2006-03-24 19:26:06 -05:00
|
|
|
|
|
|
|
: send-wheel ( up/down loc world -- )
|
|
|
|
move-hand
|
|
|
|
[ wheel-up ] [ wheel-down ] ?
|
|
|
|
hand-gadget get-global handle-gesture drop ;
|