From fda62793585ef3ebaa8823be7c1a3e3538e5349a Mon Sep 17 00:00:00 2001 From: Jaakko Salli Date: Thu, 16 Dec 2010 17:21:49 +0000 Subject: [PATCH] Instead of having wxComboCtrl mimic wxTextEntry interface, make it actually inherit from the class and implement functions to redirect to the embedded wxTextCtrl. This allows us to simplify the code and get rid of the dirty trick of directing wxComboCtrl's validator to the embedded wxTextCtrl. Also see #12779, which issue 1 is fixed by this change. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@66385 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/combo.h | 57 +++++++++++++++-------------- include/wx/generic/combo.h | 10 ++++++ include/wx/msw/combo.h | 3 ++ interface/wx/combo.h | 3 +- src/common/combocmn.cpp | 89 +++++++++++++++++++++++++++++++--------------- src/common/valtext.cpp | 12 ++++++- src/generic/combog.cpp | 18 ++-------- src/msw/combo.cpp | 4 +-- 8 files changed, 121 insertions(+), 75 deletions(-) diff --git a/include/wx/combo.h b/include/wx/combo.h index 6a190e52..f0c06eb 100644 --- a/include/wx/combo.h +++ b/include/wx/combo.h @@ -45,6 +45,7 @@ #include "wx/control.h" #include "wx/renderer.h" // this is needed for wxCONTROL_XXX flags #include "wx/bitmap.h" // wxBitmap used by-value +#include "wx/textentry.h" class WXDLLIMPEXP_FWD_CORE wxTextCtrl; class WXDLLIMPEXP_FWD_CORE wxComboPopup; @@ -139,13 +140,14 @@ struct wxComboCtrlFeatures }; -class WXDLLIMPEXP_CORE wxComboCtrlBase : public wxControl +class WXDLLIMPEXP_CORE wxComboCtrlBase : public wxControl, + public wxTextEntry { friend class wxComboPopup; friend class wxComboPopupEvtHandler; public: // ctors and such - wxComboCtrlBase() : wxControl() { Init(); } + wxComboCtrlBase() : wxControl(), wxTextEntry() { Init(); } bool Create(wxWindow *parent, wxWindowID id, @@ -196,26 +198,38 @@ public: virtual bool Enable(bool enable = true); virtual bool Show(bool show = true); virtual bool SetFont(const wxFont& font); -#if wxUSE_VALIDATORS - virtual void SetValidator(const wxValidator &validator); - virtual wxValidator *GetValidator(); -#endif // wxUSE_VALIDATORS // wxTextCtrl methods - for readonly combo they should return // without errors. - virtual wxString GetValue() const; virtual void SetValue(const wxString& value); + + // wxTextEntry methods - we basically need to override all of them + virtual void WriteText(const wxString& text); + + virtual void Replace(long from, long to, const wxString& value); + virtual void Remove(long from, long to); + virtual void Copy(); virtual void Cut(); virtual void Paste(); + + virtual void Undo(); + virtual void Redo(); + virtual bool CanUndo() const; + virtual bool CanRedo() const; + virtual void SetInsertionPoint(long pos); - virtual void SetInsertionPointEnd(); virtual long GetInsertionPoint() const; virtual long GetLastPosition() const; - virtual void Replace(long from, long to, const wxString& value); - virtual void Remove(long from, long to); + virtual void SetSelection(long from, long to); - virtual void Undo(); + virtual void GetSelection(long *from, long *to) const; + + virtual bool IsEditable() const; + virtual void SetEditable(bool editable); + + virtual bool SetHint(const wxString& hint); + virtual wxString GetHint() const; // This method sets the text without affecting list selection // (ie. wxComboPopup::SetStringValue doesn't get called). @@ -387,21 +401,6 @@ public: const wxBitmap& GetBitmapHover() const { return m_bmpHover; } const wxBitmap& GetBitmapDisabled() const { return m_bmpDisabled; } - // Hint functions mirrored from TextEntryBase - virtual bool SetHint(const wxString& hint); - virtual wxString GetHint() const; - - // Margins functions mirrored from TextEntryBase - // (wxComboCtrl does not inherit from wxTextEntry, but may embed a - // wxTextCtrl, so we need these). Also note that these functions - // have replaced SetTextIndent() in wxWidgets 2.9.1 and later. - bool SetMargins(const wxPoint& pt) - { return DoSetMargins(pt); } - bool SetMargins(wxCoord left, wxCoord top = -1) - { return DoSetMargins(wxPoint(left, top)); } - wxPoint GetMargins() const - { return DoGetMargins(); } - // Set custom style flags for embedded wxTextCtrl. Usually must be used // with two-step creation, before Create() call. void SetTextCtrlStyle( int style ); @@ -463,7 +462,7 @@ protected: // Creates wxTextCtrl. // extraStyle: Extra style parameters - void CreateTextCtrl( int extraStyle, const wxValidator& validator ); + void CreateTextCtrl( int extraStyle ); // Installs standard input handler to combo (and optionally to the textctrl) void InstallInputHandlers(); @@ -558,6 +557,10 @@ protected: virtual void DoSetToolTip( wxToolTip *tip ); #endif + // protected wxTextEntry methods + virtual wxString DoGetValue() const; + virtual wxWindow *GetEditableWindow() { return this; } + // margins functions virtual bool DoSetMargins(const wxPoint& pt); virtual wxPoint DoGetMargins() const; diff --git a/include/wx/generic/combo.h b/include/wx/generic/combo.h index 703e42f..108c8cf 100644 --- a/include/wx/generic/combo.h +++ b/include/wx/generic/combo.h @@ -80,6 +80,16 @@ public: protected: + // Dummies for platform-specific wxTextEntry implementations +#if defined(__WXGTK__) + virtual GtkEditable *GetEditable() const { return NULL; } + virtual GtkEntry *GetEntry() const { return NULL; } +#elif defined(__WXMAC__) + // Looks like there's nothing we need to override here +#elif defined(__WXPM__) + virtual WXHWND GetEditHWND() const { return NULL; } +#endif + // Mandatory virtuals virtual void OnResize(); diff --git a/include/wx/msw/combo.h b/include/wx/msw/combo.h index be85dbc..c5b2768 100644 --- a/include/wx/msw/combo.h +++ b/include/wx/msw/combo.h @@ -83,6 +83,9 @@ protected: protected: + // Dummy method - we override all functions that call this + virtual WXHWND GetEditHWND() const { return NULL; } + // customization virtual void OnResize(); virtual wxCoord GetNativeTextIndent() const; diff --git a/interface/wx/combo.h b/interface/wx/combo.h index a057ba0..32afe45 100644 --- a/interface/wx/combo.h +++ b/interface/wx/combo.h @@ -320,7 +320,8 @@ struct wxComboCtrlFeatures @see wxComboBox, wxChoice, wxOwnerDrawnComboBox, wxComboPopup, wxCommandEvent */ -class wxComboCtrl : public wxControl +class wxComboCtrl : public wxControl, + public wxTextEntry { public: /** diff --git a/src/common/combocmn.cpp b/src/common/combocmn.cpp index e87b7df..0de8dd9 100644 --- a/src/common/combocmn.cpp +++ b/src/common/combocmn.cpp @@ -963,7 +963,7 @@ void wxComboCtrlBase::InstallInputHandlers() } void -wxComboCtrlBase::CreateTextCtrl(int style, const wxValidator& validator) +wxComboCtrlBase::CreateTextCtrl(int style) { if ( !(m_windowStyle & wxCB_READONLY) ) { @@ -990,7 +990,7 @@ wxComboCtrlBase::CreateTextCtrl(int style, const wxValidator& validator) m_text = new wxComboCtrlTextCtrl(); m_text->Create(this, wxID_ANY, m_valueString, wxDefaultPosition, wxSize(10,-1), - style, validator); + style); m_text->SetHint(m_hintText); } } @@ -1391,25 +1391,6 @@ void wxComboCtrlBase::DoSetToolTip(wxToolTip *tooltip) } #endif // wxUSE_TOOLTIPS -#if wxUSE_VALIDATORS -void wxComboCtrlBase::SetValidator(const wxValidator& validator) -{ - wxTextCtrl* textCtrl = GetTextCtrl(); - - if ( textCtrl ) - textCtrl->SetValidator( validator ); - else - wxControl::SetValidator( validator ); -} - -wxValidator* wxComboCtrlBase::GetValidator() -{ - wxTextCtrl* textCtrl = GetTextCtrl(); - - return textCtrl ? textCtrl->GetValidator() : wxControl::GetValidator(); -} -#endif // wxUSE_VALIDATORS - bool wxComboCtrlBase::SetForegroundColour(const wxColour& colour) { if ( wxControl::SetForegroundColour(colour) ) @@ -2564,10 +2545,10 @@ void wxComboCtrlBase::SetTextCtrlStyle( int style ) } // ---------------------------------------------------------------------------- -// methods forwarded to wxTextCtrl +// wxTextEntry interface // ---------------------------------------------------------------------------- -wxString wxComboCtrlBase::GetValue() const +wxString wxComboCtrlBase::DoGetValue() const { if ( m_text ) return m_text->GetValue(); @@ -2649,12 +2630,6 @@ void wxComboCtrlBase::SetInsertionPoint(long pos) m_text->SetInsertionPoint(pos); } -void wxComboCtrlBase::SetInsertionPointEnd() -{ - if ( m_text ) - m_text->SetInsertionPointEnd(); -} - long wxComboCtrlBase::GetInsertionPoint() const { if ( m_text ) @@ -2677,6 +2652,14 @@ void wxComboCtrlBase::Replace(long from, long to, const wxString& value) m_text->Replace(from, to, value); } +void wxComboCtrlBase::WriteText(const wxString& text) +{ + if ( m_text ) + m_text->WriteText(text); + else + SetText(m_valueString + text); +} + void wxComboCtrlBase::Remove(long from, long to) { if ( m_text ) @@ -2689,12 +2672,60 @@ void wxComboCtrlBase::SetSelection(long from, long to) m_text->SetSelection(from, to); } +void wxComboCtrlBase::GetSelection(long *from, long *to) const +{ + if ( m_text ) + { + m_text->GetSelection(from, to); + } + else + { + *from = 0; + *to = 0; + } +} + +bool wxComboCtrlBase::IsEditable() const +{ + if ( m_text ) + return m_text->IsEditable(); + return false; +} + +void wxComboCtrlBase::SetEditable(bool editable) +{ + if ( m_text ) + m_text->SetEditable(editable); +} + void wxComboCtrlBase::Undo() { if ( m_text ) m_text->Undo(); } +void wxComboCtrlBase::Redo() +{ + if ( m_text ) + m_text->Redo(); +} + +bool wxComboCtrlBase::CanUndo() const +{ + if ( m_text ) + return m_text->CanUndo(); + + return false; +} + +bool wxComboCtrlBase::CanRedo() const +{ + if ( m_text ) + return m_text->CanRedo(); + + return false; +} + bool wxComboCtrlBase::SetHint(const wxString& hint) { m_hintText = hint; diff --git a/src/common/valtext.cpp b/src/common/valtext.cpp index 795df49..43c87eb 100644 --- a/src/common/valtext.cpp +++ b/src/common/valtext.cpp @@ -33,6 +33,8 @@ #include #include +#include "wx/combo.h" + // ---------------------------------------------------------------------------- // global helpers // ---------------------------------------------------------------------------- @@ -125,8 +127,16 @@ wxTextEntry *wxTextValidator::GetTextEntry() } #endif +#if wxUSE_COMBOCTRL + if (m_validatorWindow->IsKindOf(CLASSINFO(wxComboCtrl))) + { + return (wxComboCtrl*)m_validatorWindow; + } +#endif + wxFAIL_MSG( - wxT("wxTextValidator can only be used with wxTextCtrl or wxComboBox") + "wxTextValidator can only be used with wxTextCtrl, wxComboBox, " + "or wxComboCtrl" ); return NULL; diff --git a/src/generic/combog.cpp b/src/generic/combog.cpp index ef4d471..c70e9d4 100644 --- a/src/generic/combog.cpp +++ b/src/generic/combog.cpp @@ -191,12 +191,12 @@ bool wxGenericComboCtrl::Create(wxWindow *parent, pos, size, style | wxFULL_REPAINT_ON_RESIZE, - wxDefaultValidator, + validator, name) ) return false; // Create textctrl, if necessary - CreateTextCtrl( tcBorder, validator ); + CreateTextCtrl( tcBorder ); // Add keyboard input handlers for main control and textctrl InstallInputHandlers(); @@ -408,19 +408,7 @@ void wxGenericComboCtrl::SetCustomPaintWidth( int width ) tc->RemoveEventHandler(m_textEvtHandler); delete m_textEvtHandler; -#if wxUSE_VALIDATORS - wxValidator* pValidator = tc->GetValidator(); - if ( pValidator ) - { - pValidator = (wxValidator*) pValidator->Clone(); - CreateTextCtrl( tcCreateStyle, *pValidator ); - delete pValidator; - } - else -#endif - { - CreateTextCtrl( tcCreateStyle, wxDefaultValidator ); - } + CreateTextCtrl( tcCreateStyle ); InstallInputHandlers(); } diff --git a/src/msw/combo.cpp b/src/msw/combo.cpp index 109c74c..b85366b 100644 --- a/src/msw/combo.cpp +++ b/src/msw/combo.cpp @@ -187,7 +187,7 @@ bool wxComboCtrl::Create(wxWindow *parent, pos, size, style | wxFULL_REPAINT_ON_RESIZE, - wxDefaultValidator, + validator, name) ) return false; @@ -203,7 +203,7 @@ bool wxComboCtrl::Create(wxWindow *parent, m_iFlags |= wxCC_POPUP_ON_MOUSE_UP; // Create textctrl, if necessary - CreateTextCtrl( wxNO_BORDER, validator ); + CreateTextCtrl( wxNO_BORDER ); // Add keyboard input handlers for main control and textctrl InstallInputHandlers(); -- 2.7.4