-
Notifications
You must be signed in to change notification settings - Fork 890
/
scrollview.go
445 lines (345 loc) · 10.4 KB
/
scrollview.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
// Copyright 2014 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 (
"unsafe"
"github.com/lxn/win"
)
const scrollViewWindowClass = `\o/ Walk_ScrollView_Class \o/`
func init() {
AppendToWalkInit(func() {
MustRegisterWindowClass(scrollViewWindowClass)
})
}
type ScrollView struct {
WidgetBase
composite *Composite
horizontal bool
vertical bool
}
func NewScrollView(parent Container) (*ScrollView, error) {
sv := &ScrollView{horizontal: true, vertical: true}
if err := InitWidget(
sv,
parent,
scrollViewWindowClass,
win.WS_CHILD|win.WS_HSCROLL|win.WS_VISIBLE|win.WS_VSCROLL,
win.WS_EX_CONTROLPARENT); err != nil {
return nil, err
}
succeeded := false
defer func() {
if !succeeded {
sv.Dispose()
}
}()
var err error
if sv.composite, err = NewComposite(sv); err != nil {
return nil, err
}
sv.composite.SizeChanged().Attach(func() {
sv.updateScrollBars()
})
sv.SetBackground(NullBrush())
succeeded = true
return sv, nil
}
func (sv *ScrollView) AsContainerBase() *ContainerBase {
if sv.composite == nil {
return nil
}
return sv.composite.AsContainerBase()
}
func (sv *ScrollView) ApplyDPI(dpi int) {
sv.WidgetBase.ApplyDPI(dpi)
sv.composite.ApplyDPI(dpi)
}
func (sv *ScrollView) Scrollbars() (horizontal, vertical bool) {
horizontal = sv.horizontal
vertical = sv.vertical
return
}
func (sv *ScrollView) SetScrollbars(horizontal, vertical bool) {
sv.horizontal = horizontal
sv.vertical = vertical
sv.ensureStyleBits(win.WS_HSCROLL, horizontal)
sv.ensureStyleBits(win.WS_VSCROLL, vertical)
}
func (sv *ScrollView) SetSuspended(suspend bool) {
sv.composite.SetSuspended(suspend)
sv.WidgetBase.SetSuspended(suspend)
sv.Invalidate()
}
func (sv *ScrollView) DataBinder() *DataBinder {
return sv.composite.dataBinder
}
func (sv *ScrollView) SetDataBinder(dataBinder *DataBinder) {
sv.composite.SetDataBinder(dataBinder)
}
func (sv *ScrollView) Children() *WidgetList {
if sv.composite == nil {
// Without this we would get into trouble in NewComposite.
return nil
}
return sv.composite.Children()
}
func (sv *ScrollView) Layout() Layout {
if sv.composite == nil {
return nil
}
return sv.composite.Layout()
}
func (sv *ScrollView) SetLayout(value Layout) error {
return sv.composite.SetLayout(value)
}
func (sv *ScrollView) Name() string {
if sv.composite == nil {
return ""
}
return sv.composite.Name()
}
func (sv *ScrollView) SetName(name string) {
sv.composite.SetName(name)
}
func (sv *ScrollView) Persistent() bool {
return sv.composite.Persistent()
}
func (sv *ScrollView) SetPersistent(value bool) {
sv.composite.SetPersistent(value)
}
func (sv *ScrollView) SaveState() error {
return sv.composite.SaveState()
}
func (sv *ScrollView) RestoreState() error {
return sv.composite.RestoreState()
}
func (sv *ScrollView) MouseDown() *MouseEvent {
return sv.composite.MouseDown()
}
func (sv *ScrollView) MouseMove() *MouseEvent {
return sv.composite.MouseMove()
}
func (sv *ScrollView) MouseUp() *MouseEvent {
return sv.composite.MouseUp()
}
func (sv *ScrollView) WndProc(hwnd win.HWND, msg uint32, wParam, lParam uintptr) uintptr {
if sv.composite != nil {
avoidBGArtifacts := func() {
if sv.hasComplexBackground() {
sv.composite.Invalidate()
}
}
switch msg {
case win.WM_HSCROLL:
sv.composite.SetXPixels(sv.scroll(win.SB_HORZ, win.LOWORD(uint32(wParam))))
if wParam == win.SB_ENDSCROLL {
avoidBGArtifacts()
}
case win.WM_VSCROLL:
sv.composite.SetYPixels(sv.scroll(win.SB_VERT, win.LOWORD(uint32(wParam))))
if wParam == win.SB_ENDSCROLL {
avoidBGArtifacts()
}
case win.WM_MOUSEWHEEL:
if win.GetWindowLong(sv.hWnd, win.GWL_STYLE)&win.WS_VSCROLL == 0 {
break
}
var cmd uint16
if delta := int16(win.HIWORD(uint32(wParam))); delta < 0 {
cmd = win.SB_LINEDOWN
} else {
cmd = win.SB_LINEUP
}
sv.composite.SetYPixels(sv.scroll(win.SB_VERT, cmd))
avoidBGArtifacts()
return 0
case win.WM_COMMAND, win.WM_NOTIFY:
sv.composite.WndProc(hwnd, msg, wParam, lParam)
case win.WM_WINDOWPOSCHANGED:
wp := (*win.WINDOWPOS)(unsafe.Pointer(lParam))
if wp.Flags&win.SWP_NOSIZE != 0 {
break
}
sv.updateScrollBars()
if h, v := sv.Scrollbars(); !h || !v {
sv.RequestLayout()
}
}
}
return sv.WidgetBase.WndProc(hwnd, msg, wParam, lParam)
}
func (sv *ScrollView) updateScrollBars() {
size := sv.SizePixels()
compositeSize := sv.composite.SizePixels()
var si win.SCROLLINFO
si.CbSize = uint32(unsafe.Sizeof(si))
si.FMask = win.SIF_PAGE | win.SIF_RANGE
newCompositeBounds := Rectangle{Width: compositeSize.Width, Height: compositeSize.Height}
if size != compositeSize {
dpi := uint32(sv.DPI())
vsbw := int(win.GetSystemMetricsForDpi(win.SM_CXVSCROLL, dpi))
hsbh := int(win.GetSystemMetricsForDpi(win.SM_CYHSCROLL, dpi))
if size.Width < compositeSize.Width && size.Height < compositeSize.Height {
size.Width -= vsbw
size.Height -= hsbh
}
}
si.NMax = int32(compositeSize.Width - 1)
si.NPage = uint32(size.Width)
win.SetScrollInfo(sv.hWnd, win.SB_HORZ, &si, false)
newCompositeBounds.X = sv.scroll(win.SB_HORZ, win.SB_THUMBPOSITION)
si.NMax = int32(compositeSize.Height - 1)
si.NPage = uint32(size.Height)
win.SetScrollInfo(sv.hWnd, win.SB_VERT, &si, false)
newCompositeBounds.Y = sv.scroll(win.SB_VERT, win.SB_THUMBPOSITION)
sv.composite.SetBoundsPixels(newCompositeBounds)
}
// scroll scrolls and returns new position in native pixels.
func (sv *ScrollView) scroll(sb int32, cmd uint16) int {
var pos int32
var si win.SCROLLINFO
si.CbSize = uint32(unsafe.Sizeof(si))
si.FMask = win.SIF_PAGE | win.SIF_POS | win.SIF_RANGE | win.SIF_TRACKPOS
win.GetScrollInfo(sv.hWnd, sb, &si)
pos = si.NPos
switch cmd {
case win.SB_LINELEFT: // == win.SB_LINEUP
pos -= int32(sv.IntFrom96DPI(20))
case win.SB_LINERIGHT: // == win.SB_LINEDOWN
pos += int32(sv.IntFrom96DPI(20))
case win.SB_PAGELEFT: // == win.SB_PAGEUP
pos -= int32(si.NPage)
case win.SB_PAGERIGHT: // == win.SB_PAGEDOWN
pos += int32(si.NPage)
case win.SB_THUMBTRACK:
pos = si.NTrackPos
}
if pos < 0 {
pos = 0
}
if pos > si.NMax+1-int32(si.NPage) {
pos = si.NMax + 1 - int32(si.NPage)
}
si.FMask = win.SIF_POS
si.NPos = pos
win.SetScrollInfo(sv.hWnd, sb, &si, true)
return -int(pos)
}
func (sv *ScrollView) CreateLayoutItem(ctx *LayoutContext) LayoutItem {
svli := new(scrollViewLayoutItem)
svli.ctx = ctx
cli := CreateLayoutItemsForContainerWithContext(sv.composite, ctx)
cli.AsLayoutItemBase().parent = svli
svli.children = append(svli.children, cli)
if box, ok := cli.(*boxLayoutItem); ok {
if len(box.children) > 0 {
if _, ok := box.children[len(box.children)-1].(*spacerLayoutItem); !ok {
// To retain the previous behavior with box layouts, we add a fake spacer at the end.
// Maybe this should just be an option.
box.children = append(box.children, &spacerLayoutItem{
LayoutItemBase: LayoutItemBase{ctx: ctx},
layoutFlags: ShrinkableHorz | ShrinkableVert | GrowableVert | GreedyVert,
})
}
}
}
svli.idealSize = cli.MinSize()
h, v := sv.Scrollbars()
if h {
svli.layoutFlags |= ShrinkableHorz | GrowableHorz | GreedyHorz
if !v {
maxSize := SizeFrom96DPI(sv.maxSize96dpi, ctx.dpi)
if svli.idealSize.Width > sv.geometry.ClientSize.Width && sv.geometry.ClientSize.Width > 0 && maxSize.Width == 0 ||
svli.idealSize.Width > maxSize.Width && maxSize.Width > 0 {
svli.sbSize.Height = int(win.GetSystemMetricsForDpi(win.SM_CYHSCROLL, uint32(ctx.dpi)))
svli.idealSize.Height += svli.sbSize.Height
}
svli.minSize.Height = svli.idealSize.Height
}
}
if v {
svli.layoutFlags |= GreedyVert | GrowableVert | ShrinkableVert
if !h {
maxSize := SizeFrom96DPI(sv.maxSize96dpi, ctx.dpi)
if svli.idealSize.Height > sv.geometry.ClientSize.Height && sv.geometry.ClientSize.Height > 0 && maxSize.Height == 0 ||
svli.idealSize.Height > maxSize.Height && maxSize.Height > 0 {
svli.sbSize.Width = int(win.GetSystemMetricsForDpi(win.SM_CXVSCROLL, uint32(ctx.dpi)))
svli.idealSize.Width += svli.sbSize.Width
}
svli.minSize.Width = svli.idealSize.Width
}
}
var si win.SCROLLINFO
si.CbSize = uint32(unsafe.Sizeof(si))
si.FMask = win.SIF_POS | win.SIF_RANGE
win.GetScrollInfo(sv.hWnd, win.SB_HORZ, &si)
svli.scrollX = float64(si.NPos) / float64(si.NMax)
win.GetScrollInfo(sv.hWnd, win.SB_VERT, &si)
svli.scrollY = float64(si.NPos) / float64(si.NMax)
return svli
}
type scrollViewLayoutItem struct {
ContainerLayoutItemBase
idealSize Size // in native pixels
minSize Size // in native pixels
sbSize Size // in native pixels
layoutFlags LayoutFlags
scrollX float64
scrollY float64
}
func (li *scrollViewLayoutItem) LayoutFlags() LayoutFlags {
return li.layoutFlags
}
func (li *scrollViewLayoutItem) IdealSize() Size {
return li.idealSize
}
func (li *scrollViewLayoutItem) MinSize() Size {
return li.minSize
}
func (li *scrollViewLayoutItem) MinSizeForSize(size Size) Size {
return li.MinSize()
}
func (li *scrollViewLayoutItem) HasHeightForWidth() bool {
return false
}
func (li *scrollViewLayoutItem) HeightForWidth(width int) int {
return 0
}
func (li *scrollViewLayoutItem) PerformLayout() []LayoutResultItem {
composite := li.children[0]
clientSize := li.geometry.Size
clientSize.Width -= li.sbSize.Width
clientSize.Height -= li.sbSize.Height
minSize := composite.(MinSizeForSizer).MinSizeForSize(clientSize)
if hfw, ok := composite.(HeightForWidther); ok && hfw.HasHeightForWidth() {
if minSize.Height > clientSize.Height {
if minSize.Width > clientSize.Width {
clientSize.Width = minSize.Width
minSize = composite.(MinSizeForSizer).MinSizeForSize(clientSize)
} else {
clientSize.Width -= int(win.GetSystemMetricsForDpi(win.SM_CXVSCROLL, uint32(li.ctx.dpi)))
minSize = composite.(MinSizeForSizer).MinSizeForSize(clientSize)
if minSize.Width > clientSize.Width {
clientSize.Width = minSize.Width
minSize = composite.(MinSizeForSizer).MinSizeForSize(clientSize)
}
}
}
}
s := maxSize(minSize, clientSize)
var x, y int
if clientSize.Width < minSize.Width {
x = -int(float64(minSize.Width) * li.scrollX)
}
if clientSize.Height < minSize.Height {
y = -int(float64(minSize.Height) * li.scrollY)
}
return []LayoutResultItem{
{
Item: composite,
Bounds: Rectangle{x, y, s.Width, s.Height},
},
}
}