diff --git a/basicctrls.go b/basicctrls.go index 4029d4e..8e4e926 100644 --- a/basicctrls.go +++ b/basicctrls.go @@ -143,8 +143,7 @@ func NewTextbox() Textbox { // Spinbox is a Control that provides a text entry field that accepts integers and up and down buttons to increment and decrement those values. // This control is in its preliminary state. // TODO everything: -// - TODO set increment -// - TODO set step +// - TODO set increment? (work on windows) // - TODO set page step? // - TODO wrapping // - TODO set/get integer value @@ -152,9 +151,19 @@ func NewTextbox() Textbox { // - TODO ensuring values entered in text box stay within bounds type Spinbox interface { Control + + // Value and SetValue get and set the current value of the Spinbox, respectively. + // For SetValue, if the new value is outside the current range of the Spinbox, it is set to the nearest extremity. + Value() int + SetValue(value int) } -// NewSpinbox creates a new Spinbox. -func NewSpinbox() Spinbox { - return newSpinbox() +// NewSpinbox creates a new Spinbox with the given minimum and maximum. +// The initial value will be the minimum value. +// NewSpinbox() panics if min > max. +func NewSpinbox(min int, max int) Spinbox { + if min > max { + panic("min > max in NewSpinbox()") + } + return newSpinbox(min, max) } diff --git a/spinbox_unix.go b/spinbox_unix.go index 33ac2f3..b6b2ec3 100644 --- a/spinbox_unix.go +++ b/spinbox_unix.go @@ -18,8 +18,9 @@ type spinbox struct { spinbutton *C.GtkSpinButton } -func newSpinbox() Spinbox { - widget := C.gtk_spin_button_new_with_range(0, 100, 1) +func newSpinbox(min int, max int) Spinbox { + // gtk_spin_button_new_with_range() initially sets its value to the minimum value + widget := C.gtk_spin_button_new_with_range(C.gdouble(min), C.gdouble(max), 1) s := &spinbox{ controlSingleWidget: newControlSingleWidget(widget), spinbutton: (*C.GtkSpinButton)(unsafe.Pointer(widget)), @@ -28,3 +29,20 @@ func newSpinbox() Spinbox { C.gtk_spin_button_set_numeric(s.spinbutton, C.TRUE) // digits only return s } + +func (s *spinbox) Value() int { + return int(C.gtk_spin_button_get_value(s.spinbutton)) +} + +func (s *spinbox) SetValue(value int) { + var min, max C.gdouble + + C.gtk_spin_button_get_range(s.spinbutton, &min, &max) + if value < int(min) { + value = int(min) + } + if value > int(max) { + value = int(max) + } + C.gtk_spin_button_set_value(s.spinbutton, C.gdouble(value)) +} diff --git a/zz_test.go b/zz_test.go index c7f864a..afe3c04 100644 --- a/zz_test.go +++ b/zz_test.go @@ -149,9 +149,18 @@ func (tw *testwin) addfe() { tw.openbtn, tw.fnlabel) tw.festack.SetStretchy(4) tw.festack.SetStretchy(6) - tw.festack2 = newVerticalStack(NewSpinbox(), Space(), NewTextbox()) - tw.festack2.SetStretchy(1) - tw.festack2.SetStretchy(2) + sb := NewSpinbox(0, 100) + cbutton := NewButton("Set to Invalid Low") + cbutton.OnClicked(func() { + sb.SetValue(-500) + }) + dbutton := NewButton("Set to Invalid High") + dbutton.OnClicked(func() { + sb.SetValue(500) + }) + tw.festack2 = newVerticalStack(sb, cbutton, dbutton, Space(), NewTextbox()) + tw.festack2.SetStretchy(3) + tw.festack2.SetStretchy(4) tw.festack = newHorizontalStack(tw.festack, tw.festack2) tw.festack.SetStretchy(0) tw.festack.SetStretchy(1)