2009-02-05 23:17:15 -05:00
|
|
|
! Copyright (C) 2005, 2009 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
|
2009-02-05 23:17:15 -05:00
|
|
|
strings quotations assocs combinators classes colors colors.constants
|
2008-11-11 01:28:37 -05:00
|
|
|
classes.tuple opengl opengl.gl math.vectors ui.commands ui.gadgets
|
2009-02-14 20:50:22 -05:00
|
|
|
ui.gadgets.borders ui.gadgets.labels
|
2008-11-11 01:28:37 -05:00
|
|
|
ui.gadgets.tracks ui.gadgets.packs ui.gadgets.worlds ui.gestures
|
2009-02-13 02:43:03 -05:00
|
|
|
ui.pens ui.pens.solid ui.pens.image math.rectangles locals
|
|
|
|
fry combinators.smart ;
|
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 -- )
|
2009-02-13 02:43:03 -05:00
|
|
|
dup
|
|
|
|
[ mouse-clicked? ] [ button-rollover? ] bi and
|
2007-09-20 18:09:08 -04:00
|
|
|
buttons-down? and
|
2008-09-27 15:36:04 -04:00
|
|
|
>>pressed?
|
2007-09-20 18:09:08 -04:00
|
|
|
relayout-1 ;
|
|
|
|
|
|
|
|
: if-clicked ( button quot -- )
|
2008-11-21 00:54:27 -05:00
|
|
|
[ dup button-update dup button-rollover? ] dip [ drop ] if ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
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 ] }
|
2009-01-28 01:30:57 -05:00
|
|
|
{ mouse-leave [ button-update ] }
|
|
|
|
{ mouse-enter [ button-update ] }
|
2007-09-20 18:09:08 -04:00
|
|
|
} 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
|
|
|
|
2009-02-13 02:43:03 -05:00
|
|
|
TUPLE: button-pen
|
|
|
|
plain rollover
|
|
|
|
pressed selected pressed-selected ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2009-02-13 02:43:03 -05:00
|
|
|
C: <button-pen> button-pen
|
2007-09-20 18:09:08 -04:00
|
|
|
|
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
|
|
|
|
2009-02-13 02:43:03 -05:00
|
|
|
: button-pen ( button pen -- button pen )
|
2007-10-31 01:04:54 -04:00
|
|
|
over find-button {
|
2009-02-13 02:43:03 -05:00
|
|
|
{ [ dup [ pressed?>> ] [ selected?>> ] bi and ] [ drop pressed-selected>> ] }
|
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 ;
|
|
|
|
|
2009-02-13 02:43:03 -05:00
|
|
|
M: button-pen draw-interior
|
|
|
|
button-pen dup [ draw-interior ] [ 2drop ] if ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2009-02-13 02:43:03 -05:00
|
|
|
M: button-pen draw-boundary
|
|
|
|
button-pen dup [ draw-boundary ] [ 2drop ] if ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2009-02-13 02:43:03 -05:00
|
|
|
M: button-pen pen-pref-dim
|
2009-02-12 02:39:18 -05:00
|
|
|
[
|
|
|
|
{
|
|
|
|
[ plain>> pen-pref-dim ]
|
|
|
|
[ rollover>> pen-pref-dim ]
|
|
|
|
[ pressed>> pen-pref-dim ]
|
|
|
|
[ selected>> pen-pref-dim ]
|
|
|
|
} 2cleave
|
|
|
|
] [ vmax ] reduce-outputs ;
|
|
|
|
|
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 )
|
2009-02-13 02:43:03 -05:00
|
|
|
f COLOR: black <solid> dup f f <button-pen> >>boundary
|
|
|
|
f f pressed-gradient f f <button-pen> >>interior
|
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
|
|
|
|
2009-02-14 20:50:22 -05:00
|
|
|
: <border-button-pen> ( -- paint )
|
2007-09-20 18:09:08 -04:00
|
|
|
plain-gradient
|
|
|
|
rollover-gradient
|
|
|
|
pressed-gradient
|
|
|
|
selected-gradient
|
2009-02-13 02:43:03 -05:00
|
|
|
selected-gradient
|
|
|
|
<button-pen> ;
|
2008-06-18 23:30:54 -04:00
|
|
|
|
2009-02-14 20:50:22 -05:00
|
|
|
: border-button-theme ( gadget -- gadget )
|
|
|
|
<border-button-pen> >>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
|
|
|
|
2009-02-14 20:50:22 -05:00
|
|
|
: <border-button> ( label quot -- button )
|
|
|
|
<button> border-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{
|
2009-02-12 02:39:18 -05:00
|
|
|
{ T{ button-down } [ button-clicked ] }
|
2007-09-20 18:09:08 -04:00
|
|
|
{ T{ drag } [ button-clicked ] }
|
2009-02-12 02:39:18 -05:00
|
|
|
{ T{ button-up } [ button-update ] }
|
2007-09-20 18:09:08 -04:00
|
|
|
} set-gestures
|
|
|
|
|
|
|
|
: <repeat-button> ( label quot -- button )
|
|
|
|
#! Button that calls the quotation every 100ms as long as
|
|
|
|
#! the mouse is held down.
|
2009-02-14 20:50:22 -05:00
|
|
|
repeat-button new-button border-button-theme ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2009-02-13 02:43:03 -05:00
|
|
|
: <checkmark-paint> ( -- pen )
|
|
|
|
"checkbox" theme-image <image-pen>
|
|
|
|
"checkbox" theme-image <image-pen>
|
|
|
|
"checkbox-clicked" theme-image <image-pen>
|
|
|
|
"checkbox-set" theme-image <image-pen>
|
|
|
|
"checkbox-set-clicked" theme-image <image-pen>
|
|
|
|
<button-pen> ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2007-10-31 01:04:54 -04:00
|
|
|
: <checkmark> ( -- gadget )
|
|
|
|
<gadget>
|
2009-02-13 02:43:03 -05:00
|
|
|
<checkmark-paint> >>interior
|
|
|
|
dup dup interior>> pen-pref-dim >>dim ;
|
2007-10-31 01:04:54 -04:00
|
|
|
|
|
|
|
: toggle-model ( model -- )
|
|
|
|
[ not ] change-model ;
|
|
|
|
|
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 )
|
2009-02-02 01:02:55 -05:00
|
|
|
<checkmark> label-on-right
|
2008-07-10 21:32:17 -04:00
|
|
|
[ 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-27 15:36:04 -04:00
|
|
|
swap value>> >>selected? relayout-1 ;
|
2007-11-13 18:51:10 -05:00
|
|
|
|
2009-02-13 02:43:03 -05:00
|
|
|
: <radio-paint> ( -- pen )
|
|
|
|
"radio" theme-image <image-pen>
|
|
|
|
"radio" theme-image <image-pen>
|
|
|
|
"radio-clicked" theme-image <image-pen>
|
|
|
|
"radio-set" theme-image <image-pen>
|
|
|
|
"radio-set-clicked" theme-image <image-pen>
|
|
|
|
<button-pen> ;
|
2007-10-31 01:04:54 -04:00
|
|
|
|
|
|
|
: <radio-knob> ( -- gadget )
|
2009-02-13 02:43:03 -05:00
|
|
|
<gadget>
|
|
|
|
<radio-paint> >>interior
|
|
|
|
dup dup interior>> pen-pref-dim >>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
|
2009-01-26 18:58:23 -05:00
|
|
|
2dup [ value>> ] bi@ = >>selected? relayout-1 drop ;
|
2007-10-31 01:04:54 -04:00
|
|
|
|
2009-01-26 18:58:23 -05:00
|
|
|
:: <radio-controls> ( parent model assoc quot: ( value model label -- gadget ) -- parent )
|
|
|
|
assoc model [ parent swap quot call add-gadget ] assoc-each ; inline
|
2007-10-31 01:04:54 -04:00
|
|
|
|
2007-11-14 16:35:17 -05:00
|
|
|
: <radio-button> ( value model label -- gadget )
|
2009-02-02 01:02:55 -05:00
|
|
|
<radio-knob> label-on-right <radio-control> ;
|
2007-10-31 01:04:54 -04:00
|
|
|
|
|
|
|
: <radio-buttons> ( model assoc -- gadget )
|
2008-09-27 15:36:04 -04:00
|
|
|
<filled-pile>
|
2009-01-26 18:58:23 -05:00
|
|
|
[ <radio-button> ] <radio-controls>
|
2008-09-27 15:36:04 -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 )
|
2009-02-14 20:50:22 -05:00
|
|
|
<radio-control> border-button-theme ;
|
2007-10-31 01:04:54 -04:00
|
|
|
|
|
|
|
: <toggle-buttons> ( model assoc -- gadget )
|
2008-09-27 15:36:04 -04:00
|
|
|
<shelf>
|
2009-01-26 18:58:23 -05:00
|
|
|
[ <toggle-button> ] <radio-controls> ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: command-button-quot ( target command -- quot )
|
2008-11-21 00:54:27 -05:00
|
|
|
'[ _ _ invoke-command drop ] ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: <command-button> ( target gesture command -- button )
|
2009-02-14 20:50:22 -05:00
|
|
|
[ command-string swap ] keep command-button-quot <border-button> ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: <toolbar> ( target -- toolbar )
|
2008-09-27 15:36:04 -04:00
|
|
|
<shelf>
|
2009-01-26 18:58:23 -05:00
|
|
|
1 >>fill
|
2008-09-27 15:36:04 -04:00
|
|
|
swap
|
2009-01-26 18:58:23 -05:00
|
|
|
[ [ "toolbar" ] dip class command-map commands>> ] keep
|
2008-11-21 00:54:27 -05:00
|
|
|
'[ [ _ ] 2dip <command-button> add-gadget ] assoc-each ;
|
|
|
|
|
|
|
|
: add-toolbar ( track -- track )
|
|
|
|
dup <toolbar> f track-add ;
|