forked from leaanthony/winc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controlbase.go
522 lines (425 loc) · 11.5 KB
/
controlbase.go
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
/*
* Copyright (C) 2019 The Winc Authors. All Rights Reserved.
* Copyright (C) 2010-2013 Allen Dang. All Rights Reserved.
*/
package winc
import (
"fmt"
"runtime"
"sync"
"syscall"
"unsafe"
"golang.org/x/sys/windows"
"github.com/leaanthony/winc/w32"
)
type ControlBase struct {
hwnd w32.HWND
font *Font
parent Controller
contextMenu *MenuItem
isForm bool
minWidth, minHeight int
maxWidth, maxHeight int
// General events
onCreate EventManager
onClose EventManager
// Focus events
onKillFocus EventManager
onSetFocus EventManager
// Drag and drop events
onDropFiles EventManager
// Mouse events
onLBDown EventManager
onLBUp EventManager
onLBDbl EventManager
onMBDown EventManager
onMBUp EventManager
onRBDown EventManager
onRBUp EventManager
onRBDbl EventManager
onMouseMove EventManager
// use MouseControl to capture onMouseHover and onMouseLeave events.
onMouseHover EventManager
onMouseLeave EventManager
// Keyboard events
onKeyUp EventManager
// Paint events
onPaint EventManager
onSize EventManager
m sync.Mutex
dispatchq []func()
}
// initControl is called by controls: edit, button, treeview, listview, and so on.
func (cba *ControlBase) InitControl(className string, parent Controller, exstyle, style uint) {
cba.hwnd = CreateWindow(className, parent, exstyle, style)
if cba.hwnd == 0 {
panic("cannot create window for " + className)
}
cba.parent = parent
}
// InitWindow is called by custom window based controls such as split, panel, etc.
func (cba *ControlBase) InitWindow(className string, parent Controller, exstyle, style uint) {
RegClassOnlyOnce(className)
cba.hwnd = CreateWindow(className, parent, exstyle, style)
if cba.hwnd == 0 {
panic("cannot create window for " + className)
}
cba.parent = parent
}
// SetTheme for TreeView and ListView controls.
func (cba *ControlBase) SetTheme(appName string) error {
if hr := w32.SetWindowTheme(cba.hwnd, syscall.StringToUTF16Ptr(appName), nil); w32.FAILED(hr) {
return fmt.Errorf("SetWindowTheme %d", hr)
}
return nil
}
func (cba *ControlBase) Handle() w32.HWND {
return cba.hwnd
}
func (cba *ControlBase) SetHandle(hwnd w32.HWND) {
cba.hwnd = hwnd
}
func (cba *ControlBase) GetSystemXYDPI() (w32.UINT, w32.UINT) {
screen := w32.GetDC(0)
x := w32.GetDeviceCaps(screen, w32.LOGPIXELSX)
y := w32.GetDeviceCaps(screen, w32.LOGPIXELSY)
w32.ReleaseDC(0, screen)
return w32.UINT(x), w32.UINT(y)
}
func (cba *ControlBase) GetWindowDPI() (w32.UINT, w32.UINT) {
monitor := w32.MonitorFromWindow(cba.hwnd, w32.MONITOR_DEFAULTTOPRIMARY)
var dpiX, dpiY w32.UINT
w32.GetDPIForMonitor(monitor, w32.MDT_EFFECTIVE_DPI, &dpiX, &dpiY)
return dpiX, dpiY
}
func (cba *ControlBase) SetAndClearStyleBits(set, clear uint32) error {
style := uint32(w32.GetWindowLong(cba.hwnd, w32.GWL_STYLE))
if style == 0 {
return fmt.Errorf("GetWindowLong")
}
if newStyle := style&^clear | set; newStyle != style {
if w32.SetWindowLong(cba.hwnd, w32.GWL_STYLE, newStyle) == 0 {
return fmt.Errorf("SetWindowLong")
}
}
return nil
}
func (cba *ControlBase) SetIsForm(isform bool) {
cba.isForm = isform
}
func (cba *ControlBase) SetText(caption string) {
w32.SetWindowText(cba.hwnd, caption)
}
func (cba *ControlBase) Text() string {
return w32.GetWindowText(cba.hwnd)
}
func (cba *ControlBase) Close() {
UnRegMsgHandler(cba.hwnd)
w32.DestroyWindow(cba.hwnd)
}
func (cba *ControlBase) SetTranslucentBackground() {
var accent = w32.ACCENT_POLICY{
AccentState: w32.ACCENT_ENABLE_BLURBEHIND,
}
var data w32.WINDOWCOMPOSITIONATTRIBDATA
data.Attrib = w32.WCA_ACCENT_POLICY
data.PvData = unsafe.Pointer(&accent)
data.CbData = unsafe.Sizeof(accent)
w32.SetWindowCompositionAttribute(cba.hwnd, &data)
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
func (cba *ControlBase) clampSize(width, height int) (int, int) {
if cba.minWidth != 0 {
width = max(width, cba.minWidth)
}
if cba.maxWidth != 0 {
width = min(width, cba.maxWidth)
}
if cba.minHeight != 0 {
height = max(height, cba.minHeight)
}
if cba.maxHeight != 0 {
height = min(height, cba.maxHeight)
}
return width, height
}
func supportsPerMonitorDPI() bool {
shcore := windows.NewLazyDLL("shcore.dll")
getDpiForMonitor := shcore.NewProc("GetDpiForMonitor")
shcoreErr := getDpiForMonitor.Find()
return shcoreErr == nil
}
func (cba *ControlBase) SetSize(width, height int) {
x, y := cba.Pos()
width, height = cba.clampSize(width, height)
width, height = cba.scaleWithWindowDPI(width, height)
w32.MoveWindow(cba.hwnd, x, y, width, height, true)
}
func (cba *ControlBase) SetMinSize(width, height int) {
cba.minWidth = width
cba.minHeight = height
// Ensure we set max if min > max
if cba.maxWidth > 0 {
cba.maxWidth = max(cba.minWidth, cba.maxWidth)
}
if cba.maxHeight > 0 {
cba.maxHeight = max(cba.minHeight, cba.maxHeight)
}
x, y := cba.Pos()
currentWidth, currentHeight := cba.Size()
clampedWidth, clampedHeight := cba.clampSize(currentWidth, currentHeight)
if clampedWidth != currentWidth || clampedHeight != currentHeight {
w32.MoveWindow(cba.hwnd, x, y, clampedWidth, clampedHeight, true)
}
}
func (cba *ControlBase) SetMaxSize(width, height int) {
cba.maxWidth = width
cba.maxHeight = height
// Ensure we set min if max > min
if cba.minWidth > 0 {
cba.minWidth = min(cba.maxWidth, cba.minWidth)
}
if cba.maxHeight > 0 {
cba.minHeight = min(cba.maxHeight, cba.minHeight)
}
x, y := cba.Pos()
currentWidth, currentHeight := cba.Size()
clampedWidth, clampedHeight := cba.clampSize(currentWidth, currentHeight)
if clampedWidth != currentWidth || clampedHeight != currentHeight {
w32.MoveWindow(cba.hwnd, x, y, clampedWidth, clampedHeight, true)
}
}
func (cba *ControlBase) Size() (width, height int) {
rect := w32.GetWindowRect(cba.hwnd)
width = int(rect.Right - rect.Left)
height = int(rect.Bottom - rect.Top)
return
}
func (cba *ControlBase) Width() int {
rect := w32.GetWindowRect(cba.hwnd)
return int(rect.Right - rect.Left)
}
func (cba *ControlBase) Height() int {
rect := w32.GetWindowRect(cba.hwnd)
return int(rect.Bottom - rect.Top)
}
func (cba *ControlBase) SetPos(x, y int) {
info := getMonitorInfo(cba.hwnd)
workRect := info.RcWork
w32.SetWindowPos(cba.hwnd, w32.HWND_TOP, int(workRect.Left)+x, int(workRect.Top)+y, 0, 0, w32.SWP_NOSIZE)
}
func (cba *ControlBase) Pos() (x, y int) {
rect := w32.GetWindowRect(cba.hwnd)
x = int(rect.Left)
y = int(rect.Top)
if !cba.isForm && cba.parent != nil {
x, y, _ = w32.ScreenToClient(cba.parent.Handle(), x, y)
}
return
}
func (cba *ControlBase) Visible() bool {
return w32.IsWindowVisible(cba.hwnd)
}
func (cba *ControlBase) ToggleVisible() bool {
visible := w32.IsWindowVisible(cba.hwnd)
if visible {
cba.Hide()
} else {
cba.Show()
}
return !visible
}
func (cba *ControlBase) ContextMenu() *MenuItem {
return cba.contextMenu
}
func (cba *ControlBase) SetContextMenu(menu *MenuItem) {
cba.contextMenu = menu
}
func (cba *ControlBase) Bounds() *Rect {
rect := w32.GetWindowRect(cba.hwnd)
if cba.isForm {
return &Rect{*rect}
}
return ScreenToClientRect(cba.hwnd, rect)
}
func (cba *ControlBase) ClientRect() *Rect {
rect := w32.GetClientRect(cba.hwnd)
return ScreenToClientRect(cba.hwnd, rect)
}
func (cba *ControlBase) ClientWidth() int {
rect := w32.GetClientRect(cba.hwnd)
return int(rect.Right - rect.Left)
}
func (cba *ControlBase) ClientHeight() int {
rect := w32.GetClientRect(cba.hwnd)
return int(rect.Bottom - rect.Top)
}
func (cba *ControlBase) Show() {
w32.ShowWindow(cba.hwnd, w32.SW_SHOWDEFAULT)
}
func (cba *ControlBase) Hide() {
w32.ShowWindow(cba.hwnd, w32.SW_HIDE)
}
func (cba *ControlBase) Enabled() bool {
return w32.IsWindowEnabled(cba.hwnd)
}
func (cba *ControlBase) SetEnabled(b bool) {
w32.EnableWindow(cba.hwnd, b)
}
func (cba *ControlBase) SetFocus() {
w32.SetFocus(cba.hwnd)
}
func (cba *ControlBase) Invalidate(erase bool) {
// pRect := w32.GetClientRect(cba.hwnd)
// if cba.isForm {
// w32.InvalidateRect(cba.hwnd, pRect, erase)
// } else {
// rc := ScreenToClientRect(cba.hwnd, pRect)
// w32.InvalidateRect(cba.hwnd, rc.GetW32Rect(), erase)
// }
w32.InvalidateRect(cba.hwnd, nil, erase)
}
func (cba *ControlBase) Parent() Controller {
return cba.parent
}
func (cba *ControlBase) SetParent(parent Controller) {
cba.parent = parent
}
func (cba *ControlBase) Font() *Font {
return cba.font
}
func (cba *ControlBase) SetFont(font *Font) {
w32.SendMessage(cba.hwnd, w32.WM_SETFONT, uintptr(font.hfont), 1)
cba.font = font
}
func (cba *ControlBase) EnableDragAcceptFiles(b bool) {
w32.DragAcceptFiles(cba.hwnd, b)
}
func (cba *ControlBase) InvokeRequired() bool {
if cba.hwnd == 0 {
return false
}
windowThreadId, _ := w32.GetWindowThreadProcessId(cba.hwnd)
currentThreadId := w32.GetCurrentThreadId()
return windowThreadId != currentThreadId
}
func (cba *ControlBase) Invoke(f func()) {
if cba.tryInvokeOnCurrentGoRoutine(f) {
return
}
cba.m.Lock()
cba.dispatchq = append(cba.dispatchq, f)
cba.m.Unlock()
w32.PostMessage(cba.hwnd, wmInvokeCallback, 0, 0)
}
func (cba *ControlBase) PreTranslateMessage(msg *w32.MSG) bool {
if msg.Message == w32.WM_GETDLGCODE {
println("pretranslate, WM_GETDLGCODE")
}
return false
}
//Events
func (cba *ControlBase) OnCreate() *EventManager {
return &cba.onCreate
}
func (cba *ControlBase) OnClose() *EventManager {
return &cba.onClose
}
func (cba *ControlBase) OnKillFocus() *EventManager {
return &cba.onKillFocus
}
func (cba *ControlBase) OnSetFocus() *EventManager {
return &cba.onSetFocus
}
func (cba *ControlBase) OnDropFiles() *EventManager {
return &cba.onDropFiles
}
func (cba *ControlBase) OnLBDown() *EventManager {
return &cba.onLBDown
}
func (cba *ControlBase) OnLBUp() *EventManager {
return &cba.onLBUp
}
func (cba *ControlBase) OnLBDbl() *EventManager {
return &cba.onLBDbl
}
func (cba *ControlBase) OnMBDown() *EventManager {
return &cba.onMBDown
}
func (cba *ControlBase) OnMBUp() *EventManager {
return &cba.onMBUp
}
func (cba *ControlBase) OnRBDown() *EventManager {
return &cba.onRBDown
}
func (cba *ControlBase) OnRBUp() *EventManager {
return &cba.onRBUp
}
func (cba *ControlBase) OnRBDbl() *EventManager {
return &cba.onRBDbl
}
func (cba *ControlBase) OnMouseMove() *EventManager {
return &cba.onMouseMove
}
func (cba *ControlBase) OnMouseHover() *EventManager {
return &cba.onMouseHover
}
func (cba *ControlBase) OnMouseLeave() *EventManager {
return &cba.onMouseLeave
}
func (cba *ControlBase) OnPaint() *EventManager {
return &cba.onPaint
}
func (cba *ControlBase) OnSize() *EventManager {
return &cba.onSize
}
func (cba *ControlBase) OnKeyUp() *EventManager {
return &cba.onKeyUp
}
func (cba *ControlBase) scaleWithWindowDPI(width, height int) (int, int) {
var dpix, dpiy w32.UINT
if supportsPerMonitorDPI() {
dpix, dpiy = cba.GetWindowDPI()
} else {
dpix, dpiy = cba.GetSystemXYDPI()
}
DPIScaleX := dpix / 96.0
DPIScaleY := dpiy / 96.0
width *= int(DPIScaleX)
height *= int(DPIScaleY)
return width, height
}
func (cba *ControlBase) tryInvokeOnCurrentGoRoutine(f func()) bool {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
if cba.InvokeRequired() {
return false
}
f()
return true
}
func (cba *ControlBase) invokeCallbacks() {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
if cba.InvokeRequired() {
panic("InvokeCallbacks must always be called on the window thread")
}
cba.m.Lock()
q := append([]func(){}, cba.dispatchq...)
cba.dispatchq = []func(){}
cba.m.Unlock()
for _, v := range q {
v()
}
}