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 <frmfmt.hxx>
21 #include <doc.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 
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  {
69  assert(m_pTableNode);
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(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 
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 
163  SwDoc* pDoc = GetFrameFormat()->GetDoc();
164  if( AUTOUPD_FIELD_AND_CHARTS == rIDSA.getFieldUpdateFlags(true) )
165  pDoc->getIDocumentFieldsAccess().SetFieldsDirty( true, nullptr, 0 );
166 }
167 
169 {
170  return m_pDDEType;
171 }
172 
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
195 
196  pNewTable->GetTabLines().insert( pNewTable->GetTabLines().begin(),
197  GetTabLines().begin(), GetTabLines().end() ); // move lines
198  GetTabLines().clear();
199 
202 
203  pTableNd->SetNewTable( std::move(pNewTable) ); // replace table
204 
205  return true;
206 }
207 
208 /* 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:85
bool IsDeleted() const
Definition: ddefld.hxx:85
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:93
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:293
IDocumentFieldsAccess const & getIDocumentFieldsAccess() const
Definition: doc.cxx:389
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.
SwDDEFieldType * GetDDEFieldType()
Definition: ddetbl.cxx:168
SwTableSortBoxes m_TabSortContentBoxes
Definition: swtable.hxx:115
SwTableSortBoxes & GetTabSortBoxes()
Definition: swtable.hxx:259
const OUString & GetExpansion() const
Definition: ddefld.hxx:69
bool empty() const
Definition: swtable.hxx:73
void ChangeContent()
Definition: ddetbl.cxx:124
void clear()
Definition: swtable.hxx:81
bool NoDDETable()
Definition: ddetbl.cxx:173
int i
virtual ~SwDDETable() override
Definition: ddetbl.cxx:64
bool HasOnlyOneListener()
Definition: calbck.hxx:226
Marks a character position inside a document model node.
Definition: index.hxx:37
void UnlockModify()
Definition: calbck.hxx:217
void LockModify()
Definition: calbck.hxx:216
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:263
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:759
void DecRefCnt()
Definition: ddefld.hxx:98
SwTableLines & GetTabLines()
Definition: swtable.hxx:198
IDocumentLayoutAccess const & getIDocumentLayoutAccess() const
Definition: doc.cxx:437
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:652
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:351
SwDDEFieldType * m_pDDEType
Definition: swddetbl.hxx:29
bool IsInDtor() const
Definition: doc.hxx:398
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:2336
iterator end()
Definition: swtable.hxx:77
void SetNewTable(std::unique_ptr< SwTable >, bool bNewFrames=true)
Definition: ndtbl.cxx:2455
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:97
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