tdf#140828 sw textbox: fix AS_CHAR regression

Textboxes anchored as characters don't lose
their text frames after changing the anchor
"To character".

Regression from commit 493a916a3113e877835c9bc7c93faef0d29f9a33
(tdf#140158 tdf#138598 tdf#140598 sw: fix sync of AS_CHAR textboxes).

Change-Id: I0e6d88c9dcdeff515744bc4c201a5640eb810d1b
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/112209
Tested-by: László Németh <nemeth@numbertext.org>
Reviewed-by: László Németh <nemeth@numbertext.org>
Signed-off-by: Xisco Fauli <xiscofauli@libreoffice.org>
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/113321
Tested-by: Jenkins
Reviewed-by: Attila Bakos <bakos.attilakaroly@nisz.hu>
diff --git a/sw/qa/extras/uiwriter/data3/tdf140828.docx b/sw/qa/extras/uiwriter/data3/tdf140828.docx
new file mode 100755
index 0000000..bfdabc5
--- /dev/null
+++ b/sw/qa/extras/uiwriter/data3/tdf140828.docx
Binary files differ
diff --git a/sw/qa/extras/uiwriter/uiwriter3.cxx b/sw/qa/extras/uiwriter/uiwriter3.cxx
index a2b8c8b..e0623c8 100644
--- a/sw/qa/extras/uiwriter/uiwriter3.cxx
+++ b/sw/qa/extras/uiwriter/uiwriter3.cxx
@@ -14,6 +14,7 @@
#include <com/sun/star/drawing/XDrawPageSupplier.hpp>
#include <IDocumentDrawModelAccess.hxx>
#include <com/sun/star/text/TextContentAnchorType.hpp>
#include <com/sun/star/text/XTextFrame.hpp>
#include <com/sun/star/text/XTextTablesSupplier.hpp>
#include <com/sun/star/text/XTextTable.hpp>
#include <com/sun/star/text/XTextViewCursorSupplier.hpp>
@@ -854,6 +855,37 @@ CPPUNIT_TEST_FIXTURE(SwUiWriterTest3, testTdf76636_2)
    CPPUNIT_ASSERT_EQUAL(sal_Int32(6), xTextTable->getColumns()->getCount());
}

CPPUNIT_TEST_FIXTURE(SwUiWriterTest3, testTdf140828)
{
    load(DATA_DIRECTORY, "tdf140828.docx");
    SwXTextDocument* pTextDoc = dynamic_cast<SwXTextDocument*>(mxComponent.get());
    CPPUNIT_ASSERT(pTextDoc);

    CPPUNIT_ASSERT_EQUAL(1, getShapes());
    uno::Reference<drawing::XShape> xShp = getShape(1);
    CPPUNIT_ASSERT(xShp);

    uno::Reference<beans::XPropertySet> ShpProps(xShp, uno::UNO_QUERY_THROW);
    dispatchCommand(mxComponent, ".uno:JumpToNextFrame", {});
    Scheduler::ProcessEventsToIdle();

    dispatchCommand(mxComponent, ".uno:SetAnchorAtChar", {});
    Scheduler::ProcessEventsToIdle();

    CPPUNIT_ASSERT(ShpProps->getPropertyValue("AnchorType").get<text::TextContentAnchorType>()
                   != text::TextContentAnchorType::TextContentAnchorType_AS_CHARACTER);

    uno::Reference<text::XTextFrame> xTxBx(SwTextBoxHelper::getUnoTextFrame(xShp));
    CPPUNIT_ASSERT(xTxBx);

    uno::Reference<beans::XPropertySet> TxBxProps(xTxBx, uno::UNO_QUERY_THROW);
    CPPUNIT_ASSERT_EQUAL(OUString("top left image"), xTxBx->getText()->getString());

    CPPUNIT_ASSERT_MESSAGE("Bad Relative Orientation and Position!",
                           TxBxProps->getPropertyValue("HoriOrientRelation").get<sal_Int16>()
                               != text::RelOrientation::CHAR);
}

CPPUNIT_TEST_FIXTURE(SwUiWriterTest3, testTdf132725)
{
    load(DATA_DIRECTORY, "tdf132725.odt");
diff --git a/sw/source/core/doc/textboxhelper.cxx b/sw/source/core/doc/textboxhelper.cxx
index 5257d44..6b9f876 100644
--- a/sw/source/core/doc/textboxhelper.cxx
+++ b/sw/source/core/doc/textboxhelper.cxx
@@ -165,8 +165,6 @@ void SwTextBoxHelper::create(SwFrameFormat* pShape, bool bCopyText)
    if (xShapePropertySet->getPropertyValue(UNO_NAME_TEXT_WRITINGMODE) >>= eMode)
        syncProperty(pShape, RES_FRAMEDIR, 0, uno::makeAny(sal_Int16(eMode)));

    // TODO: Text dialog attr setting to frame

    const SwFormatAnchor& rAnch = pShape->GetAnchor();
    if (!((rAnch.GetAnchorId() == RndStdIds::FLY_AT_PAGE && rAnch.GetPageNum() != 0)
          || ((rAnch.GetAnchorId() == RndStdIds::FLY_AT_PARA
@@ -723,6 +721,9 @@ void SwTextBoxHelper::syncProperty(SwFrameFormat* pShape, sal_uInt16 nWID, sal_u
                    }
                    else // Otherwise copy the anchor type of the shape
                    {
                        // tdf#140828: Do not keep CHAR rel-orientation:
                        xPropertySet->setPropertyValue(UNO_NAME_HORI_ORIENT_RELATION,
                                                       uno::Any(text::RelOrientation::FRAME));
                        xPropertySet->setPropertyValue(UNO_NAME_ANCHOR_TYPE, aValue);
                    }
                    // After anchoring the position must be set as well:
diff --git a/sw/source/core/text/porfly.cxx b/sw/source/core/text/porfly.cxx
index cafa70c..9426359 100644
--- a/sw/source/core/text/porfly.cxx
+++ b/sw/source/core/text/porfly.cxx
@@ -351,9 +351,11 @@ void SwFlyCntPortion::SetBase( const SwTextFrame& rFrame, const Point &rBase,
            // Both rectangles are absolute, SwFormatHori/VertOrient's position
            // is relative to the print area of the anchor text frame.
            tools::Rectangle aTextRectangle = SwTextBoxHelper::getTextRectangle(pShape);
            tools::Long nXoffs  = SwTextBoxHelper::getTextRectangle(pShape, false).getX();

            const auto aPos(pShape->GetAnchor().GetContentAnchor());
            SwFormatVertOrient aVert(pTextBox->GetVertOrient());
            SwFormatHoriOrient aHori(pTextBox->GetHoriOrient());

            // tdf#138598 Replace vertical alignment of As_char textboxes in footer
            // tdf#140158 Remove horizontal positioning of As_char textboxes, because
@@ -373,10 +375,12 @@ void SwFlyCntPortion::SetBase( const SwTextFrame& rFrame, const Point &rBase,

            SwFormatAnchor aNewTxBxAnchor(pTextBox->GetAnchor());
            aNewTxBxAnchor.SetAnchor(aPos);
            aHori.SetPos(nXoffs);

            pTextBox->LockModify();
            pTextBox->SetFormatAttr(aNewTxBxAnchor);
            pTextBox->SetFormatAttr(aVert);
            pTextBox->SetFormatAttr(aHori);
            pTextBox->UnlockModify();
        }
    }