2015-12-12 17:59:01 -06:00
|
|
|
// 12 december 2015
|
|
|
|
|
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
2018-08-11 15:56:13 -05:00
|
|
|
"time"
|
2015-12-12 17:59:01 -06:00
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2018-08-26 12:48:13 -05:00
|
|
|
// #include "pkgui.h"
|
2015-12-12 17:59:01 -06:00
|
|
|
import "C"
|
|
|
|
|
|
|
|
// DateTimePicker is a Control that represents a field where the user
|
|
|
|
// can enter a date and/or a time.
|
|
|
|
type DateTimePicker struct {
|
2018-08-11 18:52:29 -05:00
|
|
|
ControlBase
|
2015-12-12 17:59:01 -06:00
|
|
|
d *C.uiDateTimePicker
|
2018-08-11 15:56:13 -05:00
|
|
|
onChanged func(*DateTimePicker)
|
2015-12-12 17:59:01 -06:00
|
|
|
}
|
|
|
|
|
2018-08-11 18:52:29 -05:00
|
|
|
func finishNewDateTimePicker(dd *C.uiDateTimePicker) *DateTimePicker {
|
2015-12-12 17:59:01 -06:00
|
|
|
d := new(DateTimePicker)
|
|
|
|
|
2018-08-11 18:52:29 -05:00
|
|
|
d.d = dd
|
2015-12-12 17:59:01 -06:00
|
|
|
|
2018-08-26 12:48:13 -05:00
|
|
|
C.pkguiDateTimePickerOnChanged(d.d)
|
2018-08-11 15:56:13 -05:00
|
|
|
|
2018-08-11 18:52:29 -05:00
|
|
|
d.ControlBase = NewControlBase(d, uintptr(unsafe.Pointer(d.d)))
|
2015-12-12 17:59:01 -06:00
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2018-08-11 18:52:29 -05:00
|
|
|
// NewDateTimePicker creates a new DateTimePicker that shows
|
|
|
|
// both a date and a time.
|
|
|
|
func NewDateTimePicker() *DateTimePicker {
|
2018-08-12 10:07:33 -05:00
|
|
|
return finishNewDateTimePicker(C.uiNewDateTimePicker())
|
2018-08-11 18:52:29 -05:00
|
|
|
}
|
|
|
|
|
2015-12-12 17:59:01 -06:00
|
|
|
// NewDatePicker creates a new DateTimePicker that shows
|
|
|
|
// only a date.
|
|
|
|
func NewDatePicker() *DateTimePicker {
|
2018-08-11 18:52:29 -05:00
|
|
|
return finishNewDateTimePicker(C.uiNewDatePicker())
|
2015-12-12 17:59:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewTimePicker creates a new DateTimePicker that shows
|
|
|
|
// only a time.
|
|
|
|
func NewTimePicker() *DateTimePicker {
|
2018-08-11 18:52:29 -05:00
|
|
|
return finishNewDateTimePicker(C.uiNewTimePicker())
|
2015-12-12 17:59:01 -06:00
|
|
|
}
|
2018-08-11 15:56:13 -05:00
|
|
|
|
|
|
|
// Time returns the time stored in the uiDateTimePicker.
|
|
|
|
// The time is assumed to be local time.
|
|
|
|
func (d *DateTimePicker) Time() time.Time {
|
2018-08-26 12:48:13 -05:00
|
|
|
tm := C.pkguiAllocTime()
|
|
|
|
defer C.pkguiFreeTime(tm)
|
2018-08-11 15:56:13 -05:00
|
|
|
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.
|
2018-08-12 10:07:33 -05:00
|
|
|
// t's components are read as-is via t.Date() and t.Clock();
|
2018-08-11 18:52:29 -05:00
|
|
|
// no time zone manipulations are done.
|
2018-08-11 15:56:13 -05:00
|
|
|
func (d *DateTimePicker) SetTime(t time.Time) {
|
2018-08-26 12:48:13 -05:00
|
|
|
tm := C.pkguiAllocTime()
|
|
|
|
defer C.pkguiFreeTime(tm)
|
2018-08-11 15:56:13 -05:00
|
|
|
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)
|
2018-08-12 10:07:33 -05:00
|
|
|
hour, min, sec := t.Clock()
|
2018-08-11 15:56:13 -05:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2018-08-11 18:52:29 -05:00
|
|
|
// OnChanged registers f to be run when the user changes the time
|
|
|
|
// in the DateTimePicker. Only one function can be registered at a
|
|
|
|
// time.
|
2018-08-11 15:56:13 -05:00
|
|
|
func (d *DateTimePicker) OnChanged(f func(*DateTimePicker)) {
|
|
|
|
d.onChanged = f
|
|
|
|
}
|
|
|
|
|
2018-08-26 12:48:13 -05:00
|
|
|
//export pkguiDoDateTimePickerOnChanged
|
|
|
|
func pkguiDoDateTimePickerOnChanged(dd *C.uiDateTimePicker, data unsafe.Pointer) {
|
2018-08-11 20:29:20 -05:00
|
|
|
d := ControlFromLibui(uintptr(unsafe.Pointer(dd))).(*DateTimePicker)
|
2018-08-11 15:56:13 -05:00
|
|
|
if d.onChanged != nil {
|
|
|
|
d.onChanged(d)
|
|
|
|
}
|
|
|
|
}
|