1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * 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/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */
#include <doc.hxx>
#include <IDocumentFieldsAccess.hxx>
#include <node.hxx>
#include <frmfmt.hxx>
#include <swtable.hxx>
#include <ndtxt.hxx>
#include <swtblfmt.hxx>
#include <cellatr.hxx>
#include <docary.hxx>
#include <ddefld.hxx>
#include <swddetbl.hxx>
#include <ndindex.hxx>
#include <frameformats.hxx>
#include <vector>
#include <osl/diagnose.h>


#ifdef DBG_UTIL
#define CHECK_TABLE(t) (t).CheckConsistency();
#else
#define CHECK_TABLE(t)
#endif

namespace {

// Structure for the mapping from old and new frame formats to the
// boxes and lines of a table
struct MapTableFrameFormat
{
    const SwFrameFormat *pOld;
    SwFrameFormat *pNew;
    MapTableFrameFormat( const SwFrameFormat *pOldFormat, SwFrameFormat*pNewFormat )
        : pOld( pOldFormat ), pNew( pNewFormat )
    {}
};

}

typedef std::vector<MapTableFrameFormat> MapTableFrameFormats;

SwContentNode* SwTextNode::MakeCopy(SwDoc* pDoc, const SwNodeIndex& rIdx, bool const bNewFrames) const
{
    // the Copy-Textnode is the Node with the Text, the Copy-Attrnode is the
    // node with the collection and hard attributes. Normally is the same
    // node, but if insert a glossary without formatting, then the Attrnode
    // is the prev node of the destination position in dest. document.
    SwTextNode* pCpyTextNd = const_cast<SwTextNode*>(this);
    SwTextNode* pCpyAttrNd = pCpyTextNd;

    // Copy the formats to the other document
    SwTextFormatColl* pColl = nullptr;
    if( pDoc->IsInsOnlyTextGlossary() )
    {
        SwNodeIndex aIdx( rIdx, -1 );
        if( aIdx.GetNode().IsTextNode() )
        {
            pCpyAttrNd = aIdx.GetNode().GetTextNode();
            pColl = &pCpyAttrNd->GetTextColl()->GetNextTextFormatColl();
        }
    }
    if( !pColl )
        pColl = pDoc->CopyTextColl( *GetTextColl() );

    SwTextNode* pTextNd = pDoc->GetNodes().MakeTextNode(rIdx, pColl, bNewFrames);

    // METADATA: register copy
    pTextNd->RegisterAsCopyOf(*pCpyTextNd);

    // Copy Attribute/Text
    if( !pCpyAttrNd->HasSwAttrSet() )
        // An AttrSet was added for numbering, so delete it
        pTextNd->ResetAllAttr();

    // if Copy-Textnode unequal to Copy-Attrnode, then copy first
    // the attributes into the new Node.
    if( pCpyAttrNd != pCpyTextNd )
    {
        pCpyAttrNd->CopyAttr( pTextNd, 0, 0 );
        if( pCpyAttrNd->HasSwAttrSet() )
        {
            SwAttrSet aSet( *pCpyAttrNd->GetpSwAttrSet() );
            aSet.ClearItem( RES_PAGEDESC );
            aSet.ClearItem( RES_BREAK );
            aSet.CopyToModify( *pTextNd );
        }
    }

    // Is that enough? What about PostIts/Fields/FieldTypes?
    // #i96213# - force copy of all attributes
    pCpyTextNd->CopyText( pTextNd, SwIndex( pCpyTextNd ),
        pCpyTextNd->GetText().getLength(), true );

    if( RES_CONDTXTFMTCOLL == pColl->Which() )
        pTextNd->ChkCondColl();

    return pTextNd;
}

static bool lcl_SrchNew( const MapTableFrameFormat& rMap, SwFrameFormat** pPara )
{
    if( rMap.pOld != *pPara )
        return true;
    *pPara = rMap.pNew;
    return false;
}

namespace {

struct CopyTable
{
    SwDoc* m_pDoc;
    sal_uLong m_nOldTableSttIdx;
    MapTableFrameFormats& m_rMapArr;
    SwTableLine* m_pInsLine;
    SwTableBox* m_pInsBox;
    SwTableNode *m_pTableNd;
    const SwTable *m_pOldTable;

    CopyTable(SwDoc* pDc, MapTableFrameFormats& rArr, sal_uLong nOldStt,
               SwTableNode& rTableNd, const SwTable* pOldTable)
        : m_pDoc(pDc), m_nOldTableSttIdx(nOldStt), m_rMapArr(rArr),
          m_pInsLine(nullptr), m_pInsBox(nullptr), m_pTableNd(&rTableNd), m_pOldTable(pOldTable)
    {}
};

}

static void lcl_CopyTableLine( const SwTableLine* pLine, CopyTable* pCT );

static void lcl_CopyTableBox( SwTableBox* pBox, CopyTable* pCT )
{
    SwTableBoxFormat * pBoxFormat = static_cast<SwTableBoxFormat*>(pBox->GetFrameFormat());
    for (const auto& rMap : pCT->m_rMapArr)
        if ( !lcl_SrchNew( rMap, reinterpret_cast<SwFrameFormat**>(&pBoxFormat) ) )<--- Consider using std::any_of algorithm instead of a raw loop.
            break;

    if (pBoxFormat == pBox->GetFrameFormat()) // Create a new one?
    {
        const SfxPoolItem* pItem;
        if( SfxItemState::SET == pBoxFormat->GetItemState( RES_BOXATR_FORMULA, false,
            &pItem ) && static_cast<const SwTableBoxFormula*>(pItem)->IsIntrnlName() )
        {
            const_cast<SwTableBoxFormula*>(static_cast<const SwTableBoxFormula*>(pItem))->PtrToBoxNm(pCT->m_pOldTable);
        }

        pBoxFormat = pCT->m_pDoc->MakeTableBoxFormat();
        pBoxFormat->CopyAttrs( *pBox->GetFrameFormat() );

        if( pBox->GetSttIdx() )
        {
            SvNumberFormatter* pN = pCT->m_pDoc->GetNumberFormatter(false);
            if( pN && pN->HasMergeFormatTable() && SfxItemState::SET == pBoxFormat->
                GetItemState( RES_BOXATR_FORMAT, false, &pItem ) )
            {
                sal_uLong nOldIdx = static_cast<const SwTableBoxNumFormat*>(pItem)->GetValue();
                sal_uLong nNewIdx = pN->GetMergeFormatIndex( nOldIdx );
                if( nNewIdx != nOldIdx )
                    pBoxFormat->SetFormatAttr( SwTableBoxNumFormat( nNewIdx ));

            }
        }

        pCT->m_rMapArr.emplace_back(pBox->GetFrameFormat(), pBoxFormat);
    }

    sal_uInt16 nLines = pBox->GetTabLines().size();
    SwTableBox* pNewBox;
    if( nLines )
        pNewBox = new SwTableBox(pBoxFormat, nLines, pCT->m_pInsLine);
    else
    {
        SwNodeIndex aNewIdx(*pCT->m_pTableNd, pBox->GetSttIdx() - pCT->m_nOldTableSttIdx);
        assert(aNewIdx.GetNode().IsStartNode() && "Index is not on the start node");

        pNewBox = new SwTableBox(pBoxFormat, aNewIdx, pCT->m_pInsLine);
        pNewBox->setRowSpan( pBox->getRowSpan() );
    }

    pCT->m_pInsLine->GetTabBoxes().push_back( pNewBox );

    if (nLines)
    {
        CopyTable aPara(*pCT);
        aPara.m_pInsBox = pNewBox;
        for( const SwTableLine* pLine : pBox->GetTabLines() )
            lcl_CopyTableLine( pLine, &aPara );
    }
    else if (pNewBox->IsInHeadline(&pCT->m_pTableNd->GetTable()))
    {
        // In the headline, the paragraphs must match conditional styles
        pNewBox->GetSttNd()->CheckSectionCondColl();
    }
}

static void lcl_CopyTableLine( const SwTableLine* pLine, CopyTable* pCT )
{
    SwTableLineFormat * pLineFormat = static_cast<SwTableLineFormat*>(pLine->GetFrameFormat());
    for (const auto& rMap : pCT->m_rMapArr)
        if ( !lcl_SrchNew( rMap, reinterpret_cast<SwFrameFormat**>(&pLineFormat) ) )<--- Consider using std::any_of algorithm instead of a raw loop.
            break;

    if( pLineFormat == pLine->GetFrameFormat() ) // Create a new one?
    {
        pLineFormat = pCT->m_pDoc->MakeTableLineFormat();
        pLineFormat->CopyAttrs( *pLine->GetFrameFormat() );
        pCT->m_rMapArr.emplace_back(pLine->GetFrameFormat(), pLineFormat);
    }

    SwTableLine* pNewLine = new SwTableLine(pLineFormat, pLine->GetTabBoxes().size(), pCT->m_pInsBox);
    // Insert the new row into the table
    if (pCT->m_pInsBox)
    {
        pCT->m_pInsBox->GetTabLines().push_back(pNewLine);
    }
    else
    {
        pCT->m_pTableNd->GetTable().GetTabLines().push_back(pNewLine);
    }

    pCT->m_pInsLine = pNewLine;
    for( auto& rpBox : const_cast<SwTableLine*>(pLine)->GetTabBoxes() )
        lcl_CopyTableBox(rpBox, pCT);
}

SwTableNode* SwTableNode::MakeCopy( SwDoc* pDoc, const SwNodeIndex& rIdx ) const
{
    // In which array are we? Nodes? UndoNodes?
    SwNodes& rNds = const_cast<SwNodes&>(GetNodes());

    {
        if( rIdx < pDoc->GetNodes().GetEndOfInserts().GetIndex() &&
            rIdx >= pDoc->GetNodes().GetEndOfInserts().StartOfSectionIndex() )
            return nullptr;
    }

    // Copy the TableFrameFormat
    OUString sTableName( GetTable().GetFrameFormat()->GetName() );
    if( !pDoc->IsCopyIsMove() )
    {
        const SwFrameFormats& rTableFormats = *pDoc->GetTableFrameFormats();
        for( size_t n = rTableFormats.size(); n; )
            if( rTableFormats[ --n ]->GetName() == sTableName )
            {
                sTableName = pDoc->GetUniqueTableName();
                break;
            }
    }

    SwFrameFormat* pTableFormat = pDoc->MakeTableFrameFormat( sTableName, pDoc->GetDfltFrameFormat() );
    pTableFormat->CopyAttrs( *GetTable().GetFrameFormat() );
    SwTableNode* pTableNd = new SwTableNode( rIdx );
    SwEndNode* pEndNd = new SwEndNode( rIdx, *pTableNd );
    SwNodeIndex aInsPos( *pEndNd );

    SwTable& rTable = pTableNd->GetTable();
    rTable.RegisterToFormat( *pTableFormat );

    rTable.SetRowsToRepeat( GetTable().GetRowsToRepeat() );
    rTable.SetTableChgMode( GetTable().GetTableChgMode() );
    rTable.SetTableModel( GetTable().IsNewModel() );

    SwDDEFieldType* pDDEType = nullptr;
    if( auto pSwDDETable = dynamic_cast<const SwDDETable*>( &GetTable() ) )
    {
        // We're copying a DDE table
        // Is the field type available in the new document?
        pDDEType = const_cast<SwDDETable*>(pSwDDETable)->GetDDEFieldType();
        if( pDDEType->IsDeleted() )
            pDoc->getIDocumentFieldsAccess().InsDeletedFieldType( *pDDEType );
        else
            pDDEType = static_cast<SwDDEFieldType*>(pDoc->getIDocumentFieldsAccess().InsertFieldType( *pDDEType ));
        OSL_ENSURE( pDDEType, "unknown FieldType" );

        // Swap the table pointers in the node
        std::unique_ptr<SwDDETable> pNewTable(new SwDDETable( pTableNd->GetTable(), pDDEType ));
        pTableNd->SetNewTable( std::move(pNewTable), false );
    }
    // First copy the content of the tables, we will later assign the
    // boxes/lines and create the frames
    SwNodeRange aRg( *this, +1, *EndOfSectionNode() );

    // If there is a table in this table, the table format for the outer table
    // does not seem to be used, because the table does not have any contents yet
    // (see IsUsed). Therefore the inner table gets the same name as the outer table.
    // We have to make sure that the table node of the SwTable is accessible, even
    // without any content in m_TabSortContentBoxes. #i26629#
    pTableNd->GetTable().SetTableNode( pTableNd );
    rNds.Copy_( aRg, aInsPos, false );
    pTableNd->GetTable().SetTableNode( nullptr );

    // Special case for a single box
    if( 1 == GetTable().GetTabSortBoxes().size() )
    {
        aRg.aStart.Assign( *pTableNd, 1 );
        aRg.aEnd.Assign( *pTableNd->EndOfSectionNode() );
        pDoc->GetNodes().SectionDown( &aRg, SwTableBoxStartNode );
    }

    // Delete all frames from the copied area, they will be created
    // during the generation of the table frame
    pTableNd->DelFrames();

    MapTableFrameFormats aMapArr;
    CopyTable aPara( pDoc, aMapArr, GetIndex(), *pTableNd, &GetTable() );

    for( const SwTableLine* pLine : GetTable().GetTabLines() )
        lcl_CopyTableLine( pLine, &aPara );

    if( pDDEType )
        pDDEType->IncRefCnt();

    CHECK_TABLE( GetTable() );
    return pTableNd;
}

void SwTextNode::CopyCollFormat( SwTextNode& rDestNd )
{
    // Copy the formats into the other document:
    // Special case for PageBreak/PageDesc/ColBrk
    SwDoc* pDestDoc = rDestNd.GetDoc();
    SwAttrSet aPgBrkSet( pDestDoc->GetAttrPool(), aBreakSetRange );
    const SwAttrSet* pSet;

    pSet = rDestNd.GetpSwAttrSet();
    if( nullptr != pSet )
    {
        // Special cases for Break-Attributes
        const SfxPoolItem* pAttr;
        if( SfxItemState::SET == pSet->GetItemState( RES_BREAK, false, &pAttr ) )
            aPgBrkSet.Put( *pAttr );

        if( SfxItemState::SET == pSet->GetItemState( RES_PAGEDESC, false, &pAttr ) )
            aPgBrkSet.Put( *pAttr );
    }

    rDestNd.ChgFormatColl( pDestDoc->CopyTextColl( *GetTextColl() ));
    pSet = GetpSwAttrSet();
    if( nullptr != pSet )
        pSet->CopyToModify( rDestNd );

    if( aPgBrkSet.Count() )
        rDestNd.SetAttr( aPgBrkSet );
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */