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 21:46:13 -05:00
|
|
|
ui.gadgets.borders ui.gadgets.labels ui.gadgets.tracks
|
|
|
|
ui.gadgets.packs ui.gadgets.worlds ui.gestures ui.pens ui.pens.solid
|
|
|
|
ui.pens.image ui.pens.tile math.rectangles locals fry
|
2009-03-16 21:11:36 -04:00
|
|
|
combinators.smart ;
|
2009-05-14 17:54:16 -04:00
|
|
|
FROM: models => change-model ;
|
2007-09-20 18:09:08 -04:00
|
|
|
IN: ui.gadgets.buttons
|
|
|
|
|
2009-09-08 12:43:47 -04:00
|
|
|
TUPLE: button < border pressed? selected? quot tooltip ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2009-02-14 22:53:39 -05:00
|
|
|
<PRIVATE
|
|
|
|
|
|
|
|
: find-button ( gadget -- button )
|
|
|
|
[ button? ] find-parent ;
|
|
|
|
|
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? ;
|
|
|
|
|
2009-02-14 22:53:39 -05:00
|
|
|
PRIVATE>
|
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
: 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 ;
|
|
|
|
|
2009-09-08 12:43:47 -04:00
|
|
|
: button-enter ( button -- )
|
|
|
|
dup dup tooltip>> [ swap show-status ] [ drop ] if* button-update ;
|
|
|
|
|
|
|
|
: button-leave ( button -- )
|
|
|
|
dup "" swap show-status button-update ;
|
|
|
|
|
2009-02-14 22:53:39 -05:00
|
|
|
: button-clicked ( button -- )
|
|
|
|
dup button-update
|
|
|
|
dup button-rollover?
|
|
|
|
[ dup quot>> call( button -- ) ] [ drop ] if ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
button H{
|
|
|
|
{ T{ button-up } [ button-clicked ] }
|
|
|
|
{ T{ button-down } [ button-update ] }
|
2009-09-08 12:43:47 -04:00
|
|
|
{ mouse-leave [ button-leave ] }
|
|
|
|
{ mouse-enter [ button-enter ] }
|
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
|
|
|
|
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 ;
|
|
|
|
|
2009-02-14 22:53:39 -05:00
|
|
|
M: button-pen pen-background
|
|
|
|
button-pen pen-background ;
|
|
|
|
|
|
|
|
M: button-pen pen-foreground
|
|
|
|
button-pen pen-foreground ;
|
|
|
|
|
|
|
|
<PRIVATE
|
|
|
|
|
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
|
2009-02-14 22:53:39 -05:00
|
|
|
f f COLOR: dark-gray <solid> f f <button-pen> >>interior
|
2008-09-01 03:52:25 -04:00
|
|
|
align-left ; inline
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2009-02-14 22:53:39 -05:00
|
|
|
PRIVATE>
|
|
|
|
|
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 22:53:39 -05:00
|
|
|
<PRIVATE
|
|
|
|
|
|
|
|
: <border-button-state-pen> ( prefix background foreground -- pen )
|
2009-02-14 21:46:13 -05:00
|
|
|
[
|
|
|
|
"-left" "-middle" "-right"
|
2009-02-14 22:53:39 -05:00
|
|
|
[ append theme-image ] tri-curry@ tri
|
|
|
|
] 2dip <tile-pen> ;
|
|
|
|
|
2009-09-08 15:22:13 -04:00
|
|
|
CONSTANT: button-background COLOR: FactorLightTan
|
|
|
|
CONSTANT: button-clicked-background COLOR: FactorDarkSlateBlue
|
|
|
|
|
2009-02-14 22:53:39 -05:00
|
|
|
: <border-button-pen> ( -- pen )
|
2009-09-08 12:43:47 -04:00
|
|
|
"button" button-background button-clicked-background
|
|
|
|
<border-button-state-pen> dup
|
|
|
|
"button-clicked" button-clicked-background COLOR: white
|
|
|
|
<border-button-state-pen> dup dup
|
2009-02-14 22:53:39 -05:00
|
|
|
<button-pen> ;
|
2008-06-18 23:30:54 -04:00
|
|
|
|
2009-02-14 20:50:22 -05:00
|
|
|
: border-button-theme ( gadget -- gadget )
|
2009-09-08 12:43:47 -04:00
|
|
|
dup children>> first font>> t >>bold? drop
|
2009-02-14 21:46:13 -05:00
|
|
|
horizontal >>orientation
|
2009-02-14 20:50:22 -05:00
|
|
|
<border-button-pen> >>interior
|
2009-02-14 21:46:13 -05:00
|
|
|
dup dup interior>> pen-pref-dim >>min-dim
|
|
|
|
{ 10 0 } >>size ; inline
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2009-02-14 22:53:39 -05:00
|
|
|
PRIVATE>
|
|
|
|
|
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-14 22:53:39 -05:00
|
|
|
<PRIVATE
|
|
|
|
|
|
|
|
: <checkmark-pen> ( -- pen )
|
2009-02-13 02:43:03 -05:00
|
|
|
"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-14 22:53:39 -05:00
|
|
|
<checkmark-pen> >>interior
|
2009-02-13 02:43:03 -05:00
|
|
|
dup dup interior>> pen-pref-dim >>dim ;
|
2007-10-31 01:04:54 -04:00
|
|
|
|
|
|
|
: toggle-model ( model -- )
|
|
|
|
[ not ] change-model ;
|
|
|
|
|
2009-02-14 22:53:39 -05:00
|
|
|
PRIVATE>
|
|
|
|
|
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-14 22:53:39 -05:00
|
|
|
<PRIVATE
|
|
|
|
|
|
|
|
: <radio-pen> ( -- pen )
|
2009-02-13 02:43:03 -05:00
|
|
|
"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>
|
2009-02-14 22:53:39 -05:00
|
|
|
<radio-pen> >>interior
|
2009-02-13 02:43:03 -05:00
|
|
|
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
|
|
|
|
2009-02-14 22:53:39 -05:00
|
|
|
PRIVATE>
|
|
|
|
|
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
|
|
|
|
2009-02-18 22:00:31 -05:00
|
|
|
: command-button-quot ( target command -- quot )
|
|
|
|
'[ _ _ invoke-command ] ;
|
2009-02-15 07:01:53 -05:00
|
|
|
|
2009-09-08 12:43:47 -04:00
|
|
|
: gesture>tooltip ( gesture -- str )
|
|
|
|
[ gesture>string "Shortcut: " prepend ] [ "Shortcut Unassigned" ] if* ;
|
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
: <command-button> ( target gesture command -- button )
|
2009-09-08 12:43:47 -04:00
|
|
|
swapd [ command-name swap ] keep command-button-quot
|
|
|
|
'[ drop @ ] <border-button> swap gesture>tooltip >>tooltip ;
|
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
|
2009-02-14 21:46:13 -05:00
|
|
|
{ 5 5 } >>gap
|
2008-09-27 15:36:04 -04:00
|
|
|
swap
|
2009-02-15 03:15:51 -05:00
|
|
|
[ [ "toolbar" ] dip class command-map commands>> ]
|
|
|
|
[ '[ [ _ ] 2dip <command-button> add-gadget ] ]
|
|
|
|
bi assoc-each ;
|
2008-11-21 00:54:27 -05:00
|
|
|
|
|
|
|
: add-toolbar ( track -- track )
|
2009-02-14 21:46:13 -05:00
|
|
|
dup <toolbar> { 3 3 } <border> align-left f track-add ;
|