]> git.saurik.com Git - wxWidgets.git/blob - src/msw/datectrl.cpp
fix crash in wxWindowGTK::GTKHandleUnrealize(), closes #14752
[wxWidgets.git] / src / msw / datectrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/datectrl.cpp
3 // Purpose: wxDatePickerCtrl implementation
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 2005-01-09
7 // RCS-ID: $Id$
8 // Copyright: (c) 2005 Vadim Zeitlin <vadim@wxwindows.org>
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #if wxUSE_DATEPICKCTRL
27
28 #ifndef WX_PRECOMP
29 #include "wx/msw/wrapwin.h"
30 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
31 #include "wx/app.h"
32 #include "wx/intl.h"
33 #include "wx/dcclient.h"
34 #include "wx/settings.h"
35 #include "wx/msw/private.h"
36 #endif
37
38 #include "wx/datectrl.h"
39 #include "wx/dateevt.h"
40
41 IMPLEMENT_DYNAMIC_CLASS(wxDatePickerCtrl, wxControl)
42
43 // ============================================================================
44 // implementation
45 // ============================================================================
46
47 // ----------------------------------------------------------------------------
48 // wxDatePickerCtrl creation
49 // ----------------------------------------------------------------------------
50
51 bool
52 wxDatePickerCtrl::Create(wxWindow *parent,
53 wxWindowID id,
54 const wxDateTime& dt,
55 const wxPoint& pos,
56 const wxSize& size,
57 long style,
58 const wxValidator& validator,
59 const wxString& name)
60 {
61 // use wxDP_SPIN if wxDP_DEFAULT (0) was given as style
62 if ( !(style & wxDP_DROPDOWN) )
63 style |= wxDP_SPIN;
64
65 return MSWCreateDateTimePicker(parent, id, dt,
66 pos, size, style,
67 validator, name);
68 }
69
70 WXDWORD wxDatePickerCtrl::MSWGetStyle(long style, WXDWORD *exstyle) const
71 {
72 WXDWORD styleMSW = wxDatePickerCtrlBase::MSWGetStyle(style, exstyle);
73
74 // although MSDN doesn't mention it, DTS_UPDOWN doesn't work with
75 // comctl32.dll 4.72
76 if ( wxApp::GetComCtl32Version() > 472 && (style & wxDP_SPIN) )
77 styleMSW |= DTS_UPDOWN;
78 //else: drop down by default
79
80 #ifdef DTS_SHORTDATECENTURYFORMAT
81 if ( style & wxDP_SHOWCENTURY )
82 styleMSW |= DTS_SHORTDATECENTURYFORMAT;
83 else
84 #endif // DTS_SHORTDATECENTURYFORMAT
85 styleMSW |= DTS_SHORTDATEFORMAT;
86
87 if ( style & wxDP_ALLOWNONE )
88 styleMSW |= DTS_SHOWNONE;
89
90 return styleMSW;
91 }
92
93 // TODO: handle WM_WININICHANGE
94
95 wxLocaleInfo wxDatePickerCtrl::MSWGetFormat() const
96 {
97 return wxLOCALE_SHORT_DATE_FMT;
98 }
99
100 // ----------------------------------------------------------------------------
101 // wxDatePickerCtrl operations
102 // ----------------------------------------------------------------------------
103
104 void wxDatePickerCtrl::SetValue(const wxDateTime& dt)
105 {
106 if ( dt.IsValid() )
107 {
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.
115 //
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
118 // future.
119 wxDateTime dtStart, dtEnd;
120 GetRange(&dtStart, &dtEnd);
121 if ( (dtStart.IsValid() && dt < dtStart) ||
122 (dtEnd.IsValid() && dt > dtEnd) )
123 {
124 // Fail silently, some existing code relies on SetValue() with an
125 // out of range value simply doing nothing -- so don't.
126 return;
127 }
128 }
129
130 wxDateTimePickerCtrl::SetValue(dt);
131
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() )
135 m_date.ResetTime();
136 }
137
138 wxDateTime wxDatePickerCtrl::GetValue() const
139 {
140 #if wxDEBUG_LEVEL
141 wxDateTime dt;
142 SYSTEMTIME st;
143 if ( DateTime_GetSystemtime(GetHwnd(), &st) == GDT_VALID )
144 {
145 dt.SetFromMSWSysDate(st);
146 }
147
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
152
153 return wxDateTimePickerCtrl::GetValue();
154 }
155
156 void wxDatePickerCtrl::SetRange(const wxDateTime& dt1, const wxDateTime& dt2)
157 {
158 SYSTEMTIME st[2];
159
160 DWORD flags = 0;
161 if ( dt1.IsValid() )
162 {
163 dt1.GetAsMSWSysTime(st + 0);
164 flags |= GDTR_MIN;
165 }
166
167 if ( dt2.IsValid() )
168 {
169 dt2.GetAsMSWSysTime(st + 1);
170 flags |= GDTR_MAX;
171 }
172
173 if ( !DateTime_SetRange(GetHwnd(), flags, st) )
174 {
175 wxLogDebug(wxT("DateTime_SetRange() failed"));
176 }
177 }
178
179 bool wxDatePickerCtrl::GetRange(wxDateTime *dt1, wxDateTime *dt2) const
180 {
181 SYSTEMTIME st[2];
182
183 DWORD flags = DateTime_GetRange(GetHwnd(), st);
184 if ( dt1 )
185 {
186 if ( flags & GDTR_MIN )
187 dt1->SetFromMSWSysDate(st[0]);
188 else
189 *dt1 = wxDefaultDateTime;
190 }
191
192 if ( dt2 )
193 {
194 if ( flags & GDTR_MAX )
195 dt2->SetFromMSWSysDate(st[1]);
196 else
197 *dt2 = wxDefaultDateTime;
198 }
199
200 return flags != 0;
201 }
202
203 // ----------------------------------------------------------------------------
204 // wxDatePickerCtrl events
205 // ----------------------------------------------------------------------------
206
207 bool wxDatePickerCtrl::MSWOnDateTimeChange(const NMDATETIMECHANGE& dtch)
208 {
209 wxDateTime dt;
210 if ( dtch.dwFlags == GDT_VALID )
211 dt.SetFromMSWSysDate(dtch.st);
212
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) )
217 return false;
218
219 m_date = dt;
220 wxDateEvent event(this, dt, wxEVT_DATE_CHANGED);
221 return HandleWindowEvent(event);
222 }
223
224 #endif // wxUSE_DATEPICKCTRL