Skip to content

Commit

Permalink
Some Splitter and layout improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
lxn committed Jun 7, 2012
1 parent 79beaa9 commit 86f375e
Show file tree
Hide file tree
Showing 21 changed files with 223 additions and 138 deletions.
51 changes: 14 additions & 37 deletions boxlayout.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,55 +266,32 @@ func (l *BoxLayout) MinSize() Size {
return Size{}
}

// Begin by finding out which widgets we care about.
widgets := l.widgets()
var s Size

// Prepare some useful data.
sizes := make([]int, len(widgets))
var s2 int

for i, widget := range widgets {
min := widget.MinSize()
minHint := widget.MinSizeHint()
for _, widget := range widgets {
min := widget.BaseWidget().minSizeEffective()

if l.orientation == Horizontal {
if min.Width > 0 {
sizes[i] = mini(minHint.Width, min.Width)
} else {
sizes[i] = minHint.Width
}

s2 = maxi(s2, maxi(minHint.Height, min.Height))
s.Width += min.Width
s.Height = maxi(s.Height, min.Height)
} else {
if min.Height > 0 {
sizes[i] = mini(minHint.Height, min.Height)
} else {
sizes[i] = minHint.Height
}

s2 = maxi(s2, maxi(minHint.Width, min.Width))
s.Height += min.Height
s.Width = maxi(s.Width, min.Width)
}
}

s1 := l.spacing * (len(widgets) - 1)

if l.orientation == Horizontal {
s1 += l.margins.HNear + l.margins.HFar
s2 += l.margins.VNear + l.margins.VFar
s.Width += l.spacing * (len(widgets) - 1)
s.Width += l.margins.HNear + l.margins.HFar
s.Height += l.margins.VNear + l.margins.VFar
} else {
s1 += l.margins.VNear + l.margins.VFar
s2 += l.margins.HNear + l.margins.HFar
}

for _, s := range sizes {
s1 += s
}

if l.orientation == Horizontal {
return Size{s1, s2}
s.Height += l.spacing * (len(widgets) - 1)
s.Height += l.margins.VNear + l.margins.VFar
s.Width += l.margins.HNear + l.margins.HFar
}

return Size{s2, s1}
return s
}

func (l *BoxLayout) Update(reset bool) error {
Expand Down
6 changes: 5 additions & 1 deletion checkbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (*CheckBox) LayoutFlags() LayoutFlags {
return 0
}

func (cb *CheckBox) SizeHint() Size {
func (cb *CheckBox) MinSizeHint() Size {
defaultSize := cb.dialogBaseUnitsToPixels(Size{50, 10})
textSize := cb.calculateTextSize()

Expand All @@ -50,3 +50,7 @@ func (cb *CheckBox) SizeHint() Size {

return Size{w, h}
}

func (cb *CheckBox) SizeHint() Size {
return cb.MinSizeHint()
}
6 changes: 5 additions & 1 deletion combobox.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (*ComboBox) LayoutFlags() LayoutFlags {
return GrowableHorz
}

func (cb *ComboBox) SizeHint() Size {
func (cb *ComboBox) MinSizeHint() Size {
defaultSize := cb.dialogBaseUnitsToPixels(Size{50, 12})

if cb.model != nil && cb.maxItemTextWidth <= 0 {
Expand All @@ -70,6 +70,10 @@ func (cb *ComboBox) SizeHint() Size {
return Size{w, h}
}

func (cb *ComboBox) SizeHint() Size {
return cb.MinSizeHint()
}

func (cb *ComboBox) itemString(index int) string {
switch val := cb.model.Value(index).(type) {
case string:
Expand Down
6 changes: 5 additions & 1 deletion dateedit.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,14 @@ func (*DateEdit) LayoutFlags() LayoutFlags {
return GrowableHorz
}

func (de *DateEdit) SizeHint() Size {
func (de *DateEdit) MinSizeHint() Size {
return de.dialogBaseUnitsToPixels(Size{64, 12})
}

func (de *DateEdit) SizeHint() Size {
return de.MinSizeHint()
}

func (de *DateEdit) systemTime() (*SYSTEMTIME, error) {
var st SYSTEMTIME

Expand Down
22 changes: 5 additions & 17 deletions gridlayout.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,22 +351,14 @@ func (l *GridLayout) MinSize() Size {
widths := make([]int, len(l.cells[0]))
heights := make([]int, len(l.cells))

type minSizes struct {
minSize Size
minSizeHint Size
}

widget2MinSizes := make(map[Widget]*minSizes)
widget2MinSize := make(map[Widget]Size)

for widget, _ := range l.widget2Info {
if !shouldLayoutWidget(widget) {
continue
}

min := widget.MinSize()
hint := widget.MinSizeHint()

widget2MinSizes[widget] = &minSizes{min, hint}
widget2MinSize[widget] = widget.BaseWidget().minSizeEffective()
}

for row := 0; row < len(heights); row++ {
Expand All @@ -377,18 +369,14 @@ func (l *GridLayout) MinSize() Size {
continue
}

minSizes := widget2MinSizes[widget]

min := minSizes.minSize
hint := minSizes.minSizeHint

min := widget2MinSize[widget]
info := l.widget2Info[widget]

if info.spanHorz == 1 {
widths[col] = maxi(widths[col], maxi(hint.Width, min.Width))
widths[col] = maxi(widths[col], min.Width)
}
if info.spanVert == 1 {
heights[row] = maxi(heights[row], maxi(hint.Height, min.Height))
heights[row] = maxi(heights[row], min.Height)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion label.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (l *Label) MinSizeHint() Size {
}

func (l *Label) SizeHint() Size {
return l.calculateTextSize()
return l.MinSizeHint()
}

func (l *Label) Text() string {
Expand Down
4 changes: 4 additions & 0 deletions lineedit.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ func (*LineEdit) LayoutFlags() LayoutFlags {
return ShrinkableHorz | GrowableHorz | GreedyHorz
}

func (le *LineEdit) MinSizeHint() Size {
return le.dialogBaseUnitsToPixels(Size{20, 12})
}

func (le *LineEdit) SizeHint() Size {
return le.dialogBaseUnitsToPixels(Size{50, 12})
}
Expand Down
7 changes: 3 additions & 4 deletions listbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (*ListBox) setOrigWndProcPtr(ptr uintptr) {
}

func (*ListBox) LayoutFlags() LayoutFlags {
return ShrinkableHorz | ShrinkableVert | GrowableHorz | GrowableVert | GreedyHorz | GreedyVert
return ShrinkableHorz | ShrinkableVert | GrowableHorz | GrowableVert | GreedyHorz | GreedyVert
}

func (lb *ListBox) itemString(index int) string {
Expand All @@ -78,7 +78,7 @@ func (lb *ListBox) itemString(index int) string {
func (lb *ListBox) insertItemAt(index int) error {
str := lb.itemString(index)
lp := uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(str)))
ret := int (SendMessage(lb.hWnd, LB_INSERTSTRING, uintptr(index), lp))
ret := int(SendMessage(lb.hWnd, LB_INSERTSTRING, uintptr(index), lp))
if ret == LB_ERRSPACE || ret == LB_ERR {
return newError("SendMessage(LB_INSERTSTRING)")
}
Expand Down Expand Up @@ -111,7 +111,6 @@ func (lb *ListBox) resetItems() error {
return nil
}


func (lb *ListBox) attachModel() {
itemsResetHandler := func() {
lb.resetItems()
Expand Down Expand Up @@ -184,7 +183,7 @@ func (lb *ListBox) calculateMaxItemTextWidth() int {

var maxWidth int

if lb.model == nil{
if lb.model == nil {
return -1
}
count := lb.model.ItemCount()
Expand Down
4 changes: 4 additions & 0 deletions numberedit.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ func (*NumberEdit) LayoutFlags() LayoutFlags {
return ShrinkableHorz | GrowableHorz
}

func (ne *NumberEdit) MinSizeHint() Size {
return ne.dialogBaseUnitsToPixels(Size{20, 12})
}

func (ne *NumberEdit) SizeHint() Size {
s := ne.dialogBaseUnitsToPixels(Size{50, 12})
return Size{s.Width, maxi(s.Height, 22)}
Expand Down
4 changes: 4 additions & 0 deletions progressbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ func (*ProgressBar) LayoutFlags() LayoutFlags {
return ShrinkableHorz | GrowableHorz | GreedyHorz
}

func (pb *ProgressBar) MinSizeHint() Size {
return pb.dialogBaseUnitsToPixels(Size{10, 14})
}

func (pb *ProgressBar) SizeHint() Size {
return pb.dialogBaseUnitsToPixels(Size{50, 14})
}
Expand Down
11 changes: 5 additions & 6 deletions pushbutton.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,16 @@ func (*PushButton) LayoutFlags() LayoutFlags {
return GrowableHorz
}

func (pb *PushButton) SizeHint() Size {
func (pb *PushButton) MinSizeHint() Size {
var s Size

SendMessage(pb.hWnd, BCM_GETIDEALSIZE, 0, uintptr(unsafe.Pointer(&s)))

minSize := pb.dialogBaseUnitsToPixels(Size{50, 14})

s.Width = maxi(s.Width, minSize.Width)
s.Height = maxi(s.Height, minSize.Height)
return maxSize(s, pb.dialogBaseUnitsToPixels(Size{50, 14}))
}

return s
func (pb *PushButton) SizeHint() Size {
return pb.MinSizeHint()
}

func (pb *PushButton) ensureProperDialogDefaultButton(hwndFocus HWND) {
Expand Down
6 changes: 5 additions & 1 deletion radiobutton.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (*RadioButton) LayoutFlags() LayoutFlags {
return 0
}

func (rb *RadioButton) SizeHint() Size {
func (rb *RadioButton) MinSizeHint() Size {
defaultSize := rb.dialogBaseUnitsToPixels(Size{50, 10})
textSize := rb.calculateTextSize()

Expand All @@ -50,3 +50,7 @@ func (rb *RadioButton) SizeHint() Size {

return Size{w, h}
}

func (rb *RadioButton) SizeHint() Size {
return rb.MinSizeHint()
}
9 changes: 9 additions & 0 deletions rectangle.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ type Rectangle struct {
X, Y, Width, Height int
}

func rectangleFromRECT(r winapi.RECT) Rectangle {
return Rectangle{
X: int(r.Left),
Y: int(r.Top),
Width: int(r.Right - r.Left),
Height: int(r.Bottom - r.Top),
}
}

func (r Rectangle) Left() int {
return r.X
}
Expand Down
36 changes: 36 additions & 0 deletions size.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,39 @@ package walk
type Size struct {
Width, Height int
}

func minSize(a, b Size) Size {
var s Size

if a.Width < b.Width {
s.Width = a.Width
} else {
s.Width = b.Width
}

if a.Height < b.Height {
s.Height = a.Height
} else {
s.Height = b.Height
}

return s
}

func maxSize(a, b Size) Size {
var s Size

if a.Width > b.Width {
s.Width = a.Width
} else {
s.Width = b.Width
}

if a.Height > b.Height {
s.Height = a.Height
} else {
s.Height = b.Height
}

return s
}
Loading

0 comments on commit 86f375e

Please sign in to comment.