1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
21 #include <cellatr.hxx>
24 #include <hintids.hxx>
28 #include <rtl/ustring.hxx>
30 #include <swtable.hxx>
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.
38 SwTableBoxNumFormat::SwTableBoxNumFormat( sal_uInt32 nFormat
)
39 : SfxUInt32Item( RES_BOXATR_FORMAT
,
40 (((nFormat
% SV_COUNTRY_LANGUAGE_OFFSET
) == getSwDefaultTextFormat()) ?
41 getSwDefaultTextFormat() : nFormat
))
45 bool SwTableBoxNumFormat::operator==( const SfxPoolItem
& rAttr
) const
47 assert(SfxPoolItem::operator==(rAttr
));
48 return GetValue() == static_cast<const SwTableBoxNumFormat
&>(rAttr
).GetValue();
51 SwTableBoxNumFormat
* SwTableBoxNumFormat::Clone( SfxItemPool
* ) const
53 return new SwTableBoxNumFormat( GetValue() );
56 SwTableBoxFormula::SwTableBoxFormula( const OUString
& rFormula
)
57 : SfxPoolItem( RES_BOXATR_FORMULA
),
58 SwTableFormula( rFormula
),
59 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 );
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 auto pTableBox
= GetTableBox();
89 return pTableBox
? pTableBox
->GetSttNd() : nullptr;
92 SwTableBox
* SwTableBoxFormula::GetTableBox()
94 assert(!m_pDefinedIn
|| dynamic_cast<SwTableBoxFormat
*>(m_pDefinedIn
));
95 return m_pDefinedIn
? static_cast<SwTableBoxFormat
*>(m_pDefinedIn
)->GetTableBox() : nullptr;
98 void SwTableBoxFormula::TryBoxNmToPtr()
100 const SwNode
* pNd
= GetNodeOfFormula();
101 if (!pNd
|| &pNd
->GetNodes() != &pNd
->GetDoc().GetNodes())
103 if(const SwTableNode
* pTableNd
= pNd
->FindTableNode())
105 BoxNmToPtr(&pTableNd
->GetTable());
109 void SwTableBoxFormula::TryRelBoxNm()
111 const SwNode
* pNd
= GetNodeOfFormula();
112 if (!pNd
|| &pNd
->GetNodes() != &pNd
->GetDoc().GetNodes())
114 if(const SwTableNode
* pTableNd
= pNd
->FindTableNode())
116 ToRelBoxNm(&pTableNd
->GetTable());
120 void SwTableBoxFormula::ToSplitMergeBoxNmWithHistory(SwTableFormulaUpdate
& rUpdate
, SwHistory
* pHistory
)
124 ToSplitMergeBoxNm(rUpdate
);
127 auto pNd
= GetNodeOfFormula();
128 // for a history record the unchanged formula is needed
129 SwTableBoxFormula
aCopy(*this);
130 rUpdate
.m_bModified
= false;
131 ToSplitMergeBoxNm(rUpdate
);
132 if(rUpdate
.m_bModified
)
134 // external rendering
135 aCopy
.PtrToBoxNm(&pNd
->FindTableNode()->GetTable());
136 pHistory
->AddPoolItem(
139 pNd
->FindTableBoxStartNode()->GetIndex());
143 void SwTableBoxFormula::Calc( SwTableCalcPara
& rCalcPara
, double& rValue
)
145 if( !rCalcPara
.m_rCalc
.IsCalcError() )
147 // create pointers from box names
148 BoxNmToPtr( rCalcPara
.m_pTable
);
149 const OUString
sFormula( MakeFormula( rCalcPara
));
150 if( !rCalcPara
.m_rCalc
.IsCalcError() )
151 rValue
= rCalcPara
.m_rCalc
.Calculate( sFormula
).GetDouble();
154 ChgValid( !rCalcPara
.IsStackOverflow() ); // value is now valid again
158 SwTableBoxValue::SwTableBoxValue()
159 : SfxPoolItem( RES_BOXATR_VALUE
), m_nValue( 0 )
163 SwTableBoxValue::SwTableBoxValue( const double nVal
)
164 : SfxPoolItem( RES_BOXATR_VALUE
), m_nValue( nVal
)
168 bool SwTableBoxValue::operator==( const SfxPoolItem
& rAttr
) const
170 assert(SfxPoolItem::operator==(rAttr
));
171 SwTableBoxValue
const& rOther( static_cast<SwTableBoxValue
const&>(rAttr
) );
172 // items with NaN should be equal to enable pooling
173 return std::isnan( m_nValue
)
174 ? std::isnan( rOther
.m_nValue
)
175 : ( m_nValue
== rOther
.m_nValue
);
178 SwTableBoxValue
* SwTableBoxValue::Clone( SfxItemPool
* ) const
180 return new SwTableBoxValue( m_nValue
);
183 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */