2014-07-12 10:29:54 -05:00
|
|
|
// 12 july 2014
|
|
|
|
|
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2014-07-17 22:11:16 -05:00
|
|
|
// #include "winapi_windows.h"
|
|
|
|
import "C"
|
|
|
|
|
2014-07-12 10:29:54 -05:00
|
|
|
type window struct {
|
2014-07-17 22:11:16 -05:00
|
|
|
hwnd C.HWND
|
2014-07-12 10:29:54 -05:00
|
|
|
shownbefore bool
|
|
|
|
|
2014-07-15 21:44:27 -05:00
|
|
|
child Control
|
|
|
|
|
2014-07-12 10:29:54 -05:00
|
|
|
closing *event
|
2014-07-16 21:31:48 -05:00
|
|
|
|
|
|
|
spaced bool
|
2014-07-12 10:29:54 -05:00
|
|
|
}
|
|
|
|
|
2014-07-17 22:11:16 -05:00
|
|
|
const windowclassname = ""
|
2014-07-12 10:29:54 -05:00
|
|
|
var windowclassptr = syscall.StringToUTF16Ptr(windowclassname)
|
|
|
|
|
|
|
|
func makeWindowWindowClass() error {
|
2014-07-17 22:11:16 -05:00
|
|
|
var errmsg *C.char
|
|
|
|
|
|
|
|
err := C.makeWindowWindowClass(&errmsg)
|
|
|
|
if err != 0 || errmsg != nil {
|
|
|
|
return fmt.Errorf("%s: %v", C.GoString(errmsg), syscall.Errno(err))
|
2014-07-12 10:29:54 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func newWindow(title string, width int, height int) *Request {
|
|
|
|
c := make(chan interface{})
|
|
|
|
return &Request{
|
|
|
|
op: func() {
|
|
|
|
w := &window{
|
|
|
|
// hwnd set in WM_CREATE handler
|
|
|
|
closing: newEvent(),
|
|
|
|
}
|
2014-07-17 22:11:16 -05:00
|
|
|
hwnd := C.newWindow(toUTF16(title), C.int(width), C.int(height), unsafe.Pointer(w))
|
|
|
|
if hwnd != w.hwnd {
|
2014-07-12 10:29:54 -05:00
|
|
|
panic(fmt.Errorf("inconsistency: hwnd returned by CreateWindowEx() (%p) and hwnd stored in window (%p) differ", hwnd, w.hwnd))
|
|
|
|
}
|
|
|
|
c <- w
|
|
|
|
},
|
|
|
|
resp: c,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *window) SetControl(control Control) *Request {
|
|
|
|
c := make(chan interface{})
|
|
|
|
return &Request{
|
|
|
|
op: func() {
|
2014-07-15 21:44:27 -05:00
|
|
|
if w.child != nil { // unparent existing control
|
|
|
|
w.child.unparent()
|
|
|
|
}
|
|
|
|
control.unparent()
|
|
|
|
control.parent(w)
|
|
|
|
w.child = control
|
2014-07-12 10:29:54 -05:00
|
|
|
c <- struct{}{}
|
|
|
|
},
|
|
|
|
resp: c,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *window) Title() *Request {
|
|
|
|
c := make(chan interface{})
|
|
|
|
return &Request{
|
|
|
|
op: func() {
|
2014-07-17 22:11:16 -05:00
|
|
|
c <- C.GoString(C.getWindowText(w.hwnd))
|
2014-07-12 10:29:54 -05:00
|
|
|
},
|
|
|
|
resp: c,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *window) SetTitle(title string) *Request {
|
|
|
|
c := make(chan interface{})
|
|
|
|
return &Request{
|
|
|
|
op: func() {
|
2014-07-17 22:11:16 -05:00
|
|
|
C.setWindowText(w.hwnd, toUTF16(title))
|
2014-07-12 10:29:54 -05:00
|
|
|
c <- struct{}{}
|
|
|
|
},
|
|
|
|
resp: c,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *window) Show() *Request {
|
|
|
|
c := make(chan interface{})
|
|
|
|
return &Request{
|
|
|
|
op: func() {
|
|
|
|
if !w.shownbefore {
|
2014-07-17 22:11:16 -05:00
|
|
|
C.ShowWindow(w.hwnd, C.nCmdShow)
|
|
|
|
C.updateWindow(w.hwnd)
|
2014-07-12 10:29:54 -05:00
|
|
|
w.shownbefore = true
|
|
|
|
} else {
|
2014-07-17 22:11:16 -05:00
|
|
|
C.ShowWindow(w.hwnd, C.SW_SHOW)
|
2014-07-12 10:29:54 -05:00
|
|
|
}
|
|
|
|
c <- struct{}{}
|
|
|
|
},
|
|
|
|
resp: c,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *window) Hide() *Request {
|
|
|
|
c := make(chan interface{})
|
|
|
|
return &Request{
|
|
|
|
op: func() {
|
2014-07-17 22:11:16 -05:00
|
|
|
C.ShowWindow(w.hwnd, C.SW_HIDE)
|
2014-07-12 10:29:54 -05:00
|
|
|
c <- struct{}{}
|
|
|
|
},
|
|
|
|
resp: c,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *window) Close() *Request {
|
|
|
|
c := make(chan interface{})
|
|
|
|
return &Request{
|
|
|
|
op: func() {
|
2014-07-17 22:11:16 -05:00
|
|
|
C.windowClose(w.hwnd)
|
2014-07-12 10:29:54 -05:00
|
|
|
c <- struct{}{}
|
|
|
|
},
|
|
|
|
resp: c,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *window) OnClosing(e func(Doer) bool) *Request {
|
|
|
|
c := make(chan interface{})
|
|
|
|
return &Request{
|
|
|
|
op: func() {
|
|
|
|
w.closing.setbool(e)
|
|
|
|
c <- struct{}{}
|
|
|
|
},
|
|
|
|
resp: c,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-17 22:11:16 -05:00
|
|
|
//export storeWindowHWND
|
|
|
|
func storeWindowHWND(data unsafe.Pointer, hwnd C.HWND) {
|
|
|
|
w := (*window)(data)
|
|
|
|
w.hwnd = hwnd
|
|
|
|
}
|
|
|
|
|
|
|
|
//export windowResize
|
|
|
|
func windowResize(data unsafe.Pointer, r *C.RECT) {
|
|
|
|
w := (*window)(data)
|
|
|
|
w.doresize(int(r.right - r.left), int(r.bottom - r.top))
|
|
|
|
}
|
|
|
|
|
|
|
|
//export windowClosing
|
|
|
|
func windowClosing(data unsafe.Pointer) {
|
|
|
|
w := (*window)(data)
|
|
|
|
close := w.closing.fire()
|
|
|
|
if close {
|
|
|
|
C.windowClose(w.hwnd)
|
2014-07-12 10:29:54 -05:00
|
|
|
}
|
|
|
|
}
|