/
any_globals.go
250 lines (200 loc) · 7.59 KB
/
any_globals.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
package ui
import (
"unsafe"
"github.com/rodrigocfd/windigo/internal/util"
"github.com/rodrigocfd/windigo/ui/wm"
"github.com/rodrigocfd/windigo/win"
"github.com/rodrigocfd/windigo/win/co"
)
// Returns WM_INITDIALOG if parent is a dialog window, otherwise WM_CREATE.
func _CreateOrInitDialog(parent AnyParent) co.WM {
if parent.isDialog() {
return co.WM_INITDIALOG
}
return co.WM_CREATE
}
//------------------------------------------------------------------------------
var _globalUiFont win.HFONT // Global font, usually Segoe UI.
func _CreateGlobalUiFont() {
ncm := win.NONCLIENTMETRICS{}
ncm.SetCbSize()
win.SystemParametersInfo(co.SPI_GETNONCLIENTMETRICS,
ncm.CbSize(), unsafe.Pointer(&ncm), 0)
_globalUiFont = win.CreateFontIndirect(&ncm.LfMenuFont)
}
//------------------------------------------------------------------------------
var _globalCtrlId int = 20_000 // in-between Visual Studio Resource Editor values
func _NextCtrlId() int {
_globalCtrlId++
return _globalCtrlId
}
//------------------------------------------------------------------------------
var _globalDpi win.POINT // Global system DPI.
// Multiplies position and size by current DPI factor.
func _MultiplyDpi(pos *win.POINT, size *win.SIZE) {
if _globalDpi.X == 0 { // not initialized yet?
dc := win.HWND(0).GetDC()
_globalDpi.X = dc.GetDeviceCaps(co.GDC_LOGPIXELSX) // cache
_globalDpi.Y = dc.GetDeviceCaps(co.GDC_LOGPIXELSY)
win.HWND(0).ReleaseDC(dc)
}
if pos != nil {
pos.X = win.MulDiv(pos.X, _globalDpi.X, 96)
pos.Y = win.MulDiv(pos.Y, _globalDpi.Y, 96)
}
if size != nil {
size.Cx = win.MulDiv(size.Cx, _globalDpi.X, 96)
size.Cy = win.MulDiv(size.Cy, _globalDpi.Y, 96)
}
}
//------------------------------------------------------------------------------
// Overwrites X and Y if they are different from zero.
func _OwPt(point *win.POINT, newVal win.POINT) {
if newVal.X != 0 {
point.X = newVal.X
}
if newVal.Y != 0 {
point.Y = newVal.Y
}
}
// Overwrites Cx and Cy if they are different from zero.
func _OwSz(size *win.SIZE, newVal win.SIZE) {
if newVal.Cx != 0 {
size.Cx = newVal.Cx
}
if newVal.Cy != 0 {
size.Cy = newVal.Cy
}
}
//------------------------------------------------------------------------------
// Calculates the bound rectangle to fit the text with current system font.
func _CalcTextBoundBox(text string, considerAccelerators bool) win.SIZE {
isTextEmpty := false
if len(text) == 0 {
isTextEmpty = true
text = "Pj" // just a placeholder to get the text height
}
if considerAccelerators {
text = util.RemoveAccelAmpersands(text)
}
hwndDesktop := win.GetDesktopWindow()
hdcDesktop := hwndDesktop.GetDC()
defer hwndDesktop.ReleaseDC(hdcDesktop)
hdcCloned := hdcDesktop.CreateCompatibleDC()
defer hdcCloned.DeleteDC()
prevFont := hdcCloned.SelectObjectFont(_globalUiFont) // system font; already adjusted to current DPI
defer hdcCloned.SelectObjectFont(prevFont)
bounds := hdcCloned.GetTextExtentPoint32(text)
if isTextEmpty {
bounds.Cx = 0 // if no text was given, return just the height
}
return bounds
}
// Calculates the bound rectangle to fit the text with current system font,
// including the check box for a checkbox/radio.
func _CalcTextBoundBoxWithCheck(
text string, considerAccelerators bool) win.SIZE {
boundBox := _CalcTextBoundBox(text, considerAccelerators)
boundBox.Cx += win.GetSystemMetrics(co.SM_CXMENUCHECK) + // https://stackoverflow.com/a/1165052/6923555
win.GetSystemMetrics(co.SM_CXEDGE)
cyCheck := win.GetSystemMetrics(co.SM_CYMENUCHECK)
if cyCheck > boundBox.Cy {
boundBox.Cy = cyCheck // if the check is taller than the font, use its height
}
return boundBox
}
//------------------------------------------------------------------------------
// Runs the main window loop synchronously.
func _RunMainLoop(hWnd win.HWND, hAccel win.HACCEL) int {
msg := win.MSG{}
for {
res, fail := win.GetMessage(&msg, win.HWND(0), 0, 0)
if fail != nil {
panic(fail)
} else if res == 0 {
// WM_QUIT was sent, gracefully terminate the program.
// If it returned -1, it will simply panic.
// WParam has the program exit code.
// https://docs.microsoft.com/en-us/windows/win32/winmsg/using-messages-and-message-queues
return int(msg.WParam)
}
// Check if modeless...
// If a child window, will retrieve its top-level parent.
// If a top-level, use itself.
hTopLevel := msg.HWnd.GetAncestor(co.GA_ROOT)
// If we have an accelerator table, try to translate the message.
if hAccel != 0 && hTopLevel.TranslateAccelerator(hAccel, &msg) == nil {
continue // message translated, no further processing is done
}
if hTopLevel.IsDialogMessage(&msg) {
// Processed all keyboard actions for child controls.
continue
}
win.TranslateMessage(&msg)
win.DispatchMessage(&msg)
}
}
// Runs the modal window loop synchronously.
func _RunModalLoop(hWnd win.HWND) {
msg := win.MSG{}
for {
res, fail := win.GetMessage(&msg, win.HWND(0), 0, 0)
if fail != nil {
panic(fail)
} else if res == 0 {
// WM_QUIT was sent, exit modal loop now and signal parent.
// If it returned -1, it will simply panic.
// https://devblogs.microsoft.com/oldnewthing/20050222-00/?p=36393
win.PostQuitMessage(int32(msg.WParam))
break
}
// If a child window, will retrieve its top-level parent.
// If a top-level, use itself.
if msg.HWnd.GetAncestor(co.GA_ROOT).IsDialogMessage(&msg) {
// Processed all keyboard actions for child controls.
if hWnd == 0 {
break // our modal was destroyed, terminate loop
} else {
continue
}
}
win.TranslateMessage(&msg)
win.DispatchMessage(&msg)
if hWnd == 0 {
break // our modal was destroyed, terminate loop
}
}
}
//------------------------------------------------------------------------------
// Paints the themed border for child controls.
func _PaintThemedBorders(hWnd win.HWND, p wm.NcPaint) {
hWnd.DefWindowProc(co.WM_NCPAINT, p.Msg.WParam, p.Msg.LParam) // make system draw the scrollbar for us
exStyle := co.WS_EX(hWnd.GetWindowLongPtr(co.GWLP_EXSTYLE))
if (exStyle&co.WS_EX_CLIENTEDGE) == 0 || // has no border
!win.IsThemeActive() ||
!win.IsAppThemed() {
// No themed borders to be painted.
return
}
rc := hWnd.GetWindowRect() // window outmost coordinates, including margins
hWnd.ScreenToClientRc(&rc)
rc.Left += 2 // manual fix, because it comes up anchored at -2,-2
rc.Top += 2
rc.Right += 2
rc.Bottom += 2
hdc := hWnd.GetWindowDC()
defer hWnd.ReleaseDC(hdc)
if hTheme, err := hWnd.OpenThemeData("LISTVIEW"); err == nil { // borrow style from listview
defer hTheme.CloseThemeData()
// Clipping region; will draw only within this rectangle.
// Draw only the borders to avoid flickering.
rc2 := win.RECT{Left: rc.Left, Top: rc.Top, Right: rc.Left + 2, Bottom: rc.Bottom}
hTheme.DrawThemeBackground(hdc, co.VS_LISTVIEW_LISTGROUP, &rc, &rc2) // draw themed left border
rc2 = win.RECT{Left: rc.Left, Top: rc.Top, Right: rc.Right, Bottom: rc.Top + 2}
hTheme.DrawThemeBackground(hdc, co.VS_LISTVIEW_LISTGROUP, &rc, &rc2) // draw themed top border
rc2 = win.RECT{Left: rc.Right - 2, Top: rc.Top, Right: rc.Right, Bottom: rc.Bottom}
hTheme.DrawThemeBackground(hdc, co.VS_LISTVIEW_LISTGROUP, &rc, &rc2) // draw themed right border
rc2 = win.RECT{Left: rc.Left, Top: rc.Bottom - 2, Right: rc.Right, Bottom: rc.Bottom}
hTheme.DrawThemeBackground(hdc, co.VS_LISTVIEW_LISTGROUP, &rc, &rc2) // draw themed bottom border
}
}