summaryrefslogtreecommitdiff
path: root/datetimepicker.go
blob: ff98cede5fdd03805e5e4fea8b4206e10acf4840 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// 12 december 2015

package ui

import (
	"time"
	"unsafe"
)

// #include <time.h>
// #include "ui.h"
// static inline struct tm *allocTimeStruct(void)
// {
// 	/* TODO handle error */
// 	return (struct tm *) malloc(sizeof (struct tm));
// }
// extern void doDateTimePickerChanged(uiDateTimePicker *, void *);
// static inline void realuiDateTimePickerOnChanged(uiDateTimePicker *d)
// {
// 	uiDateTimePickerOnChanged(d, doDateTimePickerOnChanged, NULL);
// }
import "C"

// DateTimePicker is a Control that represents a field where the user
// can enter a date and/or a time.
type DateTimePicker struct {
	c	*C.uiControl
	d	*C.uiDateTimePicker

	onChanged	func(*DateTimePicker)
}

// NewDateTimePicker creates a new DateTimePicker that shows
// both a date and a time.
func NewDateTimePicker() *DateTimePicker {
	d := new(DateTimePicker)

	d.d = C.uiNewDateTimePicker()
	d.c = (*C.uiControl)(unsafe.Pointer(d.d))

	C.realuiDateTimePickerOnChanged(d.d)

	return d
}

// NewDatePicker creates a new DateTimePicker that shows
// only a date.
func NewDatePicker() *DateTimePicker {
	d := new(DateTimePicker)

	d.d = C.uiNewDatePicker()
	d.c = (*C.uiControl)(unsafe.Pointer(d.d))

	C.realuiDateTimePickerOnChanged(d.d)

	return d
}

// NewTimePicker creates a new DateTimePicker that shows
// only a time.
func NewTimePicker() *DateTimePicker {
	d := new(DateTimePicker)

	d.d = C.uiNewTimePicker()
	d.c = (*C.uiControl)(unsafe.Pointer(d.d))

	C.realuiDateTimePickerOnChanged(d.d)

	return d
}

// Destroy destroys the DateTimePicker.
func (d *DateTimePicker) Destroy() {
	C.uiControlDestroy(d.c)
}

// LibuiControl returns the libui uiControl pointer that backs
// the Window. This is only used by package ui itself and should
// not be called by programs.
func (d *DateTimePicker) LibuiControl() uintptr {
	return uintptr(unsafe.Pointer(d.c))
}

// Handle returns the OS-level handle associated with this DateTimePicker.
// On Windows this is an HWND of a standard Windows API
// DATETIMEPICK_CLASS class (as provided by Common Controls
// version 6).
// On GTK+ this is a pointer to a libui-internal class.
// On OS X this is a pointer to a NSDatePicker.
func (d *DateTimePicker) Handle() uintptr {
	return uintptr(C.uiControlHandle(d.c))
}

// Show shows the DateTimePicker.
func (d *DateTimePicker) Show() {
	C.uiControlShow(d.c)
}

// Hide hides the DateTimePicker.
func (d *DateTimePicker) Hide() {
	C.uiControlHide(d.c)
}

// Enable enables the DateTimePicker.
func (d *DateTimePicker) Enable() {
	C.uiControlEnable(d.c)
}

// Disable disables the DateTimePicker.
func (d *DateTimePicker) Disable() {
	C.uiControlDisable(d.c)
}

// Time returns the time stored in the uiDateTimePicker.
// The time is assumed to be local time.
func (d *DateTimePicker) Time() time.Time {
	tm := C.allocTimeStruct()
	defer C.free(unsafe.Pointer(tm))
	C.uiDateTimePickerTime(d.d, tm)
	return time.Date(
		int(tm.tm_year + 1900),
		time.Month(tm.tm_mon + 1),
		int(tm.tm_mday),
		int(tm.tm_hour),
		int(tm.tm_min),
		int(tm.tm_sec),
		0, time.Local)
}

// SetTime sets the time in the DateTimePicker to t.
// t's components are read as-is; no time zone manipulations
// are done.
func (d *DateTimePicker) SetTime(t time.Time) {
	tm := C.allocTimeStruct()
	defer C.free(unsafe.Pointer(tm))
	year, mon, mday := t.Date()
	tm.tm_year = C.int(year - 1900)
	tm.tm_mon = C.int(mon - 1)
	tm.tm_mday = C.int(mday)
	hour, min, sec := t.Time()
	tm.tm_hour = C.int(hour)
	tm.tm_min = C.int(min)
	tm.tm_sec = C.int(sec)
	tm.tm_isdst = -1
	C.uiDateTimePickerSetTime(d.d, tm)
}

// OnChanged registers f to be run when the user changes the time in the DateTimePicker.
// Only one function can be registered at a time.
func (d *DateTimePicker) OnChanged(f func(*DateTimePicker)) {
	d.onChanged = f
}

//export doDateTimePickerOnChanged
func doDateTimePickerOnChanged(dd *C.uiDateTimePicker, data unsafe.Pointer) {
	d := dateTimePickers[dd]
	if d.onChanged != nil {
		d.onChanged(d)
	}
}