summaryrefslogtreecommitdiffstatshomepage
path: root/customwidget.go
blob: 6f0131c9755c06e2038fa1545cc3979b912d7f94 (plain) (blame)
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
// Copyright 2010 The Walk Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build windows

package walk

import (
	"github.com/lxn/win"
)

const customWidgetWindowClass = `\o/ Walk_CustomWidget_Class \o/`

func init() {
	MustRegisterWindowClass(customWidgetWindowClass)
}

type PaintFunc func(canvas *Canvas, updateBounds Rectangle) error

type PaintMode int

const (
	PaintNormal   PaintMode = iota // erase background before PaintFunc
	PaintNoErase                   // PaintFunc clears background, single buffered
	PaintBuffered                  // PaintFunc clears background, double buffered
)

type CustomWidget struct {
	WidgetBase
	paint               PaintFunc
	invalidatesOnResize bool
	paintMode           PaintMode
}

func NewCustomWidget(parent Container, style uint, paint PaintFunc) (*CustomWidget, error) {
	cw := &CustomWidget{paint: paint}

	if err := InitWidget(
		cw,
		parent,
		customWidgetWindowClass,
		win.WS_VISIBLE|uint32(style),
		0); err != nil {
		return nil, err
	}

	return cw, nil
}

func (*CustomWidget) LayoutFlags() LayoutFlags {
	return ShrinkableHorz | ShrinkableVert | GrowableHorz | GrowableVert | GreedyHorz | GreedyVert
}

func (cw *CustomWidget) SizeHint() Size {
	return Size{100, 100}
}

// deprecated, use PaintMode
func (cw *CustomWidget) ClearsBackground() bool {
	return cw.paintMode != PaintNormal
}

// deprecated, use SetPaintMode
func (cw *CustomWidget) SetClearsBackground(value bool) {
	if value != cw.ClearsBackground() {
		if value {
			cw.paintMode = PaintNormal
		} else {
			cw.paintMode = PaintNoErase
		}
	}
}

func (cw *CustomWidget) InvalidatesOnResize() bool {
	return cw.invalidatesOnResize
}

func (cw *CustomWidget) SetInvalidatesOnResize(value bool) {
	cw.invalidatesOnResize = value
}

func (cw *CustomWidget) PaintMode() PaintMode {
	return cw.paintMode
}

func (cw *CustomWidget) SetPaintMode(value PaintMode) {
	cw.paintMode = value
}

func (cw *CustomWidget) WndProc(hwnd win.HWND, msg uint32, wParam, lParam uintptr) uintptr {
	switch msg {
	case win.WM_PAINT:
		if cw.paint == nil {
			newError("paint func is nil")
			break
		}

		var ps win.PAINTSTRUCT

		var hdc win.HDC
		if wParam == 0 {
			hdc = win.BeginPaint(cw.hWnd, &ps)
		} else {
			hdc = win.HDC(wParam)
		}
		if hdc == 0 {
			newError("BeginPaint failed")
			break
		}
		defer func() {
			if wParam == 0 {
				win.EndPaint(cw.hWnd, &ps)
			}
		}()

		canvas, err := newCanvasFromHDC(hdc)
		if err != nil {
			newError("newCanvasFromHDC failed")
			break
		}
		defer canvas.Dispose()

		r := &ps.RcPaint
		bounds := Rectangle{
			int(r.Left),
			int(r.Top),
			int(r.Right - r.Left),
			int(r.Bottom - r.Top),
		}
		if cw.paintMode == PaintBuffered {
			err = cw.bufferedPaint(canvas, bounds)
		} else {
			err = cw.paint(canvas, bounds)
		}

		if err != nil {
			newError("paint failed")
			break
		}

		return 0

	case win.WM_ERASEBKGND:
		if cw.paintMode != PaintNormal {
			return 1
		}

	case win.WM_PRINTCLIENT:
		win.SendMessage(hwnd, win.WM_PAINT, wParam, lParam)

	case win.WM_SIZE, win.WM_SIZING:
		if cw.invalidatesOnResize {
			cw.Invalidate()
		}
	}

	return cw.WidgetBase.WndProc(hwnd, msg, wParam, lParam)
}

func (cw *CustomWidget) bufferedPaint(canvas *Canvas, updateBounds Rectangle) error {
	hdc := win.CreateCompatibleDC(canvas.hdc)
	if hdc == 0 {
		return newError("CreateCompatibleDC failed")
	}
	defer win.DeleteDC(hdc)

	buffered := Canvas{hdc: hdc, doNotDispose: true}
	if _, err := buffered.init(); err != nil {
		return err
	}

	w, h := int32(updateBounds.Width), int32(updateBounds.Height)
	if w < 1 {
		w = 1
	}
	if h < 1 {
		h = 1
	}
	hbmp := win.CreateCompatibleBitmap(canvas.hdc, w, h)
	if hbmp == 0 {
		return lastError("CreateCompatibleBitmap failed")
	}
	defer win.DeleteObject(win.HGDIOBJ(hbmp))

	oldbmp := win.SelectObject(buffered.hdc, win.HGDIOBJ(hbmp))
	if oldbmp == 0 {
		return newError("SelectObject failed")
	}
	defer win.SelectObject(buffered.hdc, oldbmp)

	win.SetViewportOrgEx(buffered.hdc, -int32(updateBounds.X), -int32(updateBounds.Y), nil)
	win.SetBrushOrgEx(buffered.hdc, -int32(updateBounds.X), -int32(updateBounds.Y), nil)

	err := cw.paint(&buffered, updateBounds)

	if !win.BitBlt(canvas.hdc,
		int32(updateBounds.X), int32(updateBounds.Y), w, h,
		buffered.hdc,
		int32(updateBounds.X), int32(updateBounds.Y), win.SRCCOPY) {
		return lastError("buffered BitBlt failed")
	}

	return err
}