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.
This commit is contained in:
Vadim Zeitlin
2020-11-04 22:59:11 +01:00
parent c2a488ca42
commit 8a64b6acea
4 changed files with 46 additions and 36 deletions

View File

@@ -13,11 +13,13 @@
#include "wx/containr.h"
#include "wx/compositewin.h"
typedef wxTimePickerCtrlCommonBase<wxDateTimePickerCtrlBase> wxTimePickerCtrlGenericBase;
class WXDLLIMPEXP_ADV wxTimePickerCtrlGeneric
: public wxCompositeWindow< wxNavigationEnabled<wxTimePickerCtrlBase> >
: public wxCompositeWindow< wxNavigationEnabled<wxTimePickerCtrlGenericBase> >
{
public:
typedef wxCompositeWindow< wxNavigationEnabled<wxTimePickerCtrlBase> > Base;
typedef wxCompositeWindow< wxNavigationEnabled<wxTimePickerCtrlGenericBase> > Base;
// Creating the control.
wxTimePickerCtrlGeneric() { Init(); }

View File

@@ -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

View File

@@ -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 <typename Base>
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<wxDateTimePickerCtrl> wxTimePickerCtrlBase;
#if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
#include "wx/msw/timectrl.h"

View File

@@ -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);