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 )
63 bool SwTableBoxFormula::operator==( const SfxPoolItem
& rAttr
) const
65 assert(SfxPoolItem::operator==(rAttr
));
66 return GetFormula() == static_cast<const SwTableBoxFormula
&>(rAttr
).GetFormula() &&
67 m_pDefinedIn
== static_cast<const SwTableBoxFormula
&>(rAttr
).m_pDefinedIn
;
70 SwTableBoxFormula
* SwTableBoxFormula::Clone( SfxItemPool
* ) const
72 // switch to external rendering
73 SwTableBoxFormula
* pNew
= new SwTableBoxFormula( GetFormula() );
74 pNew
->SwTableFormula::operator=( *this );
78 /** Get node type of the node containing this formula
80 E.g. TextField -> TextNode, or
81 BoxAttribute -> BoxStartNode
83 Caution: Must override when inheriting.
85 const SwNode
* SwTableBoxFormula::GetNodeOfFormula() const
87 auto pTableBox
= GetTableBox();
88 return pTableBox
? pTableBox
->GetSttNd() : nullptr;
91 SwTableBox
* SwTableBoxFormula::GetTableBox()
93 assert(!m_pDefinedIn
|| dynamic_cast<SwTableBoxFormat
*>(m_pDefinedIn
));
94 return m_pDefinedIn
? static_cast<SwTableBoxFormat
*>(m_pDefinedIn
)->GetTableBox() : nullptr;
97 void SwTableBoxFormula::TryBoxNmToPtr()
99 const SwNode
* pNd
= GetNodeOfFormula();
100 if (!pNd
|| &pNd
->GetNodes() != &pNd
->GetDoc().GetNodes())
102 if(const SwTableNode
* pTableNd
= pNd
->FindTableNode())
104 BoxNmToPtr(&pTableNd
->GetTable());
107 void SwTableBoxFormula::ToSplitMergeBoxNmWithHistory(SwTableFormulaUpdate
& rUpdate
, SwHistory
* pHistory
)
111 ToSplitMergeBoxNm(rUpdate
);
114 auto pNd
= GetNodeOfFormula();
115 // for a history record the unchanged formula is needed
116 SwTableBoxFormula
aCopy(*this);
117 rUpdate
.m_bModified
= false;
118 ToSplitMergeBoxNm(rUpdate
);
119 if(rUpdate
.m_bModified
)
121 // external rendering
122 aCopy
.PtrToBoxNm(&pNd
->FindTableNode()->GetTable());
126 pNd
->FindTableBoxStartNode()->GetIndex());
130 void SwTableBoxFormula::Calc( SwTableCalcPara
& rCalcPara
, double& rValue
)
132 if( !rCalcPara
.m_rCalc
.IsCalcError() )
134 // create pointers from box names
135 BoxNmToPtr( rCalcPara
.m_pTable
);
136 const OUString
sFormula( MakeFormula( rCalcPara
));
137 if( !rCalcPara
.m_rCalc
.IsCalcError() )
138 rValue
= rCalcPara
.m_rCalc
.Calculate( sFormula
).GetDouble();
141 ChgValid( !rCalcPara
.IsStackOverflow() ); // value is now valid again
145 SwTableBoxValue::SwTableBoxValue()
146 : SfxPoolItem( RES_BOXATR_VALUE
), m_nValue( 0 )
150 SwTableBoxValue::SwTableBoxValue( const double nVal
)
151 : SfxPoolItem( RES_BOXATR_VALUE
), m_nValue( nVal
)
155 bool SwTableBoxValue::operator==( const SfxPoolItem
& rAttr
) const
157 assert(SfxPoolItem::operator==(rAttr
));
158 SwTableBoxValue
const& rOther( static_cast<SwTableBoxValue
const&>(rAttr
) );
159 // items with NaN should be equal to enable pooling
160 return std::isnan( m_nValue
)
161 ? std::isnan( rOther
.m_nValue
)
162 : ( m_nValue
== rOther
.m_nValue
);
165 SwTableBoxValue
* SwTableBoxValue::Clone( SfxItemPool
* ) const
167 return new SwTableBoxValue( m_nValue
);
170 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */