-
Notifications
You must be signed in to change notification settings - Fork 1
/
slider.go
204 lines (162 loc) · 4.43 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
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
// 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 winapi
import (
"strconv"
"github.com/Gipcomp/win32/comctl32"
"github.com/Gipcomp/win32/commctrl"
"github.com/Gipcomp/win32/handle"
"github.com/Gipcomp/win32/user32"
"github.com/Gipcomp/win32/win"
)
type Slider struct {
WidgetBase
valueChangedPublisher EventPublisher
layoutFlags LayoutFlags
tracking bool
persistent bool
}
type SliderCfg struct {
Orientation Orientation
ToolTipsHidden bool
}
func NewSlider(parent Container) (*Slider, error) {
return NewSliderWithOrientation(parent, Horizontal)
}
func NewSliderWithOrientation(parent Container, orientation Orientation) (*Slider, error) {
return NewSliderWithCfg(parent, &SliderCfg{Orientation: orientation})
}
func NewSliderWithCfg(parent Container, cfg *SliderCfg) (*Slider, error) {
sl := new(Slider)
var style uint32 = user32.WS_TABSTOP | user32.WS_VISIBLE
if cfg.Orientation == Vertical {
style |= comctl32.TBS_VERT
sl.layoutFlags = ShrinkableVert | GrowableVert
} else {
sl.layoutFlags = ShrinkableHorz | GrowableHorz
}
if !cfg.ToolTipsHidden {
style |= comctl32.TBS_TOOLTIPS
}
if err := InitWidget(
sl,
parent,
"msctls_trackbar32",
style,
0); err != nil {
return nil, err
}
sl.SetBackground(nullBrushSingleton)
sl.GraphicsEffects().Add(InteractionEffect)
sl.GraphicsEffects().Add(FocusEffect)
sl.MustRegisterProperty("Value", NewProperty(
func() interface{} {
return sl.Value()
},
func(v interface{}) error {
sl.SetValue(assertIntOr(v, 0))
return nil
},
sl.valueChangedPublisher.Event()))
return sl, nil
}
func (sl *Slider) MinValue() int {
return int(sl.SendMessage(comctl32.TBM_GETRANGEMIN, 0, 0))
}
func (sl *Slider) MaxValue() int {
return int(sl.SendMessage(comctl32.TBM_GETRANGEMAX, 0, 0))
}
func (sl *Slider) SetRange(min, max int) {
sl.SendMessage(comctl32.TBM_SETRANGEMIN, 0, uintptr(min))
sl.SendMessage(comctl32.TBM_SETRANGEMAX, 1, uintptr(max))
}
func (sl *Slider) Value() int {
return int(sl.SendMessage(comctl32.TBM_GETPOS, 0, 0))
}
func (sl *Slider) SetValue(value int) {
sl.SendMessage(comctl32.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) Persistent() bool {
return sl.persistent
}
func (sl *Slider) SetPersistent(value bool) {
sl.persistent = value
}
func (sl *Slider) SaveState() error {
return sl.WriteState(strconv.Itoa(sl.Value()))
}
func (sl *Slider) RestoreState() error {
s, err := sl.ReadState()
if err != nil {
return err
}
value, err := strconv.Atoi(s)
if err != nil {
return err
}
sl.SetValue(value)
return nil
}
func (sl *Slider) LineSize() int {
return int(sl.SendMessage(comctl32.TBM_GETLINESIZE, 0, 0))
}
func (sl *Slider) SetLineSize(lineSize int) {
sl.SendMessage(comctl32.TBM_SETLINESIZE, 0, uintptr(lineSize))
}
func (sl *Slider) PageSize() int {
return int(sl.SendMessage(comctl32.TBM_GETPAGESIZE, 0, 0))
}
func (sl *Slider) SetPageSize(pageSize int) {
sl.SendMessage(comctl32.TBM_SETPAGESIZE, 0, uintptr(pageSize))
}
func (sl *Slider) Tracking() bool {
return sl.tracking
}
func (sl *Slider) SetTracking(tracking bool) {
sl.tracking = tracking
}
func (sl *Slider) WndProc(hwnd handle.HWND, msg uint32, wParam, lParam uintptr) uintptr {
switch msg {
case user32.WM_HSCROLL, user32.WM_VSCROLL:
switch win.LOWORD(uint32(wParam)) {
case commctrl.TB_THUMBPOSITION, commctrl.TB_ENDTRACK:
sl.valueChangedPublisher.Publish()
case commctrl.TB_THUMBTRACK:
if sl.tracking {
sl.valueChangedPublisher.Publish()
}
}
return 0
}
return sl.WidgetBase.WndProc(hwnd, msg, wParam, lParam)
}
func (*Slider) NeedsWmSize() bool {
return true
}
func (sl *Slider) CreateLayoutItem(ctx *LayoutContext) LayoutItem {
return &sliderLayoutItem{
layoutFlags: sl.layoutFlags,
idealSize: sl.dialogBaseUnitsToPixels(Size{15, 15}),
}
}
type sliderLayoutItem struct {
LayoutItemBase
layoutFlags LayoutFlags
idealSize Size // in native pixels
}
func (li *sliderLayoutItem) LayoutFlags() LayoutFlags {
return li.layoutFlags
}
func (li *sliderLayoutItem) IdealSize() Size {
return li.idealSize
}
func (li *sliderLayoutItem) MinSize() Size {
return li.idealSize
}