LibreOffice Module sw (master)  1
cellatr.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 <calc.hxx>
21 #include <cellatr.hxx>
22 #include <doc.hxx>
23 #include <float.h>
24 #include <hintids.hxx>
25 #include <hints.hxx>
26 #include <node.hxx>
27 #include <rolbck.hxx>
28 #include <rtl/math.hxx>
29 #include <rtl/ustring.hxx>
30 #include <calbck.hxx>
31 #include <swtable.hxx>
32 
33 // The % SV_COUNTRY_LANGUAGE_OFFSET result checks if nFormat is a mere built-in
34 // @ Text format of *any* locale and if so uses the default text format. Text
35 // is text, the locale doesn't matter for Writer's number formatting purposes.
36 // The advantage is that this is the pool's default item value and some places
37 // benefit from this special treatment in that they don't have to handle/store
38 // attribute specifics, especially when writing a document.
42  getSwDefaultTextFormat() : nFormat))
43 {
44 }
45 
46 bool SwTableBoxNumFormat::operator==( const SfxPoolItem& rAttr ) const
47 {
48  assert(SfxPoolItem::operator==(rAttr));
49  return GetValue() == static_cast<const SwTableBoxNumFormat&>(rAttr).GetValue();
50 }
51 
53 {
54  return new SwTableBoxNumFormat( GetValue() );
55 }
56 
57 SwTableBoxFormula::SwTableBoxFormula( const OUString& rFormula )
59  SwTableFormula( rFormula ),
60  m_pDefinedIn( nullptr )
61 {
62 }
63 
64 bool SwTableBoxFormula::operator==( const SfxPoolItem& rAttr ) const
65 {
66  assert(SfxPoolItem::operator==(rAttr));
67  return GetFormula() == static_cast<const SwTableBoxFormula&>(rAttr).GetFormula() &&
68  m_pDefinedIn == static_cast<const SwTableBoxFormula&>(rAttr).m_pDefinedIn;
69 }
70 
72 {
73  // switch to external rendering
75  pNew->SwTableFormula::operator=( *this );
76  return pNew;
77 }
78 
87 {
88  const SwNode* pRet = nullptr;
89  if( m_pDefinedIn )
90  {
91  SwTableBox* pBox = SwIterator<SwTableBox,SwModify>( *m_pDefinedIn ).First();
92  if( pBox )
93  pRet = pBox->GetSttNd();
94  }
95  return pRet;
96 }
97 
99 {
100  SwTableBox* pBox = nullptr;
101  if( m_pDefinedIn )
102  pBox = SwIterator<SwTableBox,SwModify>( *m_pDefinedIn ).First();
103  return pBox;
104 }
105 
107 {
108  if( !m_pDefinedIn )
109  return ;
110 
111  SwTableFormulaUpdate* pUpdateField;
112  if( !pItem || RES_TABLEFML_UPDATE != pItem->Which() )
113  {
114  // reset value flag
115  ChgValid( false );
116  return ;
117  }
118 
119  pUpdateField = const_cast<SwTableFormulaUpdate*>(static_cast<const SwTableFormulaUpdate*>(pItem));
120 
121  // detect table that contains this attribute
122  const SwTableNode* pTableNd;
123  const SwNode* pNd = GetNodeOfFormula();
124  if (!pNd || &pNd->GetNodes() != &pNd->GetDoc()->GetNodes())
125  return;
126  pTableNd = pNd->FindTableNode();
127  if( pTableNd != nullptr )
128  {
129  switch( pUpdateField->m_eFlags )
130  {
131  case TBL_CALC:
132  // reset value flag
133  ChgValid( false );
134  break;
135  case TBL_BOXNAME:
136  if( &pTableNd->GetTable() == pUpdateField->m_pTable )
137  // use external rendering
138  PtrToBoxNm( pUpdateField->m_pTable );
139  break;
140  case TBL_BOXPTR:
141  // internal rendering
142  BoxNmToPtr( &pTableNd->GetTable() );
143  break;
144  case TBL_RELBOXNAME:
145  if( &pTableNd->GetTable() == pUpdateField->m_pTable )
146  // relative rendering
147  ToRelBoxNm( pUpdateField->m_pTable );
148  break;
149 
150  case TBL_SPLITTBL:
151  if( &pTableNd->GetTable() == pUpdateField->m_pTable )
152  {
153  sal_uInt16 nLnPos = SwTableFormula::GetLnPosInTable(
154  pTableNd->GetTable(), GetTableBox() );
155  pUpdateField->m_bBehindSplitLine = USHRT_MAX != nLnPos &&
156  pUpdateField->m_nSplitLine <= nLnPos;
157  }
158  else
159  pUpdateField->m_bBehindSplitLine = false;
160  [[fallthrough]];
161  case TBL_MERGETBL:
162  if( pUpdateField->m_pHistory )
163  {
164  // for a history record the unchanged formula is needed
165  SwTableBoxFormula aCopy( *this );
166  pUpdateField->m_bModified = false;
167  ToSplitMergeBoxNm( *pUpdateField );
168 
169  if( pUpdateField->m_bModified )
170  {
171  // external rendering
172  aCopy.PtrToBoxNm( &pTableNd->GetTable() );
173  pUpdateField->m_pHistory->Add(
174  &aCopy,
175  &aCopy,
176  pNd->FindTableBoxStartNode()->GetIndex());
177  }
178  }
179  else
180  ToSplitMergeBoxNm( *pUpdateField );
181  break;
182  }
183  }
184 }
185 
186 void SwTableBoxFormula::Calc( SwTableCalcPara& rCalcPara, double& rValue )
187 {
188  if( !rCalcPara.m_rCalc.IsCalcError() )
189  {
190  // create pointers from box names
191  BoxNmToPtr( rCalcPara.m_pTable );
192  const OUString sFormula( MakeFormula( rCalcPara ));
193  if( !rCalcPara.m_rCalc.IsCalcError() )
194  rValue = rCalcPara.m_rCalc.Calculate( sFormula ).GetDouble();
195  else
196  rValue = DBL_MAX;
197  ChgValid( !rCalcPara.IsStackOverflow() ); // value is now valid again
198  }
199 }
200 
202  : SfxPoolItem( RES_BOXATR_VALUE ), m_nValue( 0 )
203 {
204 }
205 
207  : SfxPoolItem( RES_BOXATR_VALUE ), m_nValue( nVal )
208 {
209 }
210 
211 bool SwTableBoxValue::operator==( const SfxPoolItem& rAttr ) const
212 {
213  assert(SfxPoolItem::operator==(rAttr));
214  SwTableBoxValue const& rOther( static_cast<SwTableBoxValue const&>(rAttr) );
215  // items with NaN should be equal to enable pooling
216  return ::rtl::math::isNan( m_nValue )
217  ? ::rtl::math::isNan( rOther.m_nValue )
218  : ( m_nValue == rOther.m_nValue );
219 }
220 
222 {
223  return new SwTableBoxValue( m_nValue );
224 }
225 
226 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual bool operator==(const SfxPoolItem &) const override
Definition: cellatr.cxx:211
sal_uLong GetIndex() const
Definition: node.hxx:282
SwHistory * m_pHistory
Definition: hints.hxx:206
const SwTable * m_pTable
current table
Definition: cellfml.hxx:46
bool IsCalcError() const
Definition: calc.hxx:238
virtual bool operator==(const SfxPoolItem &) const override
Definition: cellatr.cxx:64
OUString MakeFormula(SwTableCalcPara &rCalcPara) const
Definition: cellfml.hxx:104
void ChangeState(const SfxPoolItem *pItem)
Definition: cellatr.cxx:106
#define RES_TABLEFML_UPDATE
Definition: hintids.hxx:292
void BoxNmToPtr(const SwTable *pTable)
create from the external formula the internal
Definition: cellfml.cxx:567
const SwTable * m_pTable
Pointer to the current table.
Definition: hints.hxx:201
SwTableBoxNumFormat(sal_uInt32 nFormat=getSwDefaultTextFormat())
Definition: cellatr.cxx:39
SwTableBox * GetTableBox()
Definition: cellatr.cxx:98
const SwTable & GetTable() const
Definition: node.hxx:497
virtual SfxPoolItem * Clone(SfxItemPool *pPool=nullptr) const override
Definition: cellatr.cxx:71
static sal_uInt16 GetLnPosInTable(const SwTable &rTable, const SwTableBox *pBox)
Definition: cellfml.cxx:1044
void PtrToBoxNm(const SwTable *pTable)
create from the internal formula (for CORE) the external formula (for UI)
Definition: cellfml.cxx:542
double GetDouble() const
Definition: calc.cxx:1410
TElementType * First()
Definition: calbck.hxx:345
SwDoc * GetDoc()
Definition: node.hxx:702
virtual SfxPoolItem * Clone(SfxItemPool *pPool=nullptr) const override
Definition: cellatr.cxx:221
virtual bool operator==(const SfxPoolItem &) const override
Definition: cellatr.cxx:46
SwNodes & GetNodes()
Node is in which nodes-array/doc?
Definition: node.hxx:693
bool IsStackOverflow() const
Definition: cellfml.hxx:52
void ToSplitMergeBoxNm(SwTableFormulaUpdate &rTableUpd)
gets called before/after merging/splitting of tables
Definition: cellfml.cxx:1203
#define RES_BOXATR_VALUE
Definition: hintids.hxx:263
#define SV_COUNTRY_LANGUAGE_OFFSET
SwTableBoxFormula(const OUString &rFormula)
Definition: cellatr.cxx:57
void Add(const SfxPoolItem *pOldValue, const SfxPoolItem *pNewValue, sal_uLong nNodeIdx)
Definition: rolbck.cxx:974
SwSbxValue Calculate(const OUString &rStr)
Definition: calc.cxx:347
void Calc(SwTableCalcPara &rCalcPara, double &rValue)
Definition: cellatr.cxx:186
const SwStartNode * GetSttNd() const
Definition: swtable.hxx:439
void ChgValid(bool bNew)
Definition: cellfml.hxx:134
const OUString & GetFormula() const
Definition: cellfml.hxx:136
virtual const SwNode * GetNodeOfFormula() const override
Get node type of the node containing this formula.
Definition: cellatr.cxx:86
const o3tl::enumarray< SvxAdjust, unsigned short > aSvxToUnoAdjust USHRT_MAX
Definition: unosett.cxx:261
SwNodes & GetNodes()
Definition: doc.hxx:402
void ToRelBoxNm(const SwTable *pTable)
create from the external/internal formula the relative formula
Definition: cellfml.cxx:592
SwTableBox is one table cell in the document model.
Definition: swtable.hxx:386
virtual SfxPoolItem * Clone(SfxItemPool *pPool=nullptr) const override
Definition: cellatr.cxx:52
SwModify * m_pDefinedIn
Definition: cellatr.hxx:54
const SwStartNode * FindTableBoxStartNode() const
Definition: node.hxx:196
SwTableNode * FindTableNode()
Search table node, in which it is.
Definition: node.cxx:351
sal_uInt32 GetValue() const
double m_nValue
Definition: cellatr.hxx:80
TableFormulaUpdateFlags m_eFlags
Definition: hints.hxx:208
constexpr sal_uInt32 getSwDefaultTextFormat()
The number formatter's default locale's @ Text format.
Definition: cellatr.hxx:34
sal_uInt16 Which() const
SwCalc & m_rCalc
current Calculator
Definition: cellfml.hxx:45
#define RES_BOXATR_FORMAT
Definition: hintids.hxx:261
sal_uInt16 m_nSplitLine
Split: from this BaseLine on will be split.
Definition: hints.hxx:207
#define RES_BOXATR_FORMULA
Definition: hintids.hxx:262
Base class of the Writer document model elements.
Definition: node.hxx:79