Split NewStack() into NewHorizontalStack() and NewVerticalStack(). Unexported Orientation and its values accordingly.

This commit is contained in:
Pietro Gagliardi 2014-03-06 20:30:13 -05:00
parent 4630eaa84b
commit d6f289bc86
3 changed files with 29 additions and 20 deletions

View File

@ -6,11 +6,11 @@ import (
"sync" "sync"
) )
// Orientation defines the orientation of controls in a Stack. type orientation bool
type Orientation bool
const ( const (
Horizontal Orientation = false horizontal orientation = false
Vertical Orientation = true vertical orientation = true
) )
// A Stack stacks controls horizontally or vertically within the Stack's parent. // A Stack stacks controls horizontally or vertically within the Stack's parent.
@ -21,14 +21,13 @@ const (
type Stack struct { type Stack struct {
lock sync.Mutex lock sync.Mutex
created bool created bool
orientation Orientation orientation orientation
controls []Control controls []Control
stretchy []bool stretchy []bool
width, height []int // caches to avoid reallocating these each time width, height []int // caches to avoid reallocating these each time
} }
// NewStack creates a new Stack with the specified orientation. func newStack(o orientation, controls ...Control) *Stack {
func NewStack(o Orientation, controls ...Control) *Stack {
return &Stack{ return &Stack{
orientation: o, orientation: o,
controls: controls, controls: controls,
@ -38,6 +37,16 @@ func NewStack(o Orientation, controls ...Control) *Stack {
} }
} }
// NewHorizontalStack creates a new Stack that arranges the given Controls horizontally.
func NewHorizontalStack(controls ...Control) *Stack {
return newStack(horizontal, controls...)
}
// NewVerticalStack creates a new Stack that arranges the given Controls vertically.
func NewVerticalStack(controls ...Control) *Stack {
return newStack(vertical, controls...)
}
// SetStretchy marks a control in a Stack as stretchy. This cannot be called once the Window containing the Stack has been opened. // SetStretchy marks a control in a Stack as stretchy. This cannot be called once the Window containing the Stack has been opened.
func (s *Stack) SetStretchy(index int) { func (s *Stack) SetStretchy(index int) {
s.lock.Lock() s.lock.Lock()
@ -85,7 +94,7 @@ func (s *Stack) setRect(x int, y int, width int, height int, winheight int) erro
if err != nil { if err != nil {
return fmt.Errorf("error getting preferred size of control %d in Stack.setRect(): %v", i, err) return fmt.Errorf("error getting preferred size of control %d in Stack.setRect(): %v", i, err)
} }
if s.orientation == Horizontal { // all controls have same height if s.orientation == horizontal { // all controls have same height
s.width[i] = w s.width[i] = w
s.height[i] = height s.height[i] = height
stretchywid -= w stretchywid -= w
@ -97,7 +106,7 @@ func (s *Stack) setRect(x int, y int, width int, height int, winheight int) erro
} }
// 2) figure out size of stretchy controls // 2) figure out size of stretchy controls
if nStretchy != 0 { if nStretchy != 0 {
if s.orientation == Horizontal { // split rest of width if s.orientation == horizontal { // split rest of width
stretchywid /= nStretchy stretchywid /= nStretchy
} else { // split rest of height } else { // split rest of height
stretchyht /= nStretchy stretchyht /= nStretchy
@ -116,7 +125,7 @@ func (s *Stack) setRect(x int, y int, width int, height int, winheight int) erro
if err != nil { if err != nil {
return fmt.Errorf("error setting size of control %d in Stack.setRect(): %v", i, err) return fmt.Errorf("error setting size of control %d in Stack.setRect(): %v", i, err)
} }
if s.orientation == Horizontal { if s.orientation == horizontal {
x += s.width[i] x += s.width[i]
} else { } else {
y += s.height[i] y += s.height[i]
@ -153,7 +162,7 @@ func (s *Stack) preferredSize() (width int, height int, err error) {
maxswid = max(maxswid, w) maxswid = max(maxswid, w)
maxsht = max(maxsht, h) maxsht = max(maxsht, h)
} }
if s.orientation == Horizontal { // max vertical size if s.orientation == horizontal { // max vertical size
if !s.stretchy[i] { if !s.stretchy[i] {
width += w width += w
} }
@ -165,7 +174,7 @@ func (s *Stack) preferredSize() (width int, height int, err error) {
} }
} }
} }
if s.orientation == Horizontal { if s.orientation == horizontal {
width += nStretchy * maxswid width += nStretchy * maxswid
} else { } else {
height += nStretchy * maxsht height += nStretchy * maxsht
@ -181,5 +190,5 @@ func (s *Stack) preferredSize() (width int, height int, err error) {
// For a Grid, Space can be used to have an empty cell. (TODO stretching/sizing rules) // For a Grid, Space can be used to have an empty cell. (TODO stretching/sizing rules)
func Space() Control { func Space() Control {
// As above, a Stack with no controls draws nothing and reports no errors; its parent will still size it properly if made stretchy. // As above, a Stack with no controls draws nothing and reports no errors; its parent will still size it properly if made stretchy.
return NewStack(Horizontal) return newStack(horizontal)
} }

View File

@ -40,23 +40,23 @@ func myMain() {
w.Closing = Event() w.Closing = Event()
b := NewButton("Click Me") b := NewButton("Click Me")
b2 := NewButton("Or Me") b2 := NewButton("Or Me")
s2 := NewStack(Horizontal, b, b2) s2 := NewHorizontalStack(b, b2)
c := NewCheckbox("Check Me") c := NewCheckbox("Check Me")
cb1 := NewEditableCombobox("You can edit me!", "Yes you can!", "Yes you will!") cb1 := NewEditableCombobox("You can edit me!", "Yes you can!", "Yes you will!")
cb2 := NewCombobox("You can't edit me!", "No you can't!", "No you won't!") cb2 := NewCombobox("You can't edit me!", "No you can't!", "No you won't!")
e := NewLineEdit("Enter text here too") e := NewLineEdit("Enter text here too")
l := NewLabel("This is a label") l := NewLabel("This is a label")
b3 := NewButton("List Info") b3 := NewButton("List Info")
s3 := NewStack(Horizontal, l, b3) s3 := NewHorizontalStack(l, b3)
s3.SetStretchy(0) s3.SetStretchy(0)
// s3.SetStretchy(1) // s3.SetStretchy(1)
pbar := NewProgressBar() pbar := NewProgressBar()
prog := 0 prog := 0
incButton := NewButton("Inc") incButton := NewButton("Inc")
decButton := NewButton("Dec") decButton := NewButton("Dec")
sincdec := NewStack(Horizontal, incButton, decButton) sincdec := NewHorizontalStack(incButton, decButton)
password := NewPasswordEdit() password := NewPasswordEdit()
s0 := NewStack(Vertical, s2, c, cb1, cb2, e, s3, pbar, sincdec, Space(), password) s0 := NewVerticalStack(s2, c, cb1, cb2, e, s3, pbar, sincdec, Space(), password)
s0.SetStretchy(8) s0.SetStretchy(8)
lb1 := NewListbox(true, "Select One", "Or More", "To Continue") lb1 := NewListbox(true, "Select One", "Or More", "To Continue")
lb2 := NewListbox(false, "Select", "Only", "One", "Please") lb2 := NewListbox(false, "Select", "Only", "One", "Please")
@ -69,10 +69,10 @@ func myMain() {
i++ i++
} }
doAdjustments() doAdjustments()
s1 := NewStack(Vertical, lb2, lb1) s1 := NewVerticalStack(lb2, lb1)
s1.SetStretchy(0) s1.SetStretchy(0)
s1.SetStretchy(1) s1.SetStretchy(1)
s := NewStack(Horizontal, s1, s0) s := NewHorizontalStack(s1, s0)
s.SetStretchy(0) s.SetStretchy(0)
s.SetStretchy(1) s.SetStretchy(1)
err := w.Open(s) err := w.Open(s)

View File

@ -10,7 +10,7 @@ so I don't forget:
- [Windows, Mac OS X] should ListBox have a border style? - [Windows, Mac OS X] should ListBox have a border style?
- [Windows] a different border on LineEdits? - [Windows] a different border on LineEdits?
- padding and spacing in Stack - padding and spacing in Stack
- change Stack/Listbox constructors so that there's a separate constructor for each variant, rather than passing in parameters? - change Listbox constructor so that there's a separate constructor for each variant, rather than passing in parameters
- allow Combobox to have initial settings - allow Combobox to have initial settings
- Combobox and Listbox insertions and deletions should allow bulk (...string) - Combobox and Listbox insertions and deletions should allow bulk (...string)
- Combobox/Listbox.DeleteAll - Combobox/Listbox.DeleteAll