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"
)
// Orientation defines the orientation of controls in a Stack.
type Orientation bool
type orientation bool
const (
Horizontal Orientation = false
Vertical Orientation = true
horizontal orientation = false
vertical orientation = true
)
// A Stack stacks controls horizontally or vertically within the Stack's parent.
@ -21,14 +21,13 @@ const (
type Stack struct {
lock sync.Mutex
created bool
orientation Orientation
orientation orientation
controls []Control
stretchy []bool
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{
orientation: o,
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.
func (s *Stack) SetStretchy(index int) {
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 {
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.height[i] = height
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
if nStretchy != 0 {
if s.orientation == Horizontal { // split rest of width
if s.orientation == horizontal { // split rest of width
stretchywid /= nStretchy
} else { // split rest of height
stretchyht /= nStretchy
@ -116,7 +125,7 @@ func (s *Stack) setRect(x int, y int, width int, height int, winheight int) erro
if err != nil {
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]
} else {
y += s.height[i]
@ -153,7 +162,7 @@ func (s *Stack) preferredSize() (width int, height int, err error) {
maxswid = max(maxswid, w)
maxsht = max(maxsht, h)
}
if s.orientation == Horizontal { // max vertical size
if s.orientation == horizontal { // max vertical size
if !s.stretchy[i] {
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
} else {
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)
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.
return NewStack(Horizontal)
return newStack(horizontal)
}

View File

@ -40,23 +40,23 @@ func myMain() {
w.Closing = Event()
b := NewButton("Click Me")
b2 := NewButton("Or Me")
s2 := NewStack(Horizontal, b, b2)
s2 := NewHorizontalStack(b, b2)
c := NewCheckbox("Check Me")
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!")
e := NewLineEdit("Enter text here too")
l := NewLabel("This is a label")
b3 := NewButton("List Info")
s3 := NewStack(Horizontal, l, b3)
s3 := NewHorizontalStack(l, b3)
s3.SetStretchy(0)
// s3.SetStretchy(1)
pbar := NewProgressBar()
prog := 0
incButton := NewButton("Inc")
decButton := NewButton("Dec")
sincdec := NewStack(Horizontal, incButton, decButton)
sincdec := NewHorizontalStack(incButton, decButton)
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)
lb1 := NewListbox(true, "Select One", "Or More", "To Continue")
lb2 := NewListbox(false, "Select", "Only", "One", "Please")
@ -69,10 +69,10 @@ func myMain() {
i++
}
doAdjustments()
s1 := NewStack(Vertical, lb2, lb1)
s1 := NewVerticalStack(lb2, lb1)
s1.SetStretchy(0)
s1.SetStretchy(1)
s := NewStack(Horizontal, s1, s0)
s := NewHorizontalStack(s1, s0)
s.SetStretchy(0)
s.SetStretchy(1)
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] a different border on LineEdits?
- 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
- Combobox and Listbox insertions and deletions should allow bulk (...string)
- Combobox/Listbox.DeleteAll