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
50 #endif // old gcc headers
52 // apparently some versions of mingw define these macros erroneously
53 #ifndef DateTime_GetSystemtime
54 #define DateTime_GetSystemtime DateTime_GetSystemTime
57 #ifndef DateTime_SetSystemtime
58 #define DateTime_SetSystemtime DateTime_SetSystemTime
61 // ============================================================================
63 // ============================================================================
65 // ----------------------------------------------------------------------------
66 // helpers for wxDateTime <-> SYSTEMTIME conversion
67 // ----------------------------------------------------------------------------
69 static inline void wxFromSystemTime(wxDateTime
*dt
, const SYSTEMTIME
& st
)
72 wx_static_cast(wxDateTime::Month
, wxDateTime::Jan
+ st
.wMonth
- 1),
77 static inline void wxToSystemTime(SYSTEMTIME
*st
, const wxDateTime
& dt
)
79 const wxDateTime::Tm
tm(dt
.GetTm());
81 st
->wYear
= (WXWORD
)tm
.year
;
82 st
->wMonth
= (WXWORD
)(tm
.mon
- wxDateTime::Jan
+ 1);
89 st
->wMilliseconds
= 0;
92 // ----------------------------------------------------------------------------
93 // wxDatePickerCtrl creation
94 // ----------------------------------------------------------------------------
97 wxDatePickerCtrl::Create(wxWindow
*parent
,
103 const wxValidator
& validator
,
104 const wxString
& name
)
106 // although we already call InitCommonControls() in app.cpp which is
107 // supposed to initialize all common controls, in comctl32.dll 4.72 (and
108 // presumably earlier versions 4.70 and 4.71, date time picker not being
109 // supported in < 4.70 anyhow) it does not do it and we have to initialize
111 static bool s_initDone
= false; // MT-ok: used from GUI thread only
114 if ( wxTheApp
->GetComCtl32Version() < 470 )
116 wxLogError(_("This system doesn't support date picker control, please upgrade your version of comctl32.dll"));
121 INITCOMMONCONTROLSEX icex
;
122 icex
.dwSize
= sizeof(icex
);
123 icex
.dwICC
= ICC_DATE_CLASSES
;
125 wxDynamicLibrary
dllComCtl32(_T("comctl32.dll"), wxDL_VERBATIM
);
127 typedef BOOL (WINAPI
*ICCEx_t
)(INITCOMMONCONTROLSEX
*);
128 wxDYNLIB_FUNCTION( ICCEx_t
, InitCommonControlsEx
, dllComCtl32
);
130 if ( pfnInitCommonControlsEx
)
132 (*pfnInitCommonControlsEx
)(&icex
);
139 // use wxDP_SPIN if wxDP_DEFAULT (0) was given as style
140 if ( !(style
& wxDP_DROPDOWN
) )
143 // initialize the base class
144 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
147 // create the native control
148 if ( !MSWCreateControl(DATETIMEPICK_CLASS
, wxEmptyString
, pos
, size
) )
157 WXDWORD
wxDatePickerCtrl::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
159 WXDWORD styleMSW
= wxDatePickerCtrlBase::MSWGetStyle(style
, exstyle
);
161 // although MSDN doesn't mention it, DTS_UPDOWN doesn't work with
163 if ( wxTheApp
->GetComCtl32Version() > 472 && (style
& wxDP_SPIN
) )
164 styleMSW
|= DTS_UPDOWN
;
165 //else: drop down by default
167 #ifdef DTS_SHORTDATECENTURYFORMAT
168 if ( style
& wxDP_SHOWCENTURY
)
169 styleMSW
|= DTS_SHORTDATECENTURYFORMAT
;
171 #endif // DTS_SHORTDATECENTURYFORMAT
172 styleMSW
|= DTS_SHORTDATEFORMAT
;
174 if ( style
& wxDP_ALLOWNONE
)
175 styleMSW
|= DTS_SHOWNONE
;
180 // TODO: handle WM_WININICHANGE
182 // ----------------------------------------------------------------------------
183 // wxDatePickerCtrl geometry
184 // ----------------------------------------------------------------------------
186 wxSize
wxDatePickerCtrl::DoGetBestSize() const
188 const int y
= GetCharHeight();
190 return wxSize(DEFAULT_ITEM_WIDTH
, EDIT_HEIGHT_FROM_CHAR_HEIGHT(y
));
193 // ----------------------------------------------------------------------------
194 // wxDatePickerCtrl operations
195 // ----------------------------------------------------------------------------
197 void wxDatePickerCtrl::SetValue(const wxDateTime
& dt
)
199 wxCHECK_RET( dt
.IsValid() || HasFlag(wxDP_ALLOWNONE
),
200 _T("this control requires a valid date") );
204 wxToSystemTime(&st
, dt
);
205 if ( !DateTime_SetSystemtime(GetHwnd(),
206 dt
.IsValid() ? GDT_VALID
: GDT_NONE
,
209 wxLogDebug(_T("DateTime_SetSystemtime() failed"));
213 wxDateTime
wxDatePickerCtrl::GetValue() const
217 if ( DateTime_GetSystemtime(GetHwnd(), &st
) == GDT_VALID
)
219 wxFromSystemTime(&dt
, st
);
225 void wxDatePickerCtrl::SetRange(const wxDateTime
& dt1
, const wxDateTime
& dt2
)
232 wxToSystemTime(&st
[0], dt1
);
238 wxToSystemTime(&st
[1], dt2
);
242 if ( !DateTime_SetRange(GetHwnd(), flags
, st
) )
244 wxLogDebug(_T("DateTime_SetRange() failed"));
248 bool wxDatePickerCtrl::GetRange(wxDateTime
*dt1
, wxDateTime
*dt2
) const
252 DWORD flags
= DateTime_GetRange(GetHwnd(), st
);
255 if ( flags
& GDTR_MIN
)
256 wxFromSystemTime(dt1
, st
[0]);
258 *dt1
= wxDefaultDateTime
;
263 if ( flags
& GDTR_MAX
)
264 wxFromSystemTime(dt2
, st
[1]);
266 *dt2
= wxDefaultDateTime
;
272 // ----------------------------------------------------------------------------
273 // wxDatePickerCtrl events
274 // ----------------------------------------------------------------------------
277 wxDatePickerCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
279 NMHDR
* hdr
= (NMHDR
*)lParam
;
282 case DTN_DATETIMECHANGE
:
283 NMDATETIMECHANGE
*dtch
= (NMDATETIMECHANGE
*)hdr
;
285 if ( dtch
->dwFlags
== GDT_VALID
)
286 wxFromSystemTime(&dt
, dtch
->st
);
288 wxDateEvent
event(this, dt
, wxEVT_DATE_CHANGED
);
289 if ( GetEventHandler()->ProcessEvent(event
) )
296 return wxDatePickerCtrlBase::MSWOnNotify(idCtrl
, lParam
, result
);
299 #endif // wxUSE_DATEPICKCTRL