Added Listbox.Selection() and Listbox.SelectedIndices(). Also fixed a bug involving sysData.selectedIndices() with nothing selected.

This commit is contained in:
Pietro Gagliardi 2014-02-15 18:36:14 -05:00
parent 2d97a24463
commit 992d43ac7b
3 changed files with 31 additions and 8 deletions

View File

@ -65,9 +65,27 @@ func (l *Listbox) Delete(index int) error {
return nil
}
// TODO Selection
// Selection returns a list of strings currently selected in the Listbox, or an empty list if none have been selected. This list will have at most one item on a single-selection Listbox.
func (l *Listbox) Selection() []string {
l.lock.Lock()
defer l.lock.Unlock()
// TODO SelectedIndices
if l.created {
return l.sysData.selectedTexts()
}
return nil
}
// SelectedIndices returns a list of the currently selected indexes in the Listbox, or an empty list if none have been selected. This list will have at most one item on a single-selection Listbox.
func (l *Listbox) SelectedIndices() []int {
l.lock.Lock()
defer l.lock.Unlock()
if l.created {
return l.sysData.selectedIndices()
}
return nil
}
func (l *Listbox) make(window *sysData) (err error) {
l.lock.Lock()

14
main.go
View File

@ -19,18 +19,18 @@ func main() {
b3 := NewButton("List Info")
s3 := NewStack(Horizontal, l, b3)
s0 := NewStack(Vertical, s2, c, cb1, cb2, e, s3)
lb := NewListbox(true, "Select One", "Or More", "To Continue")
lb1 := NewListbox(true, "Select One", "Or More", "To Continue")
lb2 := NewListbox(false, "Select", "Only", "One", "Please")
i := 0
doAdjustments := func() {
cb1.Append("append")
cb2.InsertBefore(fmt.Sprintf("before %d", i), 1)
lb.InsertBefore(fmt.Sprintf("%d", i), 2)
lb1.InsertBefore(fmt.Sprintf("%d", i), 2)
lb2.Append("Please")
i++
}
doAdjustments()
s1 := NewStack(Vertical, lb2, lb)
s1 := NewStack(Vertical, lb2, lb1)
s := NewStack(Horizontal, s1, s0)
err := w.Open(s)
if err != nil {
@ -55,13 +55,15 @@ mainloop:
case <-b2.Clicked:
cb1.Delete(1)
cb2.Delete(2)
lb.Delete(3)
lb1.Delete(3)
lb2.Delete(4)
case <-b3.Clicked:
MsgBox("List Info",
"cb1: %d %q\ncb2: %d %q",
"cb1: %d %q\ncb2: %d %q\nlb1: %d %q\nlb2: %d %q",
cb1.SelectedIndex(), cb1.Selection(),
cb2.SelectedIndex(), cb2.Selection())
cb2.SelectedIndex(), cb2.Selection(),
lb1.SelectedIndices(), lb1.Selection(),
lb2.SelectedIndices(), lb2.Selection())
}
}
w.Hide()

View File

@ -385,6 +385,9 @@ func (s *sysData) selectedIndices() []int {
if r.ret == uintptr(_LB_ERR) {
panic("UI library internal error: LB_ERR from LB_GETSELCOUNT in what we know is a multi-selection listbox")
}
if r.ret == 0 { // nothing selected
return nil
}
indices := make([]int, r.ret)
uitask <- &uimsg{
call: _sendMessage,