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
|
|
|
|
|
|
|
// A Button represents a clickable button with some text.
|
|
|
|
type Button struct {
|
2014-06-10 13:59:39 -05:00
|
|
|
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,
|
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-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 {
|
|
|
|
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-06-30 21:48:12 -05:00
|
|
|
b.sysData.event = func() {
|
|
|
|
window.winhandler.Event(Clicked, b)
|
|
|
|
}
|
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-06-25 21:17:26 -05:00
|
|
|
func (b *Button) allocate(x int, y int, width int, height int, d *sysSizeData) []*allocation {
|
|
|
|
return []*allocation{&allocation{
|
2014-06-10 13:59:39 -05:00
|
|
|
x: x,
|
|
|
|
y: y,
|
|
|
|
width: width,
|
|
|
|
height: height,
|
2014-06-25 21:17:26 -05:00
|
|
|
this: b,
|
|
|
|
}}
|
2014-02-13 04:28:26 -06:00
|
|
|
}
|
2014-02-24 09:56:35 -06:00
|
|
|
|
2014-06-25 21:17:26 -05:00
|
|
|
func (b *Button) preferredSize(d *sysSizeData) (width int, height int) {
|
|
|
|
return b.sysData.preferredSize(d)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Button) commitResize(a *allocation, d *sysSizeData) {
|
2014-06-25 22:21:57 -05:00
|
|
|
b.sysData.commitResize(a, d)
|
2014-06-25 21:17:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Button) getAuxResizeInfo(d *sysSizeData) {
|
|
|
|
b.sysData.getAuxResizeInfo(d)
|
2014-02-24 09:56:35 -06:00
|
|
|
}
|