nss: upgrade to release 3.73
[LibreOffice.git] / sw / source / core / attr / cellatr.cxx
blob8b97ff2095fa88e59860146f48df1e3c9bdabf9b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
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.
39 SwTableBoxNumFormat::SwTableBoxNumFormat( sal_uInt32 nFormat )
40 : SfxUInt32Item( RES_BOXATR_FORMAT,
41 (((nFormat % SV_COUNTRY_LANGUAGE_OFFSET) == getSwDefaultTextFormat()) ?
42 getSwDefaultTextFormat() : nFormat))
46 bool SwTableBoxNumFormat::operator==( const SfxPoolItem& rAttr ) const
48 assert(SfxPoolItem::operator==(rAttr));
49 return GetValue() == static_cast<const SwTableBoxNumFormat&>(rAttr).GetValue();
52 SwTableBoxNumFormat* SwTableBoxNumFormat::Clone( SfxItemPool* ) const
54 return new SwTableBoxNumFormat( GetValue() );
57 SwTableBoxFormula::SwTableBoxFormula( const OUString& rFormula )
58 : SfxPoolItem( RES_BOXATR_FORMULA ),
59 SwTableFormula( rFormula ),
60 m_pDefinedIn( nullptr )
64 bool SwTableBoxFormula::operator==( const SfxPoolItem& rAttr ) const
66 assert(SfxPoolItem::operator==(rAttr));
67 return GetFormula() == static_cast<const SwTableBoxFormula&>(rAttr).GetFormula() &&
68 m_pDefinedIn == static_cast<const SwTableBoxFormula&>(rAttr).m_pDefinedIn;
71 SwTableBoxFormula* SwTableBoxFormula::Clone( SfxItemPool* ) const
73 // switch to external rendering
74 SwTableBoxFormula* pNew = new SwTableBoxFormula( GetFormula() );
75 pNew->SwTableFormula::operator=( *this );
76 return pNew;
79 /** Get node type of the node containing this formula
81 E.g. TextField -> TextNode, or
82 BoxAttribute -> BoxStartNode
84 Caution: Must override when inheriting.
86 const SwNode* SwTableBoxFormula::GetNodeOfFormula() const
88 const SwNode* pRet = nullptr;
89 if( m_pDefinedIn )
91 SwTableBox* pBox = SwIterator<SwTableBox,sw::BroadcastingModify>( *m_pDefinedIn ).First();
92 if( pBox )
93 pRet = pBox->GetSttNd();
95 return pRet;
98 SwTableBox* SwTableBoxFormula::GetTableBox()
100 SwTableBox* pBox = nullptr;
101 if( m_pDefinedIn )
102 pBox = SwIterator<SwTableBox,sw::BroadcastingModify>( *m_pDefinedIn ).First();
103 return pBox;
106 void SwTableBoxFormula::ChangeState( const SfxPoolItem* pItem )
108 if( !m_pDefinedIn )
109 return ;
111 SwTableFormulaUpdate* pUpdateField;
112 if( !pItem || RES_TABLEFML_UPDATE != pItem->Which() )
114 // reset value flag
115 ChgValid( false );
116 return ;
119 pUpdateField = const_cast<SwTableFormulaUpdate*>(static_cast<const SwTableFormulaUpdate*>(pItem));
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 return;
130 switch( pUpdateField->m_eFlags )
132 case TBL_CALC:
133 // reset value flag
134 ChgValid( false );
135 break;
136 case TBL_BOXNAME:
137 if( &pTableNd->GetTable() == pUpdateField->m_pTable )
138 // use external rendering
139 PtrToBoxNm( pUpdateField->m_pTable );
140 break;
141 case TBL_BOXPTR:
142 // internal rendering
143 BoxNmToPtr( &pTableNd->GetTable() );
144 break;
145 case TBL_RELBOXNAME:
146 if( &pTableNd->GetTable() == pUpdateField->m_pTable )
147 // relative rendering
148 ToRelBoxNm( pUpdateField->m_pTable );
149 break;
151 case TBL_SPLITTBL:
152 if( &pTableNd->GetTable() == pUpdateField->m_pTable )
154 sal_uInt16 nLnPos = SwTableFormula::GetLnPosInTable(
155 pTableNd->GetTable(), GetTableBox() );
156 pUpdateField->m_bBehindSplitLine = USHRT_MAX != nLnPos &&
157 pUpdateField->m_nSplitLine <= nLnPos;
159 else
160 pUpdateField->m_bBehindSplitLine = false;
161 [[fallthrough]];
162 case TBL_MERGETBL:
163 if( pUpdateField->m_pHistory )
165 // for a history record the unchanged formula is needed
166 SwTableBoxFormula aCopy( *this );
167 pUpdateField->m_bModified = false;
168 ToSplitMergeBoxNm( *pUpdateField );
170 if( pUpdateField->m_bModified )
172 // external rendering
173 aCopy.PtrToBoxNm( &pTableNd->GetTable() );
174 pUpdateField->m_pHistory->Add(
175 &aCopy,
176 &aCopy,
177 pNd->FindTableBoxStartNode()->GetIndex());
180 else
181 ToSplitMergeBoxNm( *pUpdateField );
182 break;
186 void SwTableBoxFormula::Calc( SwTableCalcPara& rCalcPara, double& rValue )
188 if( !rCalcPara.m_rCalc.IsCalcError() )
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
201 SwTableBoxValue::SwTableBoxValue()
202 : SfxPoolItem( RES_BOXATR_VALUE ), m_nValue( 0 )
206 SwTableBoxValue::SwTableBoxValue( const double nVal )
207 : SfxPoolItem( RES_BOXATR_VALUE ), m_nValue( nVal )
211 bool SwTableBoxValue::operator==( const SfxPoolItem& rAttr ) const
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 std::isnan( m_nValue )
217 ? std::isnan( rOther.m_nValue )
218 : ( m_nValue == rOther.m_nValue );
221 SwTableBoxValue* SwTableBoxValue::Clone( SfxItemPool* ) const
223 return new SwTableBoxValue( m_nValue );
226 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */