From 21d0340384a484e3c8e04280de90c0fb8e261006 Mon Sep 17 00:00:00 2001 From: Julian Smart Date: Tue, 1 Oct 2013 15:47:49 +0000 Subject: [PATCH] Applied #15226 wxRichTextCtrl: Implement setting properties with undo for objects e.g. wxRichTextTable (dghart) git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@74902 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/richtext/richtextbuffer.h | 12 +++++- interface/wx/richtext/richtextbuffer.h | 12 +++++- src/richtext/richtextbuffer.cpp | 59 +++++++++++++------------- 3 files changed, 51 insertions(+), 32 deletions(-) diff --git a/include/wx/richtext/richtextbuffer.h b/include/wx/richtext/richtextbuffer.h index c4ad8d1a0a..14fa4d437e 100644 --- a/include/wx/richtext/richtextbuffer.h +++ b/include/wx/richtext/richtextbuffer.h @@ -3333,7 +3333,7 @@ public: /** Sets with undo the properties for the given object. */ - virtual bool SetObjectPropertiesWithUndo(wxRichTextObject& obj, const wxRichTextProperties& properties); + virtual bool SetObjectPropertiesWithUndo(wxRichTextObject& obj, const wxRichTextProperties& properties, wxRichTextObject* objToSet = NULL); /** Test if this whole range has character attributes of the specified kind. If any @@ -6139,6 +6139,16 @@ public: */ const wxString& GetName() const { return m_name; } + /** + Instructs the first Do() command should be skipped as it's already been applied. + */ + void SetIgnoreFirstTime(bool b) { m_ignoreThis = b; } + + /** + Returns true if the first Do() command should be skipped as it's already been applied. + */ + bool GetIgnoreFirstTime() const { return m_ignoreThis; } + protected: // Action name wxString m_name; diff --git a/interface/wx/richtext/richtextbuffer.h b/interface/wx/richtext/richtextbuffer.h index d7118dd75e..7857043e98 100644 --- a/interface/wx/richtext/richtextbuffer.h +++ b/interface/wx/richtext/richtextbuffer.h @@ -3213,7 +3213,7 @@ public: /** Sets with undo the properties for the given object. */ - virtual bool SetObjectPropertiesWithUndo(wxRichTextObject& obj, const wxRichTextProperties& properties); + virtual bool SetObjectPropertiesWithUndo(wxRichTextObject& obj, const wxRichTextProperties& properties, wxRichTextObject* objToSet = NULL); /** Test if this whole range has character attributes of the specified kind. If any @@ -5997,6 +5997,16 @@ public: */ const wxString& GetName() const { return m_name; } + /** + Instructs the first Do() command should be skipped as it's already been applied. + */ + void SetIgnoreFirstTime(bool b); + + /** + Returns true if the first Do() command should be skipped as it's already been applied. + */ + bool GetIgnoreFirstTime() const; + protected: // Action name wxString m_name; diff --git a/src/richtext/richtextbuffer.cpp b/src/richtext/richtextbuffer.cpp index e00ff83983..33f257e6e4 100644 --- a/src/richtext/richtextbuffer.cpp +++ b/src/richtext/richtextbuffer.cpp @@ -7723,7 +7723,7 @@ wxRichTextField* wxRichTextParagraphLayoutBox::InsertFieldWithUndo(wxRichTextBuf return obj; } -bool wxRichTextParagraphLayoutBox::SetObjectPropertiesWithUndo(wxRichTextObject& obj, const wxRichTextProperties& properties) +bool wxRichTextParagraphLayoutBox::SetObjectPropertiesWithUndo(wxRichTextObject& obj, const wxRichTextProperties& properties, wxRichTextObject* objToSet) { wxRichTextBuffer* buffer = GetBuffer(); wxCHECK_MSG(buffer, false, wxT("Invalid buffer")); @@ -7733,39 +7733,28 @@ bool wxRichTextParagraphLayoutBox::SetObjectPropertiesWithUndo(wxRichTextObject& wxRichTextAction* action = NULL; wxRichTextObject* clone = NULL; -#if 1 if (rtc->SuppressingUndo()) obj.SetProperties(properties); else { clone = obj.Clone(); - clone->SetProperties(obj.GetProperties()); - action = new wxRichTextAction(NULL, _("Change Properties"), wxRICHTEXT_CHANGE_OBJECT, buffer, obj.GetParentContainer(), rtc); + if (objToSet) + { + // Necessary e.g. if when setting a wxRichTextCell's properties, when obj will be the parent table + objToSet->SetProperties(properties); + } + else + { + obj.SetProperties(properties); + } + + // The 'true' parameter in the next line says "Ignore first time"; otherwise the objects are prematurely switched + action = new wxRichTextAction(NULL, _("Change Properties"), wxRICHTEXT_CHANGE_OBJECT, buffer, obj.GetParentContainer(), rtc, true); action->SetOldAndNewObjects(& obj, clone); action->SetPosition(obj.GetRange().GetStart()); action->SetRange(obj.GetRange()); buffer->SubmitAction(action); } -#else - if (!rtc->SuppressingUndo()) - { - // Create a clone containing the current state of the object. It will be used to Undo the action - clone = obj.Clone(); - clone->SetParent(obj.GetParent()); - action = new wxRichTextAction(NULL, _("Change Properties"), wxRICHTEXT_CHANGE_OBJECT, buffer, rtc->GetFocusObject(), rtc); - action->SetObject(&obj); - action->SetPosition(GetRange().GetStart()); - } - - obj.SetProperties(properties); - - if (!rtc->SuppressingUndo()) - { - buffer->SubmitAction(action); - // Finally store the original-state clone; doing so earlier would cause various failures - action->StoreObject(clone); - } -#endif return true; } @@ -7926,11 +7915,14 @@ bool wxRichTextBuffer::SubmitAction(wxRichTextAction* action) if (BatchingUndo() && m_batchedCommand && !SuppressingUndo()) { - wxRichTextCommand* cmd = new wxRichTextCommand(action->GetName()); - cmd->AddAction(action); - cmd->Do(); - cmd->GetActions().Clear(); - delete cmd; + if (!action->GetIgnoreFirstTime()) + { + wxRichTextCommand* cmd = new wxRichTextCommand(action->GetName()); + cmd->AddAction(action); + cmd->Do(); + cmd->GetActions().Clear(); + delete cmd; + } m_batchedCommand->AddAction(action); } @@ -7940,7 +7932,14 @@ bool wxRichTextBuffer::SubmitAction(wxRichTextAction* action) cmd->AddAction(action); // Only store it if we're not suppressing undo. - return GetCommandProcessor()->Submit(cmd, !SuppressingUndo()); + if (!action->GetIgnoreFirstTime()) + { + return GetCommandProcessor()->Submit(cmd, !SuppressingUndo()); + } + else if (!SuppressingUndo()) + { + GetCommandProcessor()->Store(cmd); // Just store it, without Do()ing anything + } } return true; -- 2.45.2