android: Update app-specific/MIME type icons
[LibreOffice.git] / sw / source / core / attr / cellatr.cxx
blob9023cca2f793cc6a8bc4851edac2663700af8f3b
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/ustring.hxx>
29 #include <calbck.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 );
75 return pNew;
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())
101 return;
102 if(const SwTableNode* pTableNd = pNd->FindTableNode())
104 BoxNmToPtr(&pTableNd->GetTable());
107 void SwTableBoxFormula::ToSplitMergeBoxNmWithHistory(SwTableFormulaUpdate& rUpdate, SwHistory* pHistory)
109 if(!pHistory)
111 ToSplitMergeBoxNm(rUpdate);
112 return;
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());
123 pHistory->Add(
124 &aCopy,
125 &aCopy,
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();
139 else
140 rValue = DBL_MAX;
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: */