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 #include "wx/dateevt.h"
43 // apparently some versions of mingw define these macros erroneously
44 #ifndef DateTime_GetSystemtime
45 #define DateTime_GetSystemtime DateTime_GetSystemTime
48 #ifndef DateTime_SetSystemtime
49 #define DateTime_SetSystemtime DateTime_SetSystemTime
52 IMPLEMENT_DYNAMIC_CLASS(wxDatePickerCtrl
, wxControl
)
54 // ============================================================================
56 // ============================================================================
58 // ----------------------------------------------------------------------------
59 // wxDatePickerCtrl creation
60 // ----------------------------------------------------------------------------
63 wxDatePickerCtrl::Create(wxWindow
*parent
,
69 const wxValidator
& validator
,
72 if ( !wxMSWDateControls::CheckInitialization() )
75 // use wxDP_SPIN if wxDP_DEFAULT (0) was given as style
76 if ( !(style
& wxDP_DROPDOWN
) )
79 // initialize the base class
80 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
83 // create the native control
84 if ( !MSWCreateControl(DATETIMEPICK_CLASS
, wxEmptyString
, pos
, size
) )
87 if ( dt
.IsValid() || (style
& wxDP_ALLOWNONE
) )
90 SetValue(wxDateTime::Today());
95 WXDWORD
wxDatePickerCtrl::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
97 WXDWORD styleMSW
= wxDatePickerCtrlBase::MSWGetStyle(style
, exstyle
);
99 // although MSDN doesn't mention it, DTS_UPDOWN doesn't work with
101 if ( wxApp::GetComCtl32Version() > 472 && (style
& wxDP_SPIN
) )
102 styleMSW
|= DTS_UPDOWN
;
103 //else: drop down by default
105 #ifdef DTS_SHORTDATECENTURYFORMAT
106 if ( style
& wxDP_SHOWCENTURY
)
107 styleMSW
|= DTS_SHORTDATECENTURYFORMAT
;
109 #endif // DTS_SHORTDATECENTURYFORMAT
110 styleMSW
|= DTS_SHORTDATEFORMAT
;
112 if ( style
& wxDP_ALLOWNONE
)
113 styleMSW
|= DTS_SHOWNONE
;
118 // TODO: handle WM_WININICHANGE
120 // ----------------------------------------------------------------------------
121 // wxDatePickerCtrl geometry
122 // ----------------------------------------------------------------------------
124 wxSize
wxDatePickerCtrl::DoGetBestSize() const
126 wxClientDC
dc(wx_const_cast(wxDatePickerCtrl
*, this));
127 dc
.SetFont(GetFont());
129 // we can't use FormatDate() here as the CRT doesn't always use the same
130 // format as the date picker control
132 for ( int len
= 100; ; len
*= 2 )
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
148 const DWORD rc
= ::GetLastError();
149 if ( rc
!= ERROR_INSUFFICIENT_BUFFER
)
151 wxLogApiError(_T("GetDateFormat"), rc
);
153 // fall back on wxDateTime, what else to do?
154 s
= wxDateTime::Today().FormatDate();
159 // the control adds a lot of extra space around separators
160 s
.Replace(_T(","), _T(" , "));
163 dc
.GetTextExtent(s
, &x
, &y
);
165 wxSize
best(x
+ 40 /* margin + arrows */, EDIT_HEIGHT_FROM_CHAR_HEIGHT(y
));
170 // ----------------------------------------------------------------------------
171 // wxDatePickerCtrl operations
172 // ----------------------------------------------------------------------------
174 void wxDatePickerCtrl::SetValue(const wxDateTime
& dt
)
176 wxCHECK_RET( dt
.IsValid() || HasFlag(wxDP_ALLOWNONE
),
177 _T("this control requires a valid date") );
181 dt
.GetAsMSWSysTime(&st
);
182 if ( !DateTime_SetSystemtime(GetHwnd(),
183 dt
.IsValid() ? GDT_VALID
: GDT_NONE
,
186 wxLogDebug(_T("DateTime_SetSystemtime() failed"));
189 // we need to keep only the date part, times don't make sense for this
190 // control (in particular, comparisons with other dates would fail)
192 if ( m_date
.IsValid() )
196 wxDateTime
wxDatePickerCtrl::GetValue() const
201 if ( DateTime_GetSystemtime(GetHwnd(), &st
) == GDT_VALID
)
203 dt
.SetFromMSWSysTime(st
);
206 wxASSERT_MSG( m_date
.IsValid() == dt
.IsValid() &&
207 (!dt
.IsValid() || dt
== m_date
),
208 _T("bug in wxDatePickerCtrl: m_date not in sync") );
209 #endif // __WXDEBUG__
214 void wxDatePickerCtrl::SetRange(const wxDateTime
& dt1
, const wxDateTime
& dt2
)
221 dt1
.GetAsMSWSysTime(st
+ 0);
227 dt2
.GetAsMSWSysTime(st
+ 1);
231 if ( !DateTime_SetRange(GetHwnd(), flags
, st
) )
233 wxLogDebug(_T("DateTime_SetRange() failed"));
237 bool wxDatePickerCtrl::GetRange(wxDateTime
*dt1
, wxDateTime
*dt2
) const
241 DWORD flags
= DateTime_GetRange(GetHwnd(), st
);
244 if ( flags
& GDTR_MIN
)
245 dt1
->SetFromMSWSysTime(st
[0]);
247 *dt1
= wxDefaultDateTime
;
252 if ( flags
& GDTR_MAX
)
253 dt2
->SetFromMSWSysTime(st
[1]);
255 *dt2
= wxDefaultDateTime
;
261 // ----------------------------------------------------------------------------
262 // wxDatePickerCtrl events
263 // ----------------------------------------------------------------------------
266 wxDatePickerCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
268 NMHDR
* hdr
= (NMHDR
*)lParam
;
271 case DTN_DATETIMECHANGE
:
273 NMDATETIMECHANGE
*dtch
= (NMDATETIMECHANGE
*)hdr
;
275 if ( dtch
->dwFlags
== GDT_VALID
)
276 dt
.SetFromMSWSysTime(dtch
->st
);
278 // filter out duplicate DTN_DATETIMECHANGE events which the native
279 // control sends us when using wxDP_DROPDOWN style
280 if ( (m_date
.IsValid() != dt
.IsValid()) ||
281 (m_date
.IsValid() && dt
!= m_date
) )
284 wxDateEvent
event(this, dt
, wxEVT_DATE_CHANGED
);
285 if ( HandleWindowEvent(event
) )
291 //else: both the old and new values are invalid, nothing changed
295 return wxDatePickerCtrlBase::MSWOnNotify(idCtrl
, lParam
, result
);
298 #endif // wxUSE_DATEPICKCTRL