Skip to content

Commit ff2ef6b

Browse files
committed
container.go, customwidget.go: add WM_PRINTCLIENT handler to container and clean up the one in CustomWidget
I didn't like the way that CustomWidget sent a fake WM_PAINT; I replaced that with a method. Fixes #80 Signed-off-by: Aaron Klotz <[email protected]>
1 parent e7deaa8 commit ff2ef6b

File tree

2 files changed

+58
-44
lines changed

2 files changed

+58
-44
lines changed

container.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5+
//go:build windows
56
// +build windows
67

78
package walk
@@ -195,12 +196,8 @@ func (cb *ContainerBase) RestoreState() error {
195196
})
196197
}
197198

198-
func (cb *ContainerBase) doPaint() error {
199-
var ps win.PAINTSTRUCT
200-
201-
hdc := win.BeginPaint(cb.hWnd, &ps)
202-
defer win.EndPaint(cb.hWnd, &ps)
203-
199+
func (cb *ContainerBase) doPaint(ps *win.PAINTSTRUCT) error {
200+
hdc := ps.Hdc
204201
canvas, err := newCanvasFromHDC(hdc)
205202
if err != nil {
206203
return err
@@ -281,9 +278,26 @@ func (cb *ContainerBase) WndProc(hwnd win.HWND, msg uint32, wParam, lParam uintp
281278
break
282279
}
283280

284-
// If it fails, what can we do about it? Panic? That's extreme. So just ignore it.
285-
_ = cb.doPaint()
281+
var ps win.PAINTSTRUCT
282+
if hdc := win.BeginPaint(cb.hWnd, &ps); hdc == 0 {
283+
break
284+
}
285+
defer win.EndPaint(cb.hWnd, &ps)
286+
287+
cb.doPaint(&ps)
288+
return 0
289+
290+
case win.WM_PRINTCLIENT:
291+
if FocusEffect == nil && InteractionEffect == nil && ValidationErrorEffect == nil {
292+
break
293+
}
286294

295+
ps := win.PAINTSTRUCT{
296+
Hdc: win.HDC(wParam),
297+
}
298+
if win.GetClientRect(cb.hWnd, &ps.RcPaint) {
299+
cb.doPaint(&ps)
300+
}
287301
return 0
288302

289303
case win.WM_COMMAND:

customwidget.go

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5+
//go:build windows
56
// +build windows
67

78
package walk
@@ -110,6 +111,26 @@ func (cw *CustomWidget) SetPaintMode(value PaintMode) {
110111
cw.paintMode = value
111112
}
112113

114+
func (cw *CustomWidget) doPaint(ps *win.PAINTSTRUCT) error {
115+
hdc := ps.Hdc
116+
canvas, err := newCanvasFromHDC(hdc)
117+
if err != nil {
118+
return newError("newCanvasFromHDC failed")
119+
}
120+
defer canvas.Dispose()
121+
122+
bounds := rectangleFromRECT(ps.RcPaint)
123+
if cw.paintMode == PaintBuffered {
124+
err = cw.bufferedPaint(canvas, bounds)
125+
} else if cw.paintPixels != nil {
126+
err = cw.paintPixels(canvas, bounds)
127+
} else {
128+
err = cw.paint(canvas, RectangleTo96DPI(bounds, cw.DPI()))
129+
}
130+
131+
return err
132+
}
133+
113134
func (cw *CustomWidget) WndProc(hwnd win.HWND, msg uint32, wParam, lParam uintptr) uintptr {
114135
switch msg {
115136
case win.WM_PAINT:
@@ -119,44 +140,12 @@ func (cw *CustomWidget) WndProc(hwnd win.HWND, msg uint32, wParam, lParam uintpt
119140
}
120141

121142
var ps win.PAINTSTRUCT
122-
123-
var hdc win.HDC
124-
if wParam == 0 {
125-
hdc = win.BeginPaint(cw.hWnd, &ps)
126-
} else {
127-
hdc = win.HDC(wParam)
128-
}
129-
if hdc == 0 {
143+
if hdc := win.BeginPaint(cw.hWnd, &ps); hdc == 0 {
130144
newError("BeginPaint failed")
131145
break
132146
}
133-
defer func() {
134-
if wParam == 0 {
135-
win.EndPaint(cw.hWnd, &ps)
136-
}
137-
}()
138-
139-
canvas, err := newCanvasFromHDC(hdc)
140-
if err != nil {
141-
newError("newCanvasFromHDC failed")
142-
break
143-
}
144-
defer canvas.Dispose()
145-
146-
bounds := rectangleFromRECT(ps.RcPaint)
147-
if cw.paintMode == PaintBuffered {
148-
err = cw.bufferedPaint(canvas, bounds)
149-
} else if cw.paintPixels != nil {
150-
err = cw.paintPixels(canvas, bounds)
151-
} else {
152-
err = cw.paint(canvas, RectangleTo96DPI(bounds, cw.DPI()))
153-
}
154-
155-
if err != nil {
156-
newError("paint failed")
157-
break
158-
}
159-
147+
defer win.EndPaint(cw.hWnd, &ps)
148+
cw.doPaint(&ps)
160149
return 0
161150

162151
case win.WM_ERASEBKGND:
@@ -165,7 +154,18 @@ func (cw *CustomWidget) WndProc(hwnd win.HWND, msg uint32, wParam, lParam uintpt
165154
}
166155

167156
case win.WM_PRINTCLIENT:
168-
win.SendMessage(hwnd, win.WM_PAINT, wParam, lParam)
157+
if cw.paint == nil && cw.paintPixels == nil {
158+
newError("paint(Pixels) func is nil")
159+
break
160+
}
161+
162+
ps := win.PAINTSTRUCT{
163+
Hdc: win.HDC(wParam),
164+
}
165+
if win.GetClientRect(cw.hWnd, &ps.RcPaint) {
166+
cw.doPaint(&ps)
167+
}
168+
return 0
169169

170170
case win.WM_WINDOWPOSCHANGED:
171171
wp := (*win.WINDOWPOS)(unsafe.Pointer(lParam))

0 commit comments

Comments
 (0)