tdf#135198 tdf#138050 sw editing: fix text box position sync

Follow-up to commit c9eb53f200225f2ee6ca695e1326843a487aee51
(tdf#135198 sw editing: text box fell out of its shape)

Every time a shape is repositioned, make sure the text box
inside the shape follows the shape.

The previous solution to this bug, the one implemented in
SwObjectFormatterTextFrame::DoFormatObjs, was a little
more cumbersome. This one should produce fewer regressions,
I hope.

Change-Id: I3e88eb8616cd299cabb7b74b188ab7220746ec89
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/106421
Tested-by: Jenkins
Tested-by: László Németh <nemeth@numbertext.org>
Reviewed-by: László Németh <nemeth@numbertext.org>
diff --git a/sw/qa/core/layout/data/textbox-phantom-change.docx b/sw/qa/core/layout/data/textbox-phantom-change.docx
new file mode 100644
index 0000000..75ac039
--- /dev/null
+++ b/sw/qa/core/layout/data/textbox-phantom-change.docx
Binary files differ
diff --git a/sw/qa/core/layout/layout.cxx b/sw/qa/core/layout/layout.cxx
index 15f2c4e..2151b87 100644
--- a/sw/qa/core/layout/layout.cxx
+++ b/sw/qa/core/layout/layout.cxx
@@ -16,6 +16,7 @@
#include <unotxdoc.hxx>
#include <drawdoc.hxx>
#include <IDocumentDrawModelAccess.hxx>
#include <IDocumentState.hxx>
#include <svx/svdpage.hxx>

char const DATA_DIRECTORY[] = "/sw/qa/core/layout/data/";
@@ -180,6 +181,19 @@ CPPUNIT_TEST_FIXTURE(SwCoreLayoutTest, testTextBoxStaysInsideShape)
    assertXPath(pXmlDoc, "//fly/infos/bounds", "bottom", "7184");
}

CPPUNIT_TEST_FIXTURE(SwCoreLayoutTest, testTextBoxNotModifiedOnOpen)
{
    // tdf#138050: a freshly opened document containing a shape with a text box
    // should not appear to be modified
    load(DATA_DIRECTORY, "textbox-phantom-change.docx");
    SwXTextDocument* pTextDoc = dynamic_cast<SwXTextDocument*>(mxComponent.get());
    SwDoc* pDoc = pTextDoc->GetDocShell()->GetDoc();

    // Without the fix in place this test would have shown that the document
    // was modified due to a fix to tdf#135198
    CPPUNIT_ASSERT(!pDoc->getIDocumentState().IsModified());
}

CPPUNIT_TEST_FIXTURE(SwCoreLayoutTest, testTextBoxAutoGrowVertical)
{
    load(DATA_DIRECTORY, "textbox-autogrow-vertical.docx");
diff --git a/sw/source/core/draw/dcontact.cxx b/sw/source/core/draw/dcontact.cxx
index eb406e4..3ae40cc 100644
--- a/sw/source/core/draw/dcontact.cxx
+++ b/sw/source/core/draw/dcontact.cxx
@@ -49,6 +49,8 @@
#include <unodraw.hxx>
#include <IDocumentDrawModelAccess.hxx>
#include <IDocumentLayoutAccess.hxx>
#include <IDocumentState.hxx>
#include <IDocumentUndoRedo.hxx>
#include <doc.hxx>
#include <hints.hxx>
#include <txtfrm.hxx>
@@ -1260,7 +1262,6 @@ void SwDrawContact::Changed_( const SdrObject& rObj,
                const SwFormatVertOrient& rVert = GetFormat()->GetVertOrient();
                if ( nYPosDiff != 0 )
                {

                    if ( rVert.GetRelationOrient() == text::RelOrientation::CHAR ||
                         rVert.GetRelationOrient() == text::RelOrientation::TEXT_LINE )
                    {
@@ -1313,6 +1314,32 @@ void SwDrawContact::Changed_( const SdrObject& rObj,
                    // may affect the size of the underlying textbox.
                    lcl_textBoxSizeNotify(GetFormat());
            }

            // tdf#135198: keep text box together with its shape
            SwRect aObjRect(rObj.GetSnapRect());
            const SwPageFrame* rPageFrame = pAnchoredDrawObj->GetPageFrame();
            if (rPageFrame && rPageFrame->isFrameAreaPositionValid())
            {
                SwDoc* const pDoc = GetFormat()->GetDoc();

                // avoid Undo creation
                ::sw::UndoGuard const ug(pDoc->GetIDocumentUndoRedo());

                // hide any artificial "changes" made by synchronizing the textbox position
                const bool bEnableSetModified = pDoc->getIDocumentState().IsEnableSetModified();
                pDoc->getIDocumentState().SetEnableSetModified(false);

                SfxItemSet aSyncSet(pDoc->GetAttrPool(),
                                    svl::Items<RES_VERT_ORIENT, RES_ANCHOR>{});
                aSyncSet.Put(SwFormatVertOrient(aObjRect.Top() - rPageFrame->getFrameArea().Top(),
                                                text::VertOrientation::NONE,
                                                text::RelOrientation::PAGE_FRAME));
                aSyncSet.Put(SwFormatAnchor(RndStdIds::FLY_AT_PAGE, pAnchoredDrawObj->GetPageFrame()->GetPhyPageNum()));

                SwTextBoxHelper::syncFlyFrameAttr(*GetFormat(), aSyncSet);

                pDoc->getIDocumentState().SetEnableSetModified(bEnableSetModified);
            }
        }
        break;
        case SdrUserCallType::ChangeAttr:
diff --git a/sw/source/core/layout/objectformattertxtfrm.cxx b/sw/source/core/layout/objectformattertxtfrm.cxx
index 5c4aa77..96ea415 100644
--- a/sw/source/core/layout/objectformattertxtfrm.cxx
+++ b/sw/source/core/layout/objectformattertxtfrm.cxx
@@ -344,39 +344,6 @@ bool SwObjectFormatterTextFrame::DoFormatObjs()
           ( !mrAnchorTextFrame.IsFollow() &&
             AtLeastOneObjIsTmpConsiderWrapInfluence() ) ) )
    {
        // tdf#135198: force text box to stay inside shape after layout changes
        if (SwSortedObjs* pDrawObjs = mrAnchorTextFrame.GetDrawObjs())
        {
            // N.B.: avoid using ranged for because the iterator might get invalidated
            for (size_t i = 0; i < pDrawObjs->size(); ++i)
            {
                SwAnchoredObject* const pObj = (*pDrawObjs)[i];
                SwFrameFormat& rFormat = pObj->GetFrameFormat();
                if (SwTextBoxHelper::isTextBox(&rFormat, RES_DRAWFRMFMT))
                {
                    if (const SwPageFrame* pPageFrame = pObj->GetPageFrame())
                    {
                        SwDoc* pDoc = rFormat.GetDoc();

                        // avoid Undo creation,
                        ::sw::UndoGuard const ug(pDoc->GetIDocumentUndoRedo());

                        SfxItemSet aSet(pDoc->GetAttrPool(),
                                        svl::Items<RES_VERT_ORIENT, RES_ANCHOR>{});

                        const SwRect& rPageFrameArea = pPageFrame->getFrameArea();
                        aSet.Put(SwFormatVertOrient(pObj->GetObjRect().Top() - rPageFrameArea.Top(),
                                                    text::VertOrientation::NONE,
                                                    text::RelOrientation::PAGE_FRAME));

                        aSet.Put(SwFormatAnchor(RndStdIds::FLY_AT_PAGE, pObj->GetPageFrame()->GetPhyPageNum()));

                        SwTextBoxHelper::syncFlyFrameAttr(rFormat, aSet);
                    }
                }
            }
        }

        const bool bDoesAnchorHadPrev = ( mrAnchorTextFrame.GetIndPrev() != nullptr );

        // Format anchor text frame after its objects are formatted.