diff --git a/include/wx/msw/textctrl.h b/include/wx/msw/textctrl.h index feeef94ff3..4b85f69e3c 100644 --- a/include/wx/msw/textctrl.h +++ b/include/wx/msw/textctrl.h @@ -64,6 +64,9 @@ public: virtual void Redo() wxOVERRIDE; virtual bool CanRedo() const wxOVERRIDE; +#if wxUSE_RICHEDIT + virtual void EmptyUndoBuffer() wxOVERRIDE; +#endif // wxUSE_RICHEDIT virtual void SetInsertionPointEnd() wxOVERRIDE; virtual long GetInsertionPoint() const wxOVERRIDE; diff --git a/interface/wx/textentry.h b/interface/wx/textentry.h index 30ca009949..0323eeaeb4 100644 --- a/interface/wx/textentry.h +++ b/interface/wx/textentry.h @@ -168,8 +168,9 @@ public: /** Delete the undo history. - Currently only implemented under macOS and only for multiline text - controls, does nothing in the other ports. + Currently only implemented in wxMSW (for controls using wxTE_RICH2 + style only) and wxOSX (for multiline text controls only), does nothing + in the other ports or for the controls not using the appropriate styles. @since 3.1.6 */ diff --git a/src/msw/textctrl.cpp b/src/msw/textctrl.cpp index 8fff33e265..2819131b4d 100644 --- a/src/msw/textctrl.cpp +++ b/src/msw/textctrl.cpp @@ -690,33 +690,16 @@ bool wxTextCtrl::MSWCreateText(const wxString& value, ::SendMessage(GetHwnd(), EM_SETMARGINS, wParam, lParam); } -#if wxUSE_RICHEDIT && wxUSE_OLE && defined(wxHAS_TOM_H) +#if wxUSE_RICHEDIT // For RichEdit >= 4, SetFont(), called above from MSWCreateControl(), uses // EM_SETCHARFORMAT which affects the undo buffer, meaning that CanUndo() - // for a newly created control returns true, which is unexpected. To avoid - // this, we explicitly use Undo(tomFalse) here to clear the undo buffer. - // And since Undo(tomFalse) also disables the undo buffer, we need to - // enable it again immediately after clearing by calling Undo(tomTrue). + // for a newly created control returns true, which is unexpected, so clear + // the undo buffer. if ( GetRichVersion() >= 4 ) { - wxCOMPtr pRichEditOle; - if ( SendMessage(GetHwnd(), EM_GETOLEINTERFACE, - 0, (LPARAM)&pRichEditOle) && pRichEditOle ) - { - wxCOMPtr pDoc; - HRESULT hr = pRichEditOle->QueryInterface - ( - wxIID_PPV_ARGS(ITextDocument, &pDoc) - ); - if ( SUCCEEDED(hr) ) - { - hr = pDoc->Undo(tomFalse, NULL); - if ( SUCCEEDED(hr) ) - pDoc->Undo(tomTrue, NULL); - } - } + EmptyUndoBuffer(); } -#endif // wxUSE_RICHEDIT && wxHAS_TOM_H +#endif // wxUSE_RICHEDIT return true; } @@ -2012,6 +1995,37 @@ bool wxTextCtrl::CanRedo() const return wxTextEntry::CanRedo(); } +#if wxUSE_RICHEDIT + +void wxTextCtrl::EmptyUndoBuffer() +{ +#if wxUSE_OLE && defined(wxHAS_TOM_H) + // We need to use Undo(tomFalse) to clear the undo buffer, but calling it + // also disables the undo buffer, so we need to enable it again immediately + // after clearing by calling Undo(tomTrue). + if ( GetRichVersion() >= 4 ) + { + wxCOMPtr pRichEditOle; + if ( SendMessage(GetHwnd(), EM_GETOLEINTERFACE, + 0, (LPARAM)&pRichEditOle) && pRichEditOle ) + { + wxCOMPtr pDoc; + HRESULT hr = pRichEditOle->QueryInterface + ( + wxIID_PPV_ARGS(ITextDocument, &pDoc) + ); + if ( SUCCEEDED(hr) ) + { + hr = pDoc->Undo(tomFalse, NULL); + if ( SUCCEEDED(hr) ) + pDoc->Undo(tomTrue, NULL); + } + } + } +#endif // wxUSE_OLE && wxHAS_TOM_H +} +#endif // wxUSE_RICHEDIT + // ---------------------------------------------------------------------------- // caret handling (Windows only) // ----------------------------------------------------------------------------