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
This commit is contained in:
Jaakko Salli
2010-12-16 17:21:49 +00:00
parent 25199f9859
commit fda6279358
8 changed files with 121 additions and 75 deletions

View File

@@ -45,6 +45,7 @@
#include "wx/control.h" #include "wx/control.h"
#include "wx/renderer.h" // this is needed for wxCONTROL_XXX flags #include "wx/renderer.h" // this is needed for wxCONTROL_XXX flags
#include "wx/bitmap.h" // wxBitmap used by-value #include "wx/bitmap.h" // wxBitmap used by-value
#include "wx/textentry.h"
class WXDLLIMPEXP_FWD_CORE wxTextCtrl; class WXDLLIMPEXP_FWD_CORE wxTextCtrl;
class WXDLLIMPEXP_FWD_CORE wxComboPopup; 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 wxComboPopup;
friend class wxComboPopupEvtHandler; friend class wxComboPopupEvtHandler;
public: public:
// ctors and such // ctors and such
wxComboCtrlBase() : wxControl() { Init(); } wxComboCtrlBase() : wxControl(), wxTextEntry() { Init(); }
bool Create(wxWindow *parent, bool Create(wxWindow *parent,
wxWindowID id, wxWindowID id,
@@ -196,26 +198,38 @@ public:
virtual bool Enable(bool enable = true); virtual bool Enable(bool enable = true);
virtual bool Show(bool show = true); virtual bool Show(bool show = true);
virtual bool SetFont(const wxFont& font); 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 // wxTextCtrl methods - for readonly combo they should return
// without errors. // without errors.
virtual wxString GetValue() const;
virtual void SetValue(const wxString& value); 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 Copy();
virtual void Cut(); virtual void Cut();
virtual void Paste(); virtual void Paste();
virtual void Undo();
virtual void Redo();
virtual bool CanUndo() const;
virtual bool CanRedo() const;
virtual void SetInsertionPoint(long pos); virtual void SetInsertionPoint(long pos);
virtual void SetInsertionPointEnd();
virtual long GetInsertionPoint() const; virtual long GetInsertionPoint() const;
virtual long GetLastPosition() 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 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 // This method sets the text without affecting list selection
// (ie. wxComboPopup::SetStringValue doesn't get called). // (ie. wxComboPopup::SetStringValue doesn't get called).
@@ -387,21 +401,6 @@ public:
const wxBitmap& GetBitmapHover() const { return m_bmpHover; } const wxBitmap& GetBitmapHover() const { return m_bmpHover; }
const wxBitmap& GetBitmapDisabled() const { return m_bmpDisabled; } 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 // Set custom style flags for embedded wxTextCtrl. Usually must be used
// with two-step creation, before Create() call. // with two-step creation, before Create() call.
void SetTextCtrlStyle( int style ); void SetTextCtrlStyle( int style );
@@ -463,7 +462,7 @@ protected:
// Creates wxTextCtrl. // Creates wxTextCtrl.
// extraStyle: Extra style parameters // 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) // Installs standard input handler to combo (and optionally to the textctrl)
void InstallInputHandlers(); void InstallInputHandlers();
@@ -558,6 +557,10 @@ protected:
virtual void DoSetToolTip( wxToolTip *tip ); virtual void DoSetToolTip( wxToolTip *tip );
#endif #endif
// protected wxTextEntry methods
virtual wxString DoGetValue() const;
virtual wxWindow *GetEditableWindow() { return this; }
// margins functions // margins functions
virtual bool DoSetMargins(const wxPoint& pt); virtual bool DoSetMargins(const wxPoint& pt);
virtual wxPoint DoGetMargins() const; virtual wxPoint DoGetMargins() const;

View File

@@ -80,6 +80,16 @@ public:
protected: 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 // Mandatory virtuals
virtual void OnResize(); virtual void OnResize();

View File

@@ -83,6 +83,9 @@ protected:
protected: protected:
// Dummy method - we override all functions that call this
virtual WXHWND GetEditHWND() const { return NULL; }
// customization // customization
virtual void OnResize(); virtual void OnResize();
virtual wxCoord GetNativeTextIndent() const; virtual wxCoord GetNativeTextIndent() const;

View File

@@ -320,7 +320,8 @@ struct wxComboCtrlFeatures
@see wxComboBox, wxChoice, wxOwnerDrawnComboBox, wxComboPopup, @see wxComboBox, wxChoice, wxOwnerDrawnComboBox, wxComboPopup,
wxCommandEvent wxCommandEvent
*/ */
class wxComboCtrl : public wxControl class wxComboCtrl : public wxControl,
public wxTextEntry
{ {
public: public:
/** /**

View File

@@ -963,7 +963,7 @@ void wxComboCtrlBase::InstallInputHandlers()
} }
void void
wxComboCtrlBase::CreateTextCtrl(int style, const wxValidator& validator) wxComboCtrlBase::CreateTextCtrl(int style)
{ {
if ( !(m_windowStyle & wxCB_READONLY) ) if ( !(m_windowStyle & wxCB_READONLY) )
{ {
@@ -990,7 +990,7 @@ wxComboCtrlBase::CreateTextCtrl(int style, const wxValidator& validator)
m_text = new wxComboCtrlTextCtrl(); m_text = new wxComboCtrlTextCtrl();
m_text->Create(this, wxID_ANY, m_valueString, m_text->Create(this, wxID_ANY, m_valueString,
wxDefaultPosition, wxSize(10,-1), wxDefaultPosition, wxSize(10,-1),
style, validator); style);
m_text->SetHint(m_hintText); m_text->SetHint(m_hintText);
} }
} }
@@ -1391,25 +1391,6 @@ void wxComboCtrlBase::DoSetToolTip(wxToolTip *tooltip)
} }
#endif // wxUSE_TOOLTIPS #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) bool wxComboCtrlBase::SetForegroundColour(const wxColour& colour)
{ {
if ( wxControl::SetForegroundColour(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 ) if ( m_text )
return m_text->GetValue(); return m_text->GetValue();
@@ -2649,12 +2630,6 @@ void wxComboCtrlBase::SetInsertionPoint(long pos)
m_text->SetInsertionPoint(pos); m_text->SetInsertionPoint(pos);
} }
void wxComboCtrlBase::SetInsertionPointEnd()
{
if ( m_text )
m_text->SetInsertionPointEnd();
}
long wxComboCtrlBase::GetInsertionPoint() const long wxComboCtrlBase::GetInsertionPoint() const
{ {
if ( m_text ) if ( m_text )
@@ -2677,6 +2652,14 @@ void wxComboCtrlBase::Replace(long from, long to, const wxString& value)
m_text->Replace(from, to, 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) void wxComboCtrlBase::Remove(long from, long to)
{ {
if ( m_text ) if ( m_text )
@@ -2689,12 +2672,60 @@ void wxComboCtrlBase::SetSelection(long from, long to)
m_text->SetSelection(from, 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() void wxComboCtrlBase::Undo()
{ {
if ( m_text ) if ( m_text )
m_text->Undo(); 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) bool wxComboCtrlBase::SetHint(const wxString& hint)
{ {
m_hintText = hint; m_hintText = hint;

View File

@@ -33,6 +33,8 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include "wx/combo.h"
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// global helpers // global helpers
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@@ -125,8 +127,16 @@ wxTextEntry *wxTextValidator::GetTextEntry()
} }
#endif #endif
#if wxUSE_COMBOCTRL
if (m_validatorWindow->IsKindOf(CLASSINFO(wxComboCtrl)))
{
return (wxComboCtrl*)m_validatorWindow;
}
#endif
wxFAIL_MSG( wxFAIL_MSG(
wxT("wxTextValidator can only be used with wxTextCtrl or wxComboBox") "wxTextValidator can only be used with wxTextCtrl, wxComboBox, "
"or wxComboCtrl"
); );
return NULL; return NULL;

View File

@@ -191,12 +191,12 @@ bool wxGenericComboCtrl::Create(wxWindow *parent,
pos, pos,
size, size,
style | wxFULL_REPAINT_ON_RESIZE, style | wxFULL_REPAINT_ON_RESIZE,
wxDefaultValidator, validator,
name) ) name) )
return false; return false;
// Create textctrl, if necessary // Create textctrl, if necessary
CreateTextCtrl( tcBorder, validator ); CreateTextCtrl( tcBorder );
// Add keyboard input handlers for main control and textctrl // Add keyboard input handlers for main control and textctrl
InstallInputHandlers(); InstallInputHandlers();
@@ -408,19 +408,7 @@ void wxGenericComboCtrl::SetCustomPaintWidth( int width )
tc->RemoveEventHandler(m_textEvtHandler); tc->RemoveEventHandler(m_textEvtHandler);
delete m_textEvtHandler; delete m_textEvtHandler;
#if wxUSE_VALIDATORS CreateTextCtrl( tcCreateStyle );
wxValidator* pValidator = tc->GetValidator();
if ( pValidator )
{
pValidator = (wxValidator*) pValidator->Clone();
CreateTextCtrl( tcCreateStyle, *pValidator );
delete pValidator;
}
else
#endif
{
CreateTextCtrl( tcCreateStyle, wxDefaultValidator );
}
InstallInputHandlers(); InstallInputHandlers();
} }

View File

@@ -187,7 +187,7 @@ bool wxComboCtrl::Create(wxWindow *parent,
pos, pos,
size, size,
style | wxFULL_REPAINT_ON_RESIZE, style | wxFULL_REPAINT_ON_RESIZE,
wxDefaultValidator, validator,
name) ) name) )
return false; return false;
@@ -203,7 +203,7 @@ bool wxComboCtrl::Create(wxWindow *parent,
m_iFlags |= wxCC_POPUP_ON_MOUSE_UP; m_iFlags |= wxCC_POPUP_ON_MOUSE_UP;
// Create textctrl, if necessary // Create textctrl, if necessary
CreateTextCtrl( wxNO_BORDER, validator ); CreateTextCtrl( wxNO_BORDER );
// Add keyboard input handlers for main control and textctrl // Add keyboard input handlers for main control and textctrl
InstallInputHandlers(); InstallInputHandlers();