2006-03-15 01:20:59 -05:00
|
|
|
! Copyright (C) 2005, 2006 Slava Pestov.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2005-02-01 20:14:03 -05:00
|
|
|
IN: gadgets
|
2006-03-18 01:57:57 -05:00
|
|
|
USING: gadgets-labels gadgets-layouts kernel math namespaces
|
|
|
|
queues sequences ;
|
2005-02-01 20:14:03 -05:00
|
|
|
|
2006-03-19 00:30:57 -05:00
|
|
|
! Hand state
|
|
|
|
|
|
|
|
SYMBOL: hand-gadget
|
|
|
|
|
|
|
|
SYMBOL: hand-clicked
|
|
|
|
SYMBOL: hand-click-loc
|
|
|
|
|
|
|
|
SYMBOL: hand-buttons
|
|
|
|
V{ } clone hand-buttons set-global
|
|
|
|
|
2006-03-19 01:07:36 -05:00
|
|
|
TUPLE: hand ;
|
2005-02-01 20:14:03 -05:00
|
|
|
|
2005-10-07 20:26:21 -04:00
|
|
|
C: hand ( -- hand )
|
2006-03-19 00:30:57 -05:00
|
|
|
dup delegate>gadget ;
|
2005-07-13 22:51:43 -04:00
|
|
|
|
2006-03-17 02:50:16 -05:00
|
|
|
<hand> hand set-global
|
|
|
|
|
2005-12-17 09:55:00 -05:00
|
|
|
: button-gesture ( buttons gesture -- )
|
2005-12-17 00:12:32 -05:00
|
|
|
#! Send a gesture like [ button-down 2 ]; if nobody
|
|
|
|
#! handles it, send [ button-down ].
|
2006-03-19 00:30:57 -05:00
|
|
|
swap hand-clicked get-global 3dup >r add r> handle-gesture
|
2005-12-17 09:55:00 -05:00
|
|
|
[ nip handle-gesture drop ] [ 3drop ] if ;
|
2005-02-01 20:14:03 -05:00
|
|
|
|
2006-03-15 15:06:36 -05:00
|
|
|
: update-clicked ( -- )
|
2006-03-19 00:30:57 -05:00
|
|
|
hand-gadget get-global hand-clicked set-global
|
|
|
|
hand get rect-loc hand-click-loc set-global ;
|
2006-03-15 15:06:36 -05:00
|
|
|
|
2006-03-15 01:20:59 -05:00
|
|
|
: send-button-down ( event -- )
|
|
|
|
update-clicked
|
2006-03-19 00:30:57 -05:00
|
|
|
dup hand-buttons get-global push
|
2006-03-15 01:20:59 -05:00
|
|
|
[ button-down ] button-gesture ;
|
|
|
|
|
|
|
|
: send-button-up ( event -- )
|
2006-03-19 00:30:57 -05:00
|
|
|
dup hand-buttons get-global delete
|
2006-03-15 01:20:59 -05:00
|
|
|
[ button-up ] button-gesture ;
|
|
|
|
|
|
|
|
: send-scroll-wheel ( up/down -- )
|
|
|
|
[ wheel-up ] [ wheel-down ] ?
|
2006-03-19 00:30:57 -05:00
|
|
|
hand-gadget get-global handle-gesture drop ;
|
2006-03-15 01:20:59 -05:00
|
|
|
|
2005-12-17 00:12:32 -05:00
|
|
|
: drag-gesture ( -- )
|
|
|
|
#! Send a gesture like [ drag 2 ]; if nobody handles it,
|
|
|
|
#! send [ drag ].
|
2006-03-19 00:30:57 -05:00
|
|
|
hand-buttons get-global first [ drag ] button-gesture ;
|
2005-02-05 22:51:41 -05:00
|
|
|
|
2006-03-19 00:30:57 -05:00
|
|
|
: fire-motion ( -- )
|
2005-02-26 02:11:25 -05:00
|
|
|
#! 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.
|
2006-03-19 00:30:57 -05:00
|
|
|
[ motion ] hand-gadget get-global handle-gesture drop
|
|
|
|
hand-buttons get-global empty? [ drag-gesture ] unless ;
|
2005-07-20 18:04:29 -04:00
|
|
|
|
|
|
|
: each-gesture ( gesture seq -- )
|
|
|
|
[ handle-gesture* drop ] each-with ;
|
|
|
|
|
2005-10-09 21:27:14 -04:00
|
|
|
: hand-gestures ( new old -- )
|
|
|
|
drop-prefix reverse-slice
|
|
|
|
[ mouse-leave ] swap each-gesture
|
2006-03-19 00:30:57 -05:00
|
|
|
fire-motion
|
2005-07-20 18:04:29 -04:00
|
|
|
[ mouse-enter ] swap each-gesture ;
|
2005-02-05 11:52:24 -05:00
|
|
|
|
2005-07-20 18:04:29 -04:00
|
|
|
: focus-gestures ( new old -- )
|
2005-10-09 21:27:14 -04:00
|
|
|
drop-prefix reverse-slice
|
|
|
|
[ lose-focus ] swap each-gesture
|
2005-07-20 18:04:29 -04:00
|
|
|
[ gain-focus ] swap each-gesture ;
|
|
|
|
|
2005-07-04 18:36:07 -04:00
|
|
|
: request-focus ( gadget -- )
|
2006-03-19 01:07:36 -05:00
|
|
|
dup focusable-child swap find-world
|
|
|
|
dup focused-ancestors >r
|
|
|
|
[ set-world-focus ] keep
|
|
|
|
focused-ancestors r> focus-gestures ;
|
2005-08-27 00:22:19 -04:00
|
|
|
|
|
|
|
: drag-loc ( gadget -- loc )
|
2006-03-19 00:57:47 -05:00
|
|
|
hand get rect-loc hand-click-loc get-global v- ;
|
2006-03-18 01:57:57 -05:00
|
|
|
|
|
|
|
: relevant-help ( seq -- help )
|
|
|
|
[ gadget-help ] map [ ] find nip ;
|
|
|
|
|
|
|
|
: show-message ( string/f -- )
|
|
|
|
#! Show a message in the status bar.
|
2006-03-19 01:39:27 -05:00
|
|
|
world-status [ set-label-text* ] [ drop ] if* ;
|
2006-03-18 01:57:57 -05:00
|
|
|
|
|
|
|
: update-help ( -- string )
|
|
|
|
#! Update mouse-over help message.
|
2006-03-19 00:30:57 -05:00
|
|
|
hand-gadget get-global parents [ relevant-help ] keep
|
2006-03-18 01:57:57 -05:00
|
|
|
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-03-19 00:30:57 -05:00
|
|
|
hand-gadget get-global parents reverse-slice ;
|
2006-03-18 01:57:57 -05:00
|
|
|
|
|
|
|
: move-hand ( loc world -- )
|
2006-03-19 00:57:47 -05:00
|
|
|
under-hand >r over hand get set-rect-loc
|
|
|
|
pick-up hand-gadget set-global
|
2006-03-18 01:57:57 -05:00
|
|
|
under-hand r> hand-gestures update-help ;
|
|
|
|
|
|
|
|
: update-hand ( world -- )
|
|
|
|
#! Called when a gadget is removed or added.
|
|
|
|
hand get rect-loc swap move-hand ;
|
|
|
|
|
|
|
|
: layout-done ( gadget -- )
|
|
|
|
find-world [
|
|
|
|
dup update-hand world-handle repaint-handle
|
|
|
|
] when* ;
|
|
|
|
|
|
|
|
: layout-queued ( -- )
|
|
|
|
invalid dup queue-empty?
|
|
|
|
[ drop ] [ deque dup layout layout-done layout-queued ] if ;
|