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
) )
79 SetFormat(GetLocaleDateFormat());
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
) const
197 return m_combo
->GetParent()->HasFlag(flag
);
200 // it expands "%x" format and changes %y to %Y if wxDP_SHOWCENTURY flag
201 // is given. If the locale format can't be easily analyzed (e.g. when
202 // the month is given as a name, not number), "%x" is returned
203 wxString
GetLocaleDateFormat() const
205 wxString
x_format(wxT("%x"));
207 int year_cnt
= 0, month_cnt
= 0, day_cnt
= 0;
210 dt
.ParseFormat(wxT("2003-10-17"), wxT("%Y-%m-%d"));
211 wxString
str(dt
.Format(x_format
));
213 const wxChar
*p
= str
.c_str();
219 if (n
== dt
.GetDay())
221 fmt
.Append(wxT("%d"));
225 else if (n
== (int)dt
.GetMonth()+1)
227 fmt
.Append(wxT("%m"));
231 else if (n
== dt
.GetYear())
233 fmt
.Append(wxT("%Y"));
237 else if (n
== (dt
.GetYear() % 100))
239 if ( HasDPFlag(wxDP_SHOWCENTURY
) )
240 fmt
.Append(wxT("%Y"));
242 fmt
.Append(wxT("%y"));
247 // this shouldn't happen
256 if (year_cnt
== 1 && month_cnt
== 1 && day_cnt
== 1)
262 bool SetFormat(const wxString
& fmt
)
268 wxArrayString allowedChars
;
269 for ( wxChar c
= _T('0'); c
<= _T('9'); c
++ )
270 allowedChars
.Add(wxString(c
, 1));
272 const wxChar
*p2
= m_format
.c_str();
278 allowedChars
.Add(wxString(*p2
++, 1));
282 wxTextValidator
tv(wxFILTER_INCLUDE_CHAR_LIST
);
283 tv
.SetIncludes(allowedChars
);
284 m_combo
->SetValidator(tv
);
287 if ( GetDate().IsValid() )
288 m_combo
->SetText(GetDate().Format(m_format
));
294 virtual void SetStringValue(const wxString
& s
)
297 if ( !s
.empty() && ParseDateTime(s
, &dt
) )
299 //else: keep the old value
302 virtual wxString
GetStringValue() const
304 return GetStringValueFor(GetDate());
308 // returns either the given date representation using the current format or
309 // an empty string if it's invalid
310 wxString
GetStringValueFor(const wxDateTime
& dt
) const
314 val
= dt
.Format(m_format
);
322 DECLARE_EVENT_TABLE()
326 BEGIN_EVENT_TABLE(wxCalendarComboPopup
, wxCalendarCtrl
)
327 EVT_KEY_DOWN(wxCalendarComboPopup::OnCalKey
)
328 EVT_CALENDAR_SEL_CHANGED(wxID_ANY
, wxCalendarComboPopup::OnSelChange
)
329 EVT_CALENDAR_PAGE_CHANGED(wxID_ANY
, wxCalendarComboPopup::OnSelChange
)
330 EVT_CALENDAR(wxID_ANY
, wxCalendarComboPopup::OnSelChange
)
334 // ============================================================================
335 // wxDatePickerCtrlGeneric implementation
336 // ============================================================================
338 BEGIN_EVENT_TABLE(wxDatePickerCtrlGeneric
, wxDatePickerCtrlBase
)
339 EVT_TEXT(wxID_ANY
, wxDatePickerCtrlGeneric::OnText
)
340 EVT_SIZE(wxDatePickerCtrlGeneric::OnSize
)
341 EVT_SET_FOCUS(wxDatePickerCtrlGeneric::OnFocus
)
344 #ifndef wxHAS_NATIVE_DATEPICKCTRL
345 IMPLEMENT_DYNAMIC_CLASS(wxDatePickerCtrl
, wxControl
)
348 // ----------------------------------------------------------------------------
350 // ----------------------------------------------------------------------------
352 bool wxDatePickerCtrlGeneric::Create(wxWindow
*parent
,
354 const wxDateTime
& date
,
358 const wxValidator
& validator
,
359 const wxString
& name
)
361 wxASSERT_MSG( !(style
& wxDP_SPIN
),
362 _T("wxDP_SPIN style not supported, use wxDP_DEFAULT") );
364 if ( !wxControl::Create(parent
, id
, pos
, size
,
365 style
| wxCLIP_CHILDREN
| wxWANTS_CHARS
| wxBORDER_NONE
,
373 m_combo
= new wxComboCtrl(this, -1, wxEmptyString
,
374 wxDefaultPosition
, wxDefaultSize
);
376 m_combo
->SetCtrlMainWnd(this);
378 m_popup
= new wxCalendarComboPopup();
380 #if defined(__WXMSW__)
381 // without this keyboard navigation in month control doesn't work
382 m_combo
->UseAltPopupWindow();
384 m_combo
->SetPopupControl(m_popup
);
386 m_popup
->SetDateValue(date
.IsValid() ? date
: wxDateTime::Today());
388 SetInitialSize(size
);
394 void wxDatePickerCtrlGeneric::Init()
400 wxDatePickerCtrlGeneric::~wxDatePickerCtrlGeneric()
404 bool wxDatePickerCtrlGeneric::Destroy()
412 return wxControl::Destroy();
415 // ----------------------------------------------------------------------------
416 // overridden base class methods
417 // ----------------------------------------------------------------------------
419 wxSize
wxDatePickerCtrlGeneric::DoGetBestSize() const
421 return m_combo
->GetBestSize();
424 // ----------------------------------------------------------------------------
425 // wxDatePickerCtrlGeneric API
426 // ----------------------------------------------------------------------------
429 wxDatePickerCtrlGeneric::SetDateRange(const wxDateTime
& lowerdate
,
430 const wxDateTime
& upperdate
)
432 return m_popup
->SetDateRange(lowerdate
, upperdate
);
436 wxDateTime
wxDatePickerCtrlGeneric::GetValue() const
438 return m_popup
->GetDate();
442 void wxDatePickerCtrlGeneric::SetValue(const wxDateTime
& date
)
444 m_popup
->SetDateValue(date
);
448 bool wxDatePickerCtrlGeneric::GetRange(wxDateTime
*dt1
, wxDateTime
*dt2
) const
450 return m_popup
->GetDateRange(dt1
, dt2
);
455 wxDatePickerCtrlGeneric::SetRange(const wxDateTime
&dt1
, const wxDateTime
&dt2
)
457 m_popup
->SetDateRange(dt1
, dt2
);
460 wxCalendarCtrl
*wxDatePickerCtrlGeneric::GetCalendar() const
465 // ----------------------------------------------------------------------------
467 // ----------------------------------------------------------------------------
470 void wxDatePickerCtrlGeneric::OnSize(wxSizeEvent
& event
)
473 m_combo
->SetSize(GetClientSize());
479 void wxDatePickerCtrlGeneric::OnText(wxCommandEvent
&ev
)
481 ev
.SetEventObject(this);
483 GetParent()->GetEventHandler()->ProcessEvent(ev
);
485 // We'll create an additional event if the date is valid.
486 // If the date isn't valid, the user's probably in the middle of typing
488 if ( !m_popup
|| !m_popup
->ParseDateTime(m_combo
->GetValue(), &dt
) )
491 m_popup
->SendDateEvent(dt
);
495 void wxDatePickerCtrlGeneric::OnFocus(wxFocusEvent
& WXUNUSED(event
))
501 #endif // wxUSE_DATEPICKCTRL