Started fleshing out the Spinbox interface. Added Value() and SetValue(); implemented on GTK+. Added min and max to the constructor; implemented on GTK+.

This commit is contained in:
Pietro Gagliardi 2014-10-30 12:16:42 -04:00
parent b28781f281
commit 6428b17b7f
3 changed files with 46 additions and 10 deletions

View File

@ -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)
}

View File

@ -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))
}

View File

@ -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)