Implement EmptyUndoBuffer() for wxTextCtrl in wxMSW too

The relevant code was already present in MSWCreateText(), so just
extract it into its own function.
This commit is contained in:
Vadim Zeitlin
2021-08-20 16:35:51 +01:00
parent 3c9bb69d6e
commit 1799922d0e
3 changed files with 42 additions and 24 deletions

View File

@@ -64,6 +64,9 @@ public:
virtual void Redo() wxOVERRIDE; virtual void Redo() wxOVERRIDE;
virtual bool CanRedo() const wxOVERRIDE; virtual bool CanRedo() const wxOVERRIDE;
#if wxUSE_RICHEDIT
virtual void EmptyUndoBuffer() wxOVERRIDE;
#endif // wxUSE_RICHEDIT
virtual void SetInsertionPointEnd() wxOVERRIDE; virtual void SetInsertionPointEnd() wxOVERRIDE;
virtual long GetInsertionPoint() const wxOVERRIDE; virtual long GetInsertionPoint() const wxOVERRIDE;

View File

@@ -168,8 +168,9 @@ public:
/** /**
Delete the undo history. Delete the undo history.
Currently only implemented under macOS and only for multiline text Currently only implemented in wxMSW (for controls using wxTE_RICH2
controls, does nothing in the other ports. 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 @since 3.1.6
*/ */

View File

@@ -690,33 +690,16 @@ bool wxTextCtrl::MSWCreateText(const wxString& value,
::SendMessage(GetHwnd(), EM_SETMARGINS, wParam, lParam); ::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 // For RichEdit >= 4, SetFont(), called above from MSWCreateControl(), uses
// EM_SETCHARFORMAT which affects the undo buffer, meaning that CanUndo() // EM_SETCHARFORMAT which affects the undo buffer, meaning that CanUndo()
// for a newly created control returns true, which is unexpected. To avoid // for a newly created control returns true, which is unexpected, so clear
// this, we explicitly use Undo(tomFalse) here to clear the undo buffer. // 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).
if ( GetRichVersion() >= 4 ) if ( GetRichVersion() >= 4 )
{ {
wxCOMPtr<IRichEditOle> pRichEditOle; EmptyUndoBuffer();
if ( SendMessage(GetHwnd(), EM_GETOLEINTERFACE,
0, (LPARAM)&pRichEditOle) && pRichEditOle )
{
wxCOMPtr<ITextDocument> 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_RICHEDIT && wxHAS_TOM_H #endif // wxUSE_RICHEDIT
return true; return true;
} }
@@ -2012,6 +1995,37 @@ bool wxTextCtrl::CanRedo() const
return wxTextEntry::CanRedo(); 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<IRichEditOle> pRichEditOle;
if ( SendMessage(GetHwnd(), EM_GETOLEINTERFACE,
0, (LPARAM)&pRichEditOle) && pRichEditOle )
{
wxCOMPtr<ITextDocument> 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) // caret handling (Windows only)
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------