sw/inc/textboxhelper.hxx               |    5 ++-
 sw/qa/extras/layout/data/tdf137185.odt |binary
 sw/qa/extras/layout/layout.cxx         |   43 +++++++++++++++++++++++++++++++++
 sw/source/core/doc/textboxhelper.cxx   |   36 ++++++++++++++++++++++++++-
 sw/source/uibase/shells/drawsh.cxx     |    2 -
 5 files changed, 82 insertions(+), 4 deletions(-)

New commits:
commit 07b856e94b6e6af25394f2b18adf7ad33ebc7256
Author:     Attila Bakos (NISZ) <bakos.attilakar...@nisz.hu>
AuthorDate: Wed Oct 28 15:11:08 2020 +0100
Commit:     Gabor Kelemen <kelemen.gab...@nisz.hu>
CommitDate: Mon Mar 1 12:36:21 2021 +0100

    tdf#137185 sw: move shape text on adding textbox
    
    The original text of the shape is moved to the new
    (text frame of the) text box instead of overlapping
    the text content added later.
    
    Change-Id: I2ad8865cdbe3c424c70985737ecda3ac9315cabc
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/104942
    Tested-by: László Németh <nem...@numbertext.org>
    Reviewed-by: László Németh <nem...@numbertext.org>
    (cherry picked from commit ae8bc80952fafc791ce8bbddd99c99626a93989c)
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/111700
    Tested-by: Gabor Kelemen <kelemen.gab...@nisz.hu>
    Reviewed-by: Gabor Kelemen <kelemen.gab...@nisz.hu>

diff --git a/sw/inc/textboxhelper.hxx b/sw/inc/textboxhelper.hxx
index 746a5e90b356..e6c991d8acfa 100644
--- a/sw/inc/textboxhelper.hxx
+++ b/sw/inc/textboxhelper.hxx
@@ -58,8 +58,9 @@ public:
     using SavedLink = std::map<const SwFrameFormat*, const SwFrameFormat*>;
     /// Maps a draw format to content.
     using SavedContent = std::map<const SwFrameFormat*, SwFormatContent>;
-    /// Create a TextBox for a shape.
-    static void create(SwFrameFormat* pShape);
+    /// Create a TextBox for a shape. If the second parameter is true,
+    /// the original text in the shape will be copied to the frame
+    static void create(SwFrameFormat* pShape, bool bCopyText = false);
     /// Destroy a TextBox for a shape.
     static void destroy(SwFrameFormat* pShape);
     /// Get interface of a shape's TextBox, if there is any.
diff --git a/sw/qa/extras/layout/data/tdf137185.odt 
b/sw/qa/extras/layout/data/tdf137185.odt
new file mode 100755
index 000000000000..65d1f5de672d
Binary files /dev/null and b/sw/qa/extras/layout/data/tdf137185.odt differ
diff --git a/sw/qa/extras/layout/layout.cxx b/sw/qa/extras/layout/layout.cxx
index 11bdd07c9771..c41681b93f36 100644
--- a/sw/qa/extras/layout/layout.cxx
+++ b/sw/qa/extras/layout/layout.cxx
@@ -35,6 +35,12 @@
 #include <ndtxt.hxx>
 #include <frmatr.hxx>
 #include <IDocumentSettingAccess.hxx>
+#include <IDocumentDrawModelAccess.hxx>
+#include <textboxhelper.hxx>
+#include <unoframe.hxx>
+#include <drawdoc.hxx>
+#include <svx/svdpage.hxx>
+#include <dcontact.hxx>
 
 #include <config_features.h>
 
@@ -4464,6 +4470,43 @@ CPPUNIT_TEST_FIXTURE(SwLayoutWriter, testTdf135035)
     CPPUNIT_ASSERT_GREATER(nParentWidth, nFly3Width);
 }
 
+CPPUNIT_TEST_FIXTURE(SwLayoutWriter, testTdf137185)
+{
+    // First load the sample bugdoc
+    load(DATA_DIRECTORY, "tdf137185.odt");
+    SwXTextDocument* pTextDoc = 
dynamic_cast<SwXTextDocument*>(mxComponent.get());
+    CPPUNIT_ASSERT(pTextDoc);
+    // Get the doc shell
+    SwDoc* pDoc(pTextDoc->GetDocShell()->GetDoc());
+
+    // Get the DrawObject from page
+    auto pModel = pDoc->getIDocumentDrawModelAccess().GetDrawModel();
+    CPPUNIT_ASSERT(pModel);
+    auto pPage = pModel->GetPage(0);
+    CPPUNIT_ASSERT(pModel);
+    auto pObj = pPage->GetObj(0);
+    CPPUNIT_ASSERT(pObj);
+
+    // Get the format of the draw object
+    auto pShape = FindFrameFormat(pObj);
+    CPPUNIT_ASSERT(pShape);
+
+    // Check the text of the shape
+    uno::Reference<text::XText> xTxt(getShape(1), uno::UNO_QUERY);
+    CPPUNIT_ASSERT_EQUAL(OUString("Align me!"), xTxt->getText()->getString());
+
+    // Add a textbox to the shape
+    SwTextBoxHelper::create(pShape, true);
+
+    // Check if the text moved from the shape to the frame
+    auto pFormat = SwTextBoxHelper::getOtherTextBoxFormat(getShape(1));
+    auto xTextFrame = SwXTextFrame::CreateXTextFrame(*pFormat->GetDoc(), 
pFormat);
+
+    CPPUNIT_ASSERT_EQUAL(OUString("Align me!"), 
xTextFrame->getText()->getString());
+    CPPUNIT_ASSERT_EQUAL(OUString(), xTxt->getText()->getString());
+    // Before the patch it failled, because the text appeared 2 times on each 
other.
+}
+
 CPPUNIT_PLUGIN_IMPLEMENT();
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sw/source/core/doc/textboxhelper.cxx 
b/sw/source/core/doc/textboxhelper.cxx
index 69b369e2525c..c900255a1a7e 100644
--- a/sw/source/core/doc/textboxhelper.cxx
+++ b/sw/source/core/doc/textboxhelper.cxx
@@ -15,6 +15,7 @@
 #include <fmtfsize.hxx>
 #include <doc.hxx>
 #include <IDocumentLayoutAccess.hxx>
+#include <IDocumentState.hxx>
 #include <docsh.hxx>
 #include <unocoll.hxx>
 #include <unoframe.hxx>
@@ -49,12 +50,27 @@
 
 using namespace com::sun::star;
 
-void SwTextBoxHelper::create(SwFrameFormat* pShape)
+void SwTextBoxHelper::create(SwFrameFormat* pShape, bool bCopyText)
 {
     // If TextBox wasn't enabled previously
     if (pShape->GetAttrSet().HasItem(RES_CNTNT) && 
pShape->GetOtherTextBoxFormat())
         return;
 
+    // Store the current text conent of the shape
+    OUString sCopyableText;
+
+    if (bCopyText)
+    {
+        if (auto pSdrShape = pShape->FindRealSdrObject())
+        {
+            uno::Reference<text::XText> xSrcCnt(pSdrShape->getWeakUnoShape(), 
uno::UNO_QUERY);
+            auto xCur = xSrcCnt->createTextCursor();
+            xCur->gotoStart(false);
+            xCur->gotoEnd(true);
+            sCopyableText = xCur->getText()->getString();
+        }
+    }
+
     // Create the associated TextFrame and insert it into the document.
     uno::Reference<text::XTextContent> xTextFrame(
         SwXServiceProvider::MakeInstance(SwServiceType::TypeTextFrame, 
*pShape->GetDoc()),
@@ -171,6 +187,24 @@ void SwTextBoxHelper::create(SwFrameFormat* pShape)
 
         if (aTxFrmSet.Count())
             pFormat->SetFormatAttr(aTxFrmSet);
+
+        // Check if the shape had text before and move it to the new textframe
+        if (bCopyText && !sCopyableText.isEmpty())
+        {
+            auto pSdrShape = pShape->FindRealSdrObject();
+            if (pSdrShape)
+            {
+                auto pSourceText = dynamic_cast<SdrTextObj*>(pSdrShape);
+                uno::Reference<text::XTextRange> xDestText(xRealTextFrame, 
uno::UNO_QUERY);
+
+                xDestText->setString(sCopyableText);
+
+                if (pSourceText)
+                    pSourceText->SetText(OUString());
+
+                pShape->GetDoc()->getIDocumentState().SetModified();
+            }
+        }
     }
 }
 
diff --git a/sw/source/uibase/shells/drawsh.cxx 
b/sw/source/uibase/shells/drawsh.cxx
index 958a38b2a39a..fd7e93869695 100644
--- a/sw/source/uibase/shells/drawsh.cxx
+++ b/sw/source/uibase/shells/drawsh.cxx
@@ -359,7 +359,7 @@ void SwDrawShell::Execute(SfxRequest &rReq)
             {
                 SwFrameFormat* pFrameFormat = ::FindFrameFormat(pObj);
                 if (pFrameFormat)
-                    SwTextBoxHelper::create(pFrameFormat);
+                    SwTextBoxHelper::create(pFrameFormat, pObj->HasText());
             }
             break;
         }
_______________________________________________
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits

Reply via email to