use wxComboCtrl instead of custom popup window (patch 1575343)
[wxWidgets.git] / src / generic / datectlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/datectlg.cpp
3 // Purpose: generic wxDatePickerCtrlGeneric implementation
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
26 #if wxUSE_DATEPICKCTRL
27
28 #include "wx/datectrl.h"
29
30 // use this version if we're explicitly requested to do it or if it's the only
31 // one we have
32 #if !defined(wxHAS_NATIVE_DATEPICKCTRL) || \
33 (defined(wxUSE_DATEPICKCTRL_GENERIC) && wxUSE_DATEPICKCTRL_GENERIC)
34
35 #ifndef WX_PRECOMP
36 #include "wx/dialog.h"
37 #include "wx/dcmemory.h"
38 #include "wx/panel.h"
39 #include "wx/textctrl.h"
40 #include "wx/valtext.h"
41 #endif
42
43 #ifdef wxHAS_NATIVE_DATEPICKCTRL
44 // this header is not included from wx/datectrl.h if we have a native
45 // version, but we do need it here
46 #include "wx/generic/datectrl.h"
47 #else
48 // we need to define _WX_DEFINE_DATE_EVENTS_ before including wx/dateevt.h to
49 // define the event types we use if we're the only date picker control version
50 // being compiled -- otherwise it's defined in the native version implementation
51 #define _WX_DEFINE_DATE_EVENTS_
52 #endif
53
54 #include "wx/dateevt.h"
55
56 #include "wx/calctrl.h"
57 #include "wx/combo.h"
58
59 // ----------------------------------------------------------------------------
60 // constants
61 // ----------------------------------------------------------------------------
62
63 #if defined(__WXMSW__)
64 #define CALBORDER 0
65 #else
66 #define CALBORDER 4
67 #endif
68
69 // ----------------------------------------------------------------------------
70 // global variables
71 // ----------------------------------------------------------------------------
72
73 // this should have been a flag in wxDatePickerCtrlGeneric itself but adding it
74 // there now would break backwards compatibility, so put it here as a global:
75 // this shouldn't be a big problem as only one (GUI) thread normally can call
76 // wxDatePickerCtrlGeneric::SetValue() and so it can be only ever used for one
77 // control at a time
78 //
79 // if the value is not NULL, it points to the control which is inside SetValue()
80 static wxDatePickerCtrlGeneric *gs_inSetValue = NULL;
81
82 // ----------------------------------------------------------------------------
83 // local classes
84 // ----------------------------------------------------------------------------
85
86 class wxCalendarComboPopup : public wxCalendarCtrl,
87 public wxComboPopup
88 {
89 public:
90
91 wxCalendarComboPopup() : wxCalendarCtrl(),
92 wxComboPopup()
93 {
94 }
95
96 virtual void Init()
97 {
98 }
99
100 // NB: Don't create lazily since it didn't work that way before
101 // wxComboCtrl was used, and changing behaviour would almost
102 // certainly introduce new bugs.
103 virtual bool Create(wxWindow* parent)
104 {
105 if ( !wxCalendarCtrl::Create(parent, wxID_ANY, wxDefaultDateTime,
106 wxPoint(0, 0), wxDefaultSize,
107 wxCAL_SHOW_HOLIDAYS | wxBORDER_SUNKEN) )
108 return false;
109
110 wxWindow *yearControl = wxCalendarCtrl::GetYearControl();
111
112 wxClientDC dc(yearControl);
113 dc.SetFont(yearControl->GetFont());
114 wxCoord width, dummy;
115 dc.GetTextExtent(wxT("2000"), &width, &dummy);
116 width += ConvertDialogToPixels(wxSize(20, 0)).x;
117
118 wxSize calSize = wxCalendarCtrl::GetBestSize();
119 wxSize yearSize = yearControl->GetSize();
120 yearSize.x = width;
121
122 wxPoint yearPosition = yearControl->GetPosition();
123
124 SetFormat(wxT("%x"));
125
126 width = yearPosition.x + yearSize.x+2+CALBORDER/2;
127 if (width < calSize.x-4)
128 width = calSize.x-4;
129
130 int calPos = (width-calSize.x)/2;
131 if (calPos == -1)
132 {
133 calPos = 0;
134 width += 2;
135 }
136 wxCalendarCtrl::SetSize(calPos, 0, calSize.x, calSize.y);
137 yearControl->SetSize(width-yearSize.x-CALBORDER/2, yearPosition.y,
138 yearSize.x, yearSize.y);
139 wxCalendarCtrl::GetMonthControl()->Move(0, 0);
140
141 m_useSize.x = width+CALBORDER/2;
142 m_useSize.y = calSize.y-2+CALBORDER;
143
144 wxWindow* tx = m_combo->GetTextCtrl();
145 if ( !tx )
146 tx = m_combo;
147
148 tx->Connect(wxEVT_KILL_FOCUS,
149 wxFocusEventHandler(wxCalendarComboPopup::OnKillTextFocus),
150 NULL, this);
151
152 return true;
153 }
154
155 virtual wxSize GetAdjustedSize(int WXUNUSED(minWidth),
156 int WXUNUSED(prefHeight),
157 int WXUNUSED(maxHeight))
158 {
159 return m_useSize;
160 }
161
162 virtual wxWindow *GetControl() { return this; }
163
164 void SetDateValue(const wxDateTime& date)
165 {
166 if ( date.IsValid() )
167 {
168 m_combo->SetText(date.Format(m_format));
169 }
170 else // invalid date
171 {
172 wxASSERT_MSG( HasDPFlag(wxDP_ALLOWNONE),
173 _T("this control must have a valid date") );
174
175 m_combo->SetText(wxEmptyString);
176 }
177
178 m_currentDate = date;
179 }
180
181 const wxDateTime& GetDateValue() const
182 {
183 return m_currentDate;
184 }
185
186 bool ParseDateTime(const wxString& s, wxDateTime* pDt)
187 {
188 wxASSERT(pDt);
189
190 if ( !s.empty() )
191 {
192 pDt->ParseFormat(s, m_format);
193 if ( !pDt->IsValid() )
194 return false;
195 }
196
197 return true;
198 }
199
200 void SendDateEvent(const wxDateTime& dt)
201 {
202 //
203 // Sends both wxCalendarEvent and wxDateEvent
204 wxWindow* datePicker = m_combo->GetParent();
205
206 wxCalendarEvent cev((wxCalendarCtrl*) this, wxEVT_CALENDAR_SEL_CHANGED);
207 cev.SetEventObject(datePicker);
208 cev.SetId(datePicker->GetId());
209 cev.SetDate(dt);
210 GetParent()->ProcessEvent(cev);
211
212 wxDateEvent event(datePicker, dt, wxEVT_DATE_CHANGED);
213 datePicker->GetParent()->ProcessEvent(event);
214 }
215
216 private:
217
218 void OnCalKey(wxKeyEvent & ev)
219 {
220 if (ev.GetKeyCode() == WXK_ESCAPE && !ev.HasModifiers())
221 Dismiss();
222 else
223 ev.Skip();
224 }
225
226 void OnSelChange(wxCalendarEvent &ev)
227 {
228 m_currentDate = wxCalendarCtrl::GetDate();
229 m_combo->SetText(m_currentDate.Format(m_format));
230
231 if ( ev.GetEventType() == wxEVT_CALENDAR_DOUBLECLICKED )
232 {
233 Dismiss();
234 }
235
236 SendDateEvent(m_currentDate);
237 }
238
239 void OnKillTextFocus(wxFocusEvent &ev)
240 {
241 ev.Skip();
242
243 wxDateTime dt;
244 wxString value = m_combo->GetValue();
245 if ( !ParseDateTime(value, &dt) )
246 {
247 if ( !HasDPFlag(wxDP_ALLOWNONE) )
248 dt = m_currentDate;
249 }
250
251 if ( dt.IsValid() )
252 m_combo->SetText(dt.Format(m_format));
253 else
254 m_combo->SetText(wxEmptyString);
255
256 // notify that we had to change the date after validation
257 if ( (dt.IsValid() && (!m_currentDate.IsValid() || m_currentDate != dt)) ||
258 (!dt.IsValid() && m_currentDate.IsValid()) )
259 {
260 m_currentDate = dt;
261 SendDateEvent(dt);
262 }
263 }
264
265 bool HasDPFlag(int flag)
266 {
267 return m_combo->GetParent()->HasFlag(flag);
268 }
269
270 bool SetFormat(const wxChar *fmt)
271 {
272 m_format.clear();
273
274 wxDateTime dt;
275 dt.ParseFormat(wxT("2003-10-13"), wxT("%Y-%m-%d"));
276 wxString str(dt.Format(fmt));
277
278 const wxChar *p = str.c_str();
279 while ( *p )
280 {
281 int n=wxAtoi(p);
282 if (n == dt.GetDay())
283 {
284 m_format.Append(wxT("%d"));
285 p += 2;
286 }
287 else if (n == (int)dt.GetMonth()+1)
288 {
289 m_format.Append(wxT("%m"));
290 p += 2;
291 }
292 else if (n == dt.GetYear())
293 {
294 m_format.Append(wxT("%Y"));
295 p += 4;
296 }
297 else if (n == (dt.GetYear() % 100))
298 {
299 if ( HasDPFlag(wxDP_SHOWCENTURY) )
300 m_format.Append(wxT("%Y"));
301 else
302 m_format.Append(wxT("%y"));
303 p += 2;
304 }
305 else
306 m_format.Append(*p++);
307 }
308
309 if ( m_combo )
310 {
311 wxArrayString allowedChars;
312 for ( wxChar c = _T('0'); c <= _T('9'); c++ )
313 allowedChars.Add(wxString(c, 1));
314
315 const wxChar *p2 = m_format.c_str();
316 while ( *p2 )
317 {
318 if ( *p2 == '%')
319 p2 += 2;
320 else
321 allowedChars.Add(wxString(*p2++, 1));
322 }
323
324 #if wxUSE_VALIDATORS
325 wxTextValidator tv(wxFILTER_INCLUDE_CHAR_LIST);
326 tv.SetIncludes(allowedChars);
327 m_combo->SetValidator(tv);
328 #endif
329
330 if (m_currentDate.IsValid())
331 m_combo->SetText(m_currentDate.Format(m_format));
332 }
333
334 return true;
335 }
336
337 virtual void SetStringValue(const wxString& s)
338 {
339 wxDateTime dt;
340 if ( ParseDateTime(s, &dt) )
341 m_currentDate = dt;
342 else if ( HasDPFlag(wxDP_ALLOWNONE) )
343 m_currentDate = dt;
344 }
345
346 virtual wxString GetStringValue() const
347 {
348 if ( !m_currentDate.IsValid() )
349 return wxEmptyString;
350
351 return m_currentDate.Format(m_format);
352 }
353
354 private:
355
356 wxSize m_useSize;
357 wxString m_format;
358 wxDateTime m_currentDate;
359
360 DECLARE_EVENT_TABLE()
361 };
362
363
364 BEGIN_EVENT_TABLE(wxCalendarComboPopup, wxCalendarCtrl)
365 EVT_KEY_DOWN(wxCalendarComboPopup::OnCalKey)
366 EVT_CALENDAR_SEL_CHANGED(wxID_ANY, wxCalendarComboPopup::OnSelChange)
367 EVT_CALENDAR_DAY(wxID_ANY, wxCalendarComboPopup::OnSelChange)
368 EVT_CALENDAR_MONTH(wxID_ANY, wxCalendarComboPopup::OnSelChange)
369 EVT_CALENDAR_YEAR(wxID_ANY, wxCalendarComboPopup::OnSelChange)
370 EVT_CALENDAR(wxID_ANY, wxCalendarComboPopup::OnSelChange)
371 END_EVENT_TABLE()
372
373
374 // ============================================================================
375 // wxDatePickerCtrlGeneric implementation
376 // ============================================================================
377
378 BEGIN_EVENT_TABLE(wxDatePickerCtrlGeneric, wxDatePickerCtrlBase)
379 EVT_TEXT(wxID_ANY, wxDatePickerCtrlGeneric::OnText)
380 EVT_SIZE(wxDatePickerCtrlGeneric::OnSize)
381 END_EVENT_TABLE()
382
383 #ifndef wxHAS_NATIVE_DATEPICKCTRL
384 IMPLEMENT_DYNAMIC_CLASS(wxDatePickerCtrl, wxControl)
385 #endif
386
387 // ----------------------------------------------------------------------------
388 // creation
389 // ----------------------------------------------------------------------------
390
391 bool wxDatePickerCtrlGeneric::Create(wxWindow *parent,
392 wxWindowID id,
393 const wxDateTime& date,
394 const wxPoint& pos,
395 const wxSize& size,
396 long style,
397 const wxValidator& validator,
398 const wxString& name)
399 {
400 wxASSERT_MSG( !(style & wxDP_SPIN),
401 _T("wxDP_SPIN style not supported, use wxDP_DEFAULT") );
402
403 if ( !wxControl::Create(parent, id, pos, size,
404 style | wxCLIP_CHILDREN | wxWANTS_CHARS | wxBORDER_NONE,
405 validator, name) )
406 {
407 return false;
408 }
409
410 InheritAttributes();
411
412 m_combo = new wxComboCtrl(this, -1, wxEmptyString,
413 wxDefaultPosition, wxDefaultSize);
414
415 m_popup = new wxCalendarComboPopup();
416
417 m_combo->SetPopupControl(m_popup);
418
419 m_cal = m_popup;
420
421 m_popup->SetDateValue(date.IsValid() ? date : wxDateTime::Today());
422
423 SetBestFittingSize(size);
424
425 return true;
426 }
427
428
429 void wxDatePickerCtrlGeneric::Init()
430 {
431 m_combo = NULL;
432 m_cal = NULL;
433 m_popup = NULL;
434 }
435
436 wxDatePickerCtrlGeneric::~wxDatePickerCtrlGeneric()
437 {
438 }
439
440 bool wxDatePickerCtrlGeneric::Destroy()
441 {
442 if ( m_combo )
443 m_combo->Destroy();
444
445 m_combo = NULL;
446 m_cal = NULL;
447 m_popup = NULL;
448
449 return wxControl::Destroy();
450 }
451
452 // ----------------------------------------------------------------------------
453 // overridden base class methods
454 // ----------------------------------------------------------------------------
455
456 wxSize wxDatePickerCtrlGeneric::DoGetBestSize() const
457 {
458 return m_combo->GetBestSize();
459 }
460
461 // ----------------------------------------------------------------------------
462 // wxDatePickerCtrlGeneric API
463 // ----------------------------------------------------------------------------
464
465 bool
466 wxDatePickerCtrlGeneric::SetDateRange(const wxDateTime& lowerdate,
467 const wxDateTime& upperdate)
468 {
469 return m_cal->SetDateRange(lowerdate, upperdate);
470 }
471
472
473 wxDateTime wxDatePickerCtrlGeneric::GetValue() const
474 {
475 return m_popup->GetDateValue();
476 }
477
478
479 void wxDatePickerCtrlGeneric::SetValue(const wxDateTime& date)
480 {
481 m_popup->SetDateValue(date);
482 }
483
484
485 bool wxDatePickerCtrlGeneric::GetRange(wxDateTime *dt1, wxDateTime *dt2) const
486 {
487 if (dt1)
488 *dt1 = m_cal->GetLowerDateLimit();
489 if (dt2)
490 *dt2 = m_cal->GetUpperDateLimit();
491 return true;
492 }
493
494
495 void
496 wxDatePickerCtrlGeneric::SetRange(const wxDateTime &dt1, const wxDateTime &dt2)
497 {
498 m_cal->SetDateRange(dt1, dt2);
499 }
500
501 // ----------------------------------------------------------------------------
502 // event handlers
503 // ----------------------------------------------------------------------------
504
505
506 void wxDatePickerCtrlGeneric::OnSize(wxSizeEvent& event)
507 {
508 if ( m_combo )
509 m_combo->SetSize(GetClientSize());
510
511 event.Skip();
512 }
513
514
515 void wxDatePickerCtrlGeneric::OnText(wxCommandEvent &ev)
516 {
517 ev.SetEventObject(this);
518 ev.SetId(GetId());
519 GetParent()->ProcessEvent(ev);
520
521 // We'll create an additional event if the date is valid.
522 // If the date isn't valid, the user's probably in the middle of typing
523 wxDateTime dt;
524 if ( !m_popup->ParseDateTime(m_combo->GetValue(), &dt) )
525 return;
526
527 m_popup->SendDateEvent(dt);
528 }
529
530
531 #endif // wxUSE_DATEPICKCTRL_GENERIC
532
533 #endif // wxUSE_DATEPICKCTRL
534