LibreOffice Module sw (master)  1
ddetbl.cxx
Go to the documentation of this file.
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 <doc.hxx>
24 #include <index.hxx>
25 #include <ndtxt.hxx>
26 #include <swtable.hxx>
27 #include <swddetbl.hxx>
28 #include <ddefld.hxx>
29 #include <ndindex.hxx>
30 #include <fldupde.hxx>
31 #include <swtblfmt.hxx>
32 #include <fieldhint.hxx>
33 #include <osl/diagnose.h>
34 #include <pam.hxx>
35 
38 SwDDETable::SwDDETable( SwTable& rTable, SwDDEFieldType* pDDEType, bool bUpdate )
39  : SwTable(rTable), m_aDepends(*this), m_pDDEType(pDDEType)
40 {
42  // copy the table data
44  rTable.GetTabSortBoxes().clear();
45 
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 
65 {
66  SwDoc* pDoc = GetFrameFormat()->GetDoc();
67  if (!pDoc->IsInDtor() && !m_aLines.empty())
68  {
71  {
73  }
74  }
75 
76  // If it is the last dependent of the "deleted field" than delete it finally
78  {
80  delete m_pDDEType;
81  m_pDDEType = nullptr;
82  }
83 }
84 
85 void SwDDETable::SwClientNotify(const SwModify& rModify, const SfxHint& rHint)
86 {
87  if(auto pLegacy = dynamic_cast<const sw::LegacyModifyHint*>(&rHint))
88  {
89  switch(pLegacy->GetWhich())
90  {
91  case RES_UPDATEDDETBL:
92  ChangeContent();
93  break;
94  default:
95  SwTable::SwClientNotify(rModify, rHint);
96  }
97  }
98  else if(auto pFieldHint = dynamic_cast<const SwFieldHint*>(&rHint))
99  {
100  pFieldHint->m_pPaM->DeleteMark(); // TODO: this is really hackish
101  // replace DDETable by real table
102  NoDDETable();
103  }
104  else if(const auto pLinkAnchorHint = dynamic_cast<const sw::LinkAnchorSearchHint*>(&rHint))
105  {
106  if(pLinkAnchorHint->m_rpFoundNode)
107  return;
108  const auto pNd = GetTabSortBoxes()[0]->GetSttNd();
109  if( pNd && &pLinkAnchorHint->m_rNodes == &pNd->GetNodes() )
110  pLinkAnchorHint->m_rpFoundNode = pNd;
111  }
112  else if(const sw::InRangeSearchHint* pInRangeHint = dynamic_cast<const sw::InRangeSearchHint*>(&rHint))
113  {
114  if(pInRangeHint->m_rIsInRange)
115  return;
116  const SwTableNode* pTableNd = GetTabSortBoxes()[0]->GetSttNd()->FindTableNode();
117  if( pTableNd->GetNodes().IsDocNodes() &&
118  pInRangeHint->m_nSttNd < pTableNd->EndOfSectionIndex() &&
119  pInRangeHint->m_nEndNd > pTableNd->GetIndex() )
120  pInRangeHint->m_rIsInRange = true;
121  }
122  else if (auto pModifyChangedHint = dynamic_cast<const sw::ModifyChangedHint*>(&rHint))
123  {
124  if(m_pDDEType == &rModify)
125  m_pDDEType = const_cast<SwDDEFieldType*>(static_cast<const SwDDEFieldType*>(pModifyChangedHint->m_pNew));
126  }
127 }
128 
130 {
131  OSL_ENSURE( GetFrameFormat(), "No FrameFormat" );
132 
133  // Is this the correct NodesArray? (because of UNDO)
134  if( m_aLines.empty() )
135  return;
136  OSL_ENSURE( !GetTabSortBoxes().empty(), "Table without content?" );
137  if( !GetTabSortBoxes()[0]->GetSttNd()->GetNodes().IsDocNodes() )
138  return;
139 
140 
141  OUString aExpand = m_pDDEType->GetExpansion().replaceAll("\r", "");
142  sal_Int32 nExpandTokenPos = 0;
143 
144  for( size_t n = 0; n < m_aLines.size(); ++n )
145  {
146  OUString aLine = aExpand.getToken( 0, '\n', nExpandTokenPos );
147  sal_Int32 nLineTokenPos = 0;
148  SwTableLine* pLine = m_aLines[ n ];
149  for( size_t i = 0; i < pLine->GetTabBoxes().size(); ++i )
150  {
151  SwTableBox* pBox = pLine->GetTabBoxes()[ i ];
152  OSL_ENSURE( pBox->GetSttIdx(), "no content box" );
153  SwNodeIndex aNdIdx( *pBox->GetSttNd(), 1 );
154  SwTextNode* pTextNode = aNdIdx.GetNode().GetTextNode();
155  OSL_ENSURE( pTextNode, "No Node" );
156  SwIndex aCntIdx( pTextNode, 0 );
157  pTextNode->EraseText( aCntIdx );
158  pTextNode->InsertText( aLine.getToken( 0, '\t', nLineTokenPos ), aCntIdx );
159 
160  SwTableBoxFormat* pBoxFormat = static_cast<SwTableBoxFormat*>(pBox->GetFrameFormat());
161  pBoxFormat->LockModify();
162  pBoxFormat->ResetFormatAttr( RES_BOXATR_VALUE );
163  pBoxFormat->UnlockModify();
164  }
165  }
166 
168  SwDoc* pDoc = GetFrameFormat()->GetDoc();
169  if( AUTOUPD_FIELD_AND_CHARTS == rIDSA.getFieldUpdateFlags(true) )
170  pDoc->getIDocumentFieldsAccess().SetFieldsDirty( true, nullptr, 0 );
171 }
172 
174 {
175  return m_pDDEType;
176 }
177 
179 {
180  // search table node
181  OSL_ENSURE( GetFrameFormat(), "No FrameFormat" );
182  SwDoc* pDoc = GetFrameFormat()->GetDoc();
183 
184  // Is this the correct NodesArray? (because of UNDO)
185  if( m_aLines.empty() )
186  return false;
187  OSL_ENSURE( !GetTabSortBoxes().empty(), "Table without content?" );
188  SwNode* pNd = const_cast<SwNode*>(static_cast<SwNode const *>(GetTabSortBoxes()[0]->GetSttNd()));
189  if( !pNd->GetNodes().IsDocNodes() )
190  return false;
191 
192  SwTableNode* pTableNd = pNd->FindTableNode();
193  OSL_ENSURE( pTableNd, "Where is the table?");
194 
195  std::unique_ptr<SwTable> pNewTable(new SwTable( *this ));
196 
197  // copy the table data
198  pNewTable->GetTabSortBoxes().insert( GetTabSortBoxes() ); // move content boxes
200 
201  pNewTable->GetTabLines().insert( pNewTable->GetTabLines().begin(),
202  GetTabLines().begin(), GetTabLines().end() ); // move lines
203  GetTabLines().clear();
204 
207 
208  pTableNd->SetNewTable( std::move(pNewTable) ); // replace table
209 
210  return true;
211 }
212 
213 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SwTableNode * m_pTableNode
Definition: swtable.hxx:125
sal_uLong GetIndex() const
Definition: node.hxx:291
virtual void SwClientNotify(const SwModify &, const SfxHint &) override
Definition: swtable.cxx:316
sal_uLong GetSttIdx() const
Definition: swtable.cxx:1866
sw::WriterMultiListener m_aDepends
Definition: swddetbl.hxx:28
constexpr TypedWhichId< SwMsgPoolItem > RES_UPDATEDDETBL(171)
bool IsDeleted() const
Definition: ddefld.hxx:86
sal_Int64 n
Definition: doc.hxx:187
SwTableLine is one table row in the document model.
Definition: swtable.hxx:351
virtual void SwClientNotify(const SwModify &, const SfxHint &) override
Definition: ddetbl.cxx:85
iterator begin()
Definition: swtable.hxx:76
constexpr TypedWhichId< SwTableBoxValue > RES_BOXATR_VALUE(152)
SwTableFormat * GetFrameFormat()
Definition: swtable.hxx:203
size_type size() const
Definition: swtable.hxx:75
IDocumentFieldsAccess const & getIDocumentFieldsAccess() const
Definition: doc.cxx:357
SwTableLines m_aLines
Definition: swtable.hxx:115
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
virtual bool SetFieldsDirty(bool b, const SwNode *pChk, sal_uLong nLen)=0
iterator insert(iterator aIt, SwTableLine *pLine)
Definition: swtable.hxx:85
SwTable()
Definition: swtable.cxx:178
virtual SwFieldUpdateFlags getFieldUpdateFlags(bool bGlobalSettings) const =0
Get the current field update mode.
bool HasOnlyOneListener() const
Definition: calbck.hxx:198
SwDDEFieldType * GetDDEFieldType()
Definition: ddetbl.cxx:173
SwTableSortBoxes m_TabSortContentBoxes
Definition: swtable.hxx:116
SwTableSortBoxes & GetTabSortBoxes()
Definition: swtable.hxx:261
const OUString & GetExpansion() const
Definition: ddefld.hxx:70
int i
bool empty() const
Definition: swtable.hxx:74
void ChangeContent()
Definition: ddetbl.cxx:129
void clear()
Definition: swtable.hxx:82
bool NoDDETable()
Definition: ddetbl.cxx:178
virtual ~SwDDETable() override
Definition: ddetbl.cxx:64
Marks a character position inside a document model node.
Definition: index.hxx:33
void UnlockModify()
Definition: calbck.hxx:204
void LockModify()
Definition: calbck.hxx:203
Provides access to settings of a document.
Marks a node in the document model.
Definition: ndindex.hxx:31
SwFrameFormat * GetFrameFormat()
Definition: swtable.hxx:431
SwNodes & GetNodes()
Node is in which nodes-array/doc?
Definition: node.hxx:697
const SwDoc * GetDoc() const
The document is set in SwAttrPool now, therefore you always can access it.
Definition: format.hxx:123
const IDocumentSettingAccess & getIDocumentSettingAccess() const
Provides access to the document settings interface.
Definition: format.cxx:717
void DecRefCnt()
Definition: ddefld.hxx:99
SwTableLines & GetTabLines()
Definition: swtable.hxx:200
IDocumentLayoutAccess const & getIDocumentLayoutAccess() const
Definition: doc.cxx:405
SwTable is one table in the document model, containing rows (which contain cells).
Definition: swtable.hxx:111
void StartListening(SwModify *pDepend)
Definition: calbck.cxx:268
sal_uLong EndOfSectionIndex() const
Definition: node.hxx:681
SwTextNode is a paragraph in the document model.
Definition: ndtxt.hxx:79
SwTableBoxes & GetTabBoxes()
Definition: swtable.hxx:361
virtual bool ResetFormatAttr(sal_uInt16 nWhich1, sal_uInt16 nWhich2=0)
Definition: format.cxx:624
const SwStartNode * GetSttNd() const
Definition: swtable.hxx:445
virtual const SwViewShell * GetCurrentViewShell() const =0
Returns the layout set at the document.
SwTableBox is one table cell in the document model.
Definition: swtable.hxx:392
SwTableNode * FindTableNode()
Search table node, in which it is.
Definition: node.cxx:357
SwDDEFieldType * m_pDDEType
Definition: swddetbl.hxx:29
bool IsInDtor() const
Definition: doc.hxx:403
bool IsDocNodes() const
Is the NodesArray the regular one of Doc? (and not the UndoNds, ...) Implementation in doc...
Definition: nodes.cxx:2329
iterator end()
Definition: swtable.hxx:78
void SetNewTable(std::unique_ptr< SwTable >, bool bNewFrames=true)
Definition: ndtbl.cxx:2483
std::pair< const_iterator, bool > insert(Value &&x)
SwDDETable(SwTable &rTable, SwDDEFieldType *pDDEType, bool bUpdate=true)
Ctor moves all lines/boxes from a SwTable into itself.
Definition: ddetbl.cxx:38
void IncRefCnt()
Definition: ddefld.hxx:98
SwTextNode * GetTextNode()
Inline methods from Node.hxx.
Definition: ndtxt.hxx:845
Base class of the Writer document model elements.
Definition: node.hxx:80