]>
Commit | Line | Data |
---|---|---|
39df3acd | 1 | ///////////////////////////////////////////////////////////////////////////// |
68f2155b | 2 | // Name: src/generic/datectlg.cpp |
7ae712f5 | 3 | // Purpose: generic wxDatePickerCtrlGeneric implementation |
39df3acd VZ |
4 | // Author: Andreas Pflug |
5 | // Modified by: | |
6 | // Created: 2005-01-19 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 2005 Andreas Pflug <pgadmin@pse-consulting.de> | |
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 | ||
7ae712f5 VZ |
26 | #if wxUSE_DATEPICKCTRL |
27 | ||
a962d4e0 | 28 | #ifndef WX_PRECOMP |
a962d4e0 KH |
29 | #include "wx/dialog.h" |
30 | #include "wx/dcmemory.h" | |
4d4c704c | 31 | #include "wx/intl.h" |
a962d4e0 KH |
32 | #include "wx/panel.h" |
33 | #include "wx/textctrl.h" | |
34 | #include "wx/valtext.h" | |
35 | #endif | |
36 | ||
4a40cd9b VZ |
37 | #include "wx/calctrl.h" |
38 | #include "wx/combo.h" | |
91edf16c | 39 | |
4a40cd9b | 40 | #include "wx/datectrl.h" |
df16cb09 | 41 | #include "wx/generic/datectrl.h" |
39df3acd VZ |
42 | |
43 | // ---------------------------------------------------------------------------- | |
44 | // constants | |
45 | // ---------------------------------------------------------------------------- | |
46 | ||
930fb29e | 47 | |
aa74ad5b VZ |
48 | // ---------------------------------------------------------------------------- |
49 | // global variables | |
50 | // ---------------------------------------------------------------------------- | |
51 | ||
930fb29e | 52 | |
1721a8c0 VZ |
53 | // ---------------------------------------------------------------------------- |
54 | // local classes | |
55 | // ---------------------------------------------------------------------------- | |
56 | ||
c245a012 | 57 | class wxCalendarComboPopup : public wxCalendarCtrl, |
85fa9d60 | 58 | public wxComboPopup |
38511687 VZ |
59 | { |
60 | public: | |
38511687 | 61 | |
c245a012 | 62 | wxCalendarComboPopup() : wxCalendarCtrl(), |
85fa9d60 | 63 | wxComboPopup() |
caad7637 | 64 | { |
caad7637 JS |
65 | } |
66 | ||
85fa9d60 VZ |
67 | virtual void Init() |
68 | { | |
69 | } | |
caad7637 | 70 | |
85fa9d60 VZ |
71 | // NB: Don't create lazily since it didn't work that way before |
72 | // wxComboCtrl was used, and changing behaviour would almost | |
73 | // certainly introduce new bugs. | |
74 | virtual bool Create(wxWindow* parent) | |
75 | { | |
c245a012 | 76 | if ( !wxCalendarCtrl::Create(parent, wxID_ANY, wxDefaultDateTime, |
85fa9d60 | 77 | wxPoint(0, 0), wxDefaultSize, |
6c70323f | 78 | wxCAL_SEQUENTIAL_MONTH_SELECTION |
c245a012 | 79 | | wxCAL_SHOW_HOLIDAYS | wxBORDER_SUNKEN) ) |
85fa9d60 | 80 | return false; |
caad7637 | 81 | |
6c70323f | 82 | SetFormat(GetLocaleDateFormat()); |
caad7637 | 83 | |
c245a012 | 84 | m_useSize = wxCalendarCtrl::GetBestSize(); |
caad7637 | 85 | |
85fa9d60 VZ |
86 | wxWindow* tx = m_combo->GetTextCtrl(); |
87 | if ( !tx ) | |
88 | tx = m_combo; | |
caad7637 | 89 | |
85fa9d60 VZ |
90 | tx->Connect(wxEVT_KILL_FOCUS, |
91 | wxFocusEventHandler(wxCalendarComboPopup::OnKillTextFocus), | |
92 | NULL, this); | |
caad7637 | 93 | |
85fa9d60 VZ |
94 | return true; |
95 | } | |
38511687 | 96 | |
85fa9d60 VZ |
97 | virtual wxSize GetAdjustedSize(int WXUNUSED(minWidth), |
98 | int WXUNUSED(prefHeight), | |
99 | int WXUNUSED(maxHeight)) | |
100 | { | |
101 | return m_useSize; | |
102 | } | |
38511687 | 103 | |
85fa9d60 | 104 | virtual wxWindow *GetControl() { return this; } |
930fb29e | 105 | |
85fa9d60 VZ |
106 | void SetDateValue(const wxDateTime& date) |
107 | { | |
108 | if ( date.IsValid() ) | |
109 | { | |
110 | m_combo->SetText(date.Format(m_format)); | |
d6781628 | 111 | SetDate(date); |
85fa9d60 VZ |
112 | } |
113 | else // invalid date | |
114 | { | |
115 | wxASSERT_MSG( HasDPFlag(wxDP_ALLOWNONE), | |
9a83f860 | 116 | wxT("this control must have a valid date") ); |
caad7637 | 117 | |
85fa9d60 VZ |
118 | m_combo->SetText(wxEmptyString); |
119 | } | |
85fa9d60 | 120 | } |
94ab4d92 | 121 | |
2ccc6650 VZ |
122 | bool IsTextEmpty() const |
123 | { | |
124 | return m_combo->GetTextCtrl()->IsEmpty(); | |
125 | } | |
126 | ||
85fa9d60 VZ |
127 | bool ParseDateTime(const wxString& s, wxDateTime* pDt) |
128 | { | |
129 | wxASSERT(pDt); | |
38511687 | 130 | |
85fa9d60 VZ |
131 | if ( !s.empty() ) |
132 | { | |
e0a050e3 | 133 | pDt->ParseFormat(s.c_str(), m_format); |
85fa9d60 VZ |
134 | if ( !pDt->IsValid() ) |
135 | return false; | |
136 | } | |
38511687 | 137 | |
85fa9d60 VZ |
138 | return true; |
139 | } | |
38511687 | 140 | |
85fa9d60 VZ |
141 | void SendDateEvent(const wxDateTime& dt) |
142 | { | |
85fa9d60 VZ |
143 | // Sends both wxCalendarEvent and wxDateEvent |
144 | wxWindow* datePicker = m_combo->GetParent(); | |
38511687 | 145 | |
628e155d | 146 | wxCalendarEvent cev(datePicker, dt, wxEVT_CALENDAR_SEL_CHANGED); |
d1b736b7 | 147 | datePicker->GetEventHandler()->ProcessEvent(cev); |
38511687 | 148 | |
85fa9d60 | 149 | wxDateEvent event(datePicker, dt, wxEVT_DATE_CHANGED); |
d1b736b7 | 150 | datePicker->GetEventHandler()->ProcessEvent(event); |
85fa9d60 | 151 | } |
caad7637 | 152 | |
85fa9d60 | 153 | private: |
caad7637 | 154 | |
85fa9d60 | 155 | void OnCalKey(wxKeyEvent & ev) |
caad7637 | 156 | { |
85fa9d60 VZ |
157 | if (ev.GetKeyCode() == WXK_ESCAPE && !ev.HasModifiers()) |
158 | Dismiss(); | |
159 | else | |
160 | ev.Skip(); | |
caad7637 | 161 | } |
caad7637 | 162 | |
85fa9d60 VZ |
163 | void OnSelChange(wxCalendarEvent &ev) |
164 | { | |
2fda1fe5 | 165 | m_combo->SetText(GetDate().Format(m_format)); |
caad7637 | 166 | |
85fa9d60 VZ |
167 | if ( ev.GetEventType() == wxEVT_CALENDAR_DOUBLECLICKED ) |
168 | { | |
169 | Dismiss(); | |
170 | } | |
caad7637 | 171 | |
2fda1fe5 | 172 | SendDateEvent(GetDate()); |
38511687 VZ |
173 | } |
174 | ||
85fa9d60 VZ |
175 | void OnKillTextFocus(wxFocusEvent &ev) |
176 | { | |
177 | ev.Skip(); | |
caad7637 | 178 | |
2fda1fe5 VZ |
179 | const wxDateTime& dtOld = GetDate(); |
180 | ||
85fa9d60 VZ |
181 | wxDateTime dt; |
182 | wxString value = m_combo->GetValue(); | |
183 | if ( !ParseDateTime(value, &dt) ) | |
184 | { | |
185 | if ( !HasDPFlag(wxDP_ALLOWNONE) ) | |
2fda1fe5 | 186 | dt = dtOld; |
85fa9d60 | 187 | } |
caad7637 | 188 | |
2fda1fe5 | 189 | m_combo->SetText(GetStringValueFor(dt)); |
38511687 | 190 | |
d6781628 RD |
191 | if ( !dt.IsValid() && HasDPFlag(wxDP_ALLOWNONE) ) |
192 | return; | |
6c70323f | 193 | |
85fa9d60 | 194 | // notify that we had to change the date after validation |
2fda1fe5 VZ |
195 | if ( (dt.IsValid() && (!dtOld.IsValid() || dt != dtOld)) || |
196 | (!dt.IsValid() && dtOld.IsValid()) ) | |
85fa9d60 | 197 | { |
2fda1fe5 | 198 | SetDate(dt); |
85fa9d60 VZ |
199 | SendDateEvent(dt); |
200 | } | |
201 | } | |
38511687 | 202 | |
6c70323f | 203 | bool HasDPFlag(int flag) const |
caad7637 | 204 | { |
85fa9d60 | 205 | return m_combo->GetParent()->HasFlag(flag); |
caad7637 | 206 | } |
caad7637 | 207 | |
4d4c704c VZ |
208 | // Return the format to be used for the dates shown by the control. This |
209 | // functions honours wxDP_SHOWCENTURY flag. | |
6c70323f | 210 | wxString GetLocaleDateFormat() const |
85fa9d60 | 211 | { |
4d4c704c VZ |
212 | wxString fmt = wxLocale::GetInfo(wxLOCALE_SHORT_DATE_FMT); |
213 | if ( HasDPFlag(wxDP_SHOWCENTURY) ) | |
214 | fmt.Replace("%y", "%Y"); | |
caad7637 | 215 | |
4d4c704c | 216 | return fmt; |
6c70323f VZ |
217 | } |
218 | ||
219 | bool SetFormat(const wxString& fmt) | |
220 | { | |
221 | m_format = fmt; | |
222 | ||
85fa9d60 VZ |
223 | if ( m_combo ) |
224 | { | |
225 | wxArrayString allowedChars; | |
9a83f860 | 226 | for ( wxChar c = wxT('0'); c <= wxT('9'); c++ ) |
85fa9d60 VZ |
227 | allowedChars.Add(wxString(c, 1)); |
228 | ||
229 | const wxChar *p2 = m_format.c_str(); | |
230 | while ( *p2 ) | |
231 | { | |
232 | if ( *p2 == '%') | |
233 | p2 += 2; | |
234 | else | |
235 | allowedChars.Add(wxString(*p2++, 1)); | |
236 | } | |
237 | ||
238 | #if wxUSE_VALIDATORS | |
239 | wxTextValidator tv(wxFILTER_INCLUDE_CHAR_LIST); | |
240 | tv.SetIncludes(allowedChars); | |
241 | m_combo->SetValidator(tv); | |
242 | #endif | |
243 | ||
2fda1fe5 VZ |
244 | if ( GetDate().IsValid() ) |
245 | m_combo->SetText(GetDate().Format(m_format)); | |
85fa9d60 | 246 | } |
1721a8c0 | 247 | |
85fa9d60 | 248 | return true; |
1721a8c0 VZ |
249 | } |
250 | ||
85fa9d60 | 251 | virtual void SetStringValue(const wxString& s) |
1721a8c0 | 252 | { |
85fa9d60 | 253 | wxDateTime dt; |
d6781628 | 254 | if ( !s.empty() && ParseDateTime(s, &dt) ) |
2fda1fe5 | 255 | SetDate(dt); |
d6781628 | 256 | //else: keep the old value |
1721a8c0 | 257 | } |
1721a8c0 | 258 | |
85fa9d60 | 259 | virtual wxString GetStringValue() const |
1721a8c0 | 260 | { |
2fda1fe5 | 261 | return GetStringValueFor(GetDate()); |
1721a8c0 VZ |
262 | } |
263 | ||
85fa9d60 | 264 | private: |
2fda1fe5 VZ |
265 | // returns either the given date representation using the current format or |
266 | // an empty string if it's invalid | |
267 | wxString GetStringValueFor(const wxDateTime& dt) const | |
268 | { | |
269 | wxString val; | |
270 | if ( dt.IsValid() ) | |
271 | val = dt.Format(m_format); | |
272 | ||
273 | return val; | |
274 | } | |
85fa9d60 VZ |
275 | |
276 | wxSize m_useSize; | |
277 | wxString m_format; | |
85fa9d60 VZ |
278 | |
279 | DECLARE_EVENT_TABLE() | |
1721a8c0 VZ |
280 | }; |
281 | ||
85fa9d60 | 282 | |
c245a012 | 283 | BEGIN_EVENT_TABLE(wxCalendarComboPopup, wxCalendarCtrl) |
85fa9d60 VZ |
284 | EVT_KEY_DOWN(wxCalendarComboPopup::OnCalKey) |
285 | EVT_CALENDAR_SEL_CHANGED(wxID_ANY, wxCalendarComboPopup::OnSelChange) | |
628e155d | 286 | EVT_CALENDAR_PAGE_CHANGED(wxID_ANY, wxCalendarComboPopup::OnSelChange) |
85fa9d60 VZ |
287 | EVT_CALENDAR(wxID_ANY, wxCalendarComboPopup::OnSelChange) |
288 | END_EVENT_TABLE() | |
289 | ||
1721a8c0 | 290 | |
39df3acd | 291 | // ============================================================================ |
7ae712f5 | 292 | // wxDatePickerCtrlGeneric implementation |
39df3acd VZ |
293 | // ============================================================================ |
294 | ||
7ae712f5 | 295 | BEGIN_EVENT_TABLE(wxDatePickerCtrlGeneric, wxDatePickerCtrlBase) |
85fa9d60 | 296 | EVT_TEXT(wxID_ANY, wxDatePickerCtrlGeneric::OnText) |
caad7637 | 297 | EVT_SIZE(wxDatePickerCtrlGeneric::OnSize) |
4427c0a3 | 298 | EVT_SET_FOCUS(wxDatePickerCtrlGeneric::OnFocus) |
39df3acd VZ |
299 | END_EVENT_TABLE() |
300 | ||
b6292ff0 | 301 | #ifndef wxHAS_NATIVE_DATEPICKCTRL |
94ab4d92 | 302 | IMPLEMENT_DYNAMIC_CLASS(wxDatePickerCtrl, wxControl) |
b6292ff0 | 303 | #endif |
39df3acd VZ |
304 | |
305 | // ---------------------------------------------------------------------------- | |
306 | // creation | |
307 | // ---------------------------------------------------------------------------- | |
308 | ||
7ae712f5 VZ |
309 | bool wxDatePickerCtrlGeneric::Create(wxWindow *parent, |
310 | wxWindowID id, | |
311 | const wxDateTime& date, | |
312 | const wxPoint& pos, | |
313 | const wxSize& size, | |
314 | long style, | |
807f5038 | 315 | const wxValidator& validator, |
7ae712f5 | 316 | const wxString& name) |
39df3acd | 317 | { |
29c86948 | 318 | wxASSERT_MSG( !(style & wxDP_SPIN), |
9a83f860 | 319 | wxT("wxDP_SPIN style not supported, use wxDP_DEFAULT") ); |
29c86948 | 320 | |
39df3acd | 321 | if ( !wxControl::Create(parent, id, pos, size, |
85fa9d60 | 322 | style | wxCLIP_CHILDREN | wxWANTS_CHARS | wxBORDER_NONE, |
807f5038 | 323 | validator, name) ) |
39df3acd VZ |
324 | { |
325 | return false; | |
326 | } | |
327 | ||
39df3acd VZ |
328 | InheritAttributes(); |
329 | ||
85fa9d60 VZ |
330 | m_combo = new wxComboCtrl(this, -1, wxEmptyString, |
331 | wxDefaultPosition, wxDefaultSize); | |
39df3acd | 332 | |
4427c0a3 RR |
333 | m_combo->SetCtrlMainWnd(this); |
334 | ||
85fa9d60 | 335 | m_popup = new wxCalendarComboPopup(); |
39df3acd | 336 | |
06077aaf VZ |
337 | #if defined(__WXMSW__) |
338 | // without this keyboard navigation in month control doesn't work | |
339 | m_combo->UseAltPopupWindow(); | |
340 | #endif | |
85fa9d60 | 341 | m_combo->SetPopupControl(m_popup); |
39df3acd | 342 | |
85fa9d60 | 343 | m_popup->SetDateValue(date.IsValid() ? date : wxDateTime::Today()); |
1721a8c0 | 344 | |
170acdc9 | 345 | SetInitialSize(size); |
930fb29e | 346 | |
3a0c6181 | 347 | return true; |
39df3acd VZ |
348 | } |
349 | ||
350 | ||
7ae712f5 | 351 | void wxDatePickerCtrlGeneric::Init() |
39df3acd | 352 | { |
85fa9d60 | 353 | m_combo = NULL; |
85fa9d60 | 354 | m_popup = NULL; |
39df3acd VZ |
355 | } |
356 | ||
a9d13e15 JS |
357 | wxDatePickerCtrlGeneric::~wxDatePickerCtrlGeneric() |
358 | { | |
a9d13e15 | 359 | } |
39df3acd | 360 | |
7ae712f5 | 361 | bool wxDatePickerCtrlGeneric::Destroy() |
39df3acd | 362 | { |
85fa9d60 VZ |
363 | if ( m_combo ) |
364 | m_combo->Destroy(); | |
39df3acd | 365 | |
85fa9d60 | 366 | m_combo = NULL; |
85fa9d60 | 367 | m_popup = NULL; |
39df3acd VZ |
368 | |
369 | return wxControl::Destroy(); | |
370 | } | |
371 | ||
372 | // ---------------------------------------------------------------------------- | |
373 | // overridden base class methods | |
374 | // ---------------------------------------------------------------------------- | |
375 | ||
7ae712f5 | 376 | wxSize wxDatePickerCtrlGeneric::DoGetBestSize() const |
39df3acd | 377 | { |
85fa9d60 | 378 | return m_combo->GetBestSize(); |
39df3acd VZ |
379 | } |
380 | ||
a9e41db7 VZ |
381 | wxWindowList wxDatePickerCtrlGeneric::GetCompositeWindowParts() const |
382 | { | |
383 | wxWindowList parts; | |
384 | parts.push_back(m_combo); | |
385 | parts.push_back(m_popup); | |
386 | return parts; | |
387 | } | |
388 | ||
39df3acd | 389 | // ---------------------------------------------------------------------------- |
7ae712f5 | 390 | // wxDatePickerCtrlGeneric API |
39df3acd VZ |
391 | // ---------------------------------------------------------------------------- |
392 | ||
4b134bb2 | 393 | bool |
7ae712f5 VZ |
394 | wxDatePickerCtrlGeneric::SetDateRange(const wxDateTime& lowerdate, |
395 | const wxDateTime& upperdate) | |
4b134bb2 | 396 | { |
2fda1fe5 | 397 | return m_popup->SetDateRange(lowerdate, upperdate); |
4b134bb2 VZ |
398 | } |
399 | ||
39df3acd | 400 | |
7ae712f5 | 401 | wxDateTime wxDatePickerCtrlGeneric::GetValue() const |
39df3acd | 402 | { |
2ccc6650 VZ |
403 | if ( HasFlag(wxDP_ALLOWNONE) && m_popup->IsTextEmpty() ) |
404 | return wxInvalidDateTime; | |
2fda1fe5 | 405 | return m_popup->GetDate(); |
39df3acd VZ |
406 | } |
407 | ||
408 | ||
7ae712f5 | 409 | void wxDatePickerCtrlGeneric::SetValue(const wxDateTime& date) |
39df3acd | 410 | { |
85fa9d60 | 411 | m_popup->SetDateValue(date); |
39df3acd VZ |
412 | } |
413 | ||
414 | ||
7ae712f5 | 415 | bool wxDatePickerCtrlGeneric::GetRange(wxDateTime *dt1, wxDateTime *dt2) const |
39df3acd | 416 | { |
c245a012 | 417 | return m_popup->GetDateRange(dt1, dt2); |
39df3acd VZ |
418 | } |
419 | ||
420 | ||
7ae712f5 VZ |
421 | void |
422 | wxDatePickerCtrlGeneric::SetRange(const wxDateTime &dt1, const wxDateTime &dt2) | |
39df3acd | 423 | { |
2fda1fe5 VZ |
424 | m_popup->SetDateRange(dt1, dt2); |
425 | } | |
426 | ||
c245a012 | 427 | wxCalendarCtrl *wxDatePickerCtrlGeneric::GetCalendar() const |
2fda1fe5 VZ |
428 | { |
429 | return m_popup; | |
39df3acd VZ |
430 | } |
431 | ||
432 | // ---------------------------------------------------------------------------- | |
433 | // event handlers | |
434 | // ---------------------------------------------------------------------------- | |
435 | ||
39df3acd | 436 | |
caad7637 JS |
437 | void wxDatePickerCtrlGeneric::OnSize(wxSizeEvent& event) |
438 | { | |
85fa9d60 VZ |
439 | if ( m_combo ) |
440 | m_combo->SetSize(GetClientSize()); | |
caad7637 JS |
441 | |
442 | event.Skip(); | |
443 | } | |
444 | ||
445 | ||
7ae712f5 | 446 | void wxDatePickerCtrlGeneric::OnText(wxCommandEvent &ev) |
39df3acd VZ |
447 | { |
448 | ev.SetEventObject(this); | |
449 | ev.SetId(GetId()); | |
eafd76b0 | 450 | GetParent()->GetEventHandler()->ProcessEvent(ev); |
39df3acd VZ |
451 | |
452 | // We'll create an additional event if the date is valid. | |
1721a8c0 | 453 | // If the date isn't valid, the user's probably in the middle of typing |
39df3acd | 454 | wxDateTime dt; |
b0fb790b | 455 | if ( !m_popup || !m_popup->ParseDateTime(m_combo->GetValue(), &dt) ) |
85fa9d60 | 456 | return; |
39df3acd | 457 | |
85fa9d60 | 458 | m_popup->SendDateEvent(dt); |
39df3acd VZ |
459 | } |
460 | ||
461 | ||
4427c0a3 RR |
462 | void wxDatePickerCtrlGeneric::OnFocus(wxFocusEvent& WXUNUSED(event)) |
463 | { | |
464 | m_combo->SetFocus(); | |
465 | } | |
466 | ||
467 | ||
7ae712f5 | 468 | #endif // wxUSE_DATEPICKCTRL |
85fa9d60 | 469 |