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 
37 SwDDETable::SwDDETable( SwTable& rTable, SwDDEFieldType* pDDEType, bool bUpdate )
38  : SwTable(rTable), m_aDepends(*this), m_pDDEType(pDDEType)
39 {
41  // copy the table data
43  rTable.GetTabSortBoxes().clear();
44 
46  rTable.GetTabLines().begin(), rTable.GetTabLines().end() ); // move lines
47  rTable.GetTabLines().clear();
48 
49  if( !m_aLines.empty() )
50  {
51  const SwNode& rNd = *GetTabSortBoxes()[0]->GetSttNd();
52  if( rNd.GetNodes().IsDocNodes() )
53  {
54  pDDEType->IncRefCnt();
55 
56  // update box content only if update flag is set (false in import)
57  if (bUpdate)
58  ChangeContent();
59  }
60  }
61 }
62 
64 {
65  SwDoc* pDoc = GetFrameFormat()->GetDoc();
66  if (!pDoc->IsInDtor() && !m_aLines.empty())
67  {
68  assert(m_pTableNode);
70  {
72  }
73  }
74 
75  // If it is the last dependent of the "deleted field" than delete it finally
77  {
79  delete m_pDDEType;
80  m_pDDEType = nullptr;
81  }
82 }
83 
84 void SwDDETable::Modify( const SfxPoolItem* pOld, const SfxPoolItem* pNew )
85 {
86  if( pNew && RES_UPDATEDDETBL == pNew->Which() )
87  ChangeContent();
88  else
89  SwTable::Modify( pOld, pNew );
90 }
91 
92 void SwDDETable::SwClientNotify( const SwModify& rModify, const SfxHint& rHint )
93 {
94  SwClient::SwClientNotify(rModify, rHint);
95  if(dynamic_cast<const SwFieldHint*>(&rHint))
96  // replace DDETable by real table
97  NoDDETable();
98  else if(const auto pLinkAnchorHint = dynamic_cast<const sw::LinkAnchorSearchHint*>(&rHint))
99  {
100  if(pLinkAnchorHint->m_rpFoundNode)
101  return;
102  const auto pNd = GetTabSortBoxes()[0]->GetSttNd();
103  if( pNd && &pLinkAnchorHint->m_rNodes == &pNd->GetNodes() )
104  pLinkAnchorHint->m_rpFoundNode = pNd;
105  }
106  else if(const sw::InRangeSearchHint* pInRangeHint = dynamic_cast<const sw::InRangeSearchHint*>(&rHint))
107  {
108  if(pInRangeHint->m_rIsInRange)
109  return;
110  const SwTableNode* pTableNd = GetTabSortBoxes()[0]->GetSttNd()->FindTableNode();
111  if( pTableNd->GetNodes().IsDocNodes() &&
112  pInRangeHint->m_nSttNd < pTableNd->EndOfSectionIndex() &&
113  pInRangeHint->m_nEndNd > pTableNd->GetIndex() )
114  pInRangeHint->m_rIsInRange = true;
115  }
116  else if (auto pModifyChangedHint = dynamic_cast<const sw::ModifyChangedHint*>(&rHint))
117  {
118  if(m_pDDEType == &rModify)
119  m_pDDEType = const_cast<SwDDEFieldType*>(static_cast<const SwDDEFieldType*>(pModifyChangedHint->m_pNew));
120  }
121 }
122 
124 {
125  OSL_ENSURE( GetFrameFormat(), "No FrameFormat" );
126 
127  // Is this the correct NodesArray? (because of UNDO)
128  if( m_aLines.empty() )
129  return;
130  OSL_ENSURE( !GetTabSortBoxes().empty(), "Table without content?" );
131  if( !GetTabSortBoxes()[0]->GetSttNd()->GetNodes().IsDocNodes() )
132  return;
133 
134 
135  OUString aExpand = m_pDDEType->GetExpansion().replaceAll("\r", "");
136  sal_Int32 nExpandTokenPos = 0;
137 
138  for( size_t n = 0; n < m_aLines.size(); ++n )
139  {
140  OUString aLine = aExpand.getToken( 0, '\n', nExpandTokenPos );
141  sal_Int32 nLineTokenPos = 0;
142  SwTableLine* pLine = m_aLines[ n ];
143  for( size_t i = 0; i < pLine->GetTabBoxes().size(); ++i )
144  {
145  SwTableBox* pBox = pLine->GetTabBoxes()[ i ];
146  OSL_ENSURE( pBox->GetSttIdx(), "no content box" );
147  SwNodeIndex aNdIdx( *pBox->GetSttNd(), 1 );
148  SwTextNode* pTextNode = aNdIdx.GetNode().GetTextNode();
149  OSL_ENSURE( pTextNode, "No Node" );
150  SwIndex aCntIdx( pTextNode, 0 );
151  pTextNode->EraseText( aCntIdx );
152  pTextNode->InsertText( aLine.getToken( 0, '\t', nLineTokenPos ), aCntIdx );
153 
154  SwTableBoxFormat* pBoxFormat = static_cast<SwTableBoxFormat*>(pBox->GetFrameFormat());
155  pBoxFormat->LockModify();
156  pBoxFormat->ResetFormatAttr( RES_BOXATR_VALUE );
157  pBoxFormat->UnlockModify();
158  }
159  }
160 
162  SwDoc* pDoc = GetFrameFormat()->GetDoc();
163  if( AUTOUPD_FIELD_AND_CHARTS == rIDSA.getFieldUpdateFlags(true) )
164  pDoc->getIDocumentFieldsAccess().SetFieldsDirty( true, nullptr, 0 );
165 }
166 
168 {
169  return m_pDDEType;
170 }
171 
173 {
174  // search table node
175  OSL_ENSURE( GetFrameFormat(), "No FrameFormat" );
176  SwDoc* pDoc = GetFrameFormat()->GetDoc();
177 
178  // Is this the correct NodesArray? (because of UNDO)
179  if( m_aLines.empty() )
180  return false;
181  OSL_ENSURE( !GetTabSortBoxes().empty(), "Table without content?" );
182  SwNode* pNd = const_cast<SwNode*>(static_cast<SwNode const *>(GetTabSortBoxes()[0]->GetSttNd()));
183  if( !pNd->GetNodes().IsDocNodes() )
184  return false;
185 
186  SwTableNode* pTableNd = pNd->FindTableNode();
187  OSL_ENSURE( pTableNd, "Where is the table?");
188 
189  std::unique_ptr<SwTable> pNewTable(new SwTable( *this ));
190 
191  // copy the table data
192  pNewTable->GetTabSortBoxes().insert( GetTabSortBoxes() ); // move content boxes
194 
195  pNewTable->GetTabLines().insert( pNewTable->GetTabLines().begin(),
196  GetTabLines().begin(), GetTabLines().end() ); // move lines
197  GetTabLines().clear();
198 
201 
202  pTableNd->SetNewTable( std::move(pNewTable) ); // replace table
203 
204  return true;
205 }
206 
207 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SwTableNode * m_pTableNode
Definition: swtable.hxx:124
sal_uLong GetIndex() const
Definition: node.hxx:282
sal_uLong GetSttIdx() const
Definition: swtable.cxx:1880
sw::WriterMultiListener m_aDepends
Definition: swddetbl.hxx:28
virtual void Modify(const SfxPoolItem *, const SfxPoolItem *) override
Definition: ddetbl.cxx:84
bool IsDeleted() const
Definition: ddefld.hxx:86
Definition: doc.hxx:185
SwTableLine is one table row in the document model.
Definition: swtable.hxx:344
virtual void SwClientNotify(const SwModify &, const SfxHint &) override
Definition: ddetbl.cxx:92
virtual void SwClientNotify(const SwModify &, const SfxHint &rHint) override
Definition: calbck.cxx:108
iterator begin()
Definition: swtable.hxx:75
SwTableFormat * GetFrameFormat()
Definition: swtable.hxx:201
size_type size() const
Definition: swtable.hxx:74
#define RES_UPDATEDDETBL
Definition: hintids.hxx:295
IDocumentFieldsAccess const & getIDocumentFieldsAccess() const
Definition: doc.cxx:356
SwTableLines m_aLines
Definition: swtable.hxx:114
virtual bool SetFieldsDirty(bool b, const SwNode *pChk, sal_uLong nLen)=0
iterator insert(iterator aIt, SwTableLine *pLine)
Definition: swtable.hxx:84
SwTable()
Definition: swtable.cxx:205
virtual SwFieldUpdateFlags getFieldUpdateFlags(bool bGlobalSettings) const =0
Get the current field update mode.
bool HasOnlyOneListener() const
Definition: calbck.hxx:223
SwDDEFieldType * GetDDEFieldType()
Definition: ddetbl.cxx:167
SwTableSortBoxes m_TabSortContentBoxes
Definition: swtable.hxx:115
SwTableSortBoxes & GetTabSortBoxes()
Definition: swtable.hxx:259
const OUString & GetExpansion() const
Definition: ddefld.hxx:70
bool empty() const
Definition: swtable.hxx:73
void ChangeContent()
Definition: ddetbl.cxx:123
void clear()
Definition: swtable.hxx:81
bool NoDDETable()
Definition: ddetbl.cxx:172
int i
virtual ~SwDDETable() override
Definition: ddetbl.cxx:63
Marks a character position inside a document model node.
Definition: index.hxx:37
void UnlockModify()
Definition: calbck.hxx:214
void LockModify()
Definition: calbck.hxx:213
Provides access to settings of a document.
Marks a node in the document model.
Definition: ndindex.hxx:31
SwFrameFormat * GetFrameFormat()
Definition: swtable.hxx:425
SwNodes & GetNodes()
Node is in which nodes-array/doc?
Definition: node.hxx:693
#define RES_BOXATR_VALUE
Definition: hintids.hxx:265
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:757
void DecRefCnt()
Definition: ddefld.hxx:99
SwTableLines & GetTabLines()
Definition: swtable.hxx:198
IDocumentLayoutAccess const & getIDocumentLayoutAccess() const
Definition: doc.cxx:404
SwTable is one table in the document model, containing rows (which contain cells).
Definition: swtable.hxx:110
void StartListening(SwModify *pDepend)
Definition: calbck.cxx:335
sal_uLong EndOfSectionIndex() const
Definition: node.hxx:677
SwTextNode is a paragraph in the document model.
Definition: ndtxt.hxx:79
SwTableBoxes & GetTabBoxes()
Definition: swtable.hxx:354
virtual bool ResetFormatAttr(sal_uInt16 nWhich1, sal_uInt16 nWhich2=0)
Definition: format.cxx:650
const SwStartNode * GetSttNd() const
Definition: swtable.hxx:439
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:386
SwTableNode * FindTableNode()
Search table node, in which it is.
Definition: node.cxx:349
SwDDEFieldType * m_pDDEType
Definition: swddetbl.hxx:29
bool IsInDtor() const
Definition: doc.hxx:397
virtual void Modify(const SfxPoolItem *pOld, const SfxPoolItem *pNew) override
Definition: swtable.cxx:343
bool IsDocNodes() const
Is the NodesArray the regular one of Doc? (and not the UndoNds, ...) Implementation in doc...
Definition: nodes.cxx:2334
iterator end()
Definition: swtable.hxx:77
void SetNewTable(std::unique_ptr< SwTable >, bool bNewFrames=true)
Definition: ndtbl.cxx:2448
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:37
void IncRefCnt()
Definition: ddefld.hxx:98
sal_uInt16 Which() const
SwTextNode * GetTextNode()
Inline methods from Node.hxx.
Definition: ndtxt.hxx:842
Base class of the Writer document model elements.
Definition: node.hxx:79