-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslider.go
120 lines (97 loc) · 2.64 KB
/
slider.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
// Copyright 2016 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"
"strconv"
)
type Slider struct {
WidgetBase
valueChangedPublisher EventPublisher
layoutFlags LayoutFlags
tracking bool
}
func NewSlider(parent Container) (*Slider, error) {
return NewSliderWithOrientation(parent, Horizontal)
}
func NewSliderWithOrientation(parent Container, orientation Orientation) (*Slider, error) {
sl := new(Slider)
var style uint32 = win.WS_TABSTOP | win.WS_VISIBLE | win.TBS_TOOLTIPS
if orientation == Vertical {
style |= win.TBS_VERT
sl.layoutFlags = ShrinkableVert | GrowableVert | GreedyVert
} else {
sl.layoutFlags = ShrinkableHorz | GrowableHorz | GreedyHorz
}
if err := InitWidget(
sl,
parent,
"msctls_trackbar32",
style,
0); err != nil {
return nil, err
}
sl.MustRegisterProperty("Value", NewProperty(
func() interface{} {
return sl.Value()
},
func(v interface{}) error {
sl.SetValue(v.(int))
return nil
},
sl.valueChangedPublisher.Event()))
return sl, nil
}
func (sl *Slider) LayoutFlags() LayoutFlags {
return sl.layoutFlags
}
func (sl *Slider) SizeHint() Size {
return sl.MinSizeHint()
}
func (sl *Slider) MinSizeHint() Size {
return sl.dialogBaseUnitsToPixels(Size{20, 20})
}
func (sl *Slider) MinValue() int {
return int(sl.SendMessage(win.TBM_GETRANGEMIN, 0, 0))
}
func (sl *Slider) MaxValue() int {
return int(sl.SendMessage(win.TBM_GETRANGEMAX, 0, 0))
}
func (sl *Slider) SetRange(min, max int) {
sl.SendMessage(win.TBM_SETRANGEMIN, 0, uintptr(min))
sl.SendMessage(win.TBM_SETRANGEMAX, 1, uintptr(max))
}
func (sl *Slider) Value() int {
return int(sl.SendMessage(win.TBM_GETPOS, 0, 0))
}
func (sl *Slider) SetValue(value int) {
sl.SendMessage(win.TBM_SETPOS, 1, uintptr(value))
sl.valueChangedPublisher.Publish()
}
// ValueChanged returns an Event that can be used to track changes to Value.
func (sl *Slider) ValueChanged() *Event {
return sl.valueChangedPublisher.Event()
}
func (sl *Slider) Tracking() bool {
return sl.tracking
}
func (sl *Slider) SetTracking(tracking bool) {
sl.tracking = tracking
}
func (sl *Slider) WndProc(hwnd win.HWND, msg uint32, wParam, lParam uintptr) uintptr {
switch msg {
case win.WM_HSCROLL, win.WM_VSCROLL:
switch win.LOWORD(uint32(wParam)) {
case win.TB_THUMBPOSITION, win.TB_ENDTRACK:
sl.valueChangedPublisher.Publish()
case win.TB_THUMBTRACK:
if sl.tracking {
sl.valueChangedPublisher.Publish()
}
}
return 0
}
return sl.WidgetBase.WndProc(hwnd, msg, wParam, lParam)
}