From 8c30cae462e7be26ec51a726c583255b9bcc304a Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Sat, 15 Feb 2014 17:52:33 -0500 Subject: [PATCH] Added Combobox.Append() and Combobox.InsertBefore(). --- combobox.go | 29 ++++++++++++++++++++++++++++- main.go | 8 ++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/combobox.go b/combobox.go index 9b38fa3..9d55ed3 100644 --- a/combobox.go +++ b/combobox.go @@ -26,7 +26,34 @@ func NewCombobox(editable bool, items ...string) (c *Combobox) { return c } -// TODO Append, InsertBefore, Delete +// Append adds an item to the end of the Combobox's list. +func (c *Combobox) Append(what string) (err error) { + c.lock.Lock() + defer c.lock.Unlock() + + if c.created { + return c.sysData.append(what) + } + c.initItems = append(c.initItems, what) + return nil +} + +// InsertBefore inserts a new item in the Combobox before the item at the given position. +func (c *Combobox) InsertBefore(what string, before int) (err error) { + c.lock.Lock() + defer c.lock.Unlock() + + if c.created { + return c.sysData.insertBefore(what, before) + } + m := make([]string, 0, len(c.initItems) + 1) + m = append(m, c.initItems[:before]...) + m = append(m, what) + c.initItems = append(m, c.initItems[before:]...) + return nil +} + +// TODO Delete // Selection returns the current selection. func (c *Combobox) Selection() string { diff --git a/main.go b/main.go index e02adc5..3e87fd1 100644 --- a/main.go +++ b/main.go @@ -12,6 +12,13 @@ func main() { 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!") + i := 0 + doAdjustments := func() { + cb1.Append("append") + cb2.InsertBefore(fmt.Sprintf("before %d", i), 1) + i++ + } + doAdjustments() e := NewLineEdit("Enter text here too") l := NewLabel("This is a label") s0 := NewStack(Vertical, b, c, cb1, cb2, e, l) @@ -38,6 +45,7 @@ mainloop: if err != nil { panic(err) } + doAdjustments() } } w.Hide()