2014-08-02 21:35:58 -05:00
|
|
|
// 15 july 2014
|
|
|
|
|
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
// #include "winapi_windows.h"
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
type button struct {
|
2014-08-03 20:52:21 -05:00
|
|
|
_hwnd C.HWND
|
|
|
|
_textlen C.LONG
|
|
|
|
clicked *event
|
2014-08-02 21:35:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
var buttonclass = toUTF16("BUTTON")
|
|
|
|
|
2014-08-03 08:18:35 -05:00
|
|
|
func newButton(text string) *button {
|
2014-08-03 20:52:21 -05:00
|
|
|
hwnd := 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)
|
|
|
|
b := &button{
|
2014-08-03 20:52:21 -05:00
|
|
|
_hwnd: hwnd,
|
|
|
|
clicked: newEvent(),
|
2014-08-02 21:35:58 -05:00
|
|
|
}
|
2014-08-03 20:52:21 -05:00
|
|
|
b.SetText(text)
|
|
|
|
C.controlSetControlFont(b._hwnd)
|
|
|
|
C.setButtonSubclass(b._hwnd, unsafe.Pointer(b))
|
2014-08-02 21:35:58 -05:00
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *button) OnClicked(e func()) {
|
|
|
|
b.clicked.set(e)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *button) Text() string {
|
2014-08-03 20:52:21 -05:00
|
|
|
return baseText(b)
|
2014-08-02 21:35:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *button) SetText(text string) {
|
2014-08-03 20:52:21 -05:00
|
|
|
baseSetText(b, text)
|
2014-08-02 21:35:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//export buttonClicked
|
|
|
|
func buttonClicked(data unsafe.Pointer) {
|
|
|
|
b := (*button)(data)
|
|
|
|
b.clicked.fire()
|
|
|
|
println("button clicked")
|
|
|
|
}
|
|
|
|
|
2014-08-03 20:52:21 -05:00
|
|
|
func (b *button) hwnd() C.HWND {
|
|
|
|
return b._hwnd
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *button) textlen() C.LONG {
|
|
|
|
return b._textlen
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *button) settextlen(len C.LONG) {
|
|
|
|
b._textlen = len
|
|
|
|
}
|
|
|
|
|
2014-08-03 08:18:35 -05:00
|
|
|
func (b *button) setParent(p *controlParent) {
|
2014-08-03 20:52:21 -05:00
|
|
|
basesetParent(b, p)
|
2014-08-03 08:18:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2014-08-03 20:52:21 -05:00
|
|
|
if C.SendMessageW(b._hwnd, C.BCM_GETIDEALSIZE, 0, C.LPARAM(uintptr(unsafe.Pointer(&size)))) != C.FALSE {
|
2014-08-02 21:35:58 -05:00
|
|
|
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))
|
2014-08-03 20:52:21 -05:00
|
|
|
return xmargins + int(b._textlen), fromdlgunitsY(buttonHeight, d)
|
2014-08-02 21:35:58 -05:00
|
|
|
}
|
2014-08-03 08:18:35 -05:00
|
|
|
|
|
|
|
func (b *button) commitResize(a *allocation, d *sizing) {
|
2014-08-03 20:52:21 -05:00
|
|
|
basecommitResize(b, a, d)
|
2014-08-03 08:18:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|