1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/datectlg.cpp
3 // Purpose: generic wxDatePickerCtrlGeneric implementation
4 // Author: Andreas Pflug
8 // Copyright: (c) 2005 Andreas Pflug <pgadmin@pse-consulting.de>
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #include "wx/wxprec.h"
26 #if wxUSE_DATEPICKCTRL
29 #include "wx/dialog.h"
30 #include "wx/dcmemory.h"
32 #include "wx/textctrl.h"
33 #include "wx/valtext.h"
36 #include "wx/datectrl.h"
37 #include "wx/generic/datectrl.h"
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 class wxCalendarComboPopup
: public wxCalendarCtrl
,
59 wxCalendarComboPopup() : wxCalendarCtrl(),
68 // NB: Don't create lazily since it didn't work that way before
69 // wxComboCtrl was used, and changing behaviour would almost
70 // certainly introduce new bugs.
71 virtual bool Create(wxWindow
* parent
)
73 if ( !wxCalendarCtrl::Create(parent
, wxID_ANY
, wxDefaultDateTime
,
74 wxPoint(0, 0), wxDefaultSize
,
75 wxCAL_SEQUENTIAL_MONTH_SELECTION
76 | wxCAL_SHOW_HOLIDAYS
| wxBORDER_SUNKEN
) )
81 m_useSize
= wxCalendarCtrl::GetBestSize();
83 wxWindow
* tx
= m_combo
->GetTextCtrl();
87 tx
->Connect(wxEVT_KILL_FOCUS
,
88 wxFocusEventHandler(wxCalendarComboPopup::OnKillTextFocus
),
94 virtual wxSize
GetAdjustedSize(int WXUNUSED(minWidth
),
95 int WXUNUSED(prefHeight
),
96 int WXUNUSED(maxHeight
))
101 virtual wxWindow
*GetControl() { return this; }
103 void SetDateValue(const wxDateTime
& date
)
105 if ( date
.IsValid() )
107 m_combo
->SetText(date
.Format(m_format
));
112 wxASSERT_MSG( HasDPFlag(wxDP_ALLOWNONE
),
113 _T("this control must have a valid date") );
115 m_combo
->SetText(wxEmptyString
);
119 bool ParseDateTime(const wxString
& s
, wxDateTime
* pDt
)
125 pDt
->ParseFormat(s
.c_str(), m_format
);
126 if ( !pDt
->IsValid() )
133 void SendDateEvent(const wxDateTime
& dt
)
135 // Sends both wxCalendarEvent and wxDateEvent
136 wxWindow
* datePicker
= m_combo
->GetParent();
138 wxCalendarEvent
cev(datePicker
, dt
, wxEVT_CALENDAR_SEL_CHANGED
);
139 datePicker
->GetEventHandler()->ProcessEvent(cev
);
141 wxDateEvent
event(datePicker
, dt
, wxEVT_DATE_CHANGED
);
142 datePicker
->GetEventHandler()->ProcessEvent(event
);
147 void OnCalKey(wxKeyEvent
& ev
)
149 if (ev
.GetKeyCode() == WXK_ESCAPE
&& !ev
.HasModifiers())
155 void OnSelChange(wxCalendarEvent
&ev
)
157 m_combo
->SetText(GetDate().Format(m_format
));
159 if ( ev
.GetEventType() == wxEVT_CALENDAR_DOUBLECLICKED
)
164 SendDateEvent(GetDate());
167 void OnKillTextFocus(wxFocusEvent
&ev
)
171 const wxDateTime
& dtOld
= GetDate();
174 wxString value
= m_combo
->GetValue();
175 if ( !ParseDateTime(value
, &dt
) )
177 if ( !HasDPFlag(wxDP_ALLOWNONE
) )
181 m_combo
->SetText(GetStringValueFor(dt
));
183 if ( !dt
.IsValid() && HasDPFlag(wxDP_ALLOWNONE
) )
186 // notify that we had to change the date after validation
187 if ( (dt
.IsValid() && (!dtOld
.IsValid() || dt
!= dtOld
)) ||
188 (!dt
.IsValid() && dtOld
.IsValid()) )
195 bool HasDPFlag(int flag
)
197 return m_combo
->GetParent()->HasFlag(flag
);
200 bool SetFormat(const wxString
& fmt
)
205 dt
.ParseFormat(wxT("2003-10-13"), wxT("%Y-%m-%d"));
206 wxString
str(dt
.Format(fmt
));
208 const wxChar
*p
= str
.c_str();
212 if (n
== dt
.GetDay())
214 m_format
.Append(wxT("%d"));
217 else if (n
== (int)dt
.GetMonth()+1)
219 m_format
.Append(wxT("%m"));
222 else if (n
== dt
.GetYear())
224 m_format
.Append(wxT("%Y"));
227 else if (n
== (dt
.GetYear() % 100))
229 if ( HasDPFlag(wxDP_SHOWCENTURY
) )
230 m_format
.Append(wxT("%Y"));
232 m_format
.Append(wxT("%y"));
236 m_format
.Append(*p
++);
241 wxArrayString allowedChars
;
242 for ( wxChar c
= _T('0'); c
<= _T('9'); c
++ )
243 allowedChars
.Add(wxString(c
, 1));
245 const wxChar
*p2
= m_format
.c_str();
251 allowedChars
.Add(wxString(*p2
++, 1));
255 wxTextValidator
tv(wxFILTER_INCLUDE_CHAR_LIST
);
256 tv
.SetIncludes(allowedChars
);
257 m_combo
->SetValidator(tv
);
260 if ( GetDate().IsValid() )
261 m_combo
->SetText(GetDate().Format(m_format
));
267 virtual void SetStringValue(const wxString
& s
)
270 if ( !s
.empty() && ParseDateTime(s
, &dt
) )
272 //else: keep the old value
275 virtual wxString
GetStringValue() const
277 return GetStringValueFor(GetDate());
281 // returns either the given date representation using the current format or
282 // an empty string if it's invalid
283 wxString
GetStringValueFor(const wxDateTime
& dt
) const
287 val
= dt
.Format(m_format
);
295 DECLARE_EVENT_TABLE()
299 BEGIN_EVENT_TABLE(wxCalendarComboPopup
, wxCalendarCtrl
)
300 EVT_KEY_DOWN(wxCalendarComboPopup::OnCalKey
)
301 EVT_CALENDAR_SEL_CHANGED(wxID_ANY
, wxCalendarComboPopup::OnSelChange
)
302 EVT_CALENDAR_PAGE_CHANGED(wxID_ANY
, wxCalendarComboPopup::OnSelChange
)
303 EVT_CALENDAR(wxID_ANY
, wxCalendarComboPopup::OnSelChange
)
307 // ============================================================================
308 // wxDatePickerCtrlGeneric implementation
309 // ============================================================================
311 BEGIN_EVENT_TABLE(wxDatePickerCtrlGeneric
, wxDatePickerCtrlBase
)
312 EVT_TEXT(wxID_ANY
, wxDatePickerCtrlGeneric::OnText
)
313 EVT_SIZE(wxDatePickerCtrlGeneric::OnSize
)
314 EVT_SET_FOCUS(wxDatePickerCtrlGeneric::OnFocus
)
317 #ifndef wxHAS_NATIVE_DATEPICKCTRL
318 IMPLEMENT_DYNAMIC_CLASS(wxDatePickerCtrl
, wxControl
)
321 // ----------------------------------------------------------------------------
323 // ----------------------------------------------------------------------------
325 bool wxDatePickerCtrlGeneric::Create(wxWindow
*parent
,
327 const wxDateTime
& date
,
331 const wxValidator
& validator
,
332 const wxString
& name
)
334 wxASSERT_MSG( !(style
& wxDP_SPIN
),
335 _T("wxDP_SPIN style not supported, use wxDP_DEFAULT") );
337 if ( !wxControl::Create(parent
, id
, pos
, size
,
338 style
| wxCLIP_CHILDREN
| wxWANTS_CHARS
| wxBORDER_NONE
,
346 m_combo
= new wxComboCtrl(this, -1, wxEmptyString
,
347 wxDefaultPosition
, wxDefaultSize
);
349 m_combo
->SetCtrlMainWnd(this);
351 m_popup
= new wxCalendarComboPopup();
353 #if defined(__WXMSW__)
354 // without this keyboard navigation in month control doesn't work
355 m_combo
->UseAltPopupWindow();
357 m_combo
->SetPopupControl(m_popup
);
359 m_popup
->SetDateValue(date
.IsValid() ? date
: wxDateTime::Today());
361 SetInitialSize(size
);
367 void wxDatePickerCtrlGeneric::Init()
373 wxDatePickerCtrlGeneric::~wxDatePickerCtrlGeneric()
377 bool wxDatePickerCtrlGeneric::Destroy()
385 return wxControl::Destroy();
388 // ----------------------------------------------------------------------------
389 // overridden base class methods
390 // ----------------------------------------------------------------------------
392 wxSize
wxDatePickerCtrlGeneric::DoGetBestSize() const
394 return m_combo
->GetBestSize();
397 // ----------------------------------------------------------------------------
398 // wxDatePickerCtrlGeneric API
399 // ----------------------------------------------------------------------------
402 wxDatePickerCtrlGeneric::SetDateRange(const wxDateTime
& lowerdate
,
403 const wxDateTime
& upperdate
)
405 return m_popup
->SetDateRange(lowerdate
, upperdate
);
409 wxDateTime
wxDatePickerCtrlGeneric::GetValue() const
411 return m_popup
->GetDate();
415 void wxDatePickerCtrlGeneric::SetValue(const wxDateTime
& date
)
417 m_popup
->SetDateValue(date
);
421 bool wxDatePickerCtrlGeneric::GetRange(wxDateTime
*dt1
, wxDateTime
*dt2
) const
423 return m_popup
->GetDateRange(dt1
, dt2
);
428 wxDatePickerCtrlGeneric::SetRange(const wxDateTime
&dt1
, const wxDateTime
&dt2
)
430 m_popup
->SetDateRange(dt1
, dt2
);
433 wxCalendarCtrl
*wxDatePickerCtrlGeneric::GetCalendar() const
438 // ----------------------------------------------------------------------------
440 // ----------------------------------------------------------------------------
443 void wxDatePickerCtrlGeneric::OnSize(wxSizeEvent
& event
)
446 m_combo
->SetSize(GetClientSize());
452 void wxDatePickerCtrlGeneric::OnText(wxCommandEvent
&ev
)
454 ev
.SetEventObject(this);
456 GetParent()->GetEventHandler()->ProcessEvent(ev
);
458 // We'll create an additional event if the date is valid.
459 // If the date isn't valid, the user's probably in the middle of typing
461 if ( !m_popup
|| !m_popup
->ParseDateTime(m_combo
->GetValue(), &dt
) )
464 m_popup
->SendDateEvent(dt
);
468 void wxDatePickerCtrlGeneric::OnFocus(wxFocusEvent
& WXUNUSED(event
))
474 #endif // wxUSE_DATEPICKCTRL