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/ustring.hxx>
29 #include <calbck.hxx>
30 #include <swtable.hxx>
31 
32 // The % SV_COUNTRY_LANGUAGE_OFFSET result checks if nFormat is a mere built-in
33 // @ Text format of *any* locale and if so uses the default text format. Text
34 // is text, the locale doesn't matter for Writer's number formatting purposes.
35 // The advantage is that this is the pool's default item value and some places
36 // benefit from this special treatment in that they don't have to handle/store
37 // attribute specifics, especially when writing a document.
41  getSwDefaultTextFormat() : nFormat))
42 {
43 }
44 
45 bool SwTableBoxNumFormat::operator==( const SfxPoolItem& rAttr ) const
46 {
47  assert(SfxPoolItem::operator==(rAttr));
48  return GetValue() == static_cast<const SwTableBoxNumFormat&>(rAttr).GetValue();
49 }
50 
52 {
53  return new SwTableBoxNumFormat( GetValue() );
54 }
55 
56 SwTableBoxFormula::SwTableBoxFormula( const OUString& rFormula )
58  SwTableFormula( rFormula ),
59  m_pDefinedIn( nullptr )
60 {
61 }
62 
63 bool SwTableBoxFormula::operator==( const SfxPoolItem& rAttr ) const
64 {
65  assert(SfxPoolItem::operator==(rAttr));
66  return GetFormula() == static_cast<const SwTableBoxFormula&>(rAttr).GetFormula() &&
67  m_pDefinedIn == static_cast<const SwTableBoxFormula&>(rAttr).m_pDefinedIn;
68 }
69 
71 {
72  // switch to external rendering
74  pNew->SwTableFormula::operator=( *this );
75  return pNew;
76 }
77 
86 {
87  const SwNode* pRet = nullptr;
88  if( m_pDefinedIn )
89  {
91  if( pBox )
92  pRet = pBox->GetSttNd();
93  }
94  return pRet;
95 }
96 
98 {
99  SwTableBox* pBox = nullptr;
100  if( m_pDefinedIn )
101  pBox = SwIterator<SwTableBox,sw::BroadcastingModify>( *m_pDefinedIn ).First();
102  return pBox;
103 }
104 
106 {
107  if( !m_pDefinedIn )
108  return ;
109 
110  SwTableFormulaUpdate* pUpdateField;
111  if( !pItem || RES_TABLEFML_UPDATE != pItem->Which() )
112  {
113  // reset value flag
114  ChgValid( false );
115  return ;
116  }
117 
118  pUpdateField = const_cast<SwTableFormulaUpdate*>(static_cast<const SwTableFormulaUpdate*>(pItem));
119 
120  // detect table that contains this attribute
121  const SwTableNode* pTableNd;
122  const SwNode* pNd = GetNodeOfFormula();
123  if (!pNd || &pNd->GetNodes() != &pNd->GetDoc().GetNodes())
124  return;
125  pTableNd = pNd->FindTableNode();
126  if( pTableNd == nullptr )
127  return;
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 void SwTableBoxFormula::Calc( SwTableCalcPara& rCalcPara, double& rValue )
186 {
187  if( !rCalcPara.m_rCalc.IsCalcError() )
188  {
189  // create pointers from box names
190  BoxNmToPtr( rCalcPara.m_pTable );
191  const OUString sFormula( MakeFormula( rCalcPara ));
192  if( !rCalcPara.m_rCalc.IsCalcError() )
193  rValue = rCalcPara.m_rCalc.Calculate( sFormula ).GetDouble();
194  else
195  rValue = DBL_MAX;
196  ChgValid( !rCalcPara.IsStackOverflow() ); // value is now valid again
197  }
198 }
199 
201  : SfxPoolItem( RES_BOXATR_VALUE ), m_nValue( 0 )
202 {
203 }
204 
206  : SfxPoolItem( RES_BOXATR_VALUE ), m_nValue( nVal )
207 {
208 }
209 
210 bool SwTableBoxValue::operator==( const SfxPoolItem& rAttr ) const
211 {
212  assert(SfxPoolItem::operator==(rAttr));
213  SwTableBoxValue const& rOther( static_cast<SwTableBoxValue const&>(rAttr) );
214  // items with NaN should be equal to enable pooling
215  return std::isnan( m_nValue )
216  ? std::isnan( rOther.m_nValue )
217  : ( m_nValue == rOther.m_nValue );
218 }
219 
221 {
222  return new SwTableBoxValue( m_nValue );
223 }
224 
225 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual bool operator==(const SfxPoolItem &) const override
Definition: cellatr.cxx:210
sal_uLong GetIndex() const
Definition: node.hxx:291
constexpr TypedWhichId< SwTableFormulaUpdate > RES_TABLEFML_UPDATE(170)
constexpr TypedWhichId< SwTableBoxNumFormat > RES_BOXATR_FORMAT(RES_BOXATR_BEGIN)
SwHistory * m_pHistory
Definition: hints.hxx:248
const SwTable * m_pTable
current table
Definition: cellfml.hxx:48
bool IsCalcError() const
Definition: calc.hxx:246
virtual bool operator==(const SfxPoolItem &) const override
Definition: cellatr.cxx:63
OUString MakeFormula(SwTableCalcPara &rCalcPara) const
Definition: cellfml.hxx:106
constexpr TypedWhichId< SwTableBoxValue > RES_BOXATR_VALUE(152)
void ChangeState(const SfxPoolItem *pItem)
Definition: cellatr.cxx:105
void BoxNmToPtr(const SwTable *pTable)
create from the external formula the internal
Definition: cellfml.cxx:596
virtual SwTableBoxValue * Clone(SfxItemPool *pPool=nullptr) const override
Definition: cellatr.cxx:220
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
const SwTable * m_pTable
Pointer to the current table.
Definition: hints.hxx:243
SwTableBoxNumFormat(sal_uInt32 nFormat=getSwDefaultTextFormat())
Definition: cellatr.cxx:38
SwTableBox * GetTableBox()
Definition: cellatr.cxx:97
const SwTable & GetTable() const
Definition: node.hxx:501
static sal_uInt16 GetLnPosInTable(const SwTable &rTable, const SwTableBox *pBox)
Definition: cellfml.cxx:1073
SW_DLLPUBLIC void PtrToBoxNm(const SwTable *pTable)
create from the internal formula (for CORE) the external formula (for UI)
Definition: cellfml.cxx:571
double GetDouble() const
Definition: calc.cxx:1450
SwDoc & GetDoc()
Definition: node.hxx:212
TElementType * First()
Definition: calbck.hxx:325
virtual bool operator==(const SfxPoolItem &) const override
Definition: cellatr.cxx:45
SwNodes & GetNodes()
Node is in which nodes-array/doc?
Definition: node.hxx:697
bool IsStackOverflow() const
Definition: cellfml.hxx:54
void ToSplitMergeBoxNm(SwTableFormulaUpdate &rTableUpd)
gets called before/after merging/splitting of tables
Definition: cellfml.cxx:1232
#define SV_COUNTRY_LANGUAGE_OFFSET
SwTableBoxFormula(const OUString &rFormula)
Definition: cellatr.cxx:56
void Add(const SfxPoolItem *pOldValue, const SfxPoolItem *pNewValue, sal_uLong nNodeIdx)
Definition: rolbck.cxx:1035
SwSbxValue Calculate(const OUString &rStr)
Definition: calc.cxx:358
void Calc(SwTableCalcPara &rCalcPara, double &rValue)
Definition: cellatr.cxx:185
const SwStartNode * GetSttNd() const
Definition: swtable.hxx:445
void ChgValid(bool bNew)
Definition: cellfml.hxx:136
const OUString & GetFormula() const
Definition: cellfml.hxx:138
virtual const SwNode * GetNodeOfFormula() const override
Get node type of the node containing this formula.
Definition: cellatr.cxx:85
const o3tl::enumarray< SvxAdjust, unsigned short > aSvxToUnoAdjust USHRT_MAX
Definition: unosett.cxx:254
SwNodes & GetNodes()
Definition: doc.hxx:408
constexpr TypedWhichId< SwTableBoxFormula > RES_BOXATR_FORMULA(151)
void ToRelBoxNm(const SwTable *pTable)
create from the external/internal formula the relative formula
Definition: cellfml.cxx:621
SwTableBox is one table cell in the document model.
Definition: swtable.hxx:392
virtual SwTableBoxNumFormat * Clone(SfxItemPool *pPool=nullptr) const override
Definition: cellatr.cxx:51
const SwStartNode * FindTableBoxStartNode() const
Definition: node.hxx:197
SwTableNode * FindTableNode()
Search table node, in which it is.
Definition: node.cxx:357
sal_uInt32 GetValue() const
sw::BroadcastingModify * m_pDefinedIn
Definition: cellatr.hxx:54
double m_nValue
Definition: cellatr.hxx:80
virtual SwTableBoxFormula * Clone(SfxItemPool *pPool=nullptr) const override
Definition: cellatr.cxx:70
TableFormulaUpdateFlags m_eFlags
Definition: hints.hxx:250
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:47
sal_uInt16 m_nSplitLine
Split: from this BaseLine on will be split.
Definition: hints.hxx:249
Base class of the Writer document model elements.
Definition: node.hxx:80