-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdateedit.go
201 lines (157 loc) · 4.17 KB
/
dateedit.go
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Copyright 2011 The Walk Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package walk
import (
"time"
"unsafe"
)
import (
"github.com/lxn/win"
)
type DateEdit struct {
WidgetBase
dateChangedPublisher EventPublisher
}
func newDateEdit(parent Container, style uint32) (*DateEdit, error) {
de := new(DateEdit)
if err := InitWidget(
de,
parent,
"SysDateTimePick32",
win.WS_TABSTOP|win.WS_VISIBLE|win.DTS_SHORTDATEFORMAT|style,
0); err != nil {
return nil, err
}
de.MustRegisterProperty("Date", NewProperty(
func() interface{} {
return de.Date()
},
func(v interface{}) error {
return de.SetDate(v.(time.Time))
},
de.dateChangedPublisher.Event()))
return de, nil
}
func NewDateEdit(parent Container) (*DateEdit, error) {
return newDateEdit(parent, 0)
}
func NewDateEditWithNoneOption(parent Container) (*DateEdit, error) {
return newDateEdit(parent, win.DTS_SHOWNONE)
}
func (*DateEdit) LayoutFlags() LayoutFlags {
return GrowableHorz
}
func (de *DateEdit) MinSizeHint() Size {
return de.dialogBaseUnitsToPixels(Size{64, 12})
}
func (de *DateEdit) SizeHint() Size {
return de.MinSizeHint()
}
func (de *DateEdit) systemTimeToTime(st *win.SYSTEMTIME) time.Time {
if st == nil || !de.hasStyleBits(win.DTS_SHOWNONE) && st.WYear == 1601 && st.WMonth == 1 && st.WDay == 1 {
return time.Time{}
}
return time.Date(int(st.WYear), time.Month(st.WMonth), int(st.WDay), 0, 0, 0, 0, time.Local)
}
func (de *DateEdit) timeToSystemTime(t time.Time) *win.SYSTEMTIME {
if t.IsZero() {
if de.hasStyleBits(win.DTS_SHOWNONE) {
return nil
} else {
return &win.SYSTEMTIME{
WYear: uint16(1601),
WMonth: uint16(1),
WDay: uint16(1),
}
}
}
return &win.SYSTEMTIME{
WYear: uint16(t.Year()),
WMonth: uint16(t.Month()),
WDay: uint16(t.Day()),
}
}
func (de *DateEdit) systemTime() (*win.SYSTEMTIME, error) {
var st win.SYSTEMTIME
switch de.SendMessage(win.DTM_GETSYSTEMTIME, 0, uintptr(unsafe.Pointer(&st))) {
case win.GDT_VALID:
return &st, nil
case win.GDT_NONE:
return nil, nil
}
return nil, newError("SendMessage(DTM_GETSYSTEMTIME)")
}
func (de *DateEdit) setSystemTime(st *win.SYSTEMTIME) error {
var wParam uintptr
if st != nil {
wParam = win.GDT_VALID
} else {
wParam = win.GDT_NONE
}
if 0 == de.SendMessage(win.DTM_SETSYSTEMTIME, wParam, uintptr(unsafe.Pointer(st))) {
return newError("SendMessage(DTM_SETSYSTEMTIME)")
}
de.dateChangedPublisher.Publish()
return nil
}
func (de *DateEdit) Range() (min, max time.Time) {
var st [2]win.SYSTEMTIME
ret := de.SendMessage(win.DTM_GETRANGE, 0, uintptr(unsafe.Pointer(&st[0])))
if ret&win.GDTR_MIN > 0 {
min = de.systemTimeToTime(&st[0])
}
if ret&win.GDTR_MAX > 0 {
max = de.systemTimeToTime(&st[1])
}
return
}
func (de *DateEdit) SetRange(min, max time.Time) error {
if !min.IsZero() && !max.IsZero() {
if min.Year() > max.Year() ||
min.Year() == max.Year() && min.Month() > max.Month() ||
min.Year() == max.Year() && min.Month() == max.Month() && min.Day() > max.Day() {
return newError("invalid range")
}
}
var st [2]win.SYSTEMTIME
var wParam uintptr
if !min.IsZero() {
wParam |= win.GDTR_MIN
st[0] = *de.timeToSystemTime(min)
}
if !max.IsZero() {
wParam |= win.GDTR_MAX
st[1] = *de.timeToSystemTime(max)
}
if 0 == de.SendMessage(win.DTM_SETRANGE, wParam, uintptr(unsafe.Pointer(&st[0]))) {
return newError("SendMessage(DTM_SETRANGE)")
}
return nil
}
func (de *DateEdit) Date() time.Time {
st, err := de.systemTime()
if err != nil {
return time.Time{}
}
if st == nil {
return time.Time{}
}
return time.Unix(de.systemTimeToTime(st).Unix(), 0)
}
func (de *DateEdit) SetDate(date time.Time) error {
return de.setSystemTime(de.timeToSystemTime(date))
}
func (de *DateEdit) DateChanged() *Event {
return de.dateChangedPublisher.Event()
}
func (de *DateEdit) WndProc(hwnd win.HWND, msg uint32, wParam, lParam uintptr) uintptr {
switch msg {
case win.WM_NOTIFY:
switch uint32(((*win.NMHDR)(unsafe.Pointer(lParam))).Code) {
case win.DTN_DATETIMECHANGE:
de.dateChangedPublisher.Publish()
}
}
return de.WidgetBase.WndProc(hwnd, msg, wParam, lParam)
}