From 3bd1ec9334856e4071cc4f1b3106efc1f44db30d Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Wed, 3 Sep 2014 18:30:38 -0400 Subject: [PATCH] Exported xspan/yspan in Grid to the Add() function parameter list. --- grid.go | 15 ++++++++++----- yz_repaint_test.go | 16 ++++++++-------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/grid.go b/grid.go index 4de9304..a6bb02c 100644 --- a/grid.go +++ b/grid.go @@ -8,7 +8,7 @@ import ( // 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. -// [TODO it can also have Controls spanning multiple rows and columns.] +// 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. @@ -24,7 +24,9 @@ type Grid interface { // 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. - Add(control Control, nextTo Control, side Side, xexpand bool, xalign Align, yexpand bool, yalign Align) + // 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) } // Align represents the alignment of a Control in its cell of a Grid. @@ -112,15 +114,18 @@ func (g *grid) reorigin() { } } -func (g *grid) Add(control Control, nextTo Control, side Side, xexpand bool, xalign Align, yexpand bool, yalign Align) { +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: 1, - yspan: 1, + xspan: xspan, + yspan: yspan, } if g.parent != nil { control.setParent(g.parent) diff --git a/yz_repaint_test.go b/yz_repaint_test.go index 5623b6b..5c4f0fb 100644 --- a/yz_repaint_test.go +++ b/yz_repaint_test.go @@ -32,8 +32,8 @@ func newRepainter(times int) *repainter { r.area = NewArea(r.img.Rect.Dx(), r.img.Rect.Dy(), r) r.area.OnTextFieldDismissed(r.tfdone) grid2 := NewGrid() - grid2.Add(r.area, nil, South, true, Fill, true, Fill) - grid2.Add(NewButton("X"), nil, East, false, LeftTop, true, Center) + grid2.Add(r.area, nil, South, true, Fill, true, Fill, 1, 1) + grid2.Add(NewButton("X"), nil, East, false, LeftTop, true, Center, 1, 1) r.x = NewTextField() r.x.OnChanged(r.setx) r.y = NewTextField() @@ -47,12 +47,12 @@ func newRepainter(times int) *repainter { r.all = NewButton("All") r.all.OnClicked(r.doall) grid := NewGrid() - grid.Add(r.x, nil, North, true, Fill, false, LeftTop) - grid.Add(r.y, r.x, East, true, Fill, false, LeftTop) - grid.Add(r.repaint, nil, East, false, 0, false, LeftTop) - grid.Add(r.width, r.x, South, true, Fill, false, LeftTop) - grid.Add(r.height, nil, East, true, Fill, false, LeftTop) - grid.Add(r.all, nil, East, false, Center, false, LeftTop) + grid.Add(r.x, nil, North, true, Fill, false, LeftTop, 1, 1) + grid.Add(r.y, r.x, East, true, Fill, false, LeftTop, 1, 1) + grid.Add(r.repaint, nil, East, false, 0, false, LeftTop, 1, 1) + grid.Add(r.width, r.x, South, true, Fill, false, LeftTop, 1, 1) + grid.Add(r.height, nil, East, true, Fill, false, LeftTop, 1, 1) + grid.Add(r.all, nil, East, false, Center, false, LeftTop, 1, 1) r.stack = NewVerticalStack(grid2, grid) r.stack.SetStretchy(0) return r