]> git.saurik.com Git - wxWidgets.git/blob - src/msw/datectrl.cpp
Make mouse capture checking asserts stronger and more detailed.
[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 // Copyright: (c) 2005 Vadim Zeitlin <vadim@wxwindows.org>
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 #include "wx/wxprec.h"
20
21 #ifdef __BORLANDC__
22 #pragma hdrstop
23 #endif
24
25 #if wxUSE_DATEPICKCTRL
26
27 #ifndef WX_PRECOMP
28 #include "wx/msw/wrapwin.h"
29 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
30 #include "wx/app.h"
31 #include "wx/intl.h"
32 #include "wx/dcclient.h"
33 #include "wx/settings.h"
34 #include "wx/msw/private.h"
35 #endif
36
37 #include "wx/datectrl.h"
38 #include "wx/dateevt.h"
39
40 IMPLEMENT_DYNAMIC_CLASS(wxDatePickerCtrl, wxControl)
41
42 // ============================================================================
43 // implementation
44 // ============================================================================
45
46 // ----------------------------------------------------------------------------
47 // wxDatePickerCtrl creation
48 // ----------------------------------------------------------------------------
49
50 bool
51 wxDatePickerCtrl::Create(wxWindow *parent,
52 wxWindowID id,
53 const wxDateTime& dt,
54 const wxPoint& pos,
55 const wxSize& size,
56 long style,
57 const wxValidator& validator,
58 const wxString& name)
59 {
60 // use wxDP_SPIN if wxDP_DEFAULT (0) was given as style
61 if ( !(style & wxDP_DROPDOWN) )
62 style |= wxDP_SPIN;
63
64 return MSWCreateDateTimePicker(parent, id, dt,
65 pos, size, style,
66 validator, name);
67 }
68
69 WXDWORD wxDatePickerCtrl::MSWGetStyle(long style, WXDWORD *exstyle) const
70 {
71 WXDWORD styleMSW = wxDatePickerCtrlBase::MSWGetStyle(style, exstyle);
72
73 // although MSDN doesn't mention it, DTS_UPDOWN doesn't work with
74 // comctl32.dll 4.72
75 if ( wxApp::GetComCtl32Version() > 472 && (style & wxDP_SPIN) )
76 styleMSW |= DTS_UPDOWN;
77 //else: drop down by default
78
79 #ifdef DTS_SHORTDATECENTURYFORMAT
80 if ( style & wxDP_SHOWCENTURY )
81 styleMSW |= DTS_SHORTDATECENTURYFORMAT;
82 else
83 #endif // DTS_SHORTDATECENTURYFORMAT
84 styleMSW |= DTS_SHORTDATEFORMAT;
85
86 if ( style & wxDP_ALLOWNONE )
87 styleMSW |= DTS_SHOWNONE;
88
89 return styleMSW;
90 }
91
92 // TODO: handle WM_WININICHANGE
93
94 wxLocaleInfo wxDatePickerCtrl::MSWGetFormat() const
95 {
96 return wxLOCALE_SHORT_DATE_FMT;
97 }
98
99 // ----------------------------------------------------------------------------
100 // wxDatePickerCtrl operations
101 // ----------------------------------------------------------------------------
102
103 void wxDatePickerCtrl::SetValue(const wxDateTime& dt)
104 {
105 if ( dt.IsValid() )
106 {
107 // Don't try setting the date if it's out of range: calendar control
108 // under XP (and presumably all the other pre-Vista Windows versions)
109 // doesn't return false from DateTime_SetSystemtime() in this case but
110 // doesn't actually change the date, so we can't update our m_date
111 // unconditionally and would need to check whether it was changed
112 // before doing it. It looks simpler to just check whether it's in
113 // range here instead.
114 //
115 // If we ever drop support for XP we could rely on the return value of
116 // DateTime_SetSystemtime() but this probably won't happen in near
117 // future.
118 wxDateTime dtStart, dtEnd;
119 GetRange(&dtStart, &dtEnd);
120 if ( (dtStart.IsValid() && dt < dtStart) ||
121 (dtEnd.IsValid() && dt > dtEnd) )
122 {
123 // Fail silently, some existing code relies on SetValue() with an
124 // out of range value simply doing nothing -- so don't.
125 return;
126 }
127 }
128
129 wxDateTimePickerCtrl::SetValue(dt);
130
131 // we need to keep only the date part, times don't make sense for this
132 // control (in particular, comparisons with other dates would fail)
133 if ( m_date.IsValid() )
134 m_date.ResetTime();
135 }
136
137 wxDateTime wxDatePickerCtrl::GetValue() const
138 {
139 #if wxDEBUG_LEVEL
140 wxDateTime dt;
141 SYSTEMTIME st;
142 if ( DateTime_GetSystemtime(GetHwnd(), &st) == GDT_VALID )
143 {
144 dt.SetFromMSWSysDate(st);
145 }
146
147 wxASSERT_MSG( m_date.IsValid() == dt.IsValid() &&
148 (!dt.IsValid() || dt == m_date),
149 wxT("bug in wxDateTimePickerCtrl: m_date not in sync") );
150 #endif // wxDEBUG_LEVEL
151
152 return wxDateTimePickerCtrl::GetValue();
153 }
154
155 void wxDatePickerCtrl::SetRange(const wxDateTime& dt1, const wxDateTime& dt2)
156 {
157 SYSTEMTIME st[2];
158
159 DWORD flags = 0;
160 if ( dt1.IsValid() )
161 {
162 dt1.GetAsMSWSysTime(st + 0);
163 flags |= GDTR_MIN;
164 }
165
166 if ( dt2.IsValid() )
167 {
168 dt2.GetAsMSWSysTime(st + 1);
169 flags |= GDTR_MAX;
170 }
171
172 if ( !DateTime_SetRange(GetHwnd(), flags, st) )
173 {
174 wxLogDebug(wxT("DateTime_SetRange() failed"));
175 }
176 }
177
178 bool wxDatePickerCtrl::GetRange(wxDateTime *dt1, wxDateTime *dt2) const
179 {
180 SYSTEMTIME st[2];
181
182 DWORD flags = DateTime_GetRange(GetHwnd(), st);
183 if ( dt1 )
184 {
185 if ( flags & GDTR_MIN )
186 dt1->SetFromMSWSysDate(st[0]);
187 else
188 *dt1 = wxDefaultDateTime;
189 }
190
191 if ( dt2 )
192 {
193 if ( flags & GDTR_MAX )
194 dt2->SetFromMSWSysDate(st[1]);
195 else
196 *dt2 = wxDefaultDateTime;
197 }
198
199 return flags != 0;
200 }
201
202 // ----------------------------------------------------------------------------
203 // wxDatePickerCtrl events
204 // ----------------------------------------------------------------------------
205
206 bool wxDatePickerCtrl::MSWOnDateTimeChange(const NMDATETIMECHANGE& dtch)
207 {
208 wxDateTime dt;
209 if ( dtch.dwFlags == GDT_VALID )
210 dt.SetFromMSWSysDate(dtch.st);
211
212 // filter out duplicate DTN_DATETIMECHANGE events which the native
213 // control sends us when using wxDP_DROPDOWN style
214 if ( (m_date.IsValid() == dt.IsValid()) &&
215 (!m_date.IsValid() || dt == m_date) )
216 return false;
217
218 m_date = dt;
219 wxDateEvent event(this, dt, wxEVT_DATE_CHANGED);
220 return HandleWindowEvent(event);
221 }
222
223 #endif // wxUSE_DATEPICKCTRL