From 992d43ac7b93353448250fade4c0d68ee3160758 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Sat, 15 Feb 2014 18:36:14 -0500 Subject: [PATCH] Added Listbox.Selection() and Listbox.SelectedIndices(). Also fixed a bug involving sysData.selectedIndices() with nothing selected. --- listbox.go | 22 ++++++++++++++++++++-- main.go | 14 ++++++++------ sysdata_windows.go | 3 +++ 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/listbox.go b/listbox.go index 3c422c2..cba3925 100644 --- a/listbox.go +++ b/listbox.go @@ -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() diff --git a/main.go b/main.go index c110e99..fb79bfb 100644 --- a/main.go +++ b/main.go @@ -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() diff --git a/sysdata_windows.go b/sysdata_windows.go index 52e2806..7a5abae 100644 --- a/sysdata_windows.go +++ b/sysdata_windows.go @@ -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,