-
Notifications
You must be signed in to change notification settings - Fork 0
/
slider.go
155 lines (122 loc) · 3.21 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
// 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 (
"strconv"
"github.com/lxn/win"
)
type Slider struct {
WidgetBase
valueChangedPublisher EventPublisher
layoutFlags LayoutFlags
tracking bool
persistent 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.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(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) 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) 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)
}