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