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 #if defined(__GNUWIN32__) && ! wxCHECK_W32API_VERSION( 2, 4 )
44 typedef struct tagNMDATETIMECHANGE
49 } NMDATETIMECHANGE
, FAR
* LPNMDATETIMECHANGE
;
52 // ============================================================================
54 // ============================================================================
56 // ----------------------------------------------------------------------------
57 // helpers for wxDateTime <-> SYSTEMTIME conversion
58 // ----------------------------------------------------------------------------
60 static inline void wxFromSystemTime(wxDateTime
*dt
, const SYSTEMTIME
& st
)
63 wx_static_cast(wxDateTime::Month
, wxDateTime::Jan
+ st
.wMonth
- 1),
68 static inline void wxToSystemTime(SYSTEMTIME
*st
, const wxDateTime
& dt
)
70 const wxDateTime::Tm
tm(dt
.GetTm());
72 st
->wYear
= (WXWORD
)tm
.year
;
73 st
->wMonth
= (WXWORD
)(tm
.mon
- wxDateTime::Jan
+ 1);
80 st
->wMilliseconds
= 0;
83 // ----------------------------------------------------------------------------
84 // wxDatePickerCtrl creation
85 // ----------------------------------------------------------------------------
88 wxDatePickerCtrl::Create(wxWindow
*parent
,
94 const wxValidator
& validator
,
97 // although we already call InitCommonControls() in app.cpp which is
98 // supposed to initialize all common controls, in comctl32.dll 4.72 (and
99 // presumably earlier versions 4.70 and 4.71, date time picker not being
100 // supported in < 4.70 anyhow) it does not do it and we have to initialize
102 static bool s_initDone
= false; // MT-ok: used from GUI thread only
105 if ( wxTheApp
->GetComCtl32Version() < 470 )
107 wxLogError(_("This system doesn't support date picker control, please upgrade your version of comctl32.dll"));
112 INITCOMMONCONTROLSEX icex
;
113 icex
.dwSize
= sizeof(icex
);
114 icex
.dwICC
= ICC_DATE_CLASSES
;
116 wxDynamicLibrary
dllComCtl32(_T("comctl32.dll"), wxDL_VERBATIM
);
118 typedef BOOL (WINAPI
*ICCEx_t
)(INITCOMMONCONTROLSEX
*);
119 wxDYNLIB_FUNCTION( ICCEx_t
, InitCommonControlsEx
, dllComCtl32
);
121 if ( pfnInitCommonControlsEx
)
123 (*pfnInitCommonControlsEx
)(&icex
);
130 // use wxDP_SPIN if wxDP_DEFAULT (0) was given as style
131 if ( !(style
& wxDP_DROPDOWN
) )
134 // initialize the base class
135 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
138 // create the native control
139 if ( !MSWCreateControl(DATETIMEPICK_CLASS
, wxEmptyString
, pos
, size
) )
148 WXDWORD
wxDatePickerCtrl::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
150 WXDWORD styleMSW
= wxDatePickerCtrlBase::MSWGetStyle(style
, exstyle
);
152 // although MSDN doesn't mention it, DTS_UPDOWN doesn't work with
154 if ( wxTheApp
->GetComCtl32Version() > 472 && (style
& wxDP_SPIN
) )
155 styleMSW
|= DTS_UPDOWN
;
156 //else: drop down by default
158 #ifdef DTS_SHORTDATECENTURYFORMAT
159 if ( style
& wxDP_SHOWCENTURY
)
160 styleMSW
|= DTS_SHORTDATECENTURYFORMAT
;
162 #endif // DTS_SHORTDATECENTURYFORMAT
163 styleMSW
|= DTS_SHORTDATEFORMAT
;
168 // TODO: handle WM_WININICHANGE
170 // ----------------------------------------------------------------------------
171 // wxDatePickerCtrl geometry
172 // ----------------------------------------------------------------------------
174 wxSize
wxDatePickerCtrl::DoGetBestSize() const
176 const int y
= GetCharHeight();
178 return wxSize(DEFAULT_ITEM_WIDTH
, EDIT_HEIGHT_FROM_CHAR_HEIGHT(y
));
181 // ----------------------------------------------------------------------------
182 // wxDatePickerCtrl operations
183 // ----------------------------------------------------------------------------
185 #ifndef DateTime_GetSystemtime
186 #define DateTime_GetSystemtime DateTime_GetSystemTime
189 #ifndef DateTime_SetSystemtime
190 #define DateTime_SetSystemtime DateTime_SetSystemTime
193 void wxDatePickerCtrl::SetValue(const wxDateTime
& dt
)
195 // as we don't support DTS_SHOWNONE style so far, we don't allow setting
196 // the control to an invalid date, but this restriction may be lifted in
198 wxCHECK_RET( dt
.IsValid(), _T("invalid date") );
201 wxToSystemTime(&st
, dt
);
202 if ( !DateTime_SetSystemtime(GetHwnd(), GDT_VALID
, &st
) )
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