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 // ============================================================================
45 // ============================================================================
47 // ----------------------------------------------------------------------------
48 // helpers for wxDateTime <-> SYSTEMTIME conversion
49 // ----------------------------------------------------------------------------
51 static inline void wxFromSystemTime(wxDateTime
*dt
, const SYSTEMTIME
& st
)
54 wx_static_cast(wxDateTime::Month
, wxDateTime::Jan
+ st
.wMonth
- 1),
59 static inline void wxToSystemTime(SYSTEMTIME
*st
, const wxDateTime
& dt
)
61 const wxDateTime::Tm
tm(dt
.GetTm());
64 st
->wMonth
= tm
.mon
- wxDateTime::Jan
+ 1;
71 st
->wMilliseconds
= 0;
74 // ----------------------------------------------------------------------------
75 // wxDatePickerCtrl creation
76 // ----------------------------------------------------------------------------
79 wxDatePickerCtrl::Create(wxWindow
*parent
,
85 const wxValidator
& validator
,
88 // although we already call InitCommonControls() in app.cpp which is
89 // supposed to initialize all common controls, in comctl32.dll 4.72 (and
90 // presumably earlier versions 4.70 and 4.71, date time picker not being
91 // supported in < 4.70 anyhow) it does not do it and we have to initialize
93 static bool s_initDone
= false; // MT-ok: used from GUI thread only
96 if ( wxTheApp
->GetComCtl32Version() < 470 )
98 wxLogError(_("This system doesn't support date picker control, please upgrade your version of comctl32.dll"));
103 INITCOMMONCONTROLSEX icex
;
104 icex
.dwSize
= sizeof(icex
);
105 icex
.dwICC
= ICC_DATE_CLASSES
;
107 wxDynamicLibrary
dllComCtl32(_T("comctl32.dll"), wxDL_VERBATIM
);
109 typedef BOOL (WINAPI
*ICCEx_t
)(INITCOMMONCONTROLSEX
*);
110 wxDYNLIB_FUNCTION( ICCEx_t
, InitCommonControlsEx
, dllComCtl32
);
112 if ( pfnInitCommonControlsEx
)
114 (*pfnInitCommonControlsEx
)(&icex
);
121 // use wxDP_SPIN if wxDP_DEFAULT (0) was given as style
122 if ( !(style
& wxDP_DROPDOWN
) )
125 // initialize the base class
126 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
129 // create the native control
130 if ( !MSWCreateControl(DATETIMEPICK_CLASS
, _T(""), pos
, size
) )
139 WXDWORD
wxDatePickerCtrl::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
141 WXDWORD styleMSW
= wxDatePickerCtrlBase::MSWGetStyle(style
, exstyle
);
143 // although MSDN doesn't mention it, DTS_UPDOWN doesn't work with
145 if ( wxTheApp
->GetComCtl32Version() > 472 && (style
& wxDP_SPIN
) )
146 styleMSW
|= DTS_UPDOWN
;
147 //else: drop down by default
149 #ifdef DTS_SHORTDATECENTURYFORMAT
150 if ( style
& wxDP_SHOWCENTURY
)
151 styleMSW
|= DTS_SHORTDATECENTURYFORMAT
;
153 #endif // DTS_SHORTDATECENTURYFORMAT
154 styleMSW
|= DTS_SHORTDATEFORMAT
;
159 // TODO: handle WM_WININICHANGE
161 // ----------------------------------------------------------------------------
162 // wxDatePickerCtrl geometry
163 // ----------------------------------------------------------------------------
165 wxSize
wxDatePickerCtrl::DoGetBestSize() const
167 const int y
= GetCharHeight();
169 return wxSize(DEFAULT_ITEM_WIDTH
, EDIT_HEIGHT_FROM_CHAR_HEIGHT(y
));
172 // ----------------------------------------------------------------------------
173 // wxDatePickerCtrl operations
174 // ----------------------------------------------------------------------------
176 void wxDatePickerCtrl::SetValue(const wxDateTime
& dt
)
178 // as we don't support DTS_SHOWNONE style so far, we don't allow setting
179 // the control to an invalid date, but this restriction may be lifted in
181 wxCHECK_RET( dt
.IsValid(), _T("invalid date") );
184 wxToSystemTime(&st
, dt
);
185 if ( !DateTime_SetSystemtime(GetHwnd(), GDT_VALID
, &st
) )
187 wxLogDebug(_T("DateTime_SetSystemtime() failed"));
191 wxDateTime
wxDatePickerCtrl::GetValue() const
195 if ( DateTime_GetSystemtime(GetHwnd(), &st
) == GDT_VALID
)
197 wxFromSystemTime(&dt
, st
);
203 void wxDatePickerCtrl::SetRange(const wxDateTime
& dt1
, const wxDateTime
& dt2
)
210 wxToSystemTime(&st
[0], dt1
);
216 wxToSystemTime(&st
[1], dt2
);
220 if ( !DateTime_SetRange(GetHwnd(), flags
, st
) )
222 wxLogDebug(_T("DateTime_SetRange() failed"));
226 bool wxDatePickerCtrl::GetRange(wxDateTime
*dt1
, wxDateTime
*dt2
) const
230 DWORD flags
= DateTime_GetRange(GetHwnd(), st
);
233 if ( flags
& GDTR_MIN
)
234 wxFromSystemTime(dt1
, st
[0]);
236 *dt1
= wxDefaultDateTime
;
241 if ( flags
& GDTR_MAX
)
242 wxFromSystemTime(dt2
, st
[1]);
244 *dt2
= wxDefaultDateTime
;
250 // ----------------------------------------------------------------------------
251 // wxDatePickerCtrl events
252 // ----------------------------------------------------------------------------
255 wxDatePickerCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
257 NMHDR
* hdr
= (NMHDR
*)lParam
;
260 case DTN_DATETIMECHANGE
:
261 NMDATETIMECHANGE
*dtch
= (NMDATETIMECHANGE
*)hdr
;
263 if ( dtch
->dwFlags
== GDT_VALID
)
264 wxFromSystemTime(&dt
, dtch
->st
);
266 wxDateEvent
event(this, dt
, wxEVT_DATE_CHANGED
);
267 if ( GetEventHandler()->ProcessEvent(event
) )
274 return wxDatePickerCtrlBase::MSWOnNotify(idCtrl
, lParam
, result
);
277 #endif // wxUSE_DATEPICKCTRL