tdf#142635: use the same ScDocument for clipboard and GlobalDrawPersist

This way, the document used for chart copy has the proper sheets, and
the data ranges are created correctly.

This reverts commit 8d98dd8b3896c14af057e90e3a327172a9262e03
  Author Mike Kaganski <mike.kaganski@collabora.com>
  Date   Thu Jun 03 13:15:11 2021 +0200
    tdf#142635: skip UITest for now

Change-Id: I0a3aa6028f49ded278f8665556007b1f722e21b0
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/134251
Tested-by: Jenkins
Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
diff --git a/sc/qa/uitest/chart/copyPaste.py b/sc/qa/uitest/chart/copyPaste.py
index dfd38cb..2df579d 100644
--- a/sc/qa/uitest/chart/copyPaste.py
+++ b/sc/qa/uitest/chart/copyPaste.py
@@ -7,8 +7,6 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#

import unittest
import platform
from uitest.framework import UITestCase
from libreoffice.calc.document import get_cell_by_position
from libreoffice.uno.propertyvalue import mkPropertyValues
@@ -16,7 +14,6 @@ from uitest.uihelper.common import get_url_for_data_file

class CopyPaste(UITestCase):

  @unittest.skipIf(platform.system() == "Windows", "Fails on Windows: tdf#142635")
  def test_copy_paste_chart_with_dot_in_sheet_name(self):
    with self.ui_test.load_file(get_url_for_data_file("chartWithDotInSheetName.ods")) as calc_doc:

diff --git a/sc/source/core/data/documen3.cxx b/sc/source/core/data/documen3.cxx
index 37c0839..77afc2f 100644
--- a/sc/source/core/data/documen3.cxx
+++ b/sc/source/core/data/documen3.cxx
@@ -1954,7 +1954,8 @@ void ScDocument::SetDocOptions( const ScDocOptions& rOpt )
    assert(pDocOptions && "No DocOptions! :-(");

    *pDocOptions = rOpt;
    mxPoolHelper->SetFormTableOpt(rOpt);
    if (mxPoolHelper)
        mxPoolHelper->SetFormTableOpt(rOpt);
}

const ScViewOptions& ScDocument::GetViewOptions() const
diff --git a/sc/source/core/data/documen5.cxx b/sc/source/core/data/documen5.cxx
index 1c69524..c4f7643 100644
--- a/sc/source/core/data/documen5.cxx
+++ b/sc/source/core/data/documen5.cxx
@@ -362,6 +362,9 @@ void ScDocument::UpdateChart( const OUString& rChartName )

void ScDocument::RestoreChartListener( const OUString& rName )
{
    if (!pChartListenerCollection)
        return;

    // Read the data ranges from the chart object, and start listening to those ranges again
    // (called when a chart is saved, because then it might be swapped out and stop listening itself).

diff --git a/sc/source/core/data/document.cxx b/sc/source/core/data/document.cxx
index 8791c3a..8cf93a6 100644
--- a/sc/source/core/data/document.cxx
+++ b/sc/source/core/data/document.cxx
@@ -6155,12 +6155,12 @@ ScPatternAttr* ScDocument::GetDefPattern() const

ScDocumentPool* ScDocument::GetPool()
{
    return mxPoolHelper->GetDocPool();
    return mxPoolHelper ? mxPoolHelper->GetDocPool() : nullptr;
}

ScStyleSheetPool* ScDocument::GetStyleSheetPool() const
{
    return mxPoolHelper->GetStylePool();
    return mxPoolHelper ? mxPoolHelper->GetStylePool() : nullptr;
}

bool ScDocument::IsEmptyData(SCCOL nStartCol, SCROW nStartRow, SCCOL nEndCol, SCROW nEndRow, SCTAB nTab) const
diff --git a/sc/source/ui/app/transobj.cxx b/sc/source/ui/app/transobj.cxx
index fcc5158..d0be7e0 100644
--- a/sc/source/ui/app/transobj.cxx
+++ b/sc/source/ui/app/transobj.cxx
@@ -109,8 +109,8 @@ void ScTransferObj::PaintToDev( OutputDevice* pDev, ScDocument& rDoc, double nPr
    ScPrintFunc::DrawToDev( rDoc, pDev, nPrintFactor, aBound, &aViewData, false/*bMetaFile*/ );
}

ScTransferObj::ScTransferObj( ScDocumentUniquePtr pClipDoc, const TransferableObjectDescriptor& rDesc ) :
    m_pDoc( std::move(pClipDoc ) ),
ScTransferObj::ScTransferObj( const std::shared_ptr<ScDocument>& pClipDoc, const TransferableObjectDescriptor& rDesc ) :
    m_pDoc( pClipDoc ),
    m_nNonFiltered(0),
    m_aObjDesc( rDesc ),
    m_nDragHandleX( 0 ),
@@ -813,14 +813,14 @@ void ScTransferObj::InitDocShell(bool bLimitToPageSize)
        rDestDoc.UpdateChartListenerCollection();
}

SfxObjectShell* ScTransferObj::SetDrawClipDoc( bool bAnyOle )
SfxObjectShell* ScTransferObj::SetDrawClipDoc( bool bAnyOle, const std::shared_ptr<ScDocument>& pDoc )
{
    // update ScGlobal::xDrawClipDocShellRef

    ScGlobal::xDrawClipDocShellRef.clear();
    if (bAnyOle)
    {
        ScGlobal::xDrawClipDocShellRef = new ScDocShell(SfxModelFlags::EMBEDDED_OBJECT | SfxModelFlags::DISABLE_EMBEDDED_SCRIPTS); // there must be a ref
        ScGlobal::xDrawClipDocShellRef = new ScDocShell(SfxModelFlags::EMBEDDED_OBJECT | SfxModelFlags::DISABLE_EMBEDDED_SCRIPTS, pDoc); // there must be a ref
        ScGlobal::xDrawClipDocShellRef->DoInitNew();
    }

diff --git a/sc/source/ui/docshell/docsh.cxx b/sc/source/ui/docshell/docsh.cxx
index dd57fc6..4a55665 100644
--- a/sc/source/ui/docshell/docsh.cxx
+++ b/sc/source/ui/docshell/docsh.cxx
@@ -2857,9 +2857,9 @@ std::unique_ptr<ScDocFunc> ScDocShell::CreateDocFunc()
    return std::make_unique<ScDocFuncDirect>( *this );
}

ScDocShell::ScDocShell( const SfxModelFlags i_nSfxCreationFlags ) :
ScDocShell::ScDocShell( const SfxModelFlags i_nSfxCreationFlags, const std::shared_ptr<ScDocument>& pDoc ) :
    SfxObjectShell( i_nSfxCreationFlags ),
    m_pDocument       (std::make_shared<ScDocument>( SCDOCMODE_DOCUMENT, this )),
    m_pDocument       ( pDoc ? pDoc : std::make_shared<ScDocument>( SCDOCMODE_DOCUMENT, this )),
    m_aDdeTextFmt(OUString("TEXT")),
    m_nPrtToScreenFactor( 1.0 ),
    m_pImpl           ( new DocShell_Impl ),
diff --git a/sc/source/ui/docshell/docsh2.cxx b/sc/source/ui/docshell/docsh2.cxx
index 1668297..04faa6e 100644
--- a/sc/source/ui/docshell/docsh2.cxx
+++ b/sc/source/ui/docshell/docsh2.cxx
@@ -58,24 +58,27 @@ bool ScDocShell::InitNew( const uno::Reference < embed::XStorage >& xStor )
    // InitOptions sets the document languages, must be called before CreateStandardStyles
    InitOptions(false);

    m_pDocument->GetStyleSheetPool()->CreateStandardStyles();
    m_pDocument->UpdStlShtPtrsFrmNms();

    if (!m_bUcalcTest)
    if (ScStyleSheetPool* pStyleSheetPool = m_pDocument->GetStyleSheetPool())
    {
        /* Create styles that are imported through Orcus */
        pStyleSheetPool->CreateStandardStyles();
        m_pDocument->UpdStlShtPtrsFrmNms();

        OUString aURL("$BRAND_BASE_DIR/" LIBO_SHARE_FOLDER "/calc/styles.xml");
        rtl::Bootstrap::expandMacros(aURL);

        OUString aPath;
        osl::FileBase::getSystemPathFromFileURL(aURL, aPath);

        ScOrcusFilters* pOrcus = ScFormatFilter::Get().GetOrcusFilters();
        if (pOrcus)
        if (!m_bUcalcTest)
        {
            pOrcus->importODS_Styles(*m_pDocument, aPath);
            m_pDocument->GetStyleSheetPool()->setAllParaStandard();
            /* Create styles that are imported through Orcus */

            OUString aURL("$BRAND_BASE_DIR/" LIBO_SHARE_FOLDER "/calc/styles.xml");
            rtl::Bootstrap::expandMacros(aURL);

            OUString aPath;
            osl::FileBase::getSystemPathFromFileURL(aURL, aPath);

            ScOrcusFilters* pOrcus = ScFormatFilter::Get().GetOrcusFilters();
            if (pOrcus)
            {
                pOrcus->importODS_Styles(*m_pDocument, aPath);
                pStyleSheetPool->setAllParaStandard();
            }
        }
    }

diff --git a/sc/source/ui/inc/docsh.hxx b/sc/source/ui/inc/docsh.hxx
index a2655c0..95bd297 100644
--- a/sc/source/ui/inc/docsh.hxx
+++ b/sc/source/ui/inc/docsh.hxx
@@ -170,7 +170,7 @@ private:

public:
    explicit        ScDocShell( const ScDocShell& rDocShell ) = delete;
    explicit        ScDocShell( const SfxModelFlags i_nSfxCreationFlags = SfxModelFlags::EMBEDDED_OBJECT );
    explicit        ScDocShell( const SfxModelFlags i_nSfxCreationFlags = SfxModelFlags::EMBEDDED_OBJECT, const std::shared_ptr<ScDocument>& pDoc = {} );
                    virtual ~ScDocShell() override;

    virtual SfxUndoManager*
diff --git a/sc/source/ui/inc/transobj.hxx b/sc/source/ui/inc/transobj.hxx
index 8861cfb..adc58b6 100644
--- a/sc/source/ui/inc/transobj.hxx
+++ b/sc/source/ui/inc/transobj.hxx
@@ -38,7 +38,7 @@ namespace com::sun::star {
class SAL_DLLPUBLIC_RTTI ScTransferObj : public TransferDataContainer
{
private:
    ScDocumentUniquePtr             m_pDoc;
    std::shared_ptr<ScDocument>     m_pDoc;
    ScRange                         m_aBlock;
    SCROW                           m_nNonFiltered;       // non-filtered rows
    TransferableObjectDescriptor    m_aObjDesc;
@@ -67,7 +67,7 @@ private:
    static void GetAreaSize( const ScDocument& rDoc, SCTAB nTab1, SCTAB nTab2, SCROW& nRow, SCCOL& nCol );

public:
            ScTransferObj( ScDocumentUniquePtr pClipDoc, const TransferableObjectDescriptor& rDesc );
            ScTransferObj( const std::shared_ptr<ScDocument>& pClipDoc, const TransferableObjectDescriptor& rDesc );
    virtual ~ScTransferObj() override;

    virtual void        AddSupportedFormats() override;
@@ -104,7 +104,7 @@ public:

    static  SC_DLLPUBLIC ScTransferObj* GetOwnClipboard(const css::uno::Reference<css::datatransfer::XTransferable2>&);

    static SfxObjectShell*  SetDrawClipDoc( bool bAnyOle );     // update ScGlobal::xDrawClipDocShellRef
    static SfxObjectShell* SetDrawClipDoc(bool bAnyOle, const std::shared_ptr<ScDocument>& = {} );     // update ScGlobal::xDrawClipDocShellRef
    virtual sal_Int64 SAL_CALL getSomething( const com::sun::star::uno::Sequence< sal_Int8 >& rId ) override;
    static const com::sun::star::uno::Sequence< sal_Int8 >& getUnoTunnelId();
};
diff --git a/sc/source/ui/view/viewfun3.cxx b/sc/source/ui/view/viewfun3.cxx
index 73b32c0..6f98077 100644
--- a/sc/source/ui/view/viewfun3.cxx
+++ b/sc/source/ui/view/viewfun3.cxx
@@ -226,10 +226,12 @@ bool ScViewFunc::CopyToClipSingleRange( ScDocument* pClipDoc, const ScRangeList&
        return false;

    bool bSysClip = false;
    std::shared_ptr<ScDocument> pSysClipDoc;
    if ( !pClipDoc )                                    // no clip doc specified
    {
        // Create one (deleted by ScTransferObj).
        pClipDoc = new ScDocument( SCDOCMODE_CLIP );
        pSysClipDoc = std::make_shared<ScDocument>( SCDOCMODE_CLIP );
        pClipDoc = pSysClipDoc.get();
        bSysClip = true;                                // and copy into system
    }
    if ( !bCut )
@@ -243,7 +245,7 @@ bool ScViewFunc::CopyToClipSingleRange( ScDocument* pClipDoc, const ScRangeList&
    {
        bool bAnyOle = rDoc.HasOLEObjectsInArea( aRange );
        // Update ScGlobal::xDrawClipDocShellRef.
        ScDrawLayer::SetGlobalDrawPersist( ScTransferObj::SetDrawClipDoc( bAnyOle ) );
        ScDrawLayer::SetGlobalDrawPersist( ScTransferObj::SetDrawClipDoc( bAnyOle, pSysClipDoc ) );
    }

    // is this necessary?, will setting the doc id upset the
@@ -291,7 +293,7 @@ bool ScViewFunc::CopyToClipSingleRange( ScDocument* pClipDoc, const ScRangeList&
        aObjDesc.maDisplayName = pDocSh->GetMedium()->GetURLObject().GetURLNoPass();
        // maSize is set in ScTransferObj ctor

        rtl::Reference<ScTransferObj> pTransferObj(new ScTransferObj( ScDocumentUniquePtr(pClipDoc), aObjDesc ));
        rtl::Reference<ScTransferObj> pTransferObj(new ScTransferObj( pSysClipDoc, aObjDesc ));
        if ( ScGlobal::xDrawClipDocShellRef.is() )
        {
            SfxObjectShellRef aPersistRef( ScGlobal::xDrawClipDocShellRef.get() );