sw/qa/core/frmedt/data/floating-table-caption.docx |binary
 sw/qa/core/frmedt/frmedt.cxx                       |   28 +++++++++++++++++++++
 sw/source/core/frmedt/fews.cxx                     |   18 ++++++++++++-
 3 files changed, 45 insertions(+), 1 deletion(-)

New commits:
commit 107de1a2c6882213cf0ef6783417302f43cdada0
Author:     Miklos Vajna <vmik...@collabora.com>
AuthorDate: Thu Aug 3 08:50:39 2023 +0200
Commit:     Miklos Vajna <vmik...@collabora.com>
CommitDate: Thu Aug 3 09:30:38 2023 +0200

    tdf#156349 sw floattable: fix caption insert for tables inside split fly 
frames
    
    Inserting a long enough caption below the table inside a split fly
    resulted in a layout loop.
    
    An additional, second problem is that in case a split fly only has a
    table, that can be mapped to a floating table in Word formats, but once
    a caption is inserted after the table (but still inside the frame), then
    this will be a normal shape, which can't be split.
    
    Fix the problem by disabling the "is split allowed" property on the fly
    in case it's on, and only then insert the caption.
    
    This not only avoids the layout loop, it also results in rendering that
    will match after save + load, for Word formats.
    
    Change-Id: Ie8b04e6266dd5c4068e5add86efb09cc6a6ea01f
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/155276
    Reviewed-by: Miklos Vajna <vmik...@collabora.com>
    Tested-by: Jenkins

diff --git a/sw/qa/core/frmedt/data/floating-table-caption.docx 
b/sw/qa/core/frmedt/data/floating-table-caption.docx
new file mode 100644
index 000000000000..18c4690322d8
Binary files /dev/null and b/sw/qa/core/frmedt/data/floating-table-caption.docx 
differ
diff --git a/sw/qa/core/frmedt/frmedt.cxx b/sw/qa/core/frmedt/frmedt.cxx
index 26c64c0833f8..f5a12e9392f1 100644
--- a/sw/qa/core/frmedt/frmedt.cxx
+++ b/sw/qa/core/frmedt/frmedt.cxx
@@ -23,6 +23,9 @@
 #include <unotxdoc.hxx>
 #include <docsh.hxx>
 #include <swdtflvr.hxx>
+#include <caption.hxx>
+#include <view.hxx>
+#include <formatflysplit.hxx>
 
 /// Covers sw/source/core/frmedt/ fixes.
 class SwCoreFrmedtTest : public SwModelTestBase
@@ -168,6 +171,31 @@ CPPUNIT_TEST_FIXTURE(SwCoreFrmedtTest, 
testTextBoxSelectCursorPos)
     CPPUNIT_ASSERT_EQUAL(nAnchor, nCursor);
 }
 
+CPPUNIT_TEST_FIXTURE(SwCoreFrmedtTest, testSplitFlyInsertCaption)
+{
+    // Given a document with a full-page floating table:
+    createSwDoc("floating-table-caption.docx");
+
+    // When trying to insert a caption below that table:
+    SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell();
+    pWrtShell->GotoTable("Table1");
+    InsCaptionOpt aOpt;
+    SwView& rView = pWrtShell->GetView();
+    aOpt.SetCategory("Table");
+    aOpt.SetCaption("Numbers English-German");
+    // After, not before.
+    aOpt.SetPos(1);
+    // Without the accompanying fix in place, this call never finished, layout 
didn't handle content
+    // after the table in a floating table.
+    rView.InsertCaption(&aOpt);
+
+    // Then make sure the insertion finishes and now this is just a plain 
table-in-frame:
+    SwDoc* pDoc = getSwDoc();
+    sw::SpzFrameFormats& rFlys = *pDoc->GetSpzFrameFormats();
+    sw::SpzFrameFormat* pFly = rFlys[0];
+    CPPUNIT_ASSERT(!pFly->GetAttrSet().GetFlySplit().GetValue());
+}
+
 CPPUNIT_PLUGIN_IMPLEMENT();
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sw/source/core/frmedt/fews.cxx b/sw/source/core/frmedt/fews.cxx
index c89e26cf6a2c..cb26f3357225 100644
--- a/sw/source/core/frmedt/fews.cxx
+++ b/sw/source/core/frmedt/fews.cxx
@@ -50,6 +50,7 @@
 #include <dflyobj.hxx>
 #include <dcontact.hxx>
 #include <UndoInsert.hxx>
+#include <formatflysplit.hxx>
 
 using namespace com::sun::star;
 
@@ -452,9 +453,24 @@ void SwFEShell::InsertLabel( const SwLabelType eType, 
const OUString &rText, con
         if( pCnt->IsInTab() )
         {
             // pass down index to the TableNode for tables
-            const SwTable& rTable = *pCnt->FindTabFrame()->GetTable();
+            SwTabFrame* pTabFrame = pCnt->FindTabFrame();
+            const SwTable& rTable = *pTabFrame->GetTable();
             nIdx = rTable.GetTabSortBoxes()[ 0 ]
                         ->GetSttNd()->FindTableNode()->GetIndex();
+
+            SwFlyFrame* pFly = pTabFrame->FindFlyFrame();
+            if (pFly && pFly->IsFlySplitAllowed())
+            {
+                // This table is in a split fly, but we will insert a label, 
which means this is not
+                // a floating table anymore, disable the "can split" bit, 
it'll be hidden on the UI
+                // anyway.
+                SwFrameFormat& rFlyFormat = pFly->GetFrameFormat();
+                SfxItemSetFixed<RES_FLY_SPLIT, RES_FLY_SPLIT> 
aSet(GetDoc()->GetAttrPool());
+                SwFormatFlySplit aSplit(false);
+                aSet.Put(aSplit);
+                // SwUndoFormatAttr is created for us.
+                GetDoc()->SetFlyFrameAttr(rFlyFormat, aSet);
+            }
         }
         break;
     case SwLabelType::Draw:

Reply via email to