1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/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"
26 #if wxUSE_DATEPICKCTRL
29 #include "wx/msw/wrapwin.h"
30 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
33 #include "wx/dcclient.h"
34 #include "wx/settings.h"
35 #include "wx/msw/private.h"
38 #include "wx/datectrl.h"
39 #include "wx/dateevt.h"
41 IMPLEMENT_DYNAMIC_CLASS(wxDatePickerCtrl
, wxControl
)
43 // ============================================================================
45 // ============================================================================
47 // ----------------------------------------------------------------------------
48 // wxDatePickerCtrl creation
49 // ----------------------------------------------------------------------------
52 wxDatePickerCtrl::Create(wxWindow
*parent
,
58 const wxValidator
& validator
,
61 // use wxDP_SPIN if wxDP_DEFAULT (0) was given as style
62 if ( !(style
& wxDP_DROPDOWN
) )
65 return MSWCreateDateTimePicker(parent
, id
, dt
,
70 WXDWORD
wxDatePickerCtrl::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
72 WXDWORD styleMSW
= wxDatePickerCtrlBase::MSWGetStyle(style
, exstyle
);
74 // although MSDN doesn't mention it, DTS_UPDOWN doesn't work with
76 if ( wxApp::GetComCtl32Version() > 472 && (style
& wxDP_SPIN
) )
77 styleMSW
|= DTS_UPDOWN
;
78 //else: drop down by default
80 #ifdef DTS_SHORTDATECENTURYFORMAT
81 if ( style
& wxDP_SHOWCENTURY
)
82 styleMSW
|= DTS_SHORTDATECENTURYFORMAT
;
84 #endif // DTS_SHORTDATECENTURYFORMAT
85 styleMSW
|= DTS_SHORTDATEFORMAT
;
87 if ( style
& wxDP_ALLOWNONE
)
88 styleMSW
|= DTS_SHOWNONE
;
93 // TODO: handle WM_WININICHANGE
95 wxLocaleInfo
wxDatePickerCtrl::MSWGetFormat() const
97 return wxLOCALE_SHORT_DATE_FMT
;
100 // ----------------------------------------------------------------------------
101 // wxDatePickerCtrl operations
102 // ----------------------------------------------------------------------------
104 void wxDatePickerCtrl::SetValue(const wxDateTime
& dt
)
108 // Don't try setting the date if it's out of range: calendar control
109 // under XP (and presumably all the other pre-Vista Windows versions)
110 // doesn't return false from DateTime_SetSystemtime() in this case but
111 // doesn't actually change the date, so we can't update our m_date
112 // unconditionally and would need to check whether it was changed
113 // before doing it. It looks simpler to just check whether it's in
114 // range here instead.
116 // If we ever drop support for XP we could rely on the return value of
117 // DateTime_SetSystemtime() but this probably won't happen in near
119 wxDateTime dtStart
, dtEnd
;
120 GetRange(&dtStart
, &dtEnd
);
121 if ( (dtStart
.IsValid() && dt
< dtStart
) ||
122 (dtEnd
.IsValid() && dt
> dtEnd
) )
124 // Fail silently, some existing code relies on SetValue() with an
125 // out of range value simply doing nothing -- so don't.
130 wxDateTimePickerCtrl::SetValue(dt
);
132 // we need to keep only the date part, times don't make sense for this
133 // control (in particular, comparisons with other dates would fail)
134 if ( m_date
.IsValid() )
138 wxDateTime
wxDatePickerCtrl::GetValue() const
143 if ( DateTime_GetSystemtime(GetHwnd(), &st
) == GDT_VALID
)
145 dt
.SetFromMSWSysDate(st
);
148 wxASSERT_MSG( m_date
.IsValid() == dt
.IsValid() &&
149 (!dt
.IsValid() || dt
== m_date
),
150 wxT("bug in wxDateTimePickerCtrl: m_date not in sync") );
151 #endif // wxDEBUG_LEVEL
153 return wxDateTimePickerCtrl::GetValue();
156 void wxDatePickerCtrl::SetRange(const wxDateTime
& dt1
, const wxDateTime
& dt2
)
163 dt1
.GetAsMSWSysTime(st
+ 0);
169 dt2
.GetAsMSWSysTime(st
+ 1);
173 if ( !DateTime_SetRange(GetHwnd(), flags
, st
) )
175 wxLogDebug(wxT("DateTime_SetRange() failed"));
179 bool wxDatePickerCtrl::GetRange(wxDateTime
*dt1
, wxDateTime
*dt2
) const
183 DWORD flags
= DateTime_GetRange(GetHwnd(), st
);
186 if ( flags
& GDTR_MIN
)
187 dt1
->SetFromMSWSysDate(st
[0]);
189 *dt1
= wxDefaultDateTime
;
194 if ( flags
& GDTR_MAX
)
195 dt2
->SetFromMSWSysDate(st
[1]);
197 *dt2
= wxDefaultDateTime
;
203 // ----------------------------------------------------------------------------
204 // wxDatePickerCtrl events
205 // ----------------------------------------------------------------------------
207 bool wxDatePickerCtrl::MSWOnDateTimeChange(const NMDATETIMECHANGE
& dtch
)
210 if ( dtch
.dwFlags
== GDT_VALID
)
211 dt
.SetFromMSWSysDate(dtch
.st
);
213 // filter out duplicate DTN_DATETIMECHANGE events which the native
214 // control sends us when using wxDP_DROPDOWN style
215 if ( (m_date
.IsValid() == dt
.IsValid()) &&
216 (!m_date
.IsValid() || dt
== m_date
) )
220 wxDateEvent
event(this, dt
, wxEVT_DATE_CHANGED
);
221 return HandleWindowEvent(event
);
224 #endif // wxUSE_DATEPICKCTRL