Added right-alignment to Grid.

This commit is contained in:
Pietro Gagliardi 2014-09-01 00:56:33 -04:00
parent 2731cf3ae0
commit 91e35c1610
2 changed files with 31 additions and 31 deletions

12
grid.go
View File

@ -62,6 +62,8 @@ type gridCell struct {
// for allocate() and preferredSize() // for allocate() and preferredSize()
gridx int gridx int
gridy int gridy int
xoff int
yoff int
width int width int
height int height int
visited bool visited bool
@ -244,20 +246,19 @@ func (g *grid) allocate(x int, y int, width int, height int, d *sizing) (allocat
// 4) handle alignment // 4) handle alignment
for _, cell := range g.controls { for _, cell := range g.controls {
if cell.xexpand { cell.xoff = 0
switch cell.xalign { switch cell.xalign {
case LeftTop: case LeftTop:
// do nothing; this is the default // do nothing; this is the default
case Center: case Center:
case RightBottom:
// TODO // TODO
case RightBottom:
cell.xoff = colwidths[cell.gridx] - cell.width
case Fill: case Fill:
cell.width = colwidths[cell.gridx] cell.width = colwidths[cell.gridx]
default: default:
panic(fmt.Errorf("invalid xalign %d in Grid.allocate()", cell.xalign)) panic(fmt.Errorf("invalid xalign %d in Grid.allocate()", cell.xalign))
} }
}
if cell.yexpand {
switch cell.yalign { switch cell.yalign {
case LeftTop: case LeftTop:
// do nothing; this is the default // do nothing; this is the default
@ -270,7 +271,6 @@ func (g *grid) allocate(x int, y int, width int, height int, d *sizing) (allocat
panic(fmt.Errorf("invalid yalign %d in Grid.allocate()", cell.yalign)) panic(fmt.Errorf("invalid yalign %d in Grid.allocate()", cell.yalign))
} }
} }
}
// 5) draw // 5) draw
var current *allocation var current *allocation
@ -280,7 +280,7 @@ func (g *grid) allocate(x int, y int, width int, height int, d *sizing) (allocat
current = nil current = nil
for col, c := range xcol { for col, c := range xcol {
cell := g.controls[c] cell := g.controls[c]
as := c.allocate(x, y, cell.width, cell.height, d) as := c.allocate(x + cell.xoff, y, 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 current.neighbor = c
} }

View File

@ -44,12 +44,12 @@ func newRepainter(times int) *repainter {
r.all = NewButton("All") r.all = NewButton("All")
r.all.OnClicked(r.doall) r.all.OnClicked(r.doall)
grid := NewGrid() grid := NewGrid()
grid.Add(r.x, nil, North, true, Fill, false, 0) grid.Add(r.x, nil, North, true, Fill, false, LeftTop)
grid.Add(r.y, r.x, East, true, Fill, false, 0) grid.Add(r.y, r.x, East, true, Fill, false, LeftTop)
grid.Add(r.repaint, nil, East, false, 0, false, 0) grid.Add(r.repaint, nil, East, false, 0, false, LeftTop)
grid.Add(r.width, r.x, South, true, Fill, false, 0) grid.Add(r.width, r.x, South, true, Fill, false, LeftTop)
grid.Add(r.height, nil, East, true, Fill, false, 0) grid.Add(r.height, nil, East, true, Fill, false, LeftTop)
grid.Add(r.all, nil, East, false, 0, false, 0) grid.Add(r.all, nil, East, false, RightBottom, false, LeftTop)
r.stack = NewVerticalStack(r.area, grid) r.stack = NewVerticalStack(r.area, grid)
r.stack.SetStretchy(0) r.stack.SetStretchy(0)
return r return r