andlabs-ui/redo/controls_windows.go

115 lines
2.0 KiB
Go
Raw Normal View History

// 15 july 2014
package ui
import (
"fmt"
"syscall"
"unsafe"
)
2014-07-17 21:16:32 -05:00
// #include "winapi_windows.h"
import "C"
type widgetbase struct {
2014-07-17 21:16:32 -05:00
hwnd C.HWND
}
2014-07-17 21:16:32 -05:00
func newWidget(class C.LPCWSTR, style uintptr, extstyle uintptr) *widgetbase {
return &widgetbase{
2014-07-17 21:16:32 -05:00
hwnd: C.newWidget(class, style, extstyle),
}
}
// these few methods are embedded by all the various Controls since they all will do the same thing
func (w *widgetbase) unparent() {
2014-07-17 21:16:32 -05:00
C.controlSetParent(w.hwnd, C.msgwin)
}
func (w *widgetbase) parent(win *window) {
2014-07-17 21:16:32 -05:00
C.controlSetParent(w.hwnd, win.hwnd)
}
// don't embed these as exported; let each Control decide if it should
func (w *widgetbase) text() *Request {
c := make(chan interface{})
return &Request{
op: func() {
c <- getWindowText(w.hwnd)
},
resp: c,
}
}
func (w *widgetbase) settext(text string, results ...t_LRESULT) *Request {
c := make(chan interface{})
return &Request{
op: func() {
2014-07-17 21:16:32 -05:00
C.setWindowText(w.hwnd, toUTF16(text))
c <- struct{}{}
},
resp: c,
}
}
type button struct {
*widgetbase
clicked *event
}
2014-07-17 21:16:32 -05:00
var buttonclass = toUTF16("BUTTON")
func newButton(text string) *Request {
c := make(chan interface{})
return &Request{
op: func() {
w := newWidget(buttonclass,
2014-07-17 21:16:32 -05:00
C.BS_PUSHBUTTON | C.WS_TABSTOP,
0)
2014-07-17 21:16:32 -05:00
C.setWindowText(w.hwnd, toUTF16(text))
b := &button{
widgetbase: w,
clicked: newEvent(),
}
2014-07-17 21:16:32 -05:00
C.setButtonSubclass(w.hwnd, unsafe.Pointer(b))
c <- b
},
resp: c,
}
}
func (b *button) OnClicked(e func(c Doer)) *Request {
c := make(chan interface{})
return &Request{
op: func() {
b.clicked.set(e)
c <- struct{}{}
},
resp: c,
}
}
func (b *button) Text() *Request {
return b.text()
}
func (b *button) SetText(text string) *Request {
return b.settext(text)
}
var buttonsubprocptr uintptr
// to avoid recursive initialization loop
func init() {
buttonsubprocptr = syscall.NewCallback(buttonSubProc)
}
2014-07-17 21:16:32 -05:00
//export buttonClicked
func buttonClicked(data unsafe.Pointer) {
b := (*button)(data)
b.clicked.fire()
println("button clicked")
}