2014-08-02 21:35:58 -05:00
|
|
|
// 15 july 2014
|
|
|
|
|
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
// #include "winapi_windows.h"
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
type button struct {
|
|
|
|
*controlbase
|
|
|
|
clicked *event
|
|
|
|
}
|
|
|
|
|
|
|
|
var buttonclass = toUTF16("BUTTON")
|
|
|
|
|
2014-08-03 08:18:35 -05:00
|
|
|
func newButton(text string) *button {
|
2014-08-02 21:35:58 -05:00
|
|
|
c := newControl(buttonclass,
|
2014-08-03 08:18:35 -05:00
|
|
|
C.BS_PUSHBUTTON | C.WS_TABSTOP,
|
2014-08-02 21:35:58 -05:00
|
|
|
0)
|
|
|
|
c.setText(text)
|
|
|
|
C.controlSetControlFont(c.hwnd)
|
|
|
|
b := &button{
|
|
|
|
controlbase: c,
|
|
|
|
clicked: newEvent(),
|
|
|
|
}
|
|
|
|
C.setButtonSubclass(b.hwnd, unsafe.Pointer(b))
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *button) OnClicked(e func()) {
|
|
|
|
b.clicked.set(e)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *button) Text() string {
|
|
|
|
return b.text()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *button) SetText(text string) {
|
|
|
|
b.setText(text)
|
|
|
|
}
|
|
|
|
|
|
|
|
//export buttonClicked
|
|
|
|
func buttonClicked(data unsafe.Pointer) {
|
|
|
|
b := (*button)(data)
|
|
|
|
b.clicked.fire()
|
|
|
|
println("button clicked")
|
|
|
|
}
|
|
|
|
|
2014-08-03 08:18:35 -05:00
|
|
|
func (b *button) setParent(p *controlParent) {
|
|
|
|
basesetParent(b.controlbase, p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *button) containerShow() {
|
|
|
|
basecontainerShow(b.controlbase)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *button) containerHide() {
|
|
|
|
basecontainerHide(b.controlbase)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *button) allocate(x int, y int, width int, height int, d *sizing) []*allocation {
|
|
|
|
return baseallocate(b, x, y, width, height, d)
|
|
|
|
}
|
|
|
|
|
2014-08-02 21:35:58 -05:00
|
|
|
const (
|
|
|
|
// from http://msdn.microsoft.com/en-us/library/windows/desktop/dn742486.aspx#sizingandspacing
|
|
|
|
buttonHeight = 14
|
|
|
|
)
|
|
|
|
|
2014-08-03 08:18:35 -05:00
|
|
|
func (b *button) preferredSize(d *sizing) (width, height int) {
|
|
|
|
// comctl32.dll version 6 thankfully provides a method to grab this...
|
2014-08-02 21:35:58 -05:00
|
|
|
var size C.SIZE
|
|
|
|
|
|
|
|
size.cx = 0 // explicitly ask for ideal size
|
|
|
|
size.cy = 0
|
|
|
|
if C.SendMessageW(b.hwnd, C.BCM_GETIDEALSIZE, 0, C.LPARAM(uintptr(unsafe.Pointer(&size)))) != C.FALSE {
|
|
|
|
return int(size.cx), int(size.cy)
|
|
|
|
}
|
|
|
|
// that failed, fall back
|
|
|
|
println("message failed; falling back")
|
|
|
|
// don't worry about the error return from GetSystemMetrics(); there's no way to tell (explicitly documented as such)
|
|
|
|
xmargins := 2 * int(C.GetSystemMetrics(C.SM_CXEDGE))
|
|
|
|
return xmargins + int(b.textlen), fromdlgunitsY(buttonHeight, d)
|
|
|
|
}
|
2014-08-03 08:18:35 -05:00
|
|
|
|
|
|
|
func (b *button) commitResize(a *allocation, d *sizing) {
|
|
|
|
basecommitResize(b.controlbase, a, d)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *button) getAuxResizeInfo(d *sizing) {
|
2014-08-03 19:42:45 -05:00
|
|
|
basegetAuxResizeInfo(b, d)
|
2014-08-03 08:18:35 -05:00
|
|
|
}
|