factor/library/ui/buttons.factor

67 lines
2.1 KiB
Factor
Raw Normal View History

! Copyright (C) 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
IN: gadgets-buttons
2005-09-28 23:29:00 -04:00
USING: gadgets gadgets-borders gadgets-layouts gadgets-theme
generic io kernel lists math namespaces sdl sequences sequences
styles threads ;
2005-10-07 20:26:21 -04:00
: button-down? ( n -- ? ) hand get hand-buttons member? ;
2005-02-19 23:25:21 -05:00
2005-10-07 20:26:21 -04:00
: mouse-over? ( gadget -- ? ) hand get hand-gadget child? ;
: button-pressed? ( button -- ? )
2005-02-19 22:55:45 -05:00
#! Return true if the mouse was clicked on the button, and
#! is currently over the button.
2005-09-27 00:24:42 -04:00
dup mouse-over? 1 button-down? and
2005-10-07 20:26:21 -04:00
[ hand get hand-clicked child? ] [ drop f ] if ;
: button-update ( button -- )
dup dup mouse-over? rollover set-paint-prop
2005-03-06 19:46:29 -05:00
dup dup button-pressed? reverse-video set-paint-prop
2005-09-25 20:41:49 -04:00
relayout-1 ;
: button-clicked ( button -- )
#! If the mouse is released while still inside the button,
#! fire an action gesture.
2005-08-27 15:12:37 -04:00
dup button-update dup mouse-over?
[ [ action ] swap handle-gesture ] when drop ;
2005-05-03 19:00:52 -04:00
: button-action ( action -- quot )
2005-09-24 15:21:17 -04:00
[ [ swap handle-gesture drop ] cons ] [ [ drop ] ] if* ;
2005-05-03 19:00:52 -04:00
: button-gestures ( button quot -- )
dupd [ action ] set-action
2005-08-27 15:12:37 -04:00
dup [ button-clicked ] [ button-up 1 ] set-action
dup [ button-update ] [ button-down 1 ] set-action
dup [ button-update ] [ mouse-leave ] set-action
2005-08-27 15:12:37 -04:00
[ button-update ] [ mouse-enter ] set-action ;
2005-07-17 02:49:07 -04:00
2005-08-27 15:12:37 -04:00
TUPLE: button ;
C: button ( gadget quot -- button )
2005-10-09 21:27:14 -04:00
rot bevel-border over set-gadget-delegate
[ swap button-gestures ] keep ;
2005-08-26 00:55:56 -04:00
2005-08-26 21:42:43 -04:00
: <roll-button> ( gadget quot -- button )
2005-09-27 00:44:38 -04:00
>r dup roll-button-theme dup r> button-gestures ;
: <highlight-button> ( gadget quot -- button )
dupd button-gestures ;
2005-08-27 15:12:37 -04:00
: repeat-button-down ( button -- )
dup 100 add-timer button-clicked ;
: repeat-button-up ( button -- )
dup button-update remove-timer ;
: repeat-actions ( button -- )
dup [ repeat-button-down ] [ button-down 1 ] set-action
[ repeat-button-up ] [ button-up 1 ] set-action ;
: <repeat-button> ( gadget quot -- button )
#! Button that calls the quotation every 100ms as long as
#! the mouse is held down.
<button> dup repeat-actions ;
M: button tick ( ms object -- ) nip button-clicked ;