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 <fmtfld.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 #include <pam.hxx>
36 
39 SwDDETable::SwDDETable( SwTable& rTable, SwDDEFieldType* pDDEType, bool bUpdate )
40  : SwTable(rTable), m_aDepends(*this), m_pDDEType(pDDEType)
41 {
43  // copy the table data
45  rTable.GetTabSortBoxes().clear();
46 
48  rTable.GetTabLines().begin(), rTable.GetTabLines().end() ); // move lines
49  rTable.GetTabLines().clear();
50 
51  if( !m_aLines.empty() )
52  {
53  const SwNode& rNd = *GetTabSortBoxes()[0]->GetSttNd();
54  if( rNd.GetNodes().IsDocNodes() )
55  {
56  pDDEType->IncRefCnt();
57 
58  // update box content only if update flag is set (false in import)
59  if (bUpdate)
60  ChangeContent();
61  }
62  }
63 }
64 
66 {
67  SwDoc* pDoc = GetFrameFormat()->GetDoc();
68  if (!pDoc->IsInDtor() && !m_aLines.empty())
69  {
70  assert(m_pTableNode);
72  {
74  }
75  }
76 
77  // If it is the last dependent of the "deleted field" than delete it finally
79  {
81  delete m_pDDEType;
82  m_pDDEType = nullptr;
83  }
84 }
85 
86 void SwDDETable::SwClientNotify(const SwModify& rModify, const SfxHint& rHint)
87 {
88  if (rHint.GetId() == SfxHintId::SwLegacyModify)
89  {
90  auto pLegacy = static_cast<const sw::LegacyModifyHint*>(&rHint);
91  switch(pLegacy->GetWhich())
92  {
93  case RES_UPDATEDDETBL:
94  ChangeContent();
95  break;
96  default:
97  SwTable::SwClientNotify(rModify, rHint);
98  }
99  }
100  else if(auto pFieldHint = dynamic_cast<const SwFieldHint*>(&rHint))
101  {
102  pFieldHint->m_pPaM->DeleteMark(); // TODO: this is really hackish
103  // replace DDETable by real table
104  NoDDETable();
105  }
106  else if(const auto pLinkAnchorHint = dynamic_cast<const sw::LinkAnchorSearchHint*>(&rHint))
107  {
108  if(pLinkAnchorHint->m_rpFoundNode)
109  return;
110  const auto pNd = GetTabSortBoxes()[0]->GetSttNd();
111  if( pNd && &pLinkAnchorHint->m_rNodes == &pNd->GetNodes() )
112  pLinkAnchorHint->m_rpFoundNode = pNd;
113  }
114  else if(const sw::InRangeSearchHint* pInRangeHint = dynamic_cast<const sw::InRangeSearchHint*>(&rHint))
115  {
116  if(pInRangeHint->m_rIsInRange)
117  return;
118  const SwTableNode* pTableNd = GetTabSortBoxes()[0]->GetSttNd()->FindTableNode();
119  if( pTableNd->GetNodes().IsDocNodes() &&
120  pInRangeHint->m_nSttNd < pTableNd->EndOfSectionIndex() &&
121  pInRangeHint->m_nEndNd > pTableNd->GetIndex() )
122  pInRangeHint->m_rIsInRange = true;
123  } else if (const auto pGatherDdeTablesHint = dynamic_cast<const sw::GatherDdeTablesHint*>(&rHint))
124  {
125  pGatherDdeTablesHint->m_rvTables.push_back(this);
126  }
127  else if (auto pModifyChangedHint = dynamic_cast<const sw::ModifyChangedHint*>(&rHint))
128  {
129  if(m_pDDEType == &rModify)
130  m_pDDEType = const_cast<SwDDEFieldType*>(static_cast<const SwDDEFieldType*>(pModifyChangedHint->m_pNew));
131  }
132 }
133 
135 {
136  OSL_ENSURE( GetFrameFormat(), "No FrameFormat" );
137 
138  // Is this the correct NodesArray? (because of UNDO)
139  if( m_aLines.empty() )
140  return;
141  OSL_ENSURE( !GetTabSortBoxes().empty(), "Table without content?" );
142  if( !GetTabSortBoxes()[0]->GetSttNd()->GetNodes().IsDocNodes() )
143  return;
144 
145 
146  OUString aExpand = m_pDDEType->GetExpansion().replaceAll("\r", "");
147  sal_Int32 nExpandTokenPos = 0;
148 
149  for( size_t n = 0; n < m_aLines.size(); ++n )
150  {
151  OUString aLine = aExpand.getToken( 0, '\n', nExpandTokenPos );
152  sal_Int32 nLineTokenPos = 0;
153  SwTableLine* pLine = m_aLines[ n ];
154  for( size_t i = 0; i < pLine->GetTabBoxes().size(); ++i )
155  {
156  SwTableBox* pBox = pLine->GetTabBoxes()[ i ];
157  OSL_ENSURE( pBox->GetSttIdx(), "no content box" );
158  SwNodeIndex aNdIdx( *pBox->GetSttNd(), 1 );
159  SwTextNode* pTextNode = aNdIdx.GetNode().GetTextNode();
160  OSL_ENSURE( pTextNode, "No Node" );
161  SwIndex aCntIdx( pTextNode, 0 );
162  pTextNode->EraseText( aCntIdx );
163  pTextNode->InsertText( aLine.getToken( 0, '\t', nLineTokenPos ), aCntIdx );
164 
165  SwTableBoxFormat* pBoxFormat = static_cast<SwTableBoxFormat*>(pBox->GetFrameFormat());
166  pBoxFormat->LockModify();
167  pBoxFormat->ResetFormatAttr( RES_BOXATR_VALUE );
168  pBoxFormat->UnlockModify();
169  }
170  }
171 
173  SwDoc* pDoc = GetFrameFormat()->GetDoc();
174  if( AUTOUPD_FIELD_AND_CHARTS == rIDSA.getFieldUpdateFlags(true) )
175  pDoc->getIDocumentFieldsAccess().SetFieldsDirty( true, nullptr, SwNodeOffset(0) );
176 }
177 
179 {
180  return m_pDDEType;
181 }
182 
184 {
185  // search table node
186  OSL_ENSURE( GetFrameFormat(), "No FrameFormat" );
187  SwDoc* pDoc = GetFrameFormat()->GetDoc();
188 
189  // Is this the correct NodesArray? (because of UNDO)
190  if( m_aLines.empty() )
191  return;
192  OSL_ENSURE( !GetTabSortBoxes().empty(), "Table without content?" );
193  SwNode* pNd = const_cast<SwNode*>(static_cast<SwNode const *>(GetTabSortBoxes()[0]->GetSttNd()));
194  if( !pNd->GetNodes().IsDocNodes() )
195  return;
196 
197  SwTableNode* pTableNd = pNd->FindTableNode();
198  OSL_ENSURE( pTableNd, "Where is the table?");
199 
200  std::unique_ptr<SwTable> pNewTable(new SwTable( *this ));
201 
202  // copy the table data
203  pNewTable->GetTabSortBoxes().insert( GetTabSortBoxes() ); // move content boxes
205 
206  pNewTable->GetTabLines().insert( pNewTable->GetTabLines().begin(),
207  GetTabLines().begin(), GetTabLines().end() ); // move lines
208  GetTabLines().clear();
209 
212 
213  pTableNd->SetNewTable( std::move(pNewTable) ); // replace table
214 }
215 
216 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SwTableNode * m_pTableNode
Definition: swtable.hxx:126
virtual void SwClientNotify(const SwModify &, const SfxHint &) override
Definition: swtable.cxx:326
SwNodeOffset EndOfSectionIndex() const
Definition: node.hxx:687
sw::WriterMultiListener m_aDepends
Definition: swddetbl.hxx:28
constexpr TypedWhichId< SwMsgPoolItem > RES_UPDATEDDETBL(171)
bool IsDeleted() const
Definition: ddefld.hxx:85
sal_Int64 n
Definition: doc.hxx:187
SwTableLine is one table row in the document model.
Definition: swtable.hxx:357
virtual void SwClientNotify(const SwModify &, const SfxHint &) override
Definition: ddetbl.cxx:86
iterator begin()
Definition: swtable.hxx:77
constexpr TypedWhichId< SwTableBoxValue > RES_BOXATR_VALUE(152)
SwTableFormat * GetFrameFormat()
Definition: swtable.hxx:204
SfxHintId GetId() const
SwNodeOffset GetSttIdx() const
Definition: swtable.cxx:2051
size_type size() const
Definition: swtable.hxx:76
IDocumentFieldsAccess const & getIDocumentFieldsAccess() const
Definition: doc.cxx:357
SwTableLines m_aLines
Definition: swtable.hxx:116
iterator insert(iterator aIt, SwTableLine *pLine)
Definition: swtable.hxx:86
SwTable()
Definition: swtable.cxx:188
virtual SwFieldUpdateFlags getFieldUpdateFlags(bool bGlobalSettings) const =0
Get the current field update mode.
bool HasOnlyOneListener() const
Definition: calbck.hxx:203
SwDDEFieldType * GetDDEFieldType()
Definition: ddetbl.cxx:178
SwTableSortBoxes m_TabSortContentBoxes
Definition: swtable.hxx:117
SwTableSortBoxes & GetTabSortBoxes()
Definition: swtable.hxx:262
const OUString & GetExpansion() const
Definition: ddefld.hxx:69
int i
bool empty() const
Definition: swtable.hxx:75
void ChangeContent()
Definition: ddetbl.cxx:134
void clear()
Definition: swtable.hxx:83
SwNodeOffset GetIndex() const
Definition: node.hxx:292
virtual ~SwDDETable() override
Definition: ddetbl.cxx:65
void NoDDETable()
Definition: ddetbl.cxx:183
Marks a character position inside a document model node.
Definition: index.hxx:33
void UnlockModify()
Definition: calbck.hxx:209
virtual bool SetFieldsDirty(bool b, const SwNode *pChk, SwNodeOffset nLen)=0
void LockModify()
Definition: calbck.hxx:208
Provides access to settings of a document.
Marks a node in the document model.
Definition: ndindex.hxx:30
SwFrameFormat * GetFrameFormat()
Definition: swtable.hxx:457
SwNodes & GetNodes()
Node is in which nodes-array/doc?
Definition: node.hxx:703
const SwDoc * GetDoc() const
The document is set in SwAttrPool now, therefore you always can access it.
Definition: format.hxx:139
const IDocumentSettingAccess & getIDocumentSettingAccess() const
Provides access to the document settings interface.
Definition: format.cxx:712
void DecRefCnt()
Definition: ddefld.hxx:98
SwTableLines & GetTabLines()
Definition: swtable.hxx:201
IDocumentLayoutAccess const & getIDocumentLayoutAccess() const
Definition: doc.cxx:405
SwTable is one table in the document model, containing rows (which contain cells).
Definition: swtable.hxx:112
void StartListening(SwModify *pDepend)
Definition: calbck.cxx:269
SwTextNode is a paragraph in the document model.
Definition: ndtxt.hxx:79
SwTableBoxes & GetTabBoxes()
Definition: swtable.hxx:368
virtual bool ResetFormatAttr(sal_uInt16 nWhich1, sal_uInt16 nWhich2=0)
Definition: format.cxx:619
const SwStartNode * GetSttNd() const
Definition: swtable.hxx:471
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:418
o3tl::strong_int< sal_Int32, struct Tag_SwNodeOffset > SwNodeOffset
Definition: nodeoffset.hxx:16
SwTableNode * FindTableNode()
Search table node, in which it is.
Definition: node.cxx:358
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:2380
iterator end()
Definition: swtable.hxx:79
void SetNewTable(std::unique_ptr< SwTable >, bool bNewFrames=true)
Definition: ndtbl.cxx:2482
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:39
void IncRefCnt()
Definition: ddefld.hxx:97
SwTextNode * GetTextNode()
Inline methods from Node.hxx.
Definition: ndtxt.hxx:864
Base class of the Writer document model elements.
Definition: node.hxx:81