1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
// 12 july 2014
package ui
import (
"fmt"
"syscall"
"unsafe"
)
type window struct {
hwnd uintptr
shownbefore bool
closing *event
}
const windowclassname = "gouiwindow"
var windowclassptr = syscall.StringToUTF16Ptr(windowclassname)
func makeWindowWindowClass() error {
var wc s_WNDCLASSW
wc.lpfnWndProc = syscall.NewCallback(windowWndProc)
wc.hInstance = hInstance
wc.hIcon = hDefaultIcon
wc.hCursor = hArrowCursor
wc.hbrBackground = c_COLOR_BTNFACE + 1
wc.lpszClassName = windowclassptr
res, err := f_RegisterClassW(&wc)
if res == 0 {
return fmt.Errorf("error registering Window window class: %v", err)
}
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(),
}
hwnd, err := f_CreateWindowExW(
0,
windowclassptr,
syscall.StringToUTF16Ptr(title),
c_WS_OVERLAPPEDWINDOW,
c_CW_USEDEFAULT, c_CW_USEDEFAULT,
uintptr(width), uintptr(height),
hNULL, hNULL, hInstance, unsafe.Pointer(w))
if hwnd == hNULL {
panic(fmt.Errorf("Window creation failed: %v", err))
} else if hwnd != w.hwnd {
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() {
// TODO unparent
// TODO reparent
c <- struct{}{}
},
resp: c,
}
}
func (w *window) Title() *Request {
c := make(chan interface{})
return &Request{
op: func() {
c <- getWindowText(w.hwnd)
},
resp: c,
}
}
func (w *window) SetTitle(title string) *Request {
c := make(chan interface{})
return &Request{
op: func() {
setWindowText(w.hwnd, title, []t_LRESULT{c_FALSE})
c <- struct{}{}
},
resp: c,
}
}
func (w *window) Show() *Request {
c := make(chan interface{})
return &Request{
op: func() {
if !w.shownbefore {
// TODO get rid of need for cast
f_ShowWindow(w.hwnd, uintptr(nCmdShow))
updateWindow(w.hwnd, "Window.Show()")
w.shownbefore = true
} else {
f_ShowWindow(w.hwnd, c_SW_SHOW)
}
c <- struct{}{}
},
resp: c,
}
}
func (w *window) Hide() *Request {
c := make(chan interface{})
return &Request{
op: func() {
f_ShowWindow(w.hwnd, c_SW_HIDE)
c <- struct{}{}
},
resp: c,
}
}
func (w *window) Close() *Request {
c := make(chan interface{})
return &Request{
op: func() {
// TODO
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,
}
}
func windowWndProc(hwnd uintptr, msg t_UINT, wParam t_WPARAM, lParam t_LPARAM) t_LRESULT {
w := (*window)(unsafe.Pointer(f_GetWindowLongPtrW(hwnd, c_GWLP_USERDATA)))
if w == nil {
// the lpParam is available during WM_NCCREATE and WM_CREATE
if msg == c_WM_NCCREATE {
storelpParam(hwnd, lParam)
w := (*window)(unsafe.Pointer(f_GetWindowLongPtrW(hwnd, c_GWLP_USERDATA)))
w.hwnd = hwnd
}
// act as if we're not ready yet, even during WM_NCCREATE (nothing important to the switch statement below happens here anyway)
return f_DefWindowProcW(hwnd, msg, wParam, lParam)
}
switch msg {
default:
return f_DefWindowProcW(hwnd, msg, wParam, lParam)
}
panic(fmt.Errorf("Window message %d does not return a value (bug in windowWndProc())", msg))
}
// TODO
func newButton(string)*Request{return nil}
|