Replace dynamic casts with virtual wxWindow::WXGetTextEntry()

Instead of checking for all text-like controls one by one in
wxCommandEvent::GetString(), call a virtual function checking for this.

This is simpler, less error-prone and faster -- at the cost of
increasing the vtbl size of all wxWindow-derived classes.

Closes https://github.com/wxWidgets/wxWidgets/pull/1696
This commit is contained in:
Vadim Zeitlin
2020-01-07 03:47:23 +01:00
parent 20ecab6268
commit 52ae67ef86
11 changed files with 33 additions and 27 deletions

View File

@@ -128,6 +128,8 @@ public:
static wxVisualAttributes static wxVisualAttributes
GetClassDefaultAttributes(wxWindowVariant variant = wxWINDOW_VARIANT_NORMAL); GetClassDefaultAttributes(wxWindowVariant variant = wxWINDOW_VARIANT_NORMAL);
virtual const wxTextEntry* WXGetTextEntry() const wxOVERRIDE { return this; }
protected: protected:
// From wxWindowGTK: // From wxWindowGTK:
virtual GdkWindow *GTKGetWindow(wxArrayGdkWindows& windows) const wxOVERRIDE; virtual GdkWindow *GTKGetWindow(wxArrayGdkWindows& windows) const wxOVERRIDE;

View File

@@ -157,6 +157,8 @@ public:
static wxVisualAttributes static wxVisualAttributes
GetClassDefaultAttributes(wxWindowVariant variant = wxWINDOW_VARIANT_NORMAL); GetClassDefaultAttributes(wxWindowVariant variant = wxWINDOW_VARIANT_NORMAL);
virtual const wxTextEntry* WXGetTextEntry() const { return this; }
protected: protected:
virtual int DoInsertItems(const wxArrayStringsAdapter& items, virtual int DoInsertItems(const wxArrayStringsAdapter& items,
unsigned int pos, unsigned int pos,

View File

@@ -108,6 +108,8 @@ public:
virtual void Popup() { wxFAIL_MSG( wxT("Not implemented") ); } virtual void Popup() { wxFAIL_MSG( wxT("Not implemented") ); }
virtual void Dismiss() { wxFAIL_MSG( wxT("Not implemented") ); } virtual void Dismiss() { wxFAIL_MSG( wxT("Not implemented") ); }
virtual const wxTextEntry* WXGetTextEntry() const { return this; }
protected: protected:
virtual wxSize DoGetBestSize() const; virtual wxSize DoGetBestSize() const;
virtual void DoSetSize(int x, int y, virtual void DoSetSize(int x, int y,

View File

@@ -128,6 +128,8 @@ public:
virtual void SetLayoutDirection(wxLayoutDirection dir) wxOVERRIDE; virtual void SetLayoutDirection(wxLayoutDirection dir) wxOVERRIDE;
virtual const wxTextEntry* WXGetTextEntry() const wxOVERRIDE { return this; }
protected: protected:
#if wxUSE_TOOLTIPS #if wxUSE_TOOLTIPS
virtual void DoSetToolTip(wxToolTip *tip) wxOVERRIDE; virtual void DoSetToolTip(wxToolTip *tip) wxOVERRIDE;

View File

@@ -102,6 +102,8 @@ class WXDLLIMPEXP_CORE wxComboBox :
#endif // wxOSX_USE_COCOA #endif // wxOSX_USE_COCOA
virtual const wxTextEntry* WXGetTextEntry() const wxOVERRIDE { return this; }
// osx specific event handling common for all osx-ports // osx specific event handling common for all osx-ports
virtual bool OSXHandleClicked(double timestampsec) wxOVERRIDE; virtual bool OSXHandleClicked(double timestampsec) wxOVERRIDE;

View File

@@ -80,6 +80,8 @@ public:
virtual void Popup(); virtual void Popup();
virtual void Dismiss(); virtual void Dismiss();
virtual const wxTextEntry* WXGetTextEntry() const wxOVERRIDE { return this; }
virtual bool QtHandleFocusEvent(QWidget *handler, QFocusEvent *event) wxOVERRIDE; virtual bool QtHandleFocusEvent(QWidget *handler, QFocusEvent *event) wxOVERRIDE;
protected: protected:

View File

@@ -82,6 +82,10 @@ public:
virtual void SetDescriptiveText(const wxString& text) = 0; virtual void SetDescriptiveText(const wxString& text) = 0;
virtual wxString GetDescriptiveText() const = 0; virtual wxString GetDescriptiveText() const = 0;
#if wxUSE_NATIVE_SEARCH_CONTROL
virtual const wxTextEntry* WXGetTextEntry() const wxOVERRIDE { return this; }
#endif // wxUSE_NATIVE_SEARCH_CONTROL
private: private:
// implement wxTextEntry pure virtual method // implement wxTextEntry pure virtual method
virtual wxWindow *GetEditableWindow() wxOVERRIDE { return this; } virtual wxWindow *GetEditableWindow() wxOVERRIDE { return this; }

View File

@@ -759,6 +759,8 @@ public:
return GetCompositeControlsDefaultAttributes(variant); return GetCompositeControlsDefaultAttributes(variant);
} }
virtual const wxTextEntry* WXGetTextEntry() const wxOVERRIDE { return this; }
protected: protected:
// Override wxEvtHandler method to check for a common problem of binding // Override wxEvtHandler method to check for a common problem of binding
// wxEVT_TEXT_ENTER to a control without wxTE_PROCESS_ENTER style, which is // wxEVT_TEXT_ENTER to a control without wxTE_PROCESS_ENTER style, which is

View File

@@ -161,6 +161,8 @@ public:
virtual wxClientDataType GetClientDataType() const wxOVERRIDE; virtual wxClientDataType GetClientDataType() const wxOVERRIDE;
virtual void SetClientDataType(wxClientDataType clientDataItemsType) wxOVERRIDE; virtual void SetClientDataType(wxClientDataType clientDataItemsType) wxOVERRIDE;
virtual const wxTextEntry* WXGetTextEntry() const wxOVERRIDE { return this; }
protected: protected:
virtual int DoInsertItems(const wxArrayStringsAdapter& items, virtual int DoInsertItems(const wxArrayStringsAdapter& items,
unsigned int pos, unsigned int pos,

View File

@@ -58,6 +58,7 @@ class WXDLLIMPEXP_FWD_CORE wxDC;
class WXDLLIMPEXP_FWD_CORE wxDropTarget; class WXDLLIMPEXP_FWD_CORE wxDropTarget;
class WXDLLIMPEXP_FWD_CORE wxLayoutConstraints; class WXDLLIMPEXP_FWD_CORE wxLayoutConstraints;
class WXDLLIMPEXP_FWD_CORE wxSizer; class WXDLLIMPEXP_FWD_CORE wxSizer;
class WXDLLIMPEXP_FWD_CORE wxTextEntry;
class WXDLLIMPEXP_FWD_CORE wxToolTip; class WXDLLIMPEXP_FWD_CORE wxToolTip;
class WXDLLIMPEXP_FWD_CORE wxWindowBase; class WXDLLIMPEXP_FWD_CORE wxWindowBase;
class WXDLLIMPEXP_FWD_CORE wxWindow; class WXDLLIMPEXP_FWD_CORE wxWindow;
@@ -1582,6 +1583,8 @@ public:
return false; return false;
} }
// This is an internal helper function implemented by text-like controls.
virtual const wxTextEntry* WXGetTextEntry() const { return NULL; }
protected: protected:
// helper for the derived class Create() methods: the first overload, with // helper for the derived class Create() methods: the first overload, with

View File

@@ -37,20 +37,14 @@
#if wxUSE_GUI #if wxUSE_GUI
#include "wx/window.h" #include "wx/window.h"
#include "wx/combobox.h"
#include "wx/control.h" #include "wx/control.h"
#include "wx/dc.h" #include "wx/dc.h"
#include "wx/srchctrl.h"
#include "wx/spinbutt.h" #include "wx/spinbutt.h"
#include "wx/textctrl.h" #include "wx/textentry.h"
#include "wx/validate.h" #include "wx/validate.h"
#endif // wxUSE_GUI #endif // wxUSE_GUI
#endif #endif
#if wxUSE_GUI
#include "wx/srchctrl.h"
#endif // wxUSE_GUI
#include "wx/thread.h" #include "wx/thread.h"
#if wxUSE_BASE #if wxUSE_BASE
@@ -448,28 +442,17 @@ wxString wxCommandEvent::GetString() const
{ {
// This is part of the hack retrieving the event string from the control // This is part of the hack retrieving the event string from the control
// itself only when/if it's really needed to avoid copying potentially huge // itself only when/if it's really needed to avoid copying potentially huge
// strings coming from multiline text controls. For consistency we also do // strings coming from multiline text controls.
// it for combo boxes, even though there are no real performance advantages
// in doing this for them.
if (m_eventType == wxEVT_TEXT && m_eventObject) if (m_eventType == wxEVT_TEXT && m_eventObject)
{ {
#if wxUSE_TEXTCTRL // Only windows generate wxEVT_TEXT events, so this cast should really
wxTextCtrl *txt = wxDynamicCast(m_eventObject, wxTextCtrl); // succeed, but err on the side of caution just in case somebody
if ( txt ) // created a bogus event of this type.
return txt->GetValue(); if ( wxWindow* const w = wxDynamicCast(m_eventObject, wxWindow) )
#endif // wxUSE_TEXTCTRL {
if ( const wxTextEntry* const entry = w->WXGetTextEntry() )
#if wxUSE_COMBOBOX return entry->GetValue();
wxComboBox* combo = wxDynamicCast(m_eventObject, wxComboBox); }
if ( combo )
return combo->GetValue();
#endif // wxUSE_COMBOBOX
#if wxUSE_SEARCHCTRL
wxSearchCtrl* search = wxDynamicCast(m_eventObject, wxSearchCtrl);
if ( search )
return search->GetValue();
#endif // wxUSE_SEARCHCTRL
} }
return m_cmdString; return m_cmdString;