tdf#104596 sw COMPAT layout: wrap in header for in-table flies

You might have noticed that text in header/footers
will not wrap around fly frames, but just run underneath,
regardless of the wrap settings. Strange, eh?
[This is also true in footnotes.]

In an ancient effort to be compatible with MS strangeness,
OOo decided to do this as well for interoperability reasons.
http://openoffice.org/specs/writer/compatibility/adjust-text-wrapping.sxw

Apparently, flies in tables are exempt from that
rule in MSO, so this patch adds that exemption.

TABLE EXEMPTION IS AN EXPERIMENTAL ASSUMPTION
BASED ON VISUAL OBSERVATION FROM THIS BUG REPORT.
IT IS NOT BASED ON DOCUMENTATION.
I did look in DOC and DOCX manuals, and did a google
search, but found nothing.

A compat variable keeps older ODT files no-wrap,
so that we don't break layout of existing documents.
This variable is only read in the ODT import filter.
If it doesn't exist for ODT, it is set to false.
By default it is true, so it automatically is
enabled for anything that doesn't modify it in its
import filter, including all DOC/DOCX/RTF etc,
and newly created ODT documents.
In other words, allowing wrapping in the header for table-anchors
is the new default behaviour unless an import filter turns it off.

Headers/footers are the most common example. I also tested with
footnotes, and found that Word 2016 does wrap in that case as well,
even though the UI only allows AS_CHAR anchoring.

FYI: Allowing wrapping at ALL times
can be set with the Writer compatibility option
"Use OpenOffice.org 1.1 text wrapping around objects".

Change-Id: I9ad0c82df4af794079cce86fad9e401ea4575e59
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/92378
Tested-by: Jenkins
Reviewed-by: Justin Luth <justin_luth@sil.org>
diff --git a/sw/inc/IDocumentSettingAccess.hxx b/sw/inc/IDocumentSettingAccess.hxx
index 8ff48ff..cbf8dbf 100644
--- a/sw/inc/IDocumentSettingAccess.hxx
+++ b/sw/inc/IDocumentSettingAccess.hxx
@@ -49,6 +49,7 @@ enum class DocumentSettingId
    ADD_PARA_SPACING_TO_TABLE_CELLS,
    USE_FORMER_OBJECT_POS,
    USE_FORMER_TEXT_WRAPPING,
    ALLOW_WRAP_WHEN_ANCHORED_IN_TABLE, // change LO to match MS allowing wrapping around fly-frames anchored in tables in header & footnote
    CONSIDER_WRAP_ON_OBJECT_POSITION,

    IGNORE_FIRST_LINE_INDENT_IN_NUMBERING,
diff --git a/sw/qa/extras/odfexport/data/tdf104596_breakingExample.odt b/sw/qa/extras/odfexport/data/tdf104596_breakingExample.odt
new file mode 100644
index 0000000..19d0e85
--- /dev/null
+++ b/sw/qa/extras/odfexport/data/tdf104596_breakingExample.odt
Binary files differ
diff --git a/sw/qa/extras/odfexport/odfexport.cxx b/sw/qa/extras/odfexport/odfexport.cxx
index ddf37f1..c084c00 100644
--- a/sw/qa/extras/odfexport/odfexport.cxx
+++ b/sw/qa/extras/odfexport/odfexport.cxx
@@ -449,6 +449,12 @@ DECLARE_ODFEXPORT_TEST(testredlineTextFrame, "redlineTextFrame.odt")
    CPPUNIT_ASSERT_EQUAL(sal_Int32(1), xIndexAccess->getCount());
}

DECLARE_ODFEXPORT_TEST(testTdf104596_breakingExample, "tdf104596_breakingExample.odt")
{
    //wrapping should be ignored on in-table flies for pre-LO7.1 documents. ALLOW_WRAP_WHEN_ANCHORED_IN_TABLE
    CPPUNIT_ASSERT_EQUAL_MESSAGE( "Fits on one page", 1, getPages() );
}

DECLARE_ODFEXPORT_TEST(testTdf131621, "tdf131621.ott")
{
    CPPUNIT_ASSERT_EQUAL(12, getShapes());
diff --git a/sw/qa/extras/ww8export/data/tdf104596_wrapInHeaderTable.doc b/sw/qa/extras/ww8export/data/tdf104596_wrapInHeaderTable.doc
new file mode 100644
index 0000000..17fba79
--- /dev/null
+++ b/sw/qa/extras/ww8export/data/tdf104596_wrapInHeaderTable.doc
Binary files differ
diff --git a/sw/qa/extras/ww8export/ww8export3.cxx b/sw/qa/extras/ww8export/ww8export3.cxx
index 2de1782..13da2fe 100644
--- a/sw/qa/extras/ww8export/ww8export3.cxx
+++ b/sw/qa/extras/ww8export/ww8export3.cxx
@@ -53,6 +53,16 @@ DECLARE_WW8EXPORT_TEST(testTdf37778_readonlySection, "tdf37778_readonlySection.d
    CPPUNIT_ASSERT(drawing::FillStyle_NONE != getProperty<drawing::FillStyle>(xStyle, "FillStyle"));
}

DECLARE_WW8EXPORT_TEST(testTdf104596_wrapInHeaderTable, "tdf104596_wrapInHeaderTable.doc")
{
    xmlDocUniquePtr pXmlDoc = parseLayoutDump();

    sal_Int32 nRowHeight = getXPath(pXmlDoc, "//header/tab/row[1]/infos/bounds", "height").toInt32();
    // The fly is supposed to be no-wrap, so the text should come underneath it, not wrap-through,
    // thus making the row much higher. Before, height was 706. Now it is 1067.
    CPPUNIT_ASSERT_MESSAGE("Text must wrap under green box", nRowHeight > 1000);
}

DECLARE_WW8EXPORT_TEST(testArabicZeroNumbering, "arabic-zero-numbering.doc")
{
    auto xNumberingRules
diff --git a/sw/source/core/doc/DocumentSettingManager.cxx b/sw/source/core/doc/DocumentSettingManager.cxx
index cc092dc..73421ce 100644
--- a/sw/source/core/doc/DocumentSettingManager.cxx
+++ b/sw/source/core/doc/DocumentSettingManager.cxx
@@ -98,7 +98,8 @@ sw::DocumentSettingManager::DocumentSettingManager(SwDoc &rDoc)
    mbProtectBookmarks(false),
    mbProtectFields(false),
    mbHeaderSpacingBelowLastPara(false),
    mbFrameAutowidthWithMorePara(false)
    mbFrameAutowidthWithMorePara(false),
    mbAllowWrapWhenAnchoredInTable(true) //tdf#104596

    // COMPATIBILITY FLAGS END
{
@@ -228,6 +229,7 @@ bool sw::DocumentSettingManager::get(/*[in]*/ DocumentSettingId id) const
        case DocumentSettingId::PROTECT_FIELDS: return mbProtectFields;
        case DocumentSettingId::HEADER_SPACING_BELOW_LAST_PARA: return mbHeaderSpacingBelowLastPara;
        case DocumentSettingId::FRAME_AUTOWIDTH_WITH_MORE_PARA: return mbFrameAutowidthWithMorePara;
        case DocumentSettingId::ALLOW_WRAP_WHEN_ANCHORED_IN_TABLE: return mbAllowWrapWhenAnchoredInTable;
        default:
            OSL_FAIL("Invalid setting id");
    }
@@ -479,6 +481,9 @@ void sw::DocumentSettingManager::set(/*[in]*/ DocumentSettingId id, /*[in]*/ boo
        case DocumentSettingId::FRAME_AUTOWIDTH_WITH_MORE_PARA:
            mbFrameAutowidthWithMorePara = value;
            break;
        case DocumentSettingId::ALLOW_WRAP_WHEN_ANCHORED_IN_TABLE:
            mbAllowWrapWhenAnchoredInTable = value;
            break;
        default:
            OSL_FAIL("Invalid setting id");
    }
@@ -652,6 +657,7 @@ void sw::DocumentSettingManager::ReplaceCompatibilityOptions(const DocumentSetti
    // No mbProtectFields: this is false by default everywhere
    mbHeaderSpacingBelowLastPara = rSource.mbHeaderSpacingBelowLastPara;
    mbFrameAutowidthWithMorePara = rSource.mbFrameAutowidthWithMorePara;
    mbAllowWrapWhenAnchoredInTable = rSource.mbAllowWrapWhenAnchoredInTable;
}

sal_uInt32 sw::DocumentSettingManager::Getn32DummyCompatibilityOptions1() const
@@ -957,6 +963,11 @@ void sw::DocumentSettingManager::dumpAsXml(xmlTextWriterPtr pWriter) const
        BAD_CAST(OString::boolean(mbFrameAutowidthWithMorePara).getStr()));
    xmlTextWriterEndElement(pWriter);

    xmlTextWriterStartElement(pWriter, BAD_CAST("mbAllowWrapWhenAnchoredInTable"));
    xmlTextWriterWriteAttribute(pWriter, BAD_CAST("value"),
        BAD_CAST(OString::boolean(mbAllowWrapWhenAnchoredInTable).getStr()));
    xmlTextWriterEndElement(pWriter);

    xmlTextWriterEndElement(pWriter);
}

diff --git a/sw/source/core/inc/DocumentSettingManager.hxx b/sw/source/core/inc/DocumentSettingManager.hxx
index 8b409eb..c8caa508 100644
--- a/sw/source/core/inc/DocumentSettingManager.hxx
+++ b/sw/source/core/inc/DocumentSettingManager.hxx
@@ -167,6 +167,8 @@ class DocumentSettingManager :
    bool mbProtectFields;
    bool mbHeaderSpacingBelowLastPara;
    bool mbFrameAutowidthWithMorePara; //tdf#124423
    bool mbAllowWrapWhenAnchoredInTable;

public:

    DocumentSettingManager(SwDoc &rDoc);
diff --git a/sw/source/core/text/txtfly.cxx b/sw/source/core/text/txtfly.cxx
index 52979fd..cbaee31 100644
--- a/sw/source/core/text/txtfly.cxx
+++ b/sw/source/core/text/txtfly.cxx
@@ -834,10 +834,13 @@ SwAnchoredObjList* SwTextFly::InitAnchoredObjList()
    const size_t nCount = pSorted ? pSorted->size() : 0;
    // --> #108724# Page header/footer content doesn't have to wrap around
    //              floating screen objects
    //              which was added simply to be compatible with MS Office.
    // MSO still allows text to wrap around in-table-flies in headers/footers/footnotes
    const bool bFooterHeader = nullptr != m_pCurrFrame->FindFooterOrHeader();
    const IDocumentSettingAccess* pIDSA = &m_pCurrFrame->GetDoc().getIDocumentSettingAccess();
    // #i40155# - check, if frame is marked not to wrap
    const bool bWrapAllowed = ( pIDSA->get(DocumentSettingId::USE_FORMER_TEXT_WRAPPING) ||
                                    (pIDSA->get(DocumentSettingId::ALLOW_WRAP_WHEN_ANCHORED_IN_TABLE) && m_pCurrFrame->IsInTab()) ||
                                    (!m_pCurrFrame->IsInFootnote() && !bFooterHeader));

    bOn = false;
diff --git a/sw/source/filter/xml/xmlimp.cxx b/sw/source/filter/xml/xmlimp.cxx
index 5495d67..bcec7a2 100644
--- a/sw/source/filter/xml/xmlimp.cxx
+++ b/sw/source/filter/xml/xmlimp.cxx
@@ -1358,6 +1358,7 @@ void SwXMLImport::SetConfigurationSettings(const Sequence < PropertyValue > & aC
    bool bPropLineSpacingShrinksFirstLine = false;
    bool bSubtractFlysAnchoredAtFlys = false;
    bool bCollapseEmptyCellPara = false;
    bool bIsSetAllowWrapWhenAnchoredInTable = false;

    const PropertyValue* currentDatabaseDataSource = nullptr;
    const PropertyValue* currentDatabaseCommand = nullptr;
@@ -1454,6 +1455,8 @@ void SwXMLImport::SetConfigurationSettings(const Sequence < PropertyValue > & aC
                    bSubtractFlysAnchoredAtFlys = true;
                else if (rValue.Name == "CollapseEmptyCellPara")
                    bCollapseEmptyCellPara = true;
                else if ( rValue.Name == "AllowWrapWhenAnchoredInTable" )
                    bIsSetAllowWrapWhenAnchoredInTable = true;
            }
            catch( Exception& )
            {
@@ -1624,6 +1627,9 @@ void SwXMLImport::SetConfigurationSettings(const Sequence < PropertyValue > & aC
    if (!bCollapseEmptyCellPara)
        xProps->setPropertyValue("CollapseEmptyCellPara", makeAny(false));

    if (!bIsSetAllowWrapWhenAnchoredInTable)
        xProps->setPropertyValue("AllowWrapWhenAnchoredInTable", makeAny(false));

    SwDoc *pDoc = getDoc();
    SfxPrinter *pPrinter = pDoc->getIDocumentDeviceAccess().getPrinter( false );
    if( pPrinter )
diff --git a/sw/source/uibase/uno/SwXDocumentSettings.cxx b/sw/source/uibase/uno/SwXDocumentSettings.cxx
index 9af6745..3d103db 100644
--- a/sw/source/uibase/uno/SwXDocumentSettings.cxx
+++ b/sw/source/uibase/uno/SwXDocumentSettings.cxx
@@ -145,6 +145,7 @@ enum SwDocumentSettingsPropertyHandles
    HANDLE_PROTECT_FIELDS,
    HANDLE_HEADER_SPACING_BELOW_LAST_PARA,
    HANDLE_FRAME_AUTOWIDTH_WITH_MORE_PARA,
    HANDLE_ALLOW_WRAP_WHEN_ANCHORED_IN_TABLE,
};

}
@@ -237,6 +238,7 @@ static MasterPropertySetInfo * lcl_createSettingsInfo()
        { OUString("ProtectFields"), HANDLE_PROTECT_FIELDS, cppu::UnoType<bool>::get(), 0 },
        { OUString("HeaderSpacingBelowLastPara"), HANDLE_HEADER_SPACING_BELOW_LAST_PARA, cppu::UnoType<bool>::get(), 0 },
        { OUString("FrameAutowidthWithMorePara"), HANDLE_FRAME_AUTOWIDTH_WITH_MORE_PARA, cppu::UnoType<bool>::get(), 0 },
        { OUString("AllowWrapWhenAnchoredInTable"), HANDLE_ALLOW_WRAP_WHEN_ANCHORED_IN_TABLE, cppu::UnoType<bool>::get(), 0 },

/*
 * As OS said, we don't have a view when we need to set this, so I have to
@@ -992,6 +994,15 @@ void SwXDocumentSettings::_setSingleValue( const comphelper::PropertyInfo & rInf
            }
        }
        break;
        case HANDLE_ALLOW_WRAP_WHEN_ANCHORED_IN_TABLE:
        {
            bool bTmp;
            if (rValue >>= bTmp)
            {
                mpDoc->getIDocumentSettingAccess().set(DocumentSettingId::ALLOW_WRAP_WHEN_ANCHORED_IN_TABLE, bTmp);
            }
        }
        break;
        default:
            throw UnknownPropertyException(OUString::number(rInfo.mnHandle));
    }
@@ -1483,6 +1494,12 @@ void SwXDocumentSettings::_getSingleValue( const comphelper::PropertyInfo & rInf
                DocumentSettingId::FRAME_AUTOWIDTH_WITH_MORE_PARA);
        }
        break;
        case HANDLE_ALLOW_WRAP_WHEN_ANCHORED_IN_TABLE:
        {
            rValue <<= mpDoc->getIDocumentSettingAccess().get(
                DocumentSettingId::ALLOW_WRAP_WHEN_ANCHORED_IN_TABLE);
        }
        break;
        default:
            throw UnknownPropertyException(OUString::number(rInfo.mnHandle));
    }