diff --git a/checkbox.go b/checkbox.go index d079e17..934da90 100644 --- a/checkbox.go +++ b/checkbox.go @@ -12,7 +12,7 @@ type Checkbox struct { created bool sysData *sysData initText string - initCheck bool + initCheck bool } // NewCheckbox creates a new checkbox with the specified text. @@ -46,7 +46,19 @@ func (c *Checkbox) Text() string { return c.initText } -// Checked() returns whether or not the checkbox has been checked. +// SetChecked() changes the checked state of the Checkbox. +func (c *Checkbox) SetChecked(checked bool) { + c.lock.Lock() + defer c.lock.Unlock() + + if c.created { + c.sysData.setChecked(checked) + return + } + c.initCheck = checked +} + +// Checked() returns whether or not the Checkbox has been checked. func (c *Checkbox) Checked() bool { c.lock.Lock() defer c.lock.Unlock() @@ -54,7 +66,7 @@ func (c *Checkbox) Checked() bool { if c.created { return c.sysData.isChecked() } - return false + return c.initCheck } func (c *Checkbox) make(window *sysData) error { @@ -66,6 +78,7 @@ func (c *Checkbox) make(window *sysData) error { return err } c.sysData.setText(c.initText) + c.sysData.setChecked(c.initCheck) c.created = true return nil } diff --git a/gtkcalls_unix.go b/gtkcalls_unix.go index 2934c7c..ca5c08e 100644 --- a/gtkcalls_unix.go +++ b/gtkcalls_unix.go @@ -153,6 +153,10 @@ func gtk_toggle_button_get_active(widget *C.GtkWidget) bool { return fromgbool(C.gtk_toggle_button_get_active(togtktogglebutton(widget))) } +func gtk_toggle_button_set_active(widget *C.GtkWidget, checked bool) { + C.gtk_toggle_button_set_active(togtktogglebutton(widget), togbool(checked)) +} + func gtk_combo_box_text_new() *C.GtkWidget { w := C.gtk_combo_box_text_new() C.gtkSetComboBoxArbitrarilyResizeable(w) diff --git a/sysdata.go b/sysdata.go index 10f2546..4a95591 100644 --- a/sysdata.go +++ b/sysdata.go @@ -41,6 +41,7 @@ var _xSysData interface { setAreaSize(int, int) repaintAll() center() + setChecked(bool) } = &sysData{} // this line will error if there's an inconsistency // signal sends the event signal. This raise is done asynchronously to avoid deadlocking the UI task. diff --git a/sysdata_unix.go b/sysdata_unix.go index c7a41f0..7814fb3 100644 --- a/sysdata_unix.go +++ b/sysdata_unix.go @@ -410,3 +410,13 @@ func (s *sysData) center() { } <-ret } + +func (s *sysData) setChecked(checked bool) { + ret := make(chan struct{}) + defer close(ret) + uitask <- func() { + gtk_toggle_button_set_active(s.widget, checked) + ret <- struct{}{} + } + <-ret +} diff --git a/test/main.go b/test/main.go index 68f3463..034d712 100644 --- a/test/main.go +++ b/test/main.go @@ -34,6 +34,7 @@ func gridWindow() *Window { b12 := NewButton("1,2") l20 := NewLabel("2,0") c21 := NewCheckbox("2,1") + c21.SetChecked(true) l22 := NewLabel("2,2") g := NewGrid(3, b00, b01, b02, @@ -43,6 +44,10 @@ func gridWindow() *Window { g.SetStretchy(1, 1) w.SetSpaced(*spacingTest) w.Open(g) + go func() {for {select { + case <-b12.Clicked: + c21.SetChecked(!c21.Checked()) + }}}() return w }