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::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 
93 void SwDDETable::SwClientNotify( const SwModify& rModify, const SfxHint& rHint )
94 {
95  SwClient::SwClientNotify(rModify, rHint);
96  if(auto pFieldHint = dynamic_cast<const SwFieldHint*>(&rHint))
97  {
98  pFieldHint->m_pPaM->DeleteMark(); // TODO: this is really hackish
99  // replace DDETable by real table
100  NoDDETable();
101  }
102  else if(const auto pLinkAnchorHint = dynamic_cast<const sw::LinkAnchorSearchHint*>(&rHint))
103  {
104  if(pLinkAnchorHint->m_rpFoundNode)
105  return;
106  const auto pNd = GetTabSortBoxes()[0]->GetSttNd();
107  if( pNd && &pLinkAnchorHint->m_rNodes == &pNd->GetNodes() )
108  pLinkAnchorHint->m_rpFoundNode = pNd;
109  }
110  else if(const sw::InRangeSearchHint* pInRangeHint = dynamic_cast<const sw::InRangeSearchHint*>(&rHint))
111  {
112  if(pInRangeHint->m_rIsInRange)
113  return;
114  const SwTableNode* pTableNd = GetTabSortBoxes()[0]->GetSttNd()->FindTableNode();
115  if( pTableNd->GetNodes().IsDocNodes() &&
116  pInRangeHint->m_nSttNd < pTableNd->EndOfSectionIndex() &&
117  pInRangeHint->m_nEndNd > pTableNd->GetIndex() )
118  pInRangeHint->m_rIsInRange = true;
119  }
120  else if (auto pModifyChangedHint = dynamic_cast<const sw::ModifyChangedHint*>(&rHint))
121  {
122  if(m_pDDEType == &rModify)
123  m_pDDEType = const_cast<SwDDEFieldType*>(static_cast<const SwDDEFieldType*>(pModifyChangedHint->m_pNew));
124  }
125 }
126 
128 {
129  OSL_ENSURE( GetFrameFormat(), "No FrameFormat" );
130 
131  // Is this the correct NodesArray? (because of UNDO)
132  if( m_aLines.empty() )
133  return;
134  OSL_ENSURE( !GetTabSortBoxes().empty(), "Table without content?" );
135  if( !GetTabSortBoxes()[0]->GetSttNd()->GetNodes().IsDocNodes() )
136  return;
137 
138 
139  OUString aExpand = m_pDDEType->GetExpansion().replaceAll("\r", "");
140  sal_Int32 nExpandTokenPos = 0;
141 
142  for( size_t n = 0; n < m_aLines.size(); ++n )
143  {
144  OUString aLine = aExpand.getToken( 0, '\n', nExpandTokenPos );
145  sal_Int32 nLineTokenPos = 0;
146  SwTableLine* pLine = m_aLines[ n ];
147  for( size_t i = 0; i < pLine->GetTabBoxes().size(); ++i )
148  {
149  SwTableBox* pBox = pLine->GetTabBoxes()[ i ];
150  OSL_ENSURE( pBox->GetSttIdx(), "no content box" );
151  SwNodeIndex aNdIdx( *pBox->GetSttNd(), 1 );
152  SwTextNode* pTextNode = aNdIdx.GetNode().GetTextNode();
153  OSL_ENSURE( pTextNode, "No Node" );
154  SwIndex aCntIdx( pTextNode, 0 );
155  pTextNode->EraseText( aCntIdx );
156  pTextNode->InsertText( aLine.getToken( 0, '\t', nLineTokenPos ), aCntIdx );
157 
158  SwTableBoxFormat* pBoxFormat = static_cast<SwTableBoxFormat*>(pBox->GetFrameFormat());
159  pBoxFormat->LockModify();
160  pBoxFormat->ResetFormatAttr( RES_BOXATR_VALUE );
161  pBoxFormat->UnlockModify();
162  }
163  }
164 
166  SwDoc* pDoc = GetFrameFormat()->GetDoc();
167  if( AUTOUPD_FIELD_AND_CHARTS == rIDSA.getFieldUpdateFlags(true) )
168  pDoc->getIDocumentFieldsAccess().SetFieldsDirty( true, nullptr, 0 );
169 }
170 
172 {
173  return m_pDDEType;
174 }
175 
177 {
178  // search table node
179  OSL_ENSURE( GetFrameFormat(), "No FrameFormat" );
180  SwDoc* pDoc = GetFrameFormat()->GetDoc();
181 
182  // Is this the correct NodesArray? (because of UNDO)
183  if( m_aLines.empty() )
184  return false;
185  OSL_ENSURE( !GetTabSortBoxes().empty(), "Table without content?" );
186  SwNode* pNd = const_cast<SwNode*>(static_cast<SwNode const *>(GetTabSortBoxes()[0]->GetSttNd()));
187  if( !pNd->GetNodes().IsDocNodes() )
188  return false;
189 
190  SwTableNode* pTableNd = pNd->FindTableNode();
191  OSL_ENSURE( pTableNd, "Where is the table?");
192 
193  std::unique_ptr<SwTable> pNewTable(new SwTable( *this ));
194 
195  // copy the table data
196  pNewTable->GetTabSortBoxes().insert( GetTabSortBoxes() ); // move content boxes
198 
199  pNewTable->GetTabLines().insert( pNewTable->GetTabLines().begin(),
200  GetTabLines().begin(), GetTabLines().end() ); // move lines
201  GetTabLines().clear();
202 
205 
206  pTableNd->SetNewTable( std::move(pNewTable) ); // replace table
207 
208  return true;
209 }
210 
211 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SwTableNode * m_pTableNode
Definition: swtable.hxx:125
sal_uLong GetIndex() const
Definition: node.hxx:290
sal_uLong GetSttIdx() const
Definition: swtable.cxx:1865
sw::WriterMultiListener m_aDepends
Definition: swddetbl.hxx:28
constexpr TypedWhichId< SwTableBoxValue > RES_BOXATR_VALUE(150)
virtual void Modify(const SfxPoolItem *, const SfxPoolItem *) override
Definition: ddetbl.cxx:85
bool IsDeleted() const
Definition: ddefld.hxx:86
sal_Int64 n
Definition: doc.hxx:186
SwTableLine is one table row in the document model.
Definition: swtable.hxx:351
virtual void SwClientNotify(const SwModify &, const SfxHint &) override
Definition: ddetbl.cxx:93
virtual void SwClientNotify(const SwModify &, const SfxHint &rHint) override
Definition: calbck.cxx:111
iterator begin()
Definition: swtable.hxx:76
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:177
virtual SwFieldUpdateFlags getFieldUpdateFlags(bool bGlobalSettings) const =0
Get the current field update mode.
bool HasOnlyOneListener() const
Definition: calbck.hxx:224
SwDDEFieldType * GetDDEFieldType()
Definition: ddetbl.cxx:171
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:127
void clear()
Definition: swtable.hxx:82
bool NoDDETable()
Definition: ddetbl.cxx:176
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:215
void LockModify()
Definition: calbck.hxx:214
Provides access to settings of a document.
Marks a node in the document model.
Definition: ndindex.hxx:31
SwFrameFormat * GetFrameFormat()
Definition: swtable.hxx:432
SwNodes & GetNodes()
Node is in which nodes-array/doc?
Definition: node.hxx:696
const SwDoc * GetDoc() const
The document is set in SwAttrPool now, therefore you always can access it.
Definition: format.hxx:119
const IDocumentSettingAccess & getIDocumentSettingAccess() const
Provides access to the document settings interface.
Definition: format.cxx:736
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:328
virtual void Modify(const SfxPoolItem *, const SfxPoolItem *)
Definition: calbck.cxx:133
sal_uLong EndOfSectionIndex() const
Definition: node.hxx:680
SwTextNode is a paragraph in the document model.
Definition: ndtxt.hxx:80
SwTableBoxes & GetTabBoxes()
Definition: swtable.hxx:361
virtual bool ResetFormatAttr(sal_uInt16 nWhich1, sal_uInt16 nWhich2=0)
Definition: format.cxx:641
const SwStartNode * GetSttNd() const
Definition: swtable.hxx:446
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:393
SwTableNode * FindTableNode()
Search table node, in which it is.
Definition: node.cxx:355
SwDDEFieldType * m_pDDEType
Definition: swddetbl.hxx:29
bool IsInDtor() const
Definition: doc.hxx:400
bool IsDocNodes() const
Is the NodesArray the regular one of Doc? (and not the UndoNds, ...) Implementation in doc...
Definition: nodes.cxx:2320
iterator end()
Definition: swtable.hxx:78
void SetNewTable(std::unique_ptr< SwTable >, bool bNewFrames=true)
Definition: ndtbl.cxx:2476
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
constexpr TypedWhichId< SwMsgPoolItem > RES_UPDATEDDETBL(169)
void IncRefCnt()
Definition: ddefld.hxx:98
sal_uInt16 Which() const
SwTextNode * GetTextNode()
Inline methods from Node.hxx.
Definition: ndtxt.hxx:845
Base class of the Writer document model elements.
Definition: node.hxx:79