-
Notifications
You must be signed in to change notification settings - Fork 2
/
splitterlayout.go
235 lines (182 loc) · 4.3 KB
/
splitterlayout.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
// 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.
package walk
import (
"github.com/lxn/win"
)
type splitterLayout struct {
container Container
orientation Orientation
fractions []float64
resetNeeded bool
}
func newSplitterLayout(orientation Orientation) *splitterLayout {
return &splitterLayout{
orientation: orientation,
}
}
func (l *splitterLayout) Container() Container {
return l.container
}
func (l *splitterLayout) SetContainer(value Container) {
if value != l.container {
if l.container != nil {
l.container.SetLayout(nil)
}
l.container = value
if value != nil && value.Layout() != Layout(l) {
value.SetLayout(l)
l.Update(true)
}
}
}
func (l *splitterLayout) Margins() Margins {
return Margins{}
}
func (l *splitterLayout) SetMargins(value Margins) error {
return newError("not supported")
}
func (l *splitterLayout) Spacing() int {
return 0
}
func (l *splitterLayout) SetSpacing(value int) error {
return newError("not supported")
}
func (l *splitterLayout) Orientation() Orientation {
return l.orientation
}
func (l *splitterLayout) SetOrientation(value Orientation) error {
if value != l.orientation {
switch value {
case Horizontal, Vertical:
default:
return newError("invalid Orientation value")
}
l.orientation = value
l.Update(false)
}
return nil
}
func (l *splitterLayout) Fractions() []float64 {
return l.fractions
}
func (l *splitterLayout) SetFractions(fractions []float64) error {
l.fractions = fractions
return l.Update(false)
}
func (l *splitterLayout) LayoutFlags() LayoutFlags {
return ShrinkableHorz | ShrinkableVert | GrowableHorz | GrowableVert | GreedyHorz | GreedyVert
}
func (l *splitterLayout) MinSize() Size {
var s Size
for _, widget := range l.container.Children().items {
cur := minSizeEffective(widget)
if l.orientation == Horizontal {
s.Width += cur.Width
s.Height = maxi(s.Height, cur.Height)
} else {
s.Height += cur.Height
s.Width = maxi(s.Width, cur.Width)
}
}
return s
}
func (l *splitterLayout) spaceForRegularWidgets() int {
splitter := l.container.(*Splitter)
cb := splitter.ClientBounds().Size()
var space int
if l.orientation == Horizontal {
space = cb.Width
} else {
space = cb.Height
}
return space - (splitter.Children().Len()/2)*splitter.handleWidth
}
func (l *splitterLayout) reset() {
children := l.container.Children()
regularCount := children.Len()/2 + children.Len()%2
if cap(l.fractions) < regularCount {
temp := make([]float64, regularCount)
copy(temp, l.fractions)
l.fractions = temp
}
l.fractions = l.fractions[:regularCount]
if regularCount == 0 {
return
}
fraction := 1 / float64(regularCount)
for i := 0; i < regularCount; i++ {
l.fractions[i] = fraction
}
}
func (l *splitterLayout) Update(reset bool) error {
if l.container == nil {
return newError("container required")
}
if reset {
l.resetNeeded = true
}
if l.container.Suspended() {
return nil
}
if l.resetNeeded {
l.resetNeeded = false
l.reset()
}
widgets := l.container.Children().items
splitter := l.container.(*Splitter)
handleWidth := splitter.HandleWidth()
sizes := make([]int, len(widgets))
cb := splitter.ClientBounds()
space1 := l.spaceForRegularWidgets()
var space2 int
if l.orientation == Horizontal {
space2 = cb.Height
} else {
space2 = cb.Width
}
for i := range widgets {
j := i/2 + i%2
if i%2 == 0 {
sizes[i] = int(float64(space1) * l.fractions[j])
} else {
sizes[i] = handleWidth
}
}
hdwp := win.BeginDeferWindowPos(int32(len(widgets)))
if hdwp == 0 {
return lastError("BeginDeferWindowPos")
}
p1 := 0
for i, widget := range widgets {
var s1 int
if i == len(widgets)-1 {
s1 = space1 + len(widgets)/2*handleWidth - p1
} else {
s1 = sizes[i]
}
var x, y, w, h int
if l.orientation == Horizontal {
x, y, w, h = p1, 0, s1, space2
} else {
x, y, w, h = 0, p1, space2, s1
}
if hdwp = win.DeferWindowPos(
hdwp,
widget.Handle(),
0,
int32(x),
int32(y),
int32(w),
int32(h),
win.SWP_NOACTIVATE|win.SWP_NOOWNERZORDER|win.SWP_NOZORDER); hdwp == 0 {
return lastError("DeferWindowPos")
}
p1 += s1
}
if !win.EndDeferWindowPos(hdwp) {
return lastError("EndDeferWindowPos")
}
return nil
}