From 73a7fe6c1dfaaa34fdac21db50058bdcb8d4c055 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Wed, 3 Sep 2014 12:10:03 -0400 Subject: [PATCH] Restructured the Grid allocation algorithm to split position/size assignment and neighbor assignment. This will make xspan/yspan much easier. --- grid.go | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/grid.go b/grid.go index 7aa6c6b..d2abdca 100644 --- a/grid.go +++ b/grid.go @@ -74,6 +74,7 @@ type gridCell struct { width int height int visited bool + allocations []*allocation } // NewGrid creates a new Grid with no Controls. @@ -281,30 +282,38 @@ func (g *grid) allocate(x int, y int, width int, height int, d *sizing) (allocat } } - // 5) draw + // 5) position everything + for c, cell := range g.controls { + cx := x + cy := y + for i := 0; i < cell.gridx; i++ { + cx += colwidths[i] + d.xpadding + } + for i := 0; i < cell.gridy; i++ { + cy += rowheights[i] + d.ypadding + } + cell.allocations = c.allocate(cx + cell.xoff, cy + cell.yoff, cell.width, cell.height, d) + } + + // 6) handle neighbors and build final allocation array var current *allocation - startx := x - for row, xcol := range g.grid { + for _, xcol := range g.grid { current = nil - for col, c := range xcol { - if c != nil { // treat empty cells like spaces + for _, c := range xcol { + if c != nil { // treat empty cells like spaces cell := g.controls[c] - as := c.allocate(x + cell.xoff, y + cell.yoff, cell.width, cell.height, d) - if current != nil { // connect first left to first right + if current != nil { // connect first left to first right current.neighbor = c } - if len(as) != 0 { - current = as[0] // next left is first subwidget + if len(cell.allocations) != 0 { + current = cell.allocations[0] // next left is first subwidget } else { - current = nil // spaces don't have allocation data + current = nil // spaces don't have allocation data } - allocations = append(allocations, as...) + allocations = append(allocations, cell.allocations...) } - x += colwidths[col] + d.xpadding } - x = startx - y += rowheights[row] + d.ypadding } return allocations