From 8a64b6acea861313e0f28b7224fdf20848fbbceb Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 4 Nov 2020 22:59:11 +0100 Subject: [PATCH] Fix inheritance hierarchy of wxTimePickerCtrlGeneric This class must not derive from the native wxDatePickerCtrl, as it doesn't make much sense and resulted in the need for an ugly hack with either overriding unused and inapplicable pure virtual methods in this class itself, as was originally done in 569c7d8ccb (Add wxTimePickerCtrl class., 2011-09-29) when it was introduced, or not making these methods pure virtual in the first place, as was done in d0da5061ce (Dirty hack to allow generic wxDatePickerCtrl to compile under MSW., 2011-10-20), but didn't really fix the problem. Do fix it now by using different hierarchies for the native and generic classes. The main disadvantage of doing it is that there is no common base class for wxTimePickerCtrl and wxTimePickerCtrlGeneric providing SetTime() and GetTime() methods any more, but this seems like a relatively small price to pay because real applications won't be using these two classes simultaneously, as the calendar sample does, anyhow. --- include/wx/generic/timectrl.h | 6 ++++-- include/wx/msw/datetimectrl.h | 28 +++------------------------- include/wx/timectrl.h | 13 ++++++++++--- samples/calendar/calendar.cpp | 35 +++++++++++++++++++++++++++++------ 4 files changed, 46 insertions(+), 36 deletions(-) diff --git a/include/wx/generic/timectrl.h b/include/wx/generic/timectrl.h index eea7daa2df..84f7a4f261 100644 --- a/include/wx/generic/timectrl.h +++ b/include/wx/generic/timectrl.h @@ -13,11 +13,13 @@ #include "wx/containr.h" #include "wx/compositewin.h" +typedef wxTimePickerCtrlCommonBase wxTimePickerCtrlGenericBase; + class WXDLLIMPEXP_ADV wxTimePickerCtrlGeneric - : public wxCompositeWindow< wxNavigationEnabled > + : public wxCompositeWindow< wxNavigationEnabled > { public: - typedef wxCompositeWindow< wxNavigationEnabled > Base; + typedef wxCompositeWindow< wxNavigationEnabled > Base; // Creating the control. wxTimePickerCtrlGeneric() { Init(); } diff --git a/include/wx/msw/datetimectrl.h b/include/wx/msw/datetimectrl.h index 97afc1f40c..cc6eab89b7 100644 --- a/include/wx/msw/datetimectrl.h +++ b/include/wx/msw/datetimectrl.h @@ -46,39 +46,17 @@ protected: const wxValidator& validator, const wxString& name); - // Notice that the methods below must be overridden in all native MSW - // classes inheriting from this one but they can't be pure virtual because - // the generic implementations, not needing nor able to implement them, is - // also derived from this class currently. The real problem is, of course, - // this wrong class structure because the generic classes also inherit the - // wrong implementations of Set/GetValue() and DoGetBestSize() but as they - // override these methods anyhow, it does work -- but is definitely ugly - // and need to be changed (but how?) in the future. - #if wxUSE_INTL // Override to return the date/time format used by this control. - virtual wxLocaleInfo MSWGetFormat() const /* = 0 */ - { - wxFAIL_MSG( "Unreachable" ); - return wxLOCALE_TIME_FMT; - } + virtual wxLocaleInfo MSWGetFormat() const = 0; #endif // wxUSE_INTL // Override to indicate whether we can have no date at all. - virtual bool MSWAllowsNone() const /* = 0 */ - { - wxFAIL_MSG( "Unreachable" ); - return false; - } + virtual bool MSWAllowsNone() const = 0; // Override to update m_date and send the event when the control contents // changes, return true if the event was handled. - virtual bool MSWOnDateTimeChange(const tagNMDATETIMECHANGE& dtch) /* = 0 */ - { - wxUnusedVar(dtch); - wxFAIL_MSG( "Unreachable" ); - return false; - } + virtual bool MSWOnDateTimeChange(const tagNMDATETIMECHANGE& dtch) = 0; // the date currently shown by the control, may be invalid diff --git a/include/wx/timectrl.h b/include/wx/timectrl.h index 928d9aedf3..4e4a7a3758 100644 --- a/include/wx/timectrl.h +++ b/include/wx/timectrl.h @@ -29,7 +29,10 @@ enum // wxTimePickerCtrl: Allow the user to enter the time. // ---------------------------------------------------------------------------- -class WXDLLIMPEXP_ADV wxTimePickerCtrlBase : public wxDateTimePickerCtrl +// The template argument must be a class deriving from wxDateTimePickerCtrlBase +// (i.e. in practice either this class itself or wxDateTimePickerCtrl). +template +class wxTimePickerCtrlCommonBase : public Base { public: /* @@ -67,7 +70,7 @@ public: return false; } - SetValue(dt); + this->SetValue(dt); return true; } @@ -78,7 +81,7 @@ public: wxCHECK_MSG( hour && min && sec, false, wxS("Time component pointers must be non-NULL") ); - const wxDateTime::Tm tm = GetValue().GetTm(); + const wxDateTime::Tm tm = this->GetValue().GetTm(); *hour = tm.hour; *min = tm.min; *sec = tm.sec; @@ -87,6 +90,10 @@ public: } }; +// This class is defined mostly for compatibility and is used as the base class +// by native wxTimePickerCtrl implementations. +typedef wxTimePickerCtrlCommonBase wxTimePickerCtrlBase; + #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__) #include "wx/msw/timectrl.h" diff --git a/samples/calendar/calendar.cpp b/samples/calendar/calendar.cpp index df95946a7d..28964de1f0 100644 --- a/samples/calendar/calendar.cpp +++ b/samples/calendar/calendar.cpp @@ -218,12 +218,24 @@ class MyTimeDialog : public wxDialog public: MyTimeDialog(wxWindow* parent); - wxDateTime GetTime() const { return m_timePicker->GetValue(); } + wxDateTime GetTime() const + { +#if wxUSE_TIMEPICKCTRL_GENERIC + if ( m_timePickerGeneric ) + return m_timePickerGeneric->GetValue(); +#endif // wxUSE_TIMEPICKCTRL_GENERIC + + return m_timePicker->GetValue(); + } private: void OnTimeChange(wxDateEvent& event); - wxTimePickerCtrlBase* m_timePicker; + wxTimePickerCtrl* m_timePicker; +#if wxUSE_TIMEPICKCTRL_GENERIC + wxTimePickerCtrlGeneric* m_timePickerGeneric; +#endif // wxUSE_TIMEPICKCTRL_GENERIC + wxStaticText* m_timeText; wxDECLARE_EVENT_TABLE(); @@ -987,20 +999,31 @@ wxEND_EVENT_TABLE() MyTimeDialog::MyTimeDialog(wxWindow *parent) : wxDialog(parent, wxID_ANY, wxString("Calendar: Choose time")) { + wxWindow* timePickerWindow = NULL; + #if wxUSE_TIMEPICKCTRL_GENERIC + m_timePickerGeneric = NULL; + m_timePicker = NULL; + wxFrame *frame = (wxFrame *)wxGetTopLevelParent(parent); if ( frame && frame->GetMenuBar()->IsChecked(Calendar_TimePicker_Generic) ) - m_timePicker = new wxTimePickerCtrlGeneric(this, wxID_ANY); + { + m_timePickerGeneric = new wxTimePickerCtrlGeneric(this, wxID_ANY); + timePickerWindow = m_timePickerGeneric; + } else #endif // wxUSE_TIMEPICKCTRL_GENERIC m_timePicker = new wxTimePickerCtrl(this, wxID_ANY); - m_timeText = new wxStaticText(this, wxID_ANY, - m_timePicker->GetValue().FormatISOTime()); + + if ( !timePickerWindow ) + timePickerWindow = m_timePicker; + + m_timeText = new wxStaticText(this, wxID_ANY, GetTime().FormatISOTime()); const wxSizerFlags flags = wxSizerFlags().Centre().Border(); wxFlexGridSizer* const sizerMain = new wxFlexGridSizer(2); sizerMain->Add(new wxStaticText(this, wxID_ANY, "Enter &time:"), flags); - sizerMain->Add(m_timePicker, flags); + sizerMain->Add(timePickerWindow, flags); sizerMain->Add(new wxStaticText(this, wxID_ANY, "Time in ISO format:"), flags);