initial native implementation of wxCalendarCtrl for MSW
[wxWidgets.git] / src / msw / datectrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/datectrl.cpp
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
26 #if wxUSE_DATEPICKCTRL
27
28 #ifndef WX_PRECOMP
29 #include "wx/msw/wrapwin.h"
30 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
31 #include "wx/app.h"
32 #include "wx/intl.h"
33 #include "wx/dcclient.h"
34 #include "wx/msw/private.h"
35 #endif
36
37 #include "wx/datectrl.h"
38
39 #include "wx/msw/private/datecontrols.h"
40
41 #define _WX_DEFINE_DATE_EVENTS_
42 #include "wx/dateevt.h"
43
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
51 #endif
52
53 IMPLEMENT_DYNAMIC_CLASS(wxDatePickerCtrl, wxControl)
54
55 // ============================================================================
56 // implementation
57 // ============================================================================
58
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 {
73 if ( !wxMSWDateControls::CheckInitialization() )
74 return false;
75
76 // use wxDP_SPIN if wxDP_DEFAULT (0) was given as style
77 if ( !(style & wxDP_DROPDOWN) )
78 style |= wxDP_SPIN;
79
80 // initialize the base class
81 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
82 return false;
83
84 // create the native control
85 if ( !MSWCreateControl(DATETIMEPICK_CLASS, wxEmptyString, pos, size) )
86 return false;
87
88 if ( dt.IsValid() || (style & wxDP_ALLOWNONE) )
89 SetValue(dt);
90 else
91 SetValue(wxDateTime::Today());
92
93 return true;
94 }
95
96 WXDWORD wxDatePickerCtrl::MSWGetStyle(long style, WXDWORD *exstyle) const
97 {
98 WXDWORD styleMSW = wxDatePickerCtrlBase::MSWGetStyle(style, exstyle);
99
100 // although MSDN doesn't mention it, DTS_UPDOWN doesn't work with
101 // comctl32.dll 4.72
102 if ( wxApp::GetComCtl32Version() > 472 && (style & wxDP_SPIN) )
103 styleMSW |= DTS_UPDOWN;
104 //else: drop down by default
105
106 #ifdef DTS_SHORTDATECENTURYFORMAT
107 if ( style & wxDP_SHOWCENTURY )
108 styleMSW |= DTS_SHORTDATECENTURYFORMAT;
109 else
110 #endif // DTS_SHORTDATECENTURYFORMAT
111 styleMSW |= DTS_SHORTDATEFORMAT;
112
113 if ( style & wxDP_ALLOWNONE )
114 styleMSW |= DTS_SHOWNONE;
115
116 return styleMSW;
117 }
118
119 // TODO: handle WM_WININICHANGE
120
121 // ----------------------------------------------------------------------------
122 // wxDatePickerCtrl geometry
123 // ----------------------------------------------------------------------------
124
125 wxSize wxDatePickerCtrl::DoGetBestSize() const
126 {
127 wxClientDC dc(wx_const_cast(wxDatePickerCtrl *, this));
128 dc.SetFont(GetFont());
129
130 // we can't use FormatDate() here as the CRT doesn't always use the same
131 // format as the date picker control
132 wxString s;
133 for ( int len = 100; ; len *= 2 )
134 {
135 if ( ::GetDateFormat
136 (
137 LOCALE_USER_DEFAULT, // the control should use the same
138 DATE_SHORTDATE, // the format used by the control
139 NULL, // use current date (we don't care)
140 NULL, // no custom format
141 wxStringBuffer(s, len), // output buffer
142 len // and its length
143 ) )
144 {
145 // success
146 break;
147 }
148
149 const DWORD rc = ::GetLastError();
150 if ( rc != ERROR_INSUFFICIENT_BUFFER )
151 {
152 wxLogApiError(_T("GetDateFormat"), rc);
153
154 // fall back on wxDateTime, what else to do?
155 s = wxDateTime::Today().FormatDate();
156 break;
157 }
158 }
159
160 // the control adds a lot of extra space around separators
161 s.Replace(_T(","), _T(" , "));
162
163 int x, y;
164 dc.GetTextExtent(s, &x, &y);
165
166 wxSize best(x + 40 /* margin + arrows */, EDIT_HEIGHT_FROM_CHAR_HEIGHT(y));
167 CacheBestSize(best);
168 return best;
169 }
170
171 // ----------------------------------------------------------------------------
172 // wxDatePickerCtrl operations
173 // ----------------------------------------------------------------------------
174
175 void wxDatePickerCtrl::SetValue(const wxDateTime& dt)
176 {
177 wxCHECK_RET( dt.IsValid() || HasFlag(wxDP_ALLOWNONE),
178 _T("this control requires a valid date") );
179
180 SYSTEMTIME st;
181 if ( dt.IsValid() )
182 wxMSWDateControls::ToSystemTime(&st, dt);
183 if ( !DateTime_SetSystemtime(GetHwnd(),
184 dt.IsValid() ? GDT_VALID : GDT_NONE,
185 &st) )
186 {
187 wxLogDebug(_T("DateTime_SetSystemtime() failed"));
188 }
189
190 // we need to keep only the date part, times don't make sense for this
191 // control (in particular, comparisons with other dates would fail)
192 m_date = dt;
193 if ( m_date.IsValid() )
194 m_date.ResetTime();
195 }
196
197 wxDateTime wxDatePickerCtrl::GetValue() const
198 {
199 #ifdef __WXDEBUG__
200 wxDateTime dt;
201 SYSTEMTIME st;
202 if ( DateTime_GetSystemtime(GetHwnd(), &st) == GDT_VALID )
203 {
204 wxMSWDateControls::FromSystemTime(&dt, st);
205 }
206
207 wxASSERT_MSG( m_date.IsValid() == dt.IsValid() &&
208 (!dt.IsValid() || dt == m_date),
209 _T("bug in wxDatePickerCtrl: m_date not in sync") );
210 #endif // __WXDEBUG__
211
212 return m_date;
213 }
214
215 void wxDatePickerCtrl::SetRange(const wxDateTime& dt1, const wxDateTime& dt2)
216 {
217 SYSTEMTIME st[2];
218
219 DWORD flags = 0;
220 if ( dt1.IsValid() )
221 {
222 wxMSWDateControls::ToSystemTime(&st[0], dt1);
223 flags |= GDTR_MIN;
224 }
225
226 if ( dt2.IsValid() )
227 {
228 wxMSWDateControls::ToSystemTime(&st[1], dt2);
229 flags |= GDTR_MAX;
230 }
231
232 if ( !DateTime_SetRange(GetHwnd(), flags, st) )
233 {
234 wxLogDebug(_T("DateTime_SetRange() failed"));
235 }
236 }
237
238 bool wxDatePickerCtrl::GetRange(wxDateTime *dt1, wxDateTime *dt2) const
239 {
240 SYSTEMTIME st[2];
241
242 DWORD flags = DateTime_GetRange(GetHwnd(), st);
243 if ( dt1 )
244 {
245 if ( flags & GDTR_MIN )
246 wxMSWDateControls::FromSystemTime(dt1, st[0]);
247 else
248 *dt1 = wxDefaultDateTime;
249 }
250
251 if ( dt2 )
252 {
253 if ( flags & GDTR_MAX )
254 wxMSWDateControls::FromSystemTime(dt2, st[1]);
255 else
256 *dt2 = wxDefaultDateTime;
257 }
258
259 return flags != 0;
260 }
261
262 // ----------------------------------------------------------------------------
263 // wxDatePickerCtrl events
264 // ----------------------------------------------------------------------------
265
266 bool
267 wxDatePickerCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
268 {
269 NMHDR* hdr = (NMHDR *)lParam;
270 switch ( hdr->code )
271 {
272 case DTN_DATETIMECHANGE:
273 {
274 NMDATETIMECHANGE *dtch = (NMDATETIMECHANGE *)hdr;
275 wxDateTime dt;
276 if ( dtch->dwFlags == GDT_VALID )
277 wxMSWDateControls::FromSystemTime(&dt, dtch->st);
278
279 // filter out duplicate DTN_DATETIMECHANGE events which the native
280 // control sends us when using wxDP_DROPDOWN style
281 if ( (m_date.IsValid() != dt.IsValid()) ||
282 (m_date.IsValid() && dt != m_date) )
283 {
284 m_date = dt;
285 wxDateEvent event(this, dt, wxEVT_DATE_CHANGED);
286 if ( HandleWindowEvent(event) )
287 {
288 *result = 0;
289 return true;
290 }
291 }
292 //else: both the old and new values are invalid, nothing changed
293 }
294 }
295
296 return wxDatePickerCtrlBase::MSWOnNotify(idCtrl, lParam, result);
297 }
298
299 #endif // wxUSE_DATEPICKCTRL