2008-02-22 00:47:06 -05:00
|
|
|
! Copyright (C) 2005, 2008 Slava Pestov.
|
2007-09-20 18:09:08 -04:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2008-07-10 21:32:17 -04:00
|
|
|
USING: accessors arrays kernel math models namespaces sequences
|
2008-07-13 17:27:11 -04:00
|
|
|
strings quotations assocs combinators classes colors
|
|
|
|
classes.tuple opengl math.vectors
|
|
|
|
ui.commands ui.gadgets ui.gadgets.borders
|
|
|
|
ui.gadgets.labels ui.gadgets.theme
|
|
|
|
ui.gadgets.tracks ui.gadgets.packs ui.gadgets.worlds ui.gestures
|
|
|
|
ui.render math.geometry.rect ;
|
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
IN: ui.gadgets.buttons
|
|
|
|
|
2008-07-11 01:46:15 -04:00
|
|
|
TUPLE: button < border pressed? selected? quot ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: buttons-down? ( -- ? )
|
|
|
|
hand-buttons get-global empty? not ;
|
|
|
|
|
|
|
|
: button-rollover? ( button -- ? )
|
|
|
|
hand-gadget get-global child? ;
|
|
|
|
|
|
|
|
: mouse-clicked? ( gadget -- ? )
|
|
|
|
hand-clicked get-global child? ;
|
|
|
|
|
|
|
|
: button-update ( button -- )
|
|
|
|
dup mouse-clicked?
|
|
|
|
over button-rollover? and
|
|
|
|
buttons-down? and
|
2008-08-30 20:52:40 -04:00
|
|
|
over (>>pressed?)
|
2007-09-20 18:09:08 -04:00
|
|
|
relayout-1 ;
|
|
|
|
|
|
|
|
: if-clicked ( button quot -- )
|
|
|
|
>r dup button-update dup button-rollover? r> [ drop ] if ;
|
|
|
|
|
2008-08-30 20:52:40 -04:00
|
|
|
: button-clicked ( button -- ) dup quot>> if-clicked ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
button H{
|
|
|
|
{ T{ button-up } [ button-clicked ] }
|
|
|
|
{ T{ button-down } [ button-update ] }
|
|
|
|
{ T{ mouse-leave } [ button-update ] }
|
|
|
|
{ T{ mouse-enter } [ button-update ] }
|
|
|
|
} set-gestures
|
|
|
|
|
2008-07-10 21:32:17 -04:00
|
|
|
: new-button ( label quot class -- button )
|
2008-07-11 01:46:15 -04:00
|
|
|
[ swap >label ] dip new-border swap >>quot ; inline
|
2008-07-10 21:32:17 -04:00
|
|
|
|
2008-07-11 01:46:15 -04:00
|
|
|
: <button> ( label quot -- button )
|
2008-07-10 21:32:17 -04:00
|
|
|
button new-button ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
TUPLE: button-paint plain rollover pressed selected ;
|
|
|
|
|
|
|
|
C: <button-paint> button-paint
|
|
|
|
|
2008-06-08 16:32:55 -04:00
|
|
|
: find-button ( gadget -- button )
|
2008-08-23 00:20:49 -04:00
|
|
|
[ button? ] find-parent ;
|
2007-10-31 01:04:54 -04:00
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
: button-paint ( button paint -- button paint )
|
2007-10-31 01:04:54 -04:00
|
|
|
over find-button {
|
2008-06-18 23:30:54 -04:00
|
|
|
{ [ dup pressed?>> ] [ drop pressed>> ] }
|
|
|
|
{ [ dup selected?>> ] [ drop selected>> ] }
|
|
|
|
{ [ dup button-rollover? ] [ drop rollover>> ] }
|
|
|
|
[ drop plain>> ]
|
2007-09-20 18:09:08 -04:00
|
|
|
} cond ;
|
|
|
|
|
|
|
|
M: button-paint draw-interior
|
|
|
|
button-paint draw-interior ;
|
|
|
|
|
|
|
|
M: button-paint draw-boundary
|
|
|
|
button-paint draw-boundary ;
|
|
|
|
|
2008-09-01 03:52:25 -04:00
|
|
|
: align-left ( button -- button )
|
|
|
|
{ 0 1/2 } >>align ; inline
|
|
|
|
|
2008-06-18 23:30:54 -04:00
|
|
|
: roll-button-theme ( button -- button )
|
2008-07-11 02:33:20 -04:00
|
|
|
f black <solid> dup f <button-paint> >>boundary
|
2008-09-01 03:52:25 -04:00
|
|
|
align-left ; inline
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: <roll-button> ( label quot -- button )
|
2008-07-10 21:32:17 -04:00
|
|
|
<button> roll-button-theme ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-06-18 23:30:54 -04:00
|
|
|
: <bevel-button-paint> ( -- paint )
|
2007-09-20 18:09:08 -04:00
|
|
|
plain-gradient
|
|
|
|
rollover-gradient
|
|
|
|
pressed-gradient
|
|
|
|
selected-gradient
|
2008-06-18 23:30:54 -04:00
|
|
|
<button-paint> ;
|
|
|
|
|
|
|
|
: bevel-button-theme ( gadget -- gadget )
|
|
|
|
<bevel-button-paint> >>interior
|
2008-07-11 01:46:15 -04:00
|
|
|
{ 5 5 } >>size
|
2008-06-18 23:30:54 -04:00
|
|
|
faint-boundary ; inline
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: <bevel-button> ( label quot -- button )
|
2008-07-11 01:46:15 -04:00
|
|
|
<button> bevel-button-theme ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-07-10 21:32:17 -04:00
|
|
|
TUPLE: repeat-button < button ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
repeat-button H{
|
|
|
|
{ T{ drag } [ button-clicked ] }
|
|
|
|
} set-gestures
|
|
|
|
|
|
|
|
: <repeat-button> ( label quot -- button )
|
|
|
|
#! Button that calls the quotation every 100ms as long as
|
|
|
|
#! the mouse is held down.
|
2008-07-11 01:46:15 -04:00
|
|
|
repeat-button new-button bevel-button-theme ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2007-11-03 15:42:51 -04:00
|
|
|
TUPLE: checkmark-paint color ;
|
|
|
|
|
|
|
|
C: <checkmark-paint> checkmark-paint
|
|
|
|
|
|
|
|
M: checkmark-paint draw-interior
|
2008-08-30 20:52:40 -04:00
|
|
|
color>> set-color
|
2007-11-03 15:42:51 -04:00
|
|
|
origin get [
|
|
|
|
rect-dim
|
|
|
|
{ 0 0 } over gl-line
|
|
|
|
dup { 0 1 } v* swap { 1 0 } v* gl-line
|
|
|
|
] with-translation ;
|
|
|
|
|
2007-10-31 01:04:54 -04:00
|
|
|
: checkmark-theme ( gadget -- )
|
|
|
|
f
|
|
|
|
f
|
|
|
|
black <solid>
|
|
|
|
black <checkmark-paint>
|
|
|
|
<button-paint>
|
2008-08-29 19:44:19 -04:00
|
|
|
over (>>interior)
|
2007-10-31 01:04:54 -04:00
|
|
|
black <solid>
|
2008-08-29 19:44:19 -04:00
|
|
|
swap (>>boundary) ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2007-10-31 01:04:54 -04:00
|
|
|
: <checkmark> ( -- gadget )
|
|
|
|
<gadget>
|
|
|
|
dup checkmark-theme
|
2008-07-21 22:59:08 -04:00
|
|
|
{ 14 14 } over (>>dim) ;
|
2007-10-31 01:04:54 -04:00
|
|
|
|
|
|
|
: toggle-model ( model -- )
|
|
|
|
[ not ] change-model ;
|
|
|
|
|
2008-07-10 21:32:17 -04:00
|
|
|
: checkbox-theme ( gadget -- gadget )
|
2008-06-08 16:32:55 -04:00
|
|
|
f >>interior
|
|
|
|
{ 5 5 } >>gap
|
2008-07-10 21:32:17 -04:00
|
|
|
1/2 >>align ; inline
|
2007-10-31 01:04:54 -04:00
|
|
|
|
2008-07-10 21:32:17 -04:00
|
|
|
TUPLE: checkbox < button ;
|
2007-11-13 18:51:10 -05:00
|
|
|
|
2007-10-31 01:04:54 -04:00
|
|
|
: <checkbox> ( model label -- checkbox )
|
2008-07-10 21:32:17 -04:00
|
|
|
<checkmark> label-on-right checkbox-theme
|
|
|
|
[ model>> toggle-model ]
|
|
|
|
checkbox new-button
|
2008-09-01 03:52:25 -04:00
|
|
|
swap >>model
|
|
|
|
align-left ;
|
2007-10-31 01:04:54 -04:00
|
|
|
|
2007-11-13 18:51:10 -05:00
|
|
|
M: checkbox model-changed
|
2008-09-01 23:43:23 -04:00
|
|
|
swap value>> over (>>selected?) relayout-1 ;
|
2007-11-13 18:51:10 -05:00
|
|
|
|
2007-11-03 15:42:51 -04:00
|
|
|
TUPLE: radio-paint color ;
|
|
|
|
|
|
|
|
C: <radio-paint> radio-paint
|
|
|
|
|
|
|
|
M: radio-paint draw-interior
|
2008-08-30 20:52:40 -04:00
|
|
|
color>> set-color
|
2007-11-03 15:42:51 -04:00
|
|
|
origin get { 4 4 } v+ swap rect-dim { 8 8 } v- 12 gl-fill-circle ;
|
|
|
|
|
|
|
|
M: radio-paint draw-boundary
|
2008-08-30 20:52:40 -04:00
|
|
|
color>> set-color
|
2007-11-03 15:42:51 -04:00
|
|
|
origin get { 1 1 } v+ swap rect-dim { 2 2 } v- 12 gl-circle ;
|
|
|
|
|
2007-10-31 01:04:54 -04:00
|
|
|
: radio-knob-theme ( gadget -- )
|
|
|
|
f
|
|
|
|
f
|
|
|
|
black <radio-paint>
|
|
|
|
black <radio-paint>
|
|
|
|
<button-paint>
|
2008-08-29 19:44:19 -04:00
|
|
|
over (>>interior)
|
2007-10-31 01:04:54 -04:00
|
|
|
black <radio-paint>
|
2008-08-29 19:44:19 -04:00
|
|
|
swap (>>boundary) ;
|
2007-10-31 01:04:54 -04:00
|
|
|
|
|
|
|
: <radio-knob> ( -- gadget )
|
|
|
|
<gadget>
|
|
|
|
dup radio-knob-theme
|
2008-07-21 22:59:08 -04:00
|
|
|
{ 16 16 } over (>>dim) ;
|
2007-10-31 01:04:54 -04:00
|
|
|
|
2008-07-10 21:32:17 -04:00
|
|
|
TUPLE: radio-control < button value ;
|
2007-11-13 18:51:10 -05:00
|
|
|
|
2008-07-10 21:32:17 -04:00
|
|
|
: <radio-control> ( value model label -- control )
|
|
|
|
[ [ value>> ] keep set-control-value ]
|
|
|
|
radio-control new-button
|
|
|
|
swap >>model
|
2008-09-01 03:52:25 -04:00
|
|
|
swap >>value
|
|
|
|
align-left ; inline
|
2007-11-13 18:51:10 -05:00
|
|
|
|
|
|
|
M: radio-control model-changed
|
2008-09-01 23:43:23 -04:00
|
|
|
swap value>>
|
2008-08-30 20:52:40 -04:00
|
|
|
over value>> =
|
|
|
|
over (>>selected?)
|
2007-11-13 18:51:10 -05:00
|
|
|
relayout-1 ;
|
2007-10-31 01:04:54 -04:00
|
|
|
|
2008-07-13 17:26:13 -04:00
|
|
|
: <radio-controls> ( parent model assoc quot -- parent )
|
|
|
|
#! quot has stack effect ( value model label -- )
|
|
|
|
swapd [ swapd call add-gadget ] 2curry assoc-each ; inline
|
2007-10-31 01:04:54 -04:00
|
|
|
|
2008-07-10 21:32:17 -04:00
|
|
|
: radio-button-theme ( gadget -- gadget )
|
2008-06-08 16:32:55 -04:00
|
|
|
{ 5 5 } >>gap
|
2008-07-10 21:32:17 -04:00
|
|
|
1/2 >>align ; inline
|
2007-10-31 01:04:54 -04:00
|
|
|
|
2007-11-14 16:35:17 -05:00
|
|
|
: <radio-button> ( value model label -- gadget )
|
2008-07-10 21:32:17 -04:00
|
|
|
<radio-knob> label-on-right radio-button-theme <radio-control> ;
|
2007-10-31 01:04:54 -04:00
|
|
|
|
|
|
|
: <radio-buttons> ( model assoc -- gadget )
|
2008-07-13 17:26:13 -04:00
|
|
|
<filled-pile>
|
|
|
|
-rot
|
|
|
|
[ <radio-button> ] <radio-controls>
|
2008-08-07 18:16:09 -04:00
|
|
|
{ 5 5 } >>gap ;
|
2007-10-31 01:04:54 -04:00
|
|
|
|
2007-11-14 16:35:17 -05:00
|
|
|
: <toggle-button> ( value model label -- gadget )
|
2008-07-11 01:46:15 -04:00
|
|
|
<radio-control> bevel-button-theme ;
|
2007-10-31 01:04:54 -04:00
|
|
|
|
|
|
|
: <toggle-buttons> ( model assoc -- gadget )
|
2008-07-13 17:26:13 -04:00
|
|
|
<shelf>
|
|
|
|
-rot
|
|
|
|
[ <toggle-button> ] <radio-controls> ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: command-button-quot ( target command -- quot )
|
|
|
|
[ invoke-command drop ] 2curry ;
|
|
|
|
|
|
|
|
: <command-button> ( target gesture command -- button )
|
|
|
|
[ command-string ] keep
|
|
|
|
swapd
|
|
|
|
command-button-quot
|
|
|
|
<bevel-button> ;
|
|
|
|
|
|
|
|
: <toolbar> ( target -- toolbar )
|
2008-07-13 17:26:13 -04:00
|
|
|
<shelf>
|
|
|
|
swap
|
|
|
|
"toolbar" over class command-map commands>> swap
|
|
|
|
[ -rot <command-button> add-gadget ] curry assoc-each ;
|