diff --git a/combobox.go b/combobox.go index 9d55ed3..0659be1 100644 --- a/combobox.go +++ b/combobox.go @@ -53,7 +53,17 @@ func (c *Combobox) InsertBefore(what string, before int) (err error) { return nil } -// TODO Delete +// Delete removes the given item from the Combobox. +func (c *Combobox) Delete(index int) error { + c.lock.Lock() + defer c.lock.Unlock() + + if c.created { + return c.sysData.delete(index) + } + c.initItems = append(c.initItems[:index], c.initItems[index + 1:]...) + return nil +} // Selection returns the current selection. func (c *Combobox) Selection() string { diff --git a/listbox.go b/listbox.go index 681c972..3c422c2 100644 --- a/listbox.go +++ b/listbox.go @@ -53,6 +53,18 @@ func (l *Listbox) InsertBefore(what string, before int) (err error) { return nil } +// Delete removes the given item from the Listbox. +func (l *Listbox) Delete(index int) error { + l.lock.Lock() + defer l.lock.Unlock() + + if l.created { + return l.sysData.delete(index) + } + l.initItems = append(l.initItems[:index], l.initItems[index + 1:]...) + return nil +} + // TODO Selection // TODO SelectedIndices diff --git a/main.go b/main.go index ce773ed..49ff4f1 100644 --- a/main.go +++ b/main.go @@ -9,12 +9,14 @@ func main() { w := NewWindow("Main Window", 320, 240) w.Closing = make(chan struct{}) b := NewButton("Click Me") + b2 := NewButton("Or Me") + s2 := NewStack(Horizontal, b, b2) c := NewCheckbox("Check Me") cb1 := NewCombobox(true, "You can edit me!", "Yes you can!", "Yes you will!") cb2 := NewCombobox(false, "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") - s0 := NewStack(Vertical, b, c, cb1, cb2, e, l) + s0 := NewStack(Vertical, s2, c, cb1, cb2, e, l) lb := NewListbox(true, "Select One", "Or More", "To Continue") lb2 := NewListbox(false, "Select", "Only", "One", "Please") i := 0 @@ -48,6 +50,11 @@ mainloop: panic(err) } doAdjustments() + case <-b2.Clicked: + cb1.Delete(1) + cb2.Delete(2) + lb.Delete(3) + lb2.Delete(4) } } w.Hide() diff --git a/sysdata.go b/sysdata.go index b3b3898..e4885ac 100644 --- a/sysdata.go +++ b/sysdata.go @@ -51,6 +51,9 @@ func (c *cSysData) selectedTexts() []string { func (c *cSysData) setWindowSize(int, int) error { panic(runtime.GOOS + " sysData does not define setWindowSize()") } +func (c *cSysData) delete(int) error { + panic(runtime.GOOS + " sysData does not define delete()") +} const ( c_window = iota diff --git a/sysdata_windows.go b/sysdata_windows.go index 0cbf4de..f0ecbf3 100644 --- a/sysdata_windows.go +++ b/sysdata_windows.go @@ -467,3 +467,23 @@ func (s *sysData) setWindowSize(width int, height int) error { } return nil } + +func (s *sysData) delete(index int) (err error) { + ret := make(chan uiret) + defer close(ret) + uitask <- &uimsg{ + call: _sendMessage, + p: []uintptr{ + uintptr(s.hwnd), + uintptr(classTypes[s.ctype].deleteMsg), + uintptr(_WPARAM(index)), + uintptr(0), + }, + ret: ret, + } + r := <-ret + if r.ret == uintptr(classTypes[s.ctype].selectedIndexErr) { + return fmt.Errorf("failed to delete item from combobox/listbox (last error: %v)", r.err) + } + return nil +} diff --git a/todo.md b/todo.md index bd3ee12..943cd9d 100644 --- a/todo.md +++ b/todo.md @@ -17,6 +17,7 @@ so I don't forget: - Combobox/Listbox.DeleteAll - Combobox/Listbox.Select (with Listbox.Select allowing bulk) - Listbox.SelectAll +- have Combobox.InsertBefore, Listbox.InsertBefore, Combobox.Delete, and Listbox.Delete return an error on invalid index super ultra important things: - the windows build appears to be unstable: