1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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"
29 #if wxUSE_DATEPICKCTRL
31 #include "wx/datectrl.h"
34 #include "wx/dynlib.h"
36 #define _WX_DEFINE_DATE_EVENTS_
37 #include "wx/dateevt.h"
39 #include "wx/msw/wrapwin.h"
40 #include "wx/msw/wrapcctl.h"
41 #include "wx/msw/private.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 // helpers for wxDateTime <-> SYSTEMTIME conversion
60 // ----------------------------------------------------------------------------
62 static inline void wxFromSystemTime(wxDateTime
*dt
, const SYSTEMTIME
& st
)
65 wx_static_cast(wxDateTime::Month
, wxDateTime::Jan
+ st
.wMonth
- 1),
70 static inline void wxToSystemTime(SYSTEMTIME
*st
, const wxDateTime
& dt
)
72 const wxDateTime::Tm
tm(dt
.GetTm());
74 st
->wYear
= (WXWORD
)tm
.year
;
75 st
->wMonth
= (WXWORD
)(tm
.mon
- wxDateTime::Jan
+ 1);
82 st
->wMilliseconds
= 0;
85 // ----------------------------------------------------------------------------
86 // wxDatePickerCtrl creation
87 // ----------------------------------------------------------------------------
90 wxDatePickerCtrl::Create(wxWindow
*parent
,
96 const wxValidator
& validator
,
99 // although we already call InitCommonControls() in app.cpp which is
100 // supposed to initialize all common controls, in comctl32.dll 4.72 (and
101 // presumably earlier versions 4.70 and 4.71, date time picker not being
102 // supported in < 4.70 anyhow) it does not do it and we have to initialize
104 static bool s_initDone
= false; // MT-ok: used from GUI thread only
107 if ( wxApp::GetComCtl32Version() < 470 )
109 wxLogError(_("This system doesn't support date picker control, please upgrade your version of comctl32.dll"));
114 INITCOMMONCONTROLSEX icex
;
115 icex
.dwSize
= sizeof(icex
);
116 icex
.dwICC
= ICC_DATE_CLASSES
;
118 wxDynamicLibrary
dllComCtl32(_T("comctl32.dll"), wxDL_VERBATIM
);
120 typedef BOOL (WINAPI
*ICCEx_t
)(INITCOMMONCONTROLSEX
*);
121 wxDYNLIB_FUNCTION( ICCEx_t
, InitCommonControlsEx
, dllComCtl32
);
123 if ( pfnInitCommonControlsEx
)
125 (*pfnInitCommonControlsEx
)(&icex
);
132 // use wxDP_SPIN if wxDP_DEFAULT (0) was given as style
133 if ( !(style
& wxDP_DROPDOWN
) )
136 // initialize the base class
137 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
140 // create the native control
141 if ( !MSWCreateControl(DATETIMEPICK_CLASS
, wxEmptyString
, pos
, size
) )
144 if ( dt
.IsValid() || (style
& wxDP_ALLOWNONE
) )
150 WXDWORD
wxDatePickerCtrl::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
152 WXDWORD styleMSW
= wxDatePickerCtrlBase::MSWGetStyle(style
, exstyle
);
154 // although MSDN doesn't mention it, DTS_UPDOWN doesn't work with
156 if ( wxApp::GetComCtl32Version() > 472 && (style
& wxDP_SPIN
) )
157 styleMSW
|= DTS_UPDOWN
;
158 //else: drop down by default
160 #ifdef DTS_SHORTDATECENTURYFORMAT
161 if ( style
& wxDP_SHOWCENTURY
)
162 styleMSW
|= DTS_SHORTDATECENTURYFORMAT
;
164 #endif // DTS_SHORTDATECENTURYFORMAT
165 styleMSW
|= DTS_SHORTDATEFORMAT
;
167 if ( style
& wxDP_ALLOWNONE
)
168 styleMSW
|= DTS_SHOWNONE
;
173 // TODO: handle WM_WININICHANGE
175 // ----------------------------------------------------------------------------
176 // wxDatePickerCtrl geometry
177 // ----------------------------------------------------------------------------
179 wxSize
wxDatePickerCtrl::DoGetBestSize() const
181 const int y
= GetCharHeight();
183 wxSize
best(DEFAULT_ITEM_WIDTH
, EDIT_HEIGHT_FROM_CHAR_HEIGHT(y
));
188 // ----------------------------------------------------------------------------
189 // wxDatePickerCtrl operations
190 // ----------------------------------------------------------------------------
192 void wxDatePickerCtrl::SetValue(const wxDateTime
& dt
)
194 wxCHECK_RET( dt
.IsValid() || HasFlag(wxDP_ALLOWNONE
),
195 _T("this control requires a valid date") );
199 wxToSystemTime(&st
, dt
);
200 if ( !DateTime_SetSystemtime(GetHwnd(),
201 dt
.IsValid() ? GDT_VALID
: GDT_NONE
,
204 wxLogDebug(_T("DateTime_SetSystemtime() failed"));
208 wxDateTime
wxDatePickerCtrl::GetValue() const
212 if ( DateTime_GetSystemtime(GetHwnd(), &st
) == GDT_VALID
)
214 wxFromSystemTime(&dt
, st
);
220 void wxDatePickerCtrl::SetRange(const wxDateTime
& dt1
, const wxDateTime
& dt2
)
227 wxToSystemTime(&st
[0], dt1
);
233 wxToSystemTime(&st
[1], dt2
);
237 if ( !DateTime_SetRange(GetHwnd(), flags
, st
) )
239 wxLogDebug(_T("DateTime_SetRange() failed"));
243 bool wxDatePickerCtrl::GetRange(wxDateTime
*dt1
, wxDateTime
*dt2
) const
247 DWORD flags
= DateTime_GetRange(GetHwnd(), st
);
250 if ( flags
& GDTR_MIN
)
251 wxFromSystemTime(dt1
, st
[0]);
253 *dt1
= wxDefaultDateTime
;
258 if ( flags
& GDTR_MAX
)
259 wxFromSystemTime(dt2
, st
[1]);
261 *dt2
= wxDefaultDateTime
;
267 // ----------------------------------------------------------------------------
268 // wxDatePickerCtrl events
269 // ----------------------------------------------------------------------------
272 wxDatePickerCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
274 NMHDR
* hdr
= (NMHDR
*)lParam
;
277 case DTN_DATETIMECHANGE
:
278 NMDATETIMECHANGE
*dtch
= (NMDATETIMECHANGE
*)hdr
;
280 if ( dtch
->dwFlags
== GDT_VALID
)
281 wxFromSystemTime(&dt
, dtch
->st
);
283 wxDateEvent
event(this, dt
, wxEVT_DATE_CHANGED
);
284 if ( GetEventHandler()->ProcessEvent(event
) )
291 return wxDatePickerCtrlBase::MSWOnNotify(idCtrl
, lParam
, result
);
294 #endif // wxUSE_DATEPICKCTRL