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"
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 class wxCalendarComboPopup
: public wxCalendarCtrl
,
61 wxCalendarComboPopup() : wxCalendarCtrl(),
70 // NB: Don't create lazily since it didn't work that way before
71 // wxComboCtrl was used, and changing behaviour would almost
72 // certainly introduce new bugs.
73 virtual bool Create(wxWindow
* parent
)
75 if ( !wxCalendarCtrl::Create(parent
, wxID_ANY
, wxDefaultDateTime
,
76 wxPoint(0, 0), wxDefaultSize
,
77 wxCAL_SEQUENTIAL_MONTH_SELECTION
78 | wxCAL_SHOW_HOLIDAYS
| wxBORDER_SUNKEN
) )
81 SetFormat(GetLocaleDateFormat());
83 m_useSize
= wxCalendarCtrl::GetBestSize();
85 wxWindow
* tx
= m_combo
->GetTextCtrl();
89 tx
->Connect(wxEVT_KILL_FOCUS
,
90 wxFocusEventHandler(wxCalendarComboPopup::OnKillTextFocus
),
96 virtual wxSize
GetAdjustedSize(int WXUNUSED(minWidth
),
97 int WXUNUSED(prefHeight
),
98 int WXUNUSED(maxHeight
))
103 virtual wxWindow
*GetControl() { return this; }
105 void SetDateValue(const wxDateTime
& date
)
107 if ( date
.IsValid() )
109 m_combo
->SetText(date
.Format(m_format
));
114 wxASSERT_MSG( HasDPFlag(wxDP_ALLOWNONE
),
115 wxT("this control must have a valid date") );
117 m_combo
->SetText(wxEmptyString
);
121 bool IsTextEmpty() const
123 return m_combo
->GetTextCtrl()->IsEmpty();
126 bool ParseDateTime(const wxString
& s
, wxDateTime
* pDt
)
132 pDt
->ParseFormat(s
.c_str(), m_format
);
133 if ( !pDt
->IsValid() )
140 void SendDateEvent(const wxDateTime
& dt
)
142 // Sends both wxCalendarEvent and wxDateEvent
143 wxWindow
* datePicker
= m_combo
->GetParent();
145 wxCalendarEvent
cev(datePicker
, dt
, wxEVT_CALENDAR_SEL_CHANGED
);
146 datePicker
->GetEventHandler()->ProcessEvent(cev
);
148 wxDateEvent
event(datePicker
, dt
, wxEVT_DATE_CHANGED
);
149 datePicker
->GetEventHandler()->ProcessEvent(event
);
154 void OnCalKey(wxKeyEvent
& ev
)
156 if (ev
.GetKeyCode() == WXK_ESCAPE
&& !ev
.HasModifiers())
162 void OnSelChange(wxCalendarEvent
&ev
)
164 m_combo
->SetText(GetDate().Format(m_format
));
166 if ( ev
.GetEventType() == wxEVT_CALENDAR_DOUBLECLICKED
)
171 SendDateEvent(GetDate());
174 void OnKillTextFocus(wxFocusEvent
&ev
)
178 const wxDateTime
& dtOld
= GetDate();
181 wxString value
= m_combo
->GetValue();
182 if ( !ParseDateTime(value
, &dt
) )
184 if ( !HasDPFlag(wxDP_ALLOWNONE
) )
188 m_combo
->SetText(GetStringValueFor(dt
));
190 if ( !dt
.IsValid() && HasDPFlag(wxDP_ALLOWNONE
) )
193 // notify that we had to change the date after validation
194 if ( (dt
.IsValid() && (!dtOld
.IsValid() || dt
!= dtOld
)) ||
195 (!dt
.IsValid() && dtOld
.IsValid()) )
202 bool HasDPFlag(int flag
) const
204 return m_combo
->GetParent()->HasFlag(flag
);
207 // Return the format to be used for the dates shown by the control. This
208 // functions honours wxDP_SHOWCENTURY flag.
209 wxString
GetLocaleDateFormat() const
211 wxString fmt
= wxLocale::GetInfo(wxLOCALE_SHORT_DATE_FMT
);
212 if ( HasDPFlag(wxDP_SHOWCENTURY
) )
213 fmt
.Replace("%y", "%Y");
218 bool SetFormat(const wxString
& fmt
)
224 wxArrayString allowedChars
;
225 for ( wxChar c
= wxT('0'); c
<= wxT('9'); c
++ )
226 allowedChars
.Add(wxString(c
, 1));
228 const wxChar
*p2
= m_format
.c_str();
234 allowedChars
.Add(wxString(*p2
++, 1));
238 wxTextValidator
tv(wxFILTER_INCLUDE_CHAR_LIST
);
239 tv
.SetIncludes(allowedChars
);
240 m_combo
->SetValidator(tv
);
243 if ( GetDate().IsValid() )
244 m_combo
->SetText(GetDate().Format(m_format
));
250 virtual void SetStringValue(const wxString
& s
)
253 if ( !s
.empty() && ParseDateTime(s
, &dt
) )
255 //else: keep the old value
258 virtual wxString
GetStringValue() const
260 return GetStringValueFor(GetDate());
264 // returns either the given date representation using the current format or
265 // an empty string if it's invalid
266 wxString
GetStringValueFor(const wxDateTime
& dt
) const
270 val
= dt
.Format(m_format
);
278 DECLARE_EVENT_TABLE()
282 BEGIN_EVENT_TABLE(wxCalendarComboPopup
, wxCalendarCtrl
)
283 EVT_KEY_DOWN(wxCalendarComboPopup::OnCalKey
)
284 EVT_CALENDAR_SEL_CHANGED(wxID_ANY
, wxCalendarComboPopup::OnSelChange
)
285 EVT_CALENDAR_PAGE_CHANGED(wxID_ANY
, wxCalendarComboPopup::OnSelChange
)
286 EVT_CALENDAR(wxID_ANY
, wxCalendarComboPopup::OnSelChange
)
290 // ============================================================================
291 // wxDatePickerCtrlGeneric implementation
292 // ============================================================================
294 BEGIN_EVENT_TABLE(wxDatePickerCtrlGeneric
, wxDatePickerCtrlBase
)
295 EVT_TEXT(wxID_ANY
, wxDatePickerCtrlGeneric::OnText
)
296 EVT_SIZE(wxDatePickerCtrlGeneric::OnSize
)
297 EVT_SET_FOCUS(wxDatePickerCtrlGeneric::OnFocus
)
300 #ifndef wxHAS_NATIVE_DATEPICKCTRL
301 IMPLEMENT_DYNAMIC_CLASS(wxDatePickerCtrl
, wxControl
)
304 // ----------------------------------------------------------------------------
306 // ----------------------------------------------------------------------------
308 bool wxDatePickerCtrlGeneric::Create(wxWindow
*parent
,
310 const wxDateTime
& date
,
314 const wxValidator
& validator
,
315 const wxString
& name
)
317 wxASSERT_MSG( !(style
& wxDP_SPIN
),
318 wxT("wxDP_SPIN style not supported, use wxDP_DEFAULT") );
320 if ( !wxControl::Create(parent
, id
, pos
, size
,
321 style
| wxCLIP_CHILDREN
| wxWANTS_CHARS
| wxBORDER_NONE
,
329 m_combo
= new wxComboCtrl(this, -1, wxEmptyString
,
330 wxDefaultPosition
, wxDefaultSize
);
332 m_combo
->SetCtrlMainWnd(this);
334 m_popup
= new wxCalendarComboPopup();
336 #if defined(__WXMSW__)
337 // without this keyboard navigation in month control doesn't work
338 m_combo
->UseAltPopupWindow();
340 m_combo
->SetPopupControl(m_popup
);
342 m_popup
->SetDateValue(date
.IsValid() ? date
: wxDateTime::Today());
344 SetInitialSize(size
);
350 void wxDatePickerCtrlGeneric::Init()
356 wxDatePickerCtrlGeneric::~wxDatePickerCtrlGeneric()
360 bool wxDatePickerCtrlGeneric::Destroy()
368 return wxControl::Destroy();
371 // ----------------------------------------------------------------------------
372 // overridden base class methods
373 // ----------------------------------------------------------------------------
375 wxSize
wxDatePickerCtrlGeneric::DoGetBestSize() const
377 return m_combo
->GetBestSize();
380 // ----------------------------------------------------------------------------
381 // wxDatePickerCtrlGeneric API
382 // ----------------------------------------------------------------------------
385 wxDatePickerCtrlGeneric::SetDateRange(const wxDateTime
& lowerdate
,
386 const wxDateTime
& upperdate
)
388 return m_popup
->SetDateRange(lowerdate
, upperdate
);
392 wxDateTime
wxDatePickerCtrlGeneric::GetValue() const
394 if ( HasFlag(wxDP_ALLOWNONE
) && m_popup
->IsTextEmpty() )
395 return wxInvalidDateTime
;
396 return m_popup
->GetDate();
400 void wxDatePickerCtrlGeneric::SetValue(const wxDateTime
& date
)
402 m_popup
->SetDateValue(date
);
406 bool wxDatePickerCtrlGeneric::GetRange(wxDateTime
*dt1
, wxDateTime
*dt2
) const
408 return m_popup
->GetDateRange(dt1
, dt2
);
413 wxDatePickerCtrlGeneric::SetRange(const wxDateTime
&dt1
, const wxDateTime
&dt2
)
415 m_popup
->SetDateRange(dt1
, dt2
);
418 wxCalendarCtrl
*wxDatePickerCtrlGeneric::GetCalendar() const
423 // ----------------------------------------------------------------------------
425 // ----------------------------------------------------------------------------
428 void wxDatePickerCtrlGeneric::OnSize(wxSizeEvent
& event
)
431 m_combo
->SetSize(GetClientSize());
437 void wxDatePickerCtrlGeneric::OnText(wxCommandEvent
&ev
)
439 ev
.SetEventObject(this);
441 GetParent()->GetEventHandler()->ProcessEvent(ev
);
443 // We'll create an additional event if the date is valid.
444 // If the date isn't valid, the user's probably in the middle of typing
446 if ( !m_popup
|| !m_popup
->ParseDateTime(m_combo
->GetValue(), &dt
) )
449 m_popup
->SendDateEvent(dt
);
453 void wxDatePickerCtrlGeneric::OnFocus(wxFocusEvent
& WXUNUSED(event
))
459 #endif // wxUSE_DATEPICKCTRL