]> git.saurik.com Git - wxWidgets.git/blame - src/msw/datectrl.cpp
simplify code so it always returns the same object
[wxWidgets.git] / src / msw / datectrl.cpp
CommitLineData
feb72429 1/////////////////////////////////////////////////////////////////////////////
02e05e7e 2// Name: src/msw/datectrl.cpp
feb72429
VZ
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
02e05e7e
WS
26#if wxUSE_DATEPICKCTRL
27
feb72429 28#ifndef WX_PRECOMP
57bd4c60
WS
29 #include "wx/msw/wrapwin.h"
30 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
02e05e7e
WS
31 #include "wx/app.h"
32 #include "wx/intl.h"
33 #include "wx/dcclient.h"
8a18ea3f 34 #include "wx/settings.h"
02e05e7e 35 #include "wx/msw/private.h"
feb72429
VZ
36#endif
37
38#include "wx/datectrl.h"
feb72429
VZ
39#include "wx/dateevt.h"
40
a69e2a0a
VZ
41IMPLEMENT_DYNAMIC_CLASS(wxDatePickerCtrl, wxControl)
42
feb72429
VZ
43// ============================================================================
44// implementation
45// ============================================================================
46
feb72429
VZ
47// ----------------------------------------------------------------------------
48// wxDatePickerCtrl creation
49// ----------------------------------------------------------------------------
50
51bool
52wxDatePickerCtrl::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{
5385747e
VZ
61 // use wxDP_SPIN if wxDP_DEFAULT (0) was given as style
62 if ( !(style & wxDP_DROPDOWN) )
63 style |= wxDP_SPIN;
64
8957e55e
VZ
65 return MSWCreateDateTimePicker(parent, id, dt,
66 pos, size, style,
67 validator, name);
feb72429
VZ
68}
69
70WXDWORD wxDatePickerCtrl::MSWGetStyle(long style, WXDWORD *exstyle) const
71{
72 WXDWORD styleMSW = wxDatePickerCtrlBase::MSWGetStyle(style, exstyle);
73
0aa7cb54
VZ
74 // although MSDN doesn't mention it, DTS_UPDOWN doesn't work with
75 // comctl32.dll 4.72
2a1f999f 76 if ( wxApp::GetComCtl32Version() > 472 && (style & wxDP_SPIN) )
29c86948
VZ
77 styleMSW |= DTS_UPDOWN;
78 //else: drop down by default
79
2cfbeac8
VZ
80#ifdef DTS_SHORTDATECENTURYFORMAT
81 if ( style & wxDP_SHOWCENTURY )
82 styleMSW |= DTS_SHORTDATECENTURYFORMAT;
83 else
84#endif // DTS_SHORTDATECENTURYFORMAT
85 styleMSW |= DTS_SHORTDATEFORMAT;
feb72429 86
3200f37d
VZ
87 if ( style & wxDP_ALLOWNONE )
88 styleMSW |= DTS_SHOWNONE;
89
feb72429
VZ
90 return styleMSW;
91}
92
93// TODO: handle WM_WININICHANGE
94
8957e55e 95wxLocaleInfo wxDatePickerCtrl::MSWGetFormat() const
feb72429 96{
8957e55e 97 return wxLOCALE_SHORT_DATE_FMT;
feb72429
VZ
98}
99
100// ----------------------------------------------------------------------------
101// wxDatePickerCtrl operations
102// ----------------------------------------------------------------------------
103
104void wxDatePickerCtrl::SetValue(const wxDateTime& dt)
105{
3200f37d 106 if ( dt.IsValid() )
feb72429 107 {
ab2ef6e0
VZ
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.
5f899cbe
VZ
119 wxDateTime dtStart, dtEnd;
120 GetRange(&dtStart, &dtEnd);
ab2ef6e0
VZ
121 if ( (dtStart.IsValid() && dt < dtStart) ||
122 (dtEnd.IsValid() && dt > dtEnd) )
5f899cbe 123 {
ab2ef6e0
VZ
124 // Fail silently, some existing code relies on SetValue() with an
125 // out of range value simply doing nothing -- so don't.
126 return;
5f899cbe 127 }
ab2ef6e0
VZ
128 }
129
8957e55e 130 wxDateTimePickerCtrl::SetValue(dt);
e4164aa9 131
5541976c
VZ
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)
0bdd8074
VZ
134 if ( m_date.IsValid() )
135 m_date.ResetTime();
feb72429
VZ
136}
137
138wxDateTime wxDatePickerCtrl::GetValue() const
139{
4b6a582b 140#if wxDEBUG_LEVEL
feb72429
VZ
141 wxDateTime dt;
142 SYSTEMTIME st;
143 if ( DateTime_GetSystemtime(GetHwnd(), &st) == GDT_VALID )
144 {
edc0f733 145 dt.SetFromMSWSysDate(st);
feb72429
VZ
146 }
147
e4164aa9
VZ
148 wxASSERT_MSG( m_date.IsValid() == dt.IsValid() &&
149 (!dt.IsValid() || dt == m_date),
8957e55e 150 wxT("bug in wxDateTimePickerCtrl: m_date not in sync") );
4b6a582b 151#endif // wxDEBUG_LEVEL
e4164aa9 152
8957e55e 153 return wxDateTimePickerCtrl::GetValue();
feb72429
VZ
154}
155
156void wxDatePickerCtrl::SetRange(const wxDateTime& dt1, const wxDateTime& dt2)
157{
158 SYSTEMTIME st[2];
159
160 DWORD flags = 0;
161 if ( dt1.IsValid() )
162 {
154014d6 163 dt1.GetAsMSWSysTime(st + 0);
feb72429
VZ
164 flags |= GDTR_MIN;
165 }
166
167 if ( dt2.IsValid() )
168 {
154014d6 169 dt2.GetAsMSWSysTime(st + 1);
feb72429
VZ
170 flags |= GDTR_MAX;
171 }
172
173 if ( !DateTime_SetRange(GetHwnd(), flags, st) )
174 {
9a83f860 175 wxLogDebug(wxT("DateTime_SetRange() failed"));
feb72429
VZ
176 }
177}
178
179bool 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 )
edc0f733 187 dt1->SetFromMSWSysDate(st[0]);
feb72429
VZ
188 else
189 *dt1 = wxDefaultDateTime;
190 }
191
192 if ( dt2 )
193 {
194 if ( flags & GDTR_MAX )
edc0f733 195 dt2->SetFromMSWSysDate(st[1]);
feb72429
VZ
196 else
197 *dt2 = wxDefaultDateTime;
198 }
199
200 return flags != 0;
201}
202
203// ----------------------------------------------------------------------------
204// wxDatePickerCtrl events
205// ----------------------------------------------------------------------------
206
8957e55e 207bool wxDatePickerCtrl::MSWOnDateTimeChange(const NMDATETIMECHANGE& dtch)
feb72429 208{
8957e55e
VZ
209 wxDateTime dt;
210 if ( dtch.dwFlags == GDT_VALID )
211 dt.SetFromMSWSysDate(dtch.st);
feb72429 212
8957e55e
VZ
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);
feb72429
VZ
222}
223
9b877d18 224#endif // wxUSE_DATEPICKCTRL