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 styleMSW
|= DTS_SHORTDATEFORMAT
;
154 // TODO: handle WM_WININICHANGE
156 // ----------------------------------------------------------------------------
157 // wxDatePickerCtrl geometry
158 // ----------------------------------------------------------------------------
160 wxSize
wxDatePickerCtrl::DoGetBestSize() const
162 const int y
= GetCharHeight();
164 return wxSize(DEFAULT_ITEM_WIDTH
, EDIT_HEIGHT_FROM_CHAR_HEIGHT(y
));
167 // ----------------------------------------------------------------------------
168 // wxDatePickerCtrl operations
169 // ----------------------------------------------------------------------------
171 void wxDatePickerCtrl::SetValue(const wxDateTime
& dt
)
173 // as we don't support DTS_SHOWNONE style so far, we don't allow setting
174 // the control to an invalid date, but this restriction may be lifted in
176 wxCHECK_RET( dt
.IsValid(), _T("invalid date") );
179 wxToSystemTime(&st
, dt
);
180 if ( !DateTime_SetSystemtime(GetHwnd(), GDT_VALID
, &st
) )
182 wxLogDebug(_T("DateTime_SetSystemtime() failed"));
186 wxDateTime
wxDatePickerCtrl::GetValue() const
190 if ( DateTime_GetSystemtime(GetHwnd(), &st
) == GDT_VALID
)
192 wxFromSystemTime(&dt
, st
);
198 void wxDatePickerCtrl::SetRange(const wxDateTime
& dt1
, const wxDateTime
& dt2
)
205 wxToSystemTime(&st
[0], dt1
);
211 wxToSystemTime(&st
[1], dt2
);
215 if ( !DateTime_SetRange(GetHwnd(), flags
, st
) )
217 wxLogDebug(_T("DateTime_SetRange() failed"));
221 bool wxDatePickerCtrl::GetRange(wxDateTime
*dt1
, wxDateTime
*dt2
) const
225 DWORD flags
= DateTime_GetRange(GetHwnd(), st
);
228 if ( flags
& GDTR_MIN
)
229 wxFromSystemTime(dt1
, st
[0]);
231 *dt1
= wxDefaultDateTime
;
236 if ( flags
& GDTR_MAX
)
237 wxFromSystemTime(dt2
, st
[1]);
239 *dt2
= wxDefaultDateTime
;
245 // ----------------------------------------------------------------------------
246 // wxDatePickerCtrl events
247 // ----------------------------------------------------------------------------
250 wxDatePickerCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
252 NMHDR
* hdr
= (NMHDR
*)lParam
;
255 case DTN_DATETIMECHANGE
:
256 NMDATETIMECHANGE
*dtch
= (NMDATETIMECHANGE
*)hdr
;
258 if ( dtch
->dwFlags
== GDT_VALID
)
259 wxFromSystemTime(&dt
, dtch
->st
);
261 wxDateEvent
event(this, dt
, wxEVT_DATE_CHANGED
);
262 if ( GetEventHandler()->ProcessEvent(event
) )
269 return wxDatePickerCtrlBase::MSWOnNotify(idCtrl
, lParam
, result
);
272 #endif // wxUSE_DATEPICKCTRL