/
grid.go
477 lines (434 loc) · 11.7 KB
/
grid.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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
// 31 august 2014
package ui
import (
"fmt"
)
// Grid is a Control that arranges other Controls in a grid.
// Grid is a very powerful container: it can position and size each Control in several ways and can (and must) have Controls added to it at any time, in any direction.
// it can also have Controls spanning multiple rows and columns.
//
// Each Control in a Grid has associated "expansion" and "alignment" values in both the X and Y direction.
// Expansion determines whether all cells in the same row/column are given whatever space is left over after figuring out how big the rest of the Grid should be.
// Alignment determines the position of a Control relative to its cell after computing the above.
// The special alignment Fill can be used to grow a Control to fit its cell.
// Note that expansion and alignment are independent variables.
// For more information on expansion and alignment, read https://developer.gnome.org/gtk3/unstable/ch28s02.html.
type Grid interface {
Control
// Add adds a Control to the Grid.
// If this is the first Control in the Grid, it is merely added; nextTo should be nil.
// Otherwise, it is placed relative to nextTo.
// If nextTo is nil, it is placed next to the previously added Control.
// The effect of adding the same Control multiple times is undefined, as is the effect of adding a Control next to one not present in the Grid.
// The effect of overlapping spanning Controls is also undefined.
// Add panics if either xspan or yspan are zero or negative.
Add(control Control, nextTo Control, side Side, xexpand bool, xalign Align, yexpand bool, yalign Align, xspan int, yspan int)
// Padded and SetPadded get and set whether the controls of the Grid have padding between them.
// The size of the padding is platform-dependent.
Padded() bool
SetPadded(padded bool)
}
// Align represents the alignment of a Control in its cell of a Grid.
type Align uint
const (
LeftTop Align = iota
Center
RightBottom
Fill
)
// Side represents a side of a Control to add other Controls to a Grid to.
type Side uint
const (
West Side = iota
East
North
South
nSides
)
type grid struct {
controls []gridCell
indexof map[Control]int
prev int
parent *controlParent
padded bool
xmax int
ymax int
}
type gridCell struct {
control Control
xexpand bool
xalign Align
yexpand bool
yalign Align
xspan int
yspan int
x int
y int
finalx int
finaly int
finalwidth int
finalheight int
prefwidth int
prefheight int
}
// NewGrid creates a new Grid with no Controls.
func NewGrid() Grid {
return &grid{
indexof: map[Control]int{},
}
}
// ensures that all (x, y) pairs are 0-based
// also computes g.xmax/g.ymax
func (g *grid) reorigin() {
xmin := 0
ymin := 0
for i := range g.controls {
if g.controls[i].x < xmin {
xmin = g.controls[i].x
}
if g.controls[i].y < ymin {
ymin = g.controls[i].y
}
}
xmin = -xmin
ymin = -ymin
g.xmax = 0
g.ymax = 0
for i := range g.controls {
g.controls[i].x += xmin
g.controls[i].y += ymin
if g.xmax < g.controls[i].x+g.controls[i].xspan {
g.xmax = g.controls[i].x + g.controls[i].xspan
}
if g.ymax < g.controls[i].y+g.controls[i].yspan {
g.ymax = g.controls[i].y + g.controls[i].yspan
}
}
}
func (g *grid) Add(control Control, nextTo Control, side Side, xexpand bool, xalign Align, yexpand bool, yalign Align, xspan int, yspan int) {
if xspan <= 0 || yspan <= 0 {
panic(fmt.Errorf("invalid span %dx%d given to Grid.Add()", xspan, yspan))
}
cell := gridCell{
control: control,
xexpand: xexpand,
xalign: xalign,
yexpand: yexpand,
yalign: yalign,
xspan: xspan,
yspan: yspan,
}
if g.parent != nil {
control.setParent(g.parent)
}
// if this is the first control, just add it in directly
if len(g.controls) != 0 {
next := g.prev
if nextTo != nil {
next = g.indexof[nextTo]
}
switch side {
case West:
cell.x = g.controls[next].x - cell.xspan
cell.y = g.controls[next].y
case North:
cell.x = g.controls[next].x
cell.y = g.controls[next].y - cell.yspan
case East:
cell.x = g.controls[next].x + g.controls[next].xspan
cell.y = g.controls[next].y
case South:
cell.x = g.controls[next].x
cell.y = g.controls[next].y + g.controls[next].yspan
default:
panic(fmt.Errorf("invalid side %d in Grid.Add()", side))
}
}
g.controls = append(g.controls, cell)
g.prev = len(g.controls) - 1
g.indexof[control] = g.prev
g.reorigin()
}
func (g *grid) Padded() bool {
return g.padded
}
func (g *grid) SetPadded(padded bool) {
g.padded = padded
}
func (g *grid) setParent(p *controlParent) {
g.parent = p
for _, c := range g.controls {
c.control.setParent(g.parent)
}
}
func (g *grid) containerShow() {
for _, c := range g.controls {
c.control.containerShow()
}
}
func (g *grid) containerHide() {
for _, c := range g.controls {
c.control.containerHide()
}
}
// builds the topological cell grid; also makes colwidths and rowheights
func (g *grid) mkgrid() (gg [][]int, colwidths []int, rowheights []int) {
gg = make([][]int, g.ymax)
for y := 0; y < g.ymax; y++ {
gg[y] = make([]int, g.xmax)
for x := 0; x < g.xmax; x++ {
gg[y][x] = -1
}
}
for i := range g.controls {
for y := g.controls[i].y; y < g.controls[i].y+g.controls[i].yspan; y++ {
for x := g.controls[i].x; x < g.controls[i].x+g.controls[i].xspan; x++ {
gg[y][x] = i
}
}
}
return gg, make([]int, g.xmax), make([]int, g.ymax)
}
func (g *grid) resize(x int, y int, width int, height int, d *sizing) {
if len(g.controls) == 0 {
// nothing to do
return
}
// -2) get this Grid's padding
xpadding := d.xpadding
ypadding := d.ypadding
if !g.padded {
xpadding = 0
ypadding = 0
}
// -1) discount padding from width/height
width -= (g.xmax - 1) * xpadding
height -= (g.ymax - 1) * ypadding
// 0) build necessary data structures
gg, colwidths, rowheights := g.mkgrid()
xexpand := make([]bool, g.xmax)
yexpand := make([]bool, g.ymax)
// 1) compute colwidths and rowheights before handling expansion
// we only count non-spanning controls to avoid weirdness
for y := 0; y < len(gg); y++ {
for x := 0; x < len(gg[y]); x++ {
i := gg[y][x]
if i == -1 {
continue
}
w, h := g.controls[i].control.preferredSize(d)
if g.controls[i].xspan == 1 {
if colwidths[x] < w {
colwidths[x] = w
}
}
if g.controls[i].yspan == 1 {
if rowheights[y] < h {
rowheights[y] = h
}
}
// save these for step 6
g.controls[i].prefwidth = w
g.controls[i].prefheight = h
}
}
// 2) figure out which rows/columns expand but not span
// we need to know which expanding rows/columns don't span before we can handle the ones that do
for i := range g.controls {
if g.controls[i].xexpand && g.controls[i].xspan == 1 {
xexpand[g.controls[i].x] = true
}
if g.controls[i].yexpand && g.controls[i].yspan == 1 {
yexpand[g.controls[i].y] = true
}
}
// 2) figure out which rows/columns expand that do span
// the way we handle this is simple: if none of the spanned rows/columns expand, make all rows/columns expand
for i := range g.controls {
if g.controls[i].xexpand && g.controls[i].xspan != 1 {
do := true
for x := g.controls[i].x; x < g.controls[i].x+g.controls[i].xspan; x++ {
if xexpand[x] {
do = false
break
}
}
if do {
for x := g.controls[i].x; x < g.controls[i].x+g.controls[i].xspan; x++ {
xexpand[x] = true
}
}
}
if g.controls[i].yexpand && g.controls[i].yspan != 1 {
do := true
for y := g.controls[i].y; y < g.controls[i].y+g.controls[i].yspan; y++ {
if yexpand[y] {
do = false
break
}
}
if do {
for y := g.controls[i].y; y < g.controls[i].y+g.controls[i].yspan; y++ {
yexpand[y] = true
}
}
}
}
// 4) compute and assign expanded widths/heights
nxexpand := 0
nyexpand := 0
for x, expand := range xexpand {
if expand {
nxexpand++
} else {
width -= colwidths[x]
}
}
for y, expand := range yexpand {
if expand {
nyexpand++
} else {
height -= rowheights[y]
}
}
for x, expand := range xexpand {
if expand {
colwidths[x] = width / nxexpand
}
}
for y, expand := range yexpand {
if expand {
rowheights[y] = height / nyexpand
}
}
// 5) reset the final coordinates for the next step
for i := range g.controls {
g.controls[i].finalx = 0
g.controls[i].finaly = 0
g.controls[i].finalwidth = 0
g.controls[i].finalheight = 0
}
// 6) compute cell positions and sizes
for y := 0; y < g.ymax; y++ {
curx := 0
prev := -1
for x := 0; x < g.xmax; x++ {
i := gg[y][x]
if i != -1 && y == g.controls[i].y { // don't repeat this step if the control spans vertically
if i != prev {
g.controls[i].finalx = curx
} else {
g.controls[i].finalwidth += xpadding
}
g.controls[i].finalwidth += colwidths[x]
}
curx += colwidths[x] + xpadding
prev = i
}
}
for x := 0; x < g.xmax; x++ {
cury := 0
prev := -1
for y := 0; y < g.ymax; y++ {
i := gg[y][x]
if i != -1 && x == g.controls[i].x { // don't repeat this step if the control spans horizontally
if i != prev {
g.controls[i].finaly = cury
} else {
g.controls[i].finalheight += ypadding
}
g.controls[i].finalheight += rowheights[y]
}
cury += rowheights[y] + ypadding
prev = i
}
}
// 7) everything as it stands now is set for xalign == Fill yalign == Fill; set the correct alignments
// this is why we saved prefwidth/prefheight above
for i := range g.controls {
if g.controls[i].xalign != Fill {
switch g.controls[i].xalign {
case RightBottom:
g.controls[i].finalx += g.controls[i].finalwidth - g.controls[i].prefwidth
case Center:
g.controls[i].finalx += (g.controls[i].finalwidth - g.controls[i].prefwidth) / 2
}
g.controls[i].finalwidth = g.controls[i].prefwidth // for all three
}
if g.controls[i].yalign != Fill {
switch g.controls[i].yalign {
case RightBottom:
g.controls[i].finaly += g.controls[i].finalheight - g.controls[i].prefheight
case Center:
g.controls[i].finaly += (g.controls[i].finalheight - g.controls[i].prefheight) / 2
}
g.controls[i].finalheight = g.controls[i].prefheight // for all three
}
}
// 8) and FINALLY we draw
for _, ycol := range gg {
for _, i := range ycol {
if i != -1 { // treat empty cells like spaces
g.controls[i].control.resize(
g.controls[i].finalx+x, g.controls[i].finaly+y,
g.controls[i].finalwidth, g.controls[i].finalheight, d)
}
}
}
return
}
func (g *grid) preferredSize(d *sizing) (width, height int) {
if len(g.controls) == 0 {
// nothing to do
return 0, 0
}
// -1) get this Grid's padding
xpadding := d.xpadding
ypadding := d.ypadding
if !g.padded {
xpadding = 0
ypadding = 0
}
// 0) build necessary data structures
gg, colwidths, rowheights := g.mkgrid()
// 1) compute colwidths and rowheights before handling expansion
// TODO put this in its own function (but careful about the spanning calculation in allocate())
for y := 0; y < len(gg); y++ {
for x := 0; x < len(gg[y]); x++ {
i := gg[y][x]
if i == -1 {
continue
}
w, h := g.controls[i].control.preferredSize(d)
// allot equal space in the presence of spanning to keep things sane
if colwidths[x] < w/g.controls[i].xspan {
colwidths[x] = w / g.controls[i].xspan
}
if rowheights[y] < h/g.controls[i].yspan {
rowheights[y] = h / g.controls[i].yspan
}
// save these for step 6
g.controls[i].prefwidth = w
g.controls[i].prefheight = h
}
}
// 2) compute total column width/row height
colwidth := 0
rowheight := 0
for _, w := range colwidths {
colwidth += w
}
for _, h := range rowheights {
rowheight += h
}
// and that's it; just account for padding
return colwidth + (g.xmax-1) * xpadding,
rowheight + (g.ymax-1) * ypadding
}
func (g *grid) nTabStops() int {
n := 0
for _, c := range g.controls {
n += c.control.nTabStops()
}
return n
}