1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <frmfmt.hxx>
21 #include <doc.hxx>
22 #include <IDocumentSettingAccess.hxx>
23 #include <IDocumentFieldsAccess.hxx>
24 #include <IDocumentLayoutAccess.hxx>
25 #include <index.hxx>
26 #include <ndtxt.hxx>
27 #include <swtable.hxx>
28 #include <swddetbl.hxx>
29 #include <ddefld.hxx>
30 #include <ndindex.hxx>
31 #include <fldupde.hxx>
32 #include <swtblfmt.hxx>
33 #include <fieldhint.hxx>
34 #include <osl/diagnose.h>
35 
36 /// Ctor moves all lines/boxes from a SwTable into itself.
37 /// Afterwards the SwTable is empty and must be deleted.
SwDDETable(SwTable & rTable,SwDDEFieldType * pDDEType,bool bUpdate)38 SwDDETable::SwDDETable( SwTable& rTable, SwDDEFieldType* pDDEType, bool bUpdate )
39     : SwTable(rTable), m_aDepends(*this), m_pDDEType(pDDEType)
40 {
41     m_aDepends.StartListening(m_pDDEType);
42     // copy the table data
43     m_TabSortContentBoxes.insert(rTable.GetTabSortBoxes());
44     rTable.GetTabSortBoxes().clear();
45 
46     m_aLines.insert( m_aLines.begin(),
47                    rTable.GetTabLines().begin(), rTable.GetTabLines().end() ); // move lines
48     rTable.GetTabLines().clear();
49 
50     if( !m_aLines.empty() )
51     {
52         const SwNode& rNd = *GetTabSortBoxes()[0]->GetSttNd();
53         if( rNd.GetNodes().IsDocNodes() )
54         {
55             pDDEType->IncRefCnt();
56 
57             // update box content only if update flag is set (false in import)
58             if (bUpdate)
59                 ChangeContent();
60         }
61     }
62 }
63 
~SwDDETable()64 SwDDETable::~SwDDETable()
65 {
66     SwDoc* pDoc = GetFrameFormat()->GetDoc();
67     if (!pDoc->IsInDtor() && !m_aLines.empty())
68     {
69         assert(m_pTableNode);
70         if (m_pTableNode->GetNodes().IsDocNodes())
71         {
72             m_pDDEType->DecRefCnt();
73         }
74     }
75 
76     // If it is the last dependent of the "deleted field" than delete it finally
77     if( m_pDDEType->IsDeleted() && m_pDDEType->HasOnlyOneListener() )
78     {
79         m_aDepends.EndListeningAll();
80         delete m_pDDEType;
81         m_pDDEType = nullptr;
82     }
83 }
84 
Modify(const SfxPoolItem * pOld,const SfxPoolItem * pNew)85 void SwDDETable::Modify( const SfxPoolItem* pOld, const SfxPoolItem* pNew )
86 {
87     if( pNew && RES_UPDATEDDETBL == pNew->Which() )
88         ChangeContent();
89     else
90         SwTable::Modify( pOld, pNew );
91 }
92 
SwClientNotify(const SwModify & rModify,const SfxHint & rHint)93 void SwDDETable::SwClientNotify( const SwModify& rModify, const SfxHint& rHint )
94 {
95     SwClient::SwClientNotify(rModify, rHint);
96     if(dynamic_cast<const SwFieldHint*>(&rHint))
97         // replace DDETable by real table
98         NoDDETable();
99     else if(const auto pLinkAnchorHint = dynamic_cast<const sw::LinkAnchorSearchHint*>(&rHint))
100     {
101         if(pLinkAnchorHint->m_rpFoundNode)
102             return;
103         const auto pNd = GetTabSortBoxes()[0]->GetSttNd();
104         if( pNd && &pLinkAnchorHint->m_rNodes == &pNd->GetNodes() )
105             pLinkAnchorHint->m_rpFoundNode = pNd;
106     }
107     else if(const sw::InRangeSearchHint* pInRangeHint = dynamic_cast<const sw::InRangeSearchHint*>(&rHint))
108     {
109         if(pInRangeHint->m_rIsInRange)
110             return;
111         const SwTableNode* pTableNd = GetTabSortBoxes()[0]->GetSttNd()->FindTableNode();
112         if( pTableNd->GetNodes().IsDocNodes() &&
113                 pInRangeHint->m_nSttNd < pTableNd->EndOfSectionIndex() &&
114                 pInRangeHint->m_nEndNd > pTableNd->GetIndex() )
115             pInRangeHint->m_rIsInRange = true;
116     }
117     else if (auto pModifyChangedHint = dynamic_cast<const sw::ModifyChangedHint*>(&rHint))
118     {
119         if(m_pDDEType == &rModify)
120             m_pDDEType = const_cast<SwDDEFieldType*>(static_cast<const SwDDEFieldType*>(pModifyChangedHint->m_pNew));
121     }
122 }
123 
ChangeContent()124 void SwDDETable::ChangeContent()
125 {
126     OSL_ENSURE( GetFrameFormat(), "No FrameFormat" );
127 
128     // Is this the correct NodesArray? (because of UNDO)
129     if( m_aLines.empty() )
130         return;
131     OSL_ENSURE( !GetTabSortBoxes().empty(), "Table without content?" );
132     if( !GetTabSortBoxes()[0]->GetSttNd()->GetNodes().IsDocNodes() )
133         return;
134 
135 
136     OUString aExpand = m_pDDEType->GetExpansion().replaceAll("\r", "");
137     sal_Int32 nExpandTokenPos = 0;
138 
139     for( size_t n = 0; n < m_aLines.size(); ++n )
140     {
141         OUString aLine = aExpand.getToken( 0, '\n', nExpandTokenPos );
142         sal_Int32 nLineTokenPos = 0;
143         SwTableLine* pLine = m_aLines[ n ];
144         for( size_t i = 0; i < pLine->GetTabBoxes().size(); ++i )
145         {
146             SwTableBox* pBox = pLine->GetTabBoxes()[ i ];
147             OSL_ENSURE( pBox->GetSttIdx(), "no content box" );
148             SwNodeIndex aNdIdx( *pBox->GetSttNd(), 1 );
149             SwTextNode* pTextNode = aNdIdx.GetNode().GetTextNode();
150             OSL_ENSURE( pTextNode, "No Node" );
151             SwIndex aCntIdx( pTextNode, 0 );
152             pTextNode->EraseText( aCntIdx );
153             pTextNode->InsertText( aLine.getToken( 0, '\t', nLineTokenPos ), aCntIdx );
154 
155             SwTableBoxFormat* pBoxFormat = static_cast<SwTableBoxFormat*>(pBox->GetFrameFormat());
156             pBoxFormat->LockModify();
157             pBoxFormat->ResetFormatAttr( RES_BOXATR_VALUE );
158             pBoxFormat->UnlockModify();
159         }
160     }
161 
162     const IDocumentSettingAccess& rIDSA = GetFrameFormat()->getIDocumentSettingAccess();
163     SwDoc* pDoc = GetFrameFormat()->GetDoc();
164     if( AUTOUPD_FIELD_AND_CHARTS == rIDSA.getFieldUpdateFlags(true) )
165         pDoc->getIDocumentFieldsAccess().SetFieldsDirty( true, nullptr, 0 );
166 }
167 
GetDDEFieldType()168 SwDDEFieldType* SwDDETable::GetDDEFieldType()
169 {
170     return m_pDDEType;
171 }
172 
NoDDETable()173 bool SwDDETable::NoDDETable()
174 {
175     // search table node
176     OSL_ENSURE( GetFrameFormat(), "No FrameFormat" );
177     SwDoc* pDoc = GetFrameFormat()->GetDoc();
178 
179     // Is this the correct NodesArray? (because of UNDO)
180     if( m_aLines.empty() )
181         return false;
182     OSL_ENSURE( !GetTabSortBoxes().empty(), "Table without content?" );
183     SwNode* pNd = const_cast<SwNode*>(static_cast<SwNode const *>(GetTabSortBoxes()[0]->GetSttNd()));
184     if( !pNd->GetNodes().IsDocNodes() )
185         return false;
186 
187     SwTableNode* pTableNd = pNd->FindTableNode();
188     OSL_ENSURE( pTableNd, "Where is the table?");
189 
190     std::unique_ptr<SwTable> pNewTable(new SwTable( *this ));
191 
192     // copy the table data
193     pNewTable->GetTabSortBoxes().insert( GetTabSortBoxes() ); // move content boxes
194     GetTabSortBoxes().clear();
195 
196     pNewTable->GetTabLines().insert( pNewTable->GetTabLines().begin(),
197                                    GetTabLines().begin(), GetTabLines().end() ); // move lines
198     GetTabLines().clear();
199 
200     if( pDoc->getIDocumentLayoutAccess().GetCurrentViewShell() )
201         m_pDDEType->DecRefCnt();
202 
203     pTableNd->SetNewTable( std::move(pNewTable) );       // replace table
204 
205     return true;
206 }
207 
208 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
209