2014-02-12 12:52:34 -06:00
|
|
|
// 12 february 2014
|
2014-03-12 20:55:45 -05:00
|
|
|
|
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 {
|
2014-03-12 20:47:39 -05:00
|
|
|
// Clicked gets a message when the button is clicked.
|
2014-06-03 21:54:28 -05:00
|
|
|
// You cannot change it once the Window containing the Button has been created.
|
2014-04-10 10:59:40 -05:00
|
|
|
// If you do not respond to this signal, nothing will happen.
|
2014-06-10 13:59:39 -05:00
|
|
|
Clicked chan struct{}
|
2014-02-12 12:52:34 -06:00
|
|
|
|
2014-06-10 13:59:39 -05:00
|
|
|
lock sync.Mutex
|
|
|
|
created bool
|
|
|
|
sysData *sysData
|
|
|
|
initText string
|
2014-02-12 12:52:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewButton creates a new button with the specified text.
|
|
|
|
func NewButton(text string) (b *Button) {
|
|
|
|
return &Button{
|
2014-06-10 13:59:39 -05:00
|
|
|
sysData: mksysdata(c_button),
|
|
|
|
initText: text,
|
|
|
|
Clicked: newEvent(),
|
2014-02-12 12:52:34 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetText sets the button's text.
|
2014-03-10 09:45:15 -05:00
|
|
|
func (b *Button) SetText(text string) {
|
2014-03-18 10:50:56 -05:00
|
|
|
b.lock.Lock()
|
|
|
|
defer b.lock.Unlock()
|
|
|
|
|
2014-02-14 19:41:36 -06:00
|
|
|
if b.created {
|
2014-03-10 09:45:15 -05:00
|
|
|
b.sysData.setText(text)
|
|
|
|
return
|
2014-02-14 19:41:36 -06:00
|
|
|
}
|
2014-02-12 12:52:34 -06:00
|
|
|
b.initText = text
|
|
|
|
}
|
|
|
|
|
2014-02-15 14:51:06 -06:00
|
|
|
// Text returns the button's text.
|
|
|
|
func (b *Button) Text() string {
|
2014-03-18 10:50:56 -05:00
|
|
|
b.lock.Lock()
|
|
|
|
defer b.lock.Unlock()
|
|
|
|
|
2014-02-15 14:51:06 -06:00
|
|
|
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-04-01 15:43:56 -05:00
|
|
|
err := b.sysData.make(window)
|
2014-02-14 19:41:36 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-04-01 15:43:56 -05:00
|
|
|
b.sysData.setText(b.initText)
|
2014-02-14 19:41:36 -06:00
|
|
|
b.created = true
|
|
|
|
return nil
|
2014-02-12 12:52:34 -06:00
|
|
|
}
|
|
|
|
|
2014-03-17 20:09:03 -05:00
|
|
|
func (b *Button) setRect(x int, y int, width int, height int, rr *[]resizerequest) {
|
|
|
|
*rr = append(*rr, resizerequest{
|
2014-06-10 13:59:39 -05:00
|
|
|
sysData: b.sysData,
|
|
|
|
x: x,
|
|
|
|
y: y,
|
|
|
|
width: width,
|
|
|
|
height: height,
|
2014-03-17 20:09:03 -05:00
|
|
|
})
|
2014-02-13 04:28:26 -06:00
|
|
|
}
|
2014-02-24 09:56:35 -06:00
|
|
|
|
2014-03-09 18:44:41 -05:00
|
|
|
func (b *Button) preferredSize() (width int, height int) {
|
|
|
|
return b.sysData.preferredSize()
|
2014-02-24 09:56:35 -06:00
|
|
|
}
|