]>
Commit | Line | Data |
---|---|---|
feb72429 | 1 | ///////////////////////////////////////////////////////////////////////////// |
02e05e7e | 2 | // Name: src/msw/datectrl.cpp |
feb72429 VZ |
3 | // Purpose: wxDatePickerCtrl implementation |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 2005-01-09 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 2005 Vadim Zeitlin <vadim@wxwindows.org> | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | #include "wx/wxprec.h" | |
21 | ||
22 | #ifdef __BORLANDC__ | |
23 | #pragma hdrstop | |
24 | #endif | |
25 | ||
02e05e7e WS |
26 | #if wxUSE_DATEPICKCTRL |
27 | ||
feb72429 | 28 | #ifndef WX_PRECOMP |
57bd4c60 WS |
29 | #include "wx/msw/wrapwin.h" |
30 | #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly" | |
02e05e7e WS |
31 | #include "wx/app.h" |
32 | #include "wx/intl.h" | |
33 | #include "wx/dcclient.h" | |
8a18ea3f | 34 | #include "wx/settings.h" |
02e05e7e | 35 | #include "wx/msw/private.h" |
feb72429 VZ |
36 | #endif |
37 | ||
38 | #include "wx/datectrl.h" | |
51317496 VZ |
39 | |
40 | #include "wx/msw/private/datecontrols.h" | |
feb72429 | 41 | |
feb72429 VZ |
42 | #include "wx/dateevt.h" |
43 | ||
3200f37d VZ |
44 | // apparently some versions of mingw define these macros erroneously |
45 | #ifndef DateTime_GetSystemtime | |
46 | #define DateTime_GetSystemtime DateTime_GetSystemTime | |
47 | #endif | |
48 | ||
49 | #ifndef DateTime_SetSystemtime | |
50 | #define DateTime_SetSystemtime DateTime_SetSystemTime | |
65ab1002 JS |
51 | #endif |
52 | ||
a69e2a0a VZ |
53 | IMPLEMENT_DYNAMIC_CLASS(wxDatePickerCtrl, wxControl) |
54 | ||
feb72429 VZ |
55 | // ============================================================================ |
56 | // implementation | |
57 | // ============================================================================ | |
58 | ||
feb72429 VZ |
59 | // ---------------------------------------------------------------------------- |
60 | // wxDatePickerCtrl creation | |
61 | // ---------------------------------------------------------------------------- | |
62 | ||
63 | bool | |
64 | wxDatePickerCtrl::Create(wxWindow *parent, | |
65 | wxWindowID id, | |
66 | const wxDateTime& dt, | |
67 | const wxPoint& pos, | |
68 | const wxSize& size, | |
69 | long style, | |
70 | const wxValidator& validator, | |
71 | const wxString& name) | |
72 | { | |
51317496 VZ |
73 | if ( !wxMSWDateControls::CheckInitialization() ) |
74 | return false; | |
0aa7cb54 | 75 | |
5385747e VZ |
76 | // use wxDP_SPIN if wxDP_DEFAULT (0) was given as style |
77 | if ( !(style & wxDP_DROPDOWN) ) | |
78 | style |= wxDP_SPIN; | |
79 | ||
feb72429 VZ |
80 | // initialize the base class |
81 | if ( !CreateControl(parent, id, pos, size, style, validator, name) ) | |
82 | return false; | |
83 | ||
84 | // create the native control | |
f31a4098 | 85 | if ( !MSWCreateControl(DATETIMEPICK_CLASS, wxEmptyString, pos, size) ) |
feb72429 VZ |
86 | return false; |
87 | ||
bab3f3ea | 88 | if ( dt.IsValid() || (style & wxDP_ALLOWNONE) ) |
feb72429 | 89 | SetValue(dt); |
4d536421 RD |
90 | else |
91 | SetValue(wxDateTime::Today()); | |
feb72429 VZ |
92 | |
93 | return true; | |
94 | } | |
95 | ||
96 | WXDWORD wxDatePickerCtrl::MSWGetStyle(long style, WXDWORD *exstyle) const | |
97 | { | |
98 | WXDWORD styleMSW = wxDatePickerCtrlBase::MSWGetStyle(style, exstyle); | |
99 | ||
0aa7cb54 VZ |
100 | // although MSDN doesn't mention it, DTS_UPDOWN doesn't work with |
101 | // comctl32.dll 4.72 | |
2a1f999f | 102 | if ( wxApp::GetComCtl32Version() > 472 && (style & wxDP_SPIN) ) |
29c86948 VZ |
103 | styleMSW |= DTS_UPDOWN; |
104 | //else: drop down by default | |
105 | ||
2cfbeac8 VZ |
106 | #ifdef DTS_SHORTDATECENTURYFORMAT |
107 | if ( style & wxDP_SHOWCENTURY ) | |
108 | styleMSW |= DTS_SHORTDATECENTURYFORMAT; | |
109 | else | |
110 | #endif // DTS_SHORTDATECENTURYFORMAT | |
111 | styleMSW |= DTS_SHORTDATEFORMAT; | |
feb72429 | 112 | |
3200f37d VZ |
113 | if ( style & wxDP_ALLOWNONE ) |
114 | styleMSW |= DTS_SHOWNONE; | |
115 | ||
feb72429 VZ |
116 | return styleMSW; |
117 | } | |
118 | ||
119 | // TODO: handle WM_WININICHANGE | |
120 | ||
121 | // ---------------------------------------------------------------------------- | |
122 | // wxDatePickerCtrl geometry | |
123 | // ---------------------------------------------------------------------------- | |
124 | ||
125 | wxSize wxDatePickerCtrl::DoGetBestSize() const | |
126 | { | |
5c33522f | 127 | wxClientDC dc(const_cast<wxDatePickerCtrl *>(this)); |
feb72429 | 128 | |
c01359d9 VZ |
129 | // we can't use FormatDate() here as the CRT doesn't always use the same |
130 | // format as the date picker control | |
131 | wxString s; | |
132 | for ( int len = 100; ; len *= 2 ) | |
133 | { | |
134 | if ( ::GetDateFormat | |
135 | ( | |
136 | LOCALE_USER_DEFAULT, // the control should use the same | |
137 | DATE_SHORTDATE, // the format used by the control | |
138 | NULL, // use current date (we don't care) | |
139 | NULL, // no custom format | |
140 | wxStringBuffer(s, len), // output buffer | |
141 | len // and its length | |
142 | ) ) | |
143 | { | |
144 | // success | |
145 | break; | |
146 | } | |
147 | ||
148 | const DWORD rc = ::GetLastError(); | |
149 | if ( rc != ERROR_INSUFFICIENT_BUFFER ) | |
150 | { | |
9a83f860 | 151 | wxLogApiError(wxT("GetDateFormat"), rc); |
c01359d9 VZ |
152 | |
153 | // fall back on wxDateTime, what else to do? | |
154 | s = wxDateTime::Today().FormatDate(); | |
155 | break; | |
156 | } | |
157 | } | |
158 | ||
8a18ea3f VZ |
159 | // the best size for the control is bigger than just the string |
160 | // representation of todays date because the control must accommodate any | |
161 | // date and while the widths of all digits are usually about the same, the | |
162 | // width of the month string varies a lot, so try to account for it | |
9a83f860 | 163 | s += wxT("WW"); |
c01359d9 VZ |
164 | |
165 | int x, y; | |
166 | dc.GetTextExtent(s, &x, &y); | |
167 | ||
8a18ea3f VZ |
168 | // account for the drop-down arrow or spin arrows |
169 | x += wxSystemSettings::GetMetric(wxSYS_HSCROLL_ARROW_X); | |
170 | ||
171 | // and for the checkbox if we have it | |
172 | if ( HasFlag(wxDP_ALLOWNONE) ) | |
173 | x += 3*GetCharWidth(); | |
174 | ||
175 | wxSize best(x, EDIT_HEIGHT_FROM_CHAR_HEIGHT(y)); | |
31582e4e RD |
176 | CacheBestSize(best); |
177 | return best; | |
feb72429 VZ |
178 | } |
179 | ||
180 | // ---------------------------------------------------------------------------- | |
181 | // wxDatePickerCtrl operations | |
182 | // ---------------------------------------------------------------------------- | |
183 | ||
184 | void wxDatePickerCtrl::SetValue(const wxDateTime& dt) | |
185 | { | |
3200f37d | 186 | wxCHECK_RET( dt.IsValid() || HasFlag(wxDP_ALLOWNONE), |
9a83f860 | 187 | wxT("this control requires a valid date") ); |
feb72429 VZ |
188 | |
189 | SYSTEMTIME st; | |
3200f37d | 190 | if ( dt.IsValid() ) |
feb72429 | 191 | { |
ab2ef6e0 VZ |
192 | // Don't try setting the date if it's out of range: calendar control |
193 | // under XP (and presumably all the other pre-Vista Windows versions) | |
194 | // doesn't return false from DateTime_SetSystemtime() in this case but | |
195 | // doesn't actually change the date, so we can't update our m_date | |
196 | // unconditionally and would need to check whether it was changed | |
197 | // before doing it. It looks simpler to just check whether it's in | |
198 | // range here instead. | |
199 | // | |
200 | // If we ever drop support for XP we could rely on the return value of | |
201 | // DateTime_SetSystemtime() but this probably won't happen in near | |
202 | // future. | |
5f899cbe VZ |
203 | wxDateTime dtStart, dtEnd; |
204 | GetRange(&dtStart, &dtEnd); | |
ab2ef6e0 VZ |
205 | if ( (dtStart.IsValid() && dt < dtStart) || |
206 | (dtEnd.IsValid() && dt > dtEnd) ) | |
5f899cbe | 207 | { |
ab2ef6e0 VZ |
208 | // Fail silently, some existing code relies on SetValue() with an |
209 | // out of range value simply doing nothing -- so don't. | |
210 | return; | |
5f899cbe VZ |
211 | } |
212 | ||
ab2ef6e0 VZ |
213 | dt.GetAsMSWSysTime(&st); |
214 | } | |
215 | ||
216 | if ( !DateTime_SetSystemtime(GetHwnd(), | |
217 | dt.IsValid() ? GDT_VALID : GDT_NONE, | |
218 | &st) ) | |
219 | { | |
220 | // The only expected failure is when the date is out of range but we | |
221 | // already checked for this above. | |
222 | wxFAIL_MSG( wxT("Setting the calendar date unexpectedly failed.") ); | |
223 | ||
5f899cbe VZ |
224 | // In any case, skip updating m_date below. |
225 | return; | |
feb72429 | 226 | } |
e4164aa9 | 227 | |
5541976c VZ |
228 | // we need to keep only the date part, times don't make sense for this |
229 | // control (in particular, comparisons with other dates would fail) | |
28039907 | 230 | m_date = dt; |
0bdd8074 VZ |
231 | if ( m_date.IsValid() ) |
232 | m_date.ResetTime(); | |
feb72429 VZ |
233 | } |
234 | ||
235 | wxDateTime wxDatePickerCtrl::GetValue() const | |
236 | { | |
4b6a582b | 237 | #if wxDEBUG_LEVEL |
feb72429 VZ |
238 | wxDateTime dt; |
239 | SYSTEMTIME st; | |
240 | if ( DateTime_GetSystemtime(GetHwnd(), &st) == GDT_VALID ) | |
241 | { | |
edc0f733 | 242 | dt.SetFromMSWSysDate(st); |
feb72429 VZ |
243 | } |
244 | ||
e4164aa9 VZ |
245 | wxASSERT_MSG( m_date.IsValid() == dt.IsValid() && |
246 | (!dt.IsValid() || dt == m_date), | |
9a83f860 | 247 | wxT("bug in wxDatePickerCtrl: m_date not in sync") ); |
4b6a582b | 248 | #endif // wxDEBUG_LEVEL |
e4164aa9 VZ |
249 | |
250 | return m_date; | |
feb72429 VZ |
251 | } |
252 | ||
253 | void wxDatePickerCtrl::SetRange(const wxDateTime& dt1, const wxDateTime& dt2) | |
254 | { | |
255 | SYSTEMTIME st[2]; | |
256 | ||
257 | DWORD flags = 0; | |
258 | if ( dt1.IsValid() ) | |
259 | { | |
154014d6 | 260 | dt1.GetAsMSWSysTime(st + 0); |
feb72429 VZ |
261 | flags |= GDTR_MIN; |
262 | } | |
263 | ||
264 | if ( dt2.IsValid() ) | |
265 | { | |
154014d6 | 266 | dt2.GetAsMSWSysTime(st + 1); |
feb72429 VZ |
267 | flags |= GDTR_MAX; |
268 | } | |
269 | ||
270 | if ( !DateTime_SetRange(GetHwnd(), flags, st) ) | |
271 | { | |
9a83f860 | 272 | wxLogDebug(wxT("DateTime_SetRange() failed")); |
feb72429 VZ |
273 | } |
274 | } | |
275 | ||
276 | bool wxDatePickerCtrl::GetRange(wxDateTime *dt1, wxDateTime *dt2) const | |
277 | { | |
278 | SYSTEMTIME st[2]; | |
279 | ||
280 | DWORD flags = DateTime_GetRange(GetHwnd(), st); | |
281 | if ( dt1 ) | |
282 | { | |
283 | if ( flags & GDTR_MIN ) | |
edc0f733 | 284 | dt1->SetFromMSWSysDate(st[0]); |
feb72429 VZ |
285 | else |
286 | *dt1 = wxDefaultDateTime; | |
287 | } | |
288 | ||
289 | if ( dt2 ) | |
290 | { | |
291 | if ( flags & GDTR_MAX ) | |
edc0f733 | 292 | dt2->SetFromMSWSysDate(st[1]); |
feb72429 VZ |
293 | else |
294 | *dt2 = wxDefaultDateTime; | |
295 | } | |
296 | ||
297 | return flags != 0; | |
298 | } | |
299 | ||
300 | // ---------------------------------------------------------------------------- | |
301 | // wxDatePickerCtrl events | |
302 | // ---------------------------------------------------------------------------- | |
303 | ||
304 | bool | |
305 | wxDatePickerCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) | |
306 | { | |
307 | NMHDR* hdr = (NMHDR *)lParam; | |
308 | switch ( hdr->code ) | |
309 | { | |
310 | case DTN_DATETIMECHANGE: | |
b4446c24 | 311 | { |
feb72429 VZ |
312 | NMDATETIMECHANGE *dtch = (NMDATETIMECHANGE *)hdr; |
313 | wxDateTime dt; | |
314 | if ( dtch->dwFlags == GDT_VALID ) | |
edc0f733 | 315 | dt.SetFromMSWSysDate(dtch->st); |
feb72429 | 316 | |
e4164aa9 VZ |
317 | // filter out duplicate DTN_DATETIMECHANGE events which the native |
318 | // control sends us when using wxDP_DROPDOWN style | |
0c8433d0 VZ |
319 | if ( (m_date.IsValid() != dt.IsValid()) || |
320 | (m_date.IsValid() && dt != m_date) ) | |
feb72429 | 321 | { |
e4164aa9 VZ |
322 | m_date = dt; |
323 | wxDateEvent event(this, dt, wxEVT_DATE_CHANGED); | |
937013e0 | 324 | if ( HandleWindowEvent(event) ) |
e4164aa9 VZ |
325 | { |
326 | *result = 0; | |
327 | return true; | |
328 | } | |
feb72429 | 329 | } |
0c8433d0 | 330 | //else: both the old and new values are invalid, nothing changed |
b4446c24 | 331 | } |
feb72429 VZ |
332 | } |
333 | ||
334 | return wxDatePickerCtrlBase::MSWOnNotify(idCtrl, lParam, result); | |
335 | } | |
336 | ||
9b877d18 | 337 | #endif // wxUSE_DATEPICKCTRL |