From d319e70a7cd81c46a8d61e35670a2efbba2aa804 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 13 Nov 2020 01:19:52 +0100 Subject: [PATCH] Fix inheritance hierarchy of wxDatePickerCtrlGeneric too This is very similar to 8a64b6acea (Fix inheritance hierarchy of wxTimePickerCtrlGeneric, 2020-11-04) and is done for the same reasons (avoid having to somehow implement MSW-specific virtual methods of the native controls base class in the generic version) and suffers from the same drawback (there is no common base class for the native and generic version any more). See https://github.com/wxWidgets/wxWidgets/pull/2109 --- include/wx/datectrl.h | 9 +++++- include/wx/generic/datectrl.h | 4 ++- samples/calendar/calendar.cpp | 52 +++++++++++++++++++++++++++-------- 3 files changed, 51 insertions(+), 14 deletions(-) diff --git a/include/wx/datectrl.h b/include/wx/datectrl.h index 630f72b16c..2e5b806e0b 100644 --- a/include/wx/datectrl.h +++ b/include/wx/datectrl.h @@ -44,7 +44,10 @@ enum // wxDatePickerCtrl: allow the user to enter the date // ---------------------------------------------------------------------------- -class WXDLLIMPEXP_ADV wxDatePickerCtrlBase : public wxDateTimePickerCtrl +// The template argument must be a class deriving from wxDateTimePickerCtrlBase +// (i.e. in practice either this class itself or wxDateTimePickerCtrl). +template +class WXDLLIMPEXP_ADV wxDatePickerCtrlCommonBase : public Base { public: /* @@ -75,6 +78,10 @@ public: virtual bool GetRange(wxDateTime *dt1, wxDateTime *dt2) const = 0; }; +// This class is defined mostly for compatibility and is used as the base class +// by native wxDatePickerCtrl implementations. +typedef wxDatePickerCtrlCommonBase wxDatePickerCtrlBase; + #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__) #include "wx/msw/datectrl.h" diff --git a/include/wx/generic/datectrl.h b/include/wx/generic/datectrl.h index b4ce4b9bb6..3f67385a39 100644 --- a/include/wx/generic/datectrl.h +++ b/include/wx/generic/datectrl.h @@ -19,8 +19,10 @@ class WXDLLIMPEXP_FWD_CORE wxComboCtrl; class WXDLLIMPEXP_FWD_CORE wxCalendarCtrl; class WXDLLIMPEXP_FWD_CORE wxCalendarComboPopup; +typedef wxDatePickerCtrlCommonBase wxDatePickerCtrlGenericBase; + class WXDLLIMPEXP_CORE wxDatePickerCtrlGeneric - : public wxCompositeWindow< wxNavigationEnabled > + : public wxCompositeWindow< wxNavigationEnabled > { public: // creating the control diff --git a/samples/calendar/calendar.cpp b/samples/calendar/calendar.cpp index 28964de1f0..b7ef65f823 100644 --- a/samples/calendar/calendar.cpp +++ b/samples/calendar/calendar.cpp @@ -195,13 +195,25 @@ class MyDateDialog : public wxDialog public: MyDateDialog(wxWindow *parent, const wxDateTime& dt, int dtpStyle); - wxDateTime GetDate() const { return m_datePicker->GetValue(); } + wxDateTime GetDate() const + { +#if wxUSE_DATEPICKCTRL_GENERIC + if ( m_datePickerGeneric ) + return m_datePickerGeneric->GetValue(); +#endif // wxUSE_DATEPICKCTRL_GENERIC + + return m_datePicker->GetValue(); + } private: void OnDateChange(wxDateEvent& event); - wxDatePickerCtrlBase *m_datePicker; + wxDatePickerCtrl *m_datePicker; +#if wxUSE_DATEPICKCTRL_GENERIC + wxDatePickerCtrlGeneric *m_datePickerGeneric; +#endif // wxUSE_DATEPICKCTRL_GENERIC + wxStaticText *m_dateText; @@ -941,20 +953,36 @@ wxEND_EVENT_TABLE() MyDateDialog::MyDateDialog(wxWindow *parent, const wxDateTime& dt, int dtpStyle) : wxDialog(parent, wxID_ANY, wxString("Calendar: Choose a date")) { + wxWindow* datePickerWindow = NULL; + #if wxUSE_DATEPICKCTRL_GENERIC + m_datePickerGeneric = NULL; + m_datePicker = NULL; + wxFrame *frame = (wxFrame *)wxGetTopLevelParent(parent); if ( frame && frame->GetMenuBar()->IsChecked(Calendar_DatePicker_Generic) ) - m_datePicker = new wxDatePickerCtrlGeneric(this, wxID_ANY, dt, - wxDefaultPosition, - wxDefaultSize, - dtpStyle); + { + m_datePickerGeneric = new wxDatePickerCtrlGeneric(this, wxID_ANY, dt, + wxDefaultPosition, + wxDefaultSize, + dtpStyle); + m_datePickerGeneric->SetRange(wxDateTime(1, wxDateTime::Jan, 1900), + wxDefaultDateTime); + + datePickerWindow = m_datePickerGeneric; + } else #endif // wxUSE_DATEPICKCTRL_GENERIC - m_datePicker = new wxDatePickerCtrl(this, wxID_ANY, dt, - wxDefaultPosition, wxDefaultSize, - dtpStyle); - m_datePicker->SetRange(wxDateTime(1, wxDateTime::Jan, 1900), - wxDefaultDateTime); + { + m_datePicker = new wxDatePickerCtrl(this, wxID_ANY, dt, + wxDefaultPosition, wxDefaultSize, + dtpStyle); + m_datePicker->SetRange(wxDateTime(1, wxDateTime::Jan, 1900), + wxDefaultDateTime); + + datePickerWindow = m_datePicker; + } + m_dateText = new wxStaticText(this, wxID_ANY, dt.IsValid() ? dt.FormatISODate() : wxString()); @@ -962,7 +990,7 @@ MyDateDialog::MyDateDialog(wxWindow *parent, const wxDateTime& dt, int dtpStyle) const wxSizerFlags flags = wxSizerFlags().Centre().Border(); wxFlexGridSizer* const sizerMain = new wxFlexGridSizer(2); sizerMain->Add(new wxStaticText(this, wxID_ANY, "Enter &date:"), flags); - sizerMain->Add(m_datePicker, flags); + sizerMain->Add(datePickerWindow, flags); sizerMain->Add(new wxStaticText(this, wxID_ANY, "Date in ISO format:"), flags);