1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/datectrl.cpp
3 // Purpose: wxDatePickerCtrl implementation
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2005 Vadim Zeitlin <vadim@wxwindows.org>
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #include "wx/wxprec.h"
26 #if wxUSE_DATEPICKCTRL
29 #include "wx/msw/wrapwin.h"
30 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
33 #include "wx/dcclient.h"
34 #include "wx/msw/private.h"
37 #include "wx/datectrl.h"
39 #include "wx/msw/private/datecontrols.h"
41 #define _WX_DEFINE_DATE_EVENTS_
42 #include "wx/dateevt.h"
44 // apparently some versions of mingw define these macros erroneously
45 #ifndef DateTime_GetSystemtime
46 #define DateTime_GetSystemtime DateTime_GetSystemTime
49 #ifndef DateTime_SetSystemtime
50 #define DateTime_SetSystemtime DateTime_SetSystemTime
53 IMPLEMENT_DYNAMIC_CLASS(wxDatePickerCtrl
, wxControl
)
55 // ============================================================================
57 // ============================================================================
59 // ----------------------------------------------------------------------------
60 // wxDatePickerCtrl creation
61 // ----------------------------------------------------------------------------
64 wxDatePickerCtrl::Create(wxWindow
*parent
,
70 const wxValidator
& validator
,
73 if ( !wxMSWDateControls::CheckInitialization() )
76 // use wxDP_SPIN if wxDP_DEFAULT (0) was given as style
77 if ( !(style
& wxDP_DROPDOWN
) )
80 // initialize the base class
81 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
84 // create the native control
85 if ( !MSWCreateControl(DATETIMEPICK_CLASS
, wxEmptyString
, pos
, size
) )
88 if ( dt
.IsValid() || (style
& wxDP_ALLOWNONE
) )
91 SetValue(wxDateTime::Today());
96 WXDWORD
wxDatePickerCtrl::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
98 WXDWORD styleMSW
= wxDatePickerCtrlBase::MSWGetStyle(style
, exstyle
);
100 // although MSDN doesn't mention it, DTS_UPDOWN doesn't work with
102 if ( wxApp::GetComCtl32Version() > 472 && (style
& wxDP_SPIN
) )
103 styleMSW
|= DTS_UPDOWN
;
104 //else: drop down by default
106 #ifdef DTS_SHORTDATECENTURYFORMAT
107 if ( style
& wxDP_SHOWCENTURY
)
108 styleMSW
|= DTS_SHORTDATECENTURYFORMAT
;
110 #endif // DTS_SHORTDATECENTURYFORMAT
111 styleMSW
|= DTS_SHORTDATEFORMAT
;
113 if ( style
& wxDP_ALLOWNONE
)
114 styleMSW
|= DTS_SHOWNONE
;
119 // TODO: handle WM_WININICHANGE
121 // ----------------------------------------------------------------------------
122 // wxDatePickerCtrl geometry
123 // ----------------------------------------------------------------------------
125 wxSize
wxDatePickerCtrl::DoGetBestSize() const
127 wxClientDC
dc(wx_const_cast(wxDatePickerCtrl
*, this));
128 dc
.SetFont(GetFont());
130 // we can't use FormatDate() here as the CRT doesn't always use the same
131 // format as the date picker control
133 for ( int len
= 100; ; len
*= 2 )
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
149 const DWORD rc
= ::GetLastError();
150 if ( rc
!= ERROR_INSUFFICIENT_BUFFER
)
152 wxLogApiError(_T("GetDateFormat"), rc
);
154 // fall back on wxDateTime, what else to do?
155 s
= wxDateTime::Today().FormatDate();
160 // the control adds a lot of extra space around separators
161 s
.Replace(_T(","), _T(" , "));
164 dc
.GetTextExtent(s
, &x
, &y
);
166 wxSize
best(x
+ 40 /* margin + arrows */, EDIT_HEIGHT_FROM_CHAR_HEIGHT(y
));
171 // ----------------------------------------------------------------------------
172 // wxDatePickerCtrl operations
173 // ----------------------------------------------------------------------------
175 void wxDatePickerCtrl::SetValue(const wxDateTime
& dt
)
177 wxCHECK_RET( dt
.IsValid() || HasFlag(wxDP_ALLOWNONE
),
178 _T("this control requires a valid date") );
182 wxMSWDateControls::ToSystemTime(&st
, dt
);
183 if ( !DateTime_SetSystemtime(GetHwnd(),
184 dt
.IsValid() ? GDT_VALID
: GDT_NONE
,
187 wxLogDebug(_T("DateTime_SetSystemtime() failed"));
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)
193 if ( m_date
.IsValid() )
197 wxDateTime
wxDatePickerCtrl::GetValue() const
202 if ( DateTime_GetSystemtime(GetHwnd(), &st
) == GDT_VALID
)
204 wxMSWDateControls::FromSystemTime(&dt
, st
);
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__
215 void wxDatePickerCtrl::SetRange(const wxDateTime
& dt1
, const wxDateTime
& dt2
)
222 wxMSWDateControls::ToSystemTime(&st
[0], dt1
);
228 wxMSWDateControls::ToSystemTime(&st
[1], dt2
);
232 if ( !DateTime_SetRange(GetHwnd(), flags
, st
) )
234 wxLogDebug(_T("DateTime_SetRange() failed"));
238 bool wxDatePickerCtrl::GetRange(wxDateTime
*dt1
, wxDateTime
*dt2
) const
242 DWORD flags
= DateTime_GetRange(GetHwnd(), st
);
245 if ( flags
& GDTR_MIN
)
246 wxMSWDateControls::FromSystemTime(dt1
, st
[0]);
248 *dt1
= wxDefaultDateTime
;
253 if ( flags
& GDTR_MAX
)
254 wxMSWDateControls::FromSystemTime(dt2
, st
[1]);
256 *dt2
= wxDefaultDateTime
;
262 // ----------------------------------------------------------------------------
263 // wxDatePickerCtrl events
264 // ----------------------------------------------------------------------------
267 wxDatePickerCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
269 NMHDR
* hdr
= (NMHDR
*)lParam
;
272 case DTN_DATETIMECHANGE
:
274 NMDATETIMECHANGE
*dtch
= (NMDATETIMECHANGE
*)hdr
;
276 if ( dtch
->dwFlags
== GDT_VALID
)
277 wxMSWDateControls::FromSystemTime(&dt
, dtch
->st
);
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
) )
285 wxDateEvent
event(this, dt
, wxEVT_DATE_CHANGED
);
286 if ( HandleWindowEvent(event
) )
292 //else: both the old and new values are invalid, nothing changed
296 return wxDatePickerCtrlBase::MSWOnNotify(idCtrl
, lParam
, result
);
299 #endif // wxUSE_DATEPICKCTRL