2014-02-12 12:52:34 -06:00
// 12 february 2014
2014-02-19 10:41:10 -06:00
package ui
2014-02-12 12:52:34 -06:00
import (
"sync"
)
// A Button represents a clickable button with some text.
type Button struct {
// This channel gets a message when the button is clicked. Unlike other channels in this package, this channel is initialized to non-nil when creating a new button, and cannot be set to nil later.
Clicked chan struct { }
lock sync . Mutex
created bool
sysData * sysData
initText string
}
// NewButton creates a new button with the specified text.
func NewButton ( text string ) ( b * Button ) {
return & Button {
2014-02-14 10:02:59 -06:00
sysData : mksysdata ( c_button ) ,
2014-02-12 12:52:34 -06:00
initText : text ,
2014-02-18 09:53:15 -06:00
Clicked : Event ( ) ,
2014-02-12 12:52:34 -06:00
}
}
// SetText sets the button's text.
func ( b * Button ) SetText ( text string ) ( err error ) {
b . lock . Lock ( )
defer b . lock . Unlock ( )
2014-02-14 19:41:36 -06:00
if b . created {
return b . sysData . setText ( text )
}
2014-02-12 12:52:34 -06:00
b . initText = text
return nil
}
2014-02-15 14:51:06 -06:00
// Text returns the button's text.
func ( b * Button ) Text ( ) string {
b . lock . Lock ( )
defer b . lock . Unlock ( )
if b . created {
return b . sysData . text ( )
}
return b . initText
}
2014-02-14 10:12:08 -06:00
func ( b * Button ) make ( window * sysData ) error {
2014-02-12 12:52:34 -06:00
b . lock . Lock ( )
defer b . lock . Unlock ( )
2014-02-12 20:26:18 -06:00
b . sysData . event = b . Clicked
2014-02-15 12:07:46 -06:00
err := b . sysData . make ( b . initText , window )
2014-02-14 19:41:36 -06:00
if err != nil {
return err
}
b . created = true
return nil
2014-02-12 12:52:34 -06:00
}
2014-02-13 04:28:26 -06:00
func ( b * Button ) setRect ( x int , y int , width int , height int ) error {
b . lock . Lock ( )
defer b . lock . Unlock ( )
return b . sysData . setRect ( x , y , width , height )
}
2014-02-24 09:56:35 -06:00
func ( b * Button ) preferredSize ( ) ( width int , height int , err error ) {
b . lock . Lock ( )
defer b . lock . Unlock ( )
width , height = b . sysData . preferredSize ( )
return
}