summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPietro Gagliardi <[email protected]>2014-02-25 21:54:13 -0500
committerPietro Gagliardi <[email protected]>2014-02-25 21:54:13 -0500
commite1677a8941876982fd80bd1d30176ff80b205884 (patch)
treeee57c305a72cbad07ed67e80d256a01f5d3207f0
parent5eed0db0cd177bb6baf51f73fd5615a54a54621b (diff)
Added a way to let Controls in a Grid be sized to fill their cells.
-rw-r--r--grid.go28
-rw-r--r--main_test.go1
2 files changed, 27 insertions, 2 deletions
diff --git a/grid.go b/grid.go
index 9622c59..3cb09a4 100644
--- a/grid.go
+++ b/grid.go
@@ -7,15 +7,18 @@ import (
)
// A Grid arranges Controls in a two-dimensional grid.
-// All Controls in a Grid maintain their preferred sizes.
// The height of each row and the width of each column is the maximum preferred height and width (respectively) of all the controls in that row or column (respectively).
// Controls are aligned to the top left corner of each cell.
+// All Controls in a Grid maintain their preferred sizes by default; if a Control is marked as being "filling", it will be sized to fill its cell.
+// Even if a Control is marked as filling, its preferred size is used to calculate cell sizes.
+// All cooridnates in a Grid are given in (row,column) form with (0,0) being the top-left cell.
// Unlike other UI toolkit Grids, this Grid does not (yet? TODO) allow Controls to span multiple rows or columns.
// TODO differnet row/column control alignment; stretchy controls or other resizing options
type Grid struct {
lock sync.Mutex
created bool
controls [][]Control
+ filling [][]bool
widths, heights [][]int // caches to avoid reallocating each time
rowheights, colwidths []int
}
@@ -34,11 +37,13 @@ func NewGrid(nPerRow int, controls ...Control) *Grid {
}
nRows := len(controls) / nPerRow
cc := make([][]Control, nRows)
+ cf := make([][]bool, nRows)
cw := make([][]int, nRows)
ch := make([][]int, nRows)
i := 0
for row := 0; row < nRows; row++ {
cc[row] = make([]Control, nPerRow)
+ cf[row] = make([]bool, nPerRow)
cw[row] = make([]int, nPerRow)
ch[row] = make([]int, nPerRow)
for x := 0; x < nPerRow; x++ {
@@ -48,6 +53,7 @@ func NewGrid(nPerRow int, controls ...Control) *Grid {
}
return &Grid{
controls: cc,
+ filling: cf,
widths: cw,
heights: ch,
rowheights: make([]int, nRows),
@@ -55,6 +61,18 @@ func NewGrid(nPerRow int, controls ...Control) *Grid {
}
}
+// SetFilling sets the given control of the Grid as filling its cell instead of staying at its preferred size.
+// This function cannot be called after the Window that contains the Grid has been created.
+func (g *Grid) SetFilling(row int, column int) {
+ g.lock.Lock()
+ defer g.lock.Unlock()
+
+ if g.created {
+ panic("Grid.SetFilling() called after window create") // TODO
+ }
+ g.filling[row][column] = true
+}
+
func (g *Grid) make(window *sysData) error {
g.lock.Lock()
defer g.lock.Unlock()
@@ -106,7 +124,13 @@ func (g *Grid) setRect(x int, y int, width int, height int) error {
startx := x
for row, xcol := range g.controls {
for col, c := range xcol {
- err := c.setRect(x, y, g.widths[row][col], g.heights[row][col])
+ w := g.widths[row][col]
+ h := g.heights[row][col]
+ if g.filling[row][col] {
+ w = g.colwidths[col]
+ h = g.rowheights[row]
+ }
+ err := c.setRect(x, y, w, h)
if err != nil {
return fmt.Errorf("error setting size of control (%d,%d) in Grid.setRect(): %v", row, col, err)
}
diff --git a/main_test.go b/main_test.go
index aad01a2..ae3e425 100644
--- a/main_test.go
+++ b/main_test.go
@@ -20,6 +20,7 @@ func gridWindow() (*Window, error) {
b00, b01, b02,
Space(), l11, b12,
l20, c21, l22)
+ g.SetFilling(1, 2)
return w, w.Open(g)
}