nss: upgrade to release 3.73
[LibreOffice.git] / sw / source / core / fields / tblcalc.cxx
blobc93d04a5c4bd1d110a3cc4fc04266b09bf91709e
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 <sal/config.h>
22 #include <o3tl/any.hxx>
24 #include <calc.hxx>
25 #include <calbck.hxx>
26 #include <doc.hxx>
27 #include <ndtxt.hxx>
28 #include <fmtfld.hxx>
29 #include <txtfld.hxx>
30 #include <expfld.hxx>
31 #include <unofldmid.h>
33 using namespace ::com::sun::star;
35 SwTableFieldType::SwTableFieldType(SwDoc* pDocPtr)
36 : SwValueFieldType( pDocPtr, SwFieldIds::Table )
39 std::unique_ptr<SwFieldType> SwTableFieldType::Copy() const
41 return std::make_unique<SwTableFieldType>(GetDoc());
44 void SwTableField::CalcField( SwTableCalcPara& rCalcPara )
46 if( rCalcPara.m_rCalc.IsCalcError() ) // stop if there is already an error set
47 return;
49 // create pointers from box name
50 BoxNmToPtr( rCalcPara.m_pTable );
51 OUString sFormula( MakeFormula( rCalcPara ));
52 SetValue( rCalcPara.m_rCalc.Calculate( sFormula ).GetDouble() );
53 ChgValid( !rCalcPara.IsStackOverflow() ); // is the value again valid?
56 SwTableField::SwTableField( SwTableFieldType* pInitType, const OUString& rFormel,
57 sal_uInt16 nType, sal_uLong nFormat )
58 : SwValueField( pInitType, nFormat ), SwTableFormula( rFormel ),
59 m_nSubType(nType)
61 m_sExpand = "0";
64 std::unique_ptr<SwField> SwTableField::Copy() const
66 std::unique_ptr<SwTableField> pTmp(new SwTableField( static_cast<SwTableFieldType*>(GetTyp()),
67 SwTableFormula::GetFormula(), m_nSubType, GetFormat() ));
68 pTmp->m_sExpand = m_sExpand;
69 pTmp->SwValueField::SetValue(GetValue());
70 pTmp->SwTableFormula::operator=( *this );
71 pTmp->SetAutomaticLanguage(IsAutomaticLanguage());
72 return std::unique_ptr<SwField>(pTmp.release());
75 OUString SwTableField::GetFieldName() const
77 return GetTyp()->GetName() + " " + const_cast<SwTableField *>(this)->GetCommand();
80 /// search TextNode containing this field
81 const SwNode* SwTableField::GetNodeOfFormula() const
83 auto pFormat = GetTyp()->FindFormatForField(this);
84 return pFormat ? &pFormat->GetTextField()->GetTextNode() : nullptr;
87 OUString SwTableField::GetCommand()
89 if (EXTRNL_NAME != GetNameType())
91 SwNode const*const pNd = GetNodeOfFormula();
92 SwTableNode const*const pTableNd = pNd ? pNd->FindTableNode() : nullptr;
93 if (pTableNd)
95 PtrToBoxNm( &pTableNd->GetTable() );
98 return (EXTRNL_NAME == GetNameType())
99 ? SwTableFormula::GetFormula()
100 : OUString();
103 OUString SwTableField::ExpandImpl(SwRootFrame const*const) const
105 if (m_nSubType & nsSwExtendedSubType::SUB_CMD)
107 return const_cast<SwTableField *>(this)->GetCommand();
110 if(m_nSubType & nsSwGetSetExpType::GSE_STRING)
112 // it is a string
113 return m_sExpand.copy(1, m_sExpand.getLength()-2);
116 return m_sExpand;
119 sal_uInt16 SwTableField::GetSubType() const
121 return m_nSubType;
124 void SwTableField::SetSubType(sal_uInt16 nType)
126 m_nSubType = nType;
129 void SwTableField::SetValue( const double& rVal )
131 SwValueField::SetValue(rVal);
132 m_sExpand = static_cast<SwValueFieldType*>(GetTyp())->ExpandValue(rVal, GetFormat(), GetLanguage());
135 OUString SwTableField::GetPar2() const
137 return SwTableFormula::GetFormula();
140 void SwTableField::SetPar2(const OUString& rStr)
142 SetFormula( rStr );
145 bool SwTableField::QueryValue( uno::Any& rAny, sal_uInt16 nWhichId ) const
147 bool bRet = true;
148 switch ( nWhichId )
150 case FIELD_PROP_PAR2:
152 sal_uInt16 nOldSubType = m_nSubType;
153 SwTableField* pThis = const_cast<SwTableField*>(this);
154 pThis->m_nSubType |= nsSwExtendedSubType::SUB_CMD;
155 rAny <<= ExpandImpl(nullptr);
156 pThis->m_nSubType = nOldSubType;
158 break;
159 case FIELD_PROP_BOOL1:
160 rAny <<= 0 != (nsSwExtendedSubType::SUB_CMD & m_nSubType);
161 break;
162 case FIELD_PROP_PAR1:
163 rAny <<= m_sExpand;
164 break;
165 case FIELD_PROP_FORMAT:
166 rAny <<= static_cast<sal_Int32>(GetFormat());
167 break;
168 default:
169 bRet = false;
171 return bRet;
174 bool SwTableField::PutValue( const uno::Any& rAny, sal_uInt16 nWhichId )
176 bool bRet = true;
177 switch ( nWhichId )
179 case FIELD_PROP_PAR2:
181 OUString sTmp;
182 rAny >>= sTmp;
183 SetFormula( sTmp );
185 break;
186 case FIELD_PROP_BOOL1:
187 if(*o3tl::doAccess<bool>(rAny))
188 m_nSubType = nsSwGetSetExpType::GSE_FORMULA|nsSwExtendedSubType::SUB_CMD;
189 else
190 m_nSubType = nsSwGetSetExpType::GSE_FORMULA;
191 break;
192 case FIELD_PROP_PAR1:
194 OUString sTmp;
195 rAny >>= sTmp;
196 ChgExpStr( sTmp );
198 break;
199 case FIELD_PROP_FORMAT:
201 sal_Int32 nTmp = 0;
202 rAny >>= nTmp;
203 SetFormat(nTmp);
205 break;
206 default:
207 bRet = false;
209 return bRet;
212 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */