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"
33 #include "wx/textctrl.h"
34 #include "wx/valtext.h"
37 #include "wx/calctrl.h"
40 #include "wx/datectrl.h"
41 #include "wx/generic/datectrl.h"
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 class wxCalendarComboPopup
: public wxCalendarCtrl
,
62 wxCalendarComboPopup() : wxCalendarCtrl(),
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
)
76 if ( !wxCalendarCtrl::Create(parent
, wxID_ANY
, wxDefaultDateTime
,
77 wxPoint(0, 0), wxDefaultSize
,
78 wxCAL_SEQUENTIAL_MONTH_SELECTION
79 | wxCAL_SHOW_HOLIDAYS
| wxBORDER_SUNKEN
) )
82 SetFormat(GetLocaleDateFormat());
84 m_useSize
= wxCalendarCtrl::GetBestSize();
86 wxWindow
* tx
= m_combo
->GetTextCtrl();
90 tx
->Connect(wxEVT_KILL_FOCUS
,
91 wxFocusEventHandler(wxCalendarComboPopup::OnKillTextFocus
),
97 virtual wxSize
GetAdjustedSize(int WXUNUSED(minWidth
),
98 int WXUNUSED(prefHeight
),
99 int WXUNUSED(maxHeight
))
104 virtual wxWindow
*GetControl() { return this; }
106 void SetDateValue(const wxDateTime
& date
)
108 if ( date
.IsValid() )
110 m_combo
->SetText(date
.Format(m_format
));
115 wxASSERT_MSG( HasDPFlag(wxDP_ALLOWNONE
),
116 wxT("this control must have a valid date") );
118 m_combo
->SetText(wxEmptyString
);
122 bool IsTextEmpty() const
124 return m_combo
->GetTextCtrl()->IsEmpty();
127 bool ParseDateTime(const wxString
& s
, wxDateTime
* pDt
)
133 pDt
->ParseFormat(s
.c_str(), m_format
);
134 if ( !pDt
->IsValid() )
141 void SendDateEvent(const wxDateTime
& dt
)
143 // Sends both wxCalendarEvent and wxDateEvent
144 wxWindow
* datePicker
= m_combo
->GetParent();
146 wxCalendarEvent
cev(datePicker
, dt
, wxEVT_CALENDAR_SEL_CHANGED
);
147 datePicker
->GetEventHandler()->ProcessEvent(cev
);
149 wxDateEvent
event(datePicker
, dt
, wxEVT_DATE_CHANGED
);
150 datePicker
->GetEventHandler()->ProcessEvent(event
);
155 void OnCalKey(wxKeyEvent
& ev
)
157 if (ev
.GetKeyCode() == WXK_ESCAPE
&& !ev
.HasModifiers())
163 void OnSelChange(wxCalendarEvent
&ev
)
165 m_combo
->SetText(GetDate().Format(m_format
));
167 if ( ev
.GetEventType() == wxEVT_CALENDAR_DOUBLECLICKED
)
172 SendDateEvent(GetDate());
175 void OnKillTextFocus(wxFocusEvent
&ev
)
179 const wxDateTime
& dtOld
= GetDate();
182 wxString value
= m_combo
->GetValue();
183 if ( !ParseDateTime(value
, &dt
) )
185 if ( !HasDPFlag(wxDP_ALLOWNONE
) )
189 m_combo
->SetText(GetStringValueFor(dt
));
191 if ( !dt
.IsValid() && HasDPFlag(wxDP_ALLOWNONE
) )
194 // notify that we had to change the date after validation
195 if ( (dt
.IsValid() && (!dtOld
.IsValid() || dt
!= dtOld
)) ||
196 (!dt
.IsValid() && dtOld
.IsValid()) )
203 bool HasDPFlag(int flag
) const
205 return m_combo
->GetParent()->HasFlag(flag
);
208 // Return the format to be used for the dates shown by the control. This
209 // functions honours wxDP_SHOWCENTURY flag.
210 wxString
GetLocaleDateFormat() const
212 wxString fmt
= wxLocale::GetInfo(wxLOCALE_SHORT_DATE_FMT
);
213 if ( HasDPFlag(wxDP_SHOWCENTURY
) )
214 fmt
.Replace("%y", "%Y");
219 bool SetFormat(const wxString
& fmt
)
225 wxArrayString allowedChars
;
226 for ( wxChar c
= wxT('0'); c
<= wxT('9'); c
++ )
227 allowedChars
.Add(wxString(c
, 1));
229 const wxChar
*p2
= m_format
.c_str();
235 allowedChars
.Add(wxString(*p2
++, 1));
239 wxTextValidator
tv(wxFILTER_INCLUDE_CHAR_LIST
);
240 tv
.SetIncludes(allowedChars
);
241 m_combo
->SetValidator(tv
);
244 if ( GetDate().IsValid() )
245 m_combo
->SetText(GetDate().Format(m_format
));
251 virtual void SetStringValue(const wxString
& s
)
254 if ( !s
.empty() && ParseDateTime(s
, &dt
) )
256 //else: keep the old value
259 virtual wxString
GetStringValue() const
261 return GetStringValueFor(GetDate());
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
271 val
= dt
.Format(m_format
);
279 DECLARE_EVENT_TABLE()
283 BEGIN_EVENT_TABLE(wxCalendarComboPopup
, wxCalendarCtrl
)
284 EVT_KEY_DOWN(wxCalendarComboPopup::OnCalKey
)
285 EVT_CALENDAR_SEL_CHANGED(wxID_ANY
, wxCalendarComboPopup::OnSelChange
)
286 EVT_CALENDAR_PAGE_CHANGED(wxID_ANY
, wxCalendarComboPopup::OnSelChange
)
287 EVT_CALENDAR(wxID_ANY
, wxCalendarComboPopup::OnSelChange
)
291 // ============================================================================
292 // wxDatePickerCtrlGeneric implementation
293 // ============================================================================
295 BEGIN_EVENT_TABLE(wxDatePickerCtrlGeneric
, wxDatePickerCtrlBase
)
296 EVT_TEXT(wxID_ANY
, wxDatePickerCtrlGeneric::OnText
)
297 EVT_SIZE(wxDatePickerCtrlGeneric::OnSize
)
298 EVT_SET_FOCUS(wxDatePickerCtrlGeneric::OnFocus
)
301 #ifndef wxHAS_NATIVE_DATEPICKCTRL
302 IMPLEMENT_DYNAMIC_CLASS(wxDatePickerCtrl
, wxControl
)
305 // ----------------------------------------------------------------------------
307 // ----------------------------------------------------------------------------
309 bool wxDatePickerCtrlGeneric::Create(wxWindow
*parent
,
311 const wxDateTime
& date
,
315 const wxValidator
& validator
,
316 const wxString
& name
)
318 wxASSERT_MSG( !(style
& wxDP_SPIN
),
319 wxT("wxDP_SPIN style not supported, use wxDP_DEFAULT") );
321 if ( !wxControl::Create(parent
, id
, pos
, size
,
322 style
| wxCLIP_CHILDREN
| wxWANTS_CHARS
| wxBORDER_NONE
,
330 m_combo
= new wxComboCtrl(this, -1, wxEmptyString
,
331 wxDefaultPosition
, wxDefaultSize
);
333 m_combo
->SetCtrlMainWnd(this);
335 m_popup
= new wxCalendarComboPopup();
337 #if defined(__WXMSW__)
338 // without this keyboard navigation in month control doesn't work
339 m_combo
->UseAltPopupWindow();
341 m_combo
->SetPopupControl(m_popup
);
343 m_popup
->SetDateValue(date
.IsValid() ? date
: wxDateTime::Today());
345 SetInitialSize(size
);
351 void wxDatePickerCtrlGeneric::Init()
357 wxDatePickerCtrlGeneric::~wxDatePickerCtrlGeneric()
361 bool wxDatePickerCtrlGeneric::Destroy()
369 return wxControl::Destroy();
372 // ----------------------------------------------------------------------------
373 // overridden base class methods
374 // ----------------------------------------------------------------------------
376 wxSize
wxDatePickerCtrlGeneric::DoGetBestSize() const
378 return m_combo
->GetBestSize();
381 wxWindowList
wxDatePickerCtrlGeneric::GetCompositeWindowParts() const
384 parts
.push_back(m_combo
);
385 parts
.push_back(m_popup
);
389 // ----------------------------------------------------------------------------
390 // wxDatePickerCtrlGeneric API
391 // ----------------------------------------------------------------------------
394 wxDatePickerCtrlGeneric::SetDateRange(const wxDateTime
& lowerdate
,
395 const wxDateTime
& upperdate
)
397 return m_popup
->SetDateRange(lowerdate
, upperdate
);
401 wxDateTime
wxDatePickerCtrlGeneric::GetValue() const
403 if ( HasFlag(wxDP_ALLOWNONE
) && m_popup
->IsTextEmpty() )
404 return wxInvalidDateTime
;
405 return m_popup
->GetDate();
409 void wxDatePickerCtrlGeneric::SetValue(const wxDateTime
& date
)
411 m_popup
->SetDateValue(date
);
415 bool wxDatePickerCtrlGeneric::GetRange(wxDateTime
*dt1
, wxDateTime
*dt2
) const
417 return m_popup
->GetDateRange(dt1
, dt2
);
422 wxDatePickerCtrlGeneric::SetRange(const wxDateTime
&dt1
, const wxDateTime
&dt2
)
424 m_popup
->SetDateRange(dt1
, dt2
);
427 wxCalendarCtrl
*wxDatePickerCtrlGeneric::GetCalendar() const
432 // ----------------------------------------------------------------------------
434 // ----------------------------------------------------------------------------
437 void wxDatePickerCtrlGeneric::OnSize(wxSizeEvent
& event
)
440 m_combo
->SetSize(GetClientSize());
446 void wxDatePickerCtrlGeneric::OnText(wxCommandEvent
&ev
)
448 ev
.SetEventObject(this);
450 GetParent()->GetEventHandler()->ProcessEvent(ev
);
452 // We'll create an additional event if the date is valid.
453 // If the date isn't valid, the user's probably in the middle of typing
455 if ( !m_popup
|| !m_popup
->ParseDateTime(m_combo
->GetValue(), &dt
) )
458 m_popup
->SendDateEvent(dt
);
462 void wxDatePickerCtrlGeneric::OnFocus(wxFocusEvent
& WXUNUSED(event
))
468 #endif // wxUSE_DATEPICKCTRL