2014-07-15 21:29:45 -05:00
|
|
|
// 15 july 2014
|
|
|
|
|
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"syscall"
|
2014-07-17 14:10:26 -05:00
|
|
|
"unsafe"
|
2014-07-15 21:29:45 -05:00
|
|
|
)
|
|
|
|
|
2014-07-17 21:16:32 -05:00
|
|
|
// #include "winapi_windows.h"
|
|
|
|
import "C"
|
|
|
|
|
2014-07-15 21:29:45 -05:00
|
|
|
type widgetbase struct {
|
2014-07-17 21:16:32 -05:00
|
|
|
hwnd C.HWND
|
2014-07-15 21:29:45 -05:00
|
|
|
}
|
|
|
|
|
2014-07-17 21:16:32 -05:00
|
|
|
func newWidget(class C.LPCWSTR, style uintptr, extstyle uintptr) *widgetbase {
|
2014-07-15 21:29:45 -05:00
|
|
|
return &widgetbase{
|
2014-07-17 21:16:32 -05:00
|
|
|
hwnd: C.newWidget(class, style, extstyle),
|
2014-07-15 21:29:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
2014-07-15 21:29:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *widgetbase) parent(win *window) {
|
2014-07-17 21:16:32 -05:00
|
|
|
C.controlSetParent(w.hwnd, win.hwnd)
|
2014-07-15 21:29:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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() {
|
2014-07-17 22:45:12 -05:00
|
|
|
c <- getWindowText(w.hwnd)
|
2014-07-15 21:29:45 -05:00
|
|
|
},
|
|
|
|
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))
|
2014-07-15 21:29:45 -05:00
|
|
|
c <- struct{}{}
|
|
|
|
},
|
|
|
|
resp: c,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type button struct {
|
|
|
|
*widgetbase
|
2014-07-17 14:10:26 -05:00
|
|
|
clicked *event
|
2014-07-15 21:29:45 -05:00
|
|
|
}
|
|
|
|
|
2014-07-17 21:16:32 -05:00
|
|
|
var buttonclass = toUTF16("BUTTON")
|
2014-07-15 21:29:45 -05:00
|
|
|
|
|
|
|
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,
|
2014-07-15 21:29:45 -05:00
|
|
|
0)
|
2014-07-17 21:16:32 -05:00
|
|
|
C.setWindowText(w.hwnd, toUTF16(text))
|
2014-07-17 17:36:44 -05:00
|
|
|
b := &button{
|
2014-07-15 21:29:45 -05:00
|
|
|
widgetbase: w,
|
2014-07-17 14:10:26 -05:00
|
|
|
clicked: newEvent(),
|
2014-07-15 21:29:45 -05:00
|
|
|
}
|
2014-07-17 21:16:32 -05:00
|
|
|
C.setButtonSubclass(w.hwnd, unsafe.Pointer(b))
|
2014-07-17 17:36:44 -05:00
|
|
|
c <- b
|
2014-07-15 21:29:45 -05:00
|
|
|
},
|
|
|
|
resp: c,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *button) OnClicked(e func(c Doer)) *Request {
|
2014-07-17 14:10:26 -05:00
|
|
|
c := make(chan interface{})
|
|
|
|
return &Request{
|
|
|
|
op: func() {
|
|
|
|
b.clicked.set(e)
|
|
|
|
c <- struct{}{}
|
|
|
|
},
|
|
|
|
resp: c,
|
|
|
|
}
|
2014-07-15 21:29:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *button) Text() *Request {
|
|
|
|
return b.text()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *button) SetText(text string) *Request {
|
|
|
|
return b.settext(text)
|
|
|
|
}
|
2014-07-17 14:10:26 -05:00
|
|
|
|
2014-07-17 17:36:44 -05:00
|
|
|
var buttonsubprocptr uintptr
|
|
|
|
|
|
|
|
// to avoid recursive initialization loop
|
|
|
|
func init() {
|
|
|
|
buttonsubprocptr = syscall.NewCallback(buttonSubProc)
|
|
|
|
}
|
2014-07-17 14:10:26 -05:00
|
|
|
|
2014-07-17 21:16:32 -05:00
|
|
|
//export buttonClicked
|
|
|
|
func buttonClicked(data unsafe.Pointer) {
|
|
|
|
b := (*button)(data)
|
|
|
|
b.clicked.fire()
|
|
|
|
println("button clicked")
|
2014-07-17 14:10:26 -05:00
|
|
|
}
|