tdf#137803 sw: fix AutoSize and Wrap of textboxes

When autosize (AutoGrowHeight) turned on
the textbox fell apart. After changing the text
of the textbox the positions still were different
for the shape and the textframe.

Now this fix solves, if the text changes,
the positions (with the size what worked only
before) synchronized so the textboxes are kept together.

Also Wrap text inside the shape now works like
the editeng text did, except in the case of empty
text boxes.

Note: this fixes only editing (without positioning
the textboxes by the mouse before or after the setting),
but not import/export, yet.

The ooxmlimport test has been restored to its original
state, because the fix for the tdf#135198 is fixed
by this commit.

To start the unit test just give the following command:

$ (cd sw && make UITest_writer_tests7 UITEST_TEST_NAME="tdf137803.tdf137803.test_tdf137803" SAL_USE_VCLPLUGIN=gen)

Change-Id: Ie13a7a1e81e4099cd8540edd7e008118e58134bf
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/107515
Tested-by: László Németh <nemeth@numbertext.org>
Reviewed-by: László Németh <nemeth@numbertext.org>
diff --git a/sw/inc/textboxhelper.hxx b/sw/inc/textboxhelper.hxx
index 0c20711..51261a8 100644
--- a/sw/inc/textboxhelper.hxx
+++ b/sw/inc/textboxhelper.hxx
@@ -72,6 +72,8 @@ public:
    /// Get a property of the underlying TextFrame.
    static void getProperty(SwFrameFormat const* pShape, sal_uInt16 nWID, sal_uInt8 nMemberID,
                            css::uno::Any& rValue);
    /// Get a property of the underlying TextFrame.
    static css::uno::Any getProperty(SwFrameFormat const* pShape, OUString sPropName);

    /// There are two types of enum of anchor type, so this function maps this.
    static css::text::TextContentAnchorType mapAnchorType(const RndStdIds& rAnchorID);
diff --git a/sw/qa/extras/ooxmlimport/ooxmlimport.cxx b/sw/qa/extras/ooxmlimport/ooxmlimport.cxx
index 3d185a7..cf9781f 100644
--- a/sw/qa/extras/ooxmlimport/ooxmlimport.cxx
+++ b/sw/qa/extras/ooxmlimport/ooxmlimport.cxx
@@ -1067,8 +1067,7 @@ DECLARE_OOXMLIMPORT_TEST(textboxWpsOnly, "textbox-wps-only.docx")
    if ( nsScreen.frame.size.width * scaleFactor > 4000 )
        return;
#endif
    // Vertically oriented to page due to tdf#135198
    CPPUNIT_ASSERT_EQUAL(sal_Int32(5304), getProperty<sal_Int32>(xFrame, "VertOrientPosition"));
    CPPUNIT_ASSERT_EQUAL(sal_Int32(2805), getProperty<sal_Int32>(xFrame, "VertOrientPosition"));
}

DECLARE_OOXMLIMPORT_TEST(testGroupshapeRelsize, "groupshape-relsize.docx")
diff --git a/sw/qa/uitest/data/tdf137803.odt b/sw/qa/uitest/data/tdf137803.odt
new file mode 100644
index 0000000..8f2f7d5
--- /dev/null
+++ b/sw/qa/uitest/data/tdf137803.odt
Binary files differ
diff --git a/sw/qa/uitest/writer_tests7/tdf137803.py b/sw/qa/uitest/writer_tests7/tdf137803.py
new file mode 100644
index 0000000..249dbbb
--- /dev/null
+++ b/sw/qa/uitest/writer_tests7/tdf137803.py
@@ -0,0 +1,61 @@
# -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
from uitest.framework import UITestCase
from libreoffice.uno.propertyvalue import mkPropertyValues
from uitest.uihelper.common import get_state_as_dict
import importlib
import time
import org.libreoffice.unotest
import pathlib

def get_url_for_data_file(file_name):
    return pathlib.Path(org.libreoffice.unotest.makeCopyFromTDOC(file_name)).as_uri()

class tdf137803(UITestCase):
    def test_tdf137803(self):
        # load the sample file
        self.ui_test.load_file(get_url_for_data_file("tdf137803.odt"))
        xWriterDoc = self.xUITest.getTopFocusWindow()
        xWriterEdit = xWriterDoc.getChild("writer_edit")
        document = self.ui_test.get_component()

        # select the shape
        self.xUITest.executeCommand(".uno:JumpToNextFrame")
        self.ui_test.wait_until_child_is_available(xWriterEdit, 'metricfield')

        # open textattrs dialog
        self.ui_test.execute_dialog_through_command(".uno:TextAttributes")
        TextDialog = self.xUITest.getTopFocusWindow();

        # check autosize on
        TSB_AUTOGROW_SIZE = TextDialog.getChild('TSB_AUTOGROW_SIZE')
        TSB_AUTOGROW_SIZE.executeAction("CLICK",tuple())
        ok = TextDialog.getChild("ok")
        self.ui_test.close_dialog_through_button(ok)

        # get the shape
        drawPage = document.getDrawPages().getByIndex(0)
        shape = drawPage.getByIndex(0)

        # and the textbox
        frame = shape.getText()

        # get the positions
        shapeYPos = shape.getPropertyValue("VertOrientPosition")
        frameYPos = frame.getPropertyValue("VertOrientPosition")
        shpsize = shape.getSize().Height

        xToolkit = self.xContext.ServiceManager.createInstance('com.sun.star.awt.Toolkit')
        xToolkit.processEventsToIdle()

        # without the fix, at this point the textbox falls apart so this won't be passed
        self.assertLess(frameYPos, shapeYPos + shpsize)

        # close the doc
        self.ui_test.close_doc()

# vim: set shiftwidth=4 softtabstop=4 expandtab:
diff --git a/sw/source/core/doc/textboxhelper.cxx b/sw/source/core/doc/textboxhelper.cxx
index 848a891..634685b 100644
--- a/sw/source/core/doc/textboxhelper.cxx
+++ b/sw/source/core/doc/textboxhelper.cxx
@@ -582,6 +582,21 @@ void SwTextBoxHelper::getProperty(SwFrameFormat const* pShape, sal_uInt16 nWID, 
    }
}

css::uno::Any SwTextBoxHelper::getProperty(SwFrameFormat const* pShape, OUString sPropName)
{
    if (!pShape)
        return uno::Any();

    SwFrameFormat* pFormat = getOtherTextBoxFormat(pShape, RES_DRAWFRMFMT);
    if (!pFormat)
        return uno::Any();

    uno::Reference<beans::XPropertySet> const xPropertySet(
        SwXTextFrame::CreateXTextFrame(*pFormat->GetDoc(), pFormat), uno::UNO_QUERY);

    return xPropertySet->getPropertyValue(sPropName);
}

void SwTextBoxHelper::syncProperty(SwFrameFormat* pShape, sal_uInt16 nWID, sal_uInt8 nMemberID,
                                   const css::uno::Any& rValue)
{
@@ -663,6 +678,9 @@ void SwTextBoxHelper::syncProperty(SwFrameFormat* pShape, sal_uInt16 nWID, sal_u
        case RES_FRM_SIZE:
            switch (nMemberID)
            {
                case MID_FRMSIZE_WIDTH_TYPE:
                    aPropertyName = UNO_NAME_WIDTH_TYPE;
                    break;
                case MID_FRMSIZE_IS_AUTO_HEIGHT:
                    aPropertyName = UNO_NAME_FRAME_ISAUTOMATIC_HEIGHT;
                    break;
@@ -1040,9 +1058,15 @@ void SwTextBoxHelper::updateTextBoxMargin(SdrObject* pObj)
    syncProperty(pParentFormat, UNO_NAME_TEXT_HORZADJUST,
                 xPropertySet->getPropertyValue(UNO_NAME_TEXT_HORZADJUST));

    //FIXME: Sync autogrow: needs repositioning after sync
    //syncProperty(pParentFormat, RES_FRM_SIZE, MID_FRMSIZE_IS_AUTO_HEIGHT,
    //             xPropertySet->getPropertyValue(UNO_NAME_TEXT_AUTOGROWHEIGHT));
    // tdf137803: Sync autogrow:
    const bool bIsAutoGrow
        = xPropertySet->getPropertyValue(UNO_NAME_TEXT_AUTOGROWHEIGHT).get<bool>();
    const bool bIsAutoWrap = xPropertySet->getPropertyValue(UNO_NAME_TEXT_WORDWRAP).get<bool>();

    syncProperty(pParentFormat, RES_FRM_SIZE, MID_FRMSIZE_IS_AUTO_HEIGHT, uno::Any(bIsAutoGrow));

    syncProperty(pParentFormat, RES_FRM_SIZE, MID_FRMSIZE_WIDTH_TYPE,
                 uno::Any(bIsAutoWrap ? text::SizeType::FIX : text::SizeType::MIN));
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sw/source/core/layout/flycnt.cxx b/sw/source/core/layout/flycnt.cxx
index c139bd8..da79d70 100644
--- a/sw/source/core/layout/flycnt.cxx
+++ b/sw/source/core/layout/flycnt.cxx
@@ -47,6 +47,10 @@
#include "objectformattertxtfrm.hxx"
#include <HandleAnchorNodeChg.hxx>
#include <ndtxt.hxx>
#include <textboxhelper.hxx>
#include <fmtfollowtextflow.hxx>
#include <fmtfsize.hxx>
#include <unoprnms.hxx>

using namespace ::com::sun::star;

@@ -379,6 +383,7 @@ void SwFlyAtContentFrame::MakeAll(vcl::RenderContext* pRenderContext)
        SwRectFnSet aRectFnSet(this);
        Point aOldPos( aRectFnSet.GetPos(getFrameArea()) );
        SwFlyFreeFrame::MakeAll(pRenderContext);

        const bool bPosChgDueToOwnFormat =
                                aOldPos != aRectFnSet.GetPos(getFrameArea());
        // #i3317#
@@ -506,6 +511,28 @@ void SwFlyAtContentFrame::MakeAll(vcl::RenderContext* pRenderContext)
            }
        }
    }
    // tdf#137803: Fix the position of the shape during autoSize
    SwFrameFormat* pShapeFormat
        = SwTextBoxHelper::getOtherTextBoxFormat(GetFormat(), RES_FLYFRMFMT);
    // FIXME: According to tdf37153, ignore FollowTextFlow objs, because
    // wrong position will applied in that case. FollowTextFlow needs fix.
    if (pShapeFormat && !pShapeFormat->GetFollowTextFlow().GetValue() &&
        SwTextBoxHelper::getProperty(pShapeFormat,
            UNO_NAME_FRAME_ISAUTOMATIC_HEIGHT).get<bool>() )
    {
        // get the text area of the shape
        const tools::Rectangle aTextRectangle
            = SwTextBoxHelper::getTextRectangle(pShapeFormat, false);
        // get the original textframe position
        SwFormatHoriOrient aHOri = pShapeFormat->GetHoriOrient();
        SwFormatVertOrient aVOri = pShapeFormat->GetVertOrient();
        // calc the right position of the shape depending on text area
        aHOri.SetPos(aHOri.GetPos() + aTextRectangle.getX());
        aVOri.SetPos(aVOri.GetPos() + aTextRectangle.getY());
        // save the new position for the shape
        GetFormat()->SetFormatAttr(aHOri);
        GetFormat()->SetFormatAttr(aVOri);
    }
    if ( bOsz || bConsiderWrapInfluenceDueToOverlapPrevCol ||
         // #i40444#
         bConsiderWrapInfluenceDueToMovedFwdAnchor )