summaryrefslogtreecommitdiff
path: root/spinbox_unix.go
blob: b6b2ec31c098c247a92f806a5f44ea936c554de2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// +build !windows,!darwin

// 28 october 2014

package ui

import (
	"unsafe"
)

// #include "gtk_unix.h"
import "C"

// TODO preferred width may be too wide

type spinbox struct {
	*controlSingleWidget
	spinbutton	*C.GtkSpinButton
}

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)),
	}
	C.gtk_spin_button_set_digits(s.spinbutton, 0)				// integers
	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))
}