android: Update app-specific/MIME type icons
[LibreOffice.git] / sw / source / core / fields / tblcalc.cxx
blobd9c78be88ede713e0c46cd4c5f1e7a701fda8981
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 <doc.hxx>
26 #include <ndtxt.hxx>
27 #include <fmtfld.hxx>
28 #include <txtfld.hxx>
29 #include <expfld.hxx>
30 #include <unofldmid.h>
32 using namespace ::com::sun::star;
34 SwTableFieldType::SwTableFieldType(SwDoc* pDocPtr)
35 : SwValueFieldType( pDocPtr, SwFieldIds::Table )
38 std::unique_ptr<SwFieldType> SwTableFieldType::Copy() const
40 return std::make_unique<SwTableFieldType>(GetDoc());
43 void SwTableField::CalcField( SwTableCalcPara& rCalcPara )
45 if( rCalcPara.m_rCalc.IsCalcError() ) // stop if there is already an error set
46 return;
48 // create pointers from box name
49 BoxNmToPtr( rCalcPara.m_pTable );
50 OUString sFormula( MakeFormula( rCalcPara ));
51 SetValue( rCalcPara.m_rCalc.Calculate( sFormula ).GetDouble() );
52 ChgValid( !rCalcPara.IsStackOverflow() ); // is the value again valid?
55 SwTableField::SwTableField( SwTableFieldType* pInitType, const OUString& rFormel,
56 sal_uInt16 nType, sal_uLong nFormat )
57 : SwValueField( pInitType, nFormat ), SwTableFormula( rFormel ),
58 m_nSubType(nType)
60 m_sExpand = "0";
63 std::unique_ptr<SwField> SwTableField::Copy() const
65 std::unique_ptr<SwTableField> pTmp(new SwTableField( static_cast<SwTableFieldType*>(GetTyp()),
66 SwTableFormula::GetFormula(), m_nSubType, GetFormat() ));
67 pTmp->m_sExpand = m_sExpand;
68 pTmp->SwValueField::SetValue(GetValue());
69 pTmp->SwTableFormula::operator=( *this );
70 pTmp->SetAutomaticLanguage(IsAutomaticLanguage());
71 return std::unique_ptr<SwField>(pTmp.release());
74 OUString SwTableField::GetFieldName() const
76 return GetTyp()->GetName() + " " + const_cast<SwTableField *>(this)->GetCommand();
79 /// search TextNode containing this field
80 const SwNode* SwTableField::GetNodeOfFormula() const
82 auto pFormat = GetTyp()->FindFormatForField(this);
83 return pFormat ? &pFormat->GetTextField()->GetTextNode() : nullptr;
86 OUString SwTableField::GetCommand()
88 if (EXTRNL_NAME != GetNameType())
90 SwNode const*const pNd = GetNodeOfFormula();
91 SwTableNode const*const pTableNd = pNd ? pNd->FindTableNode() : nullptr;
92 if (pTableNd)
94 PtrToBoxNm( &pTableNd->GetTable() );
97 return (EXTRNL_NAME == GetNameType())
98 ? SwTableFormula::GetFormula()
99 : OUString();
102 OUString SwTableField::ExpandImpl(SwRootFrame const*const) const
104 if (m_nSubType & nsSwExtendedSubType::SUB_CMD)
106 return const_cast<SwTableField *>(this)->GetCommand();
109 if(m_nSubType & nsSwGetSetExpType::GSE_STRING)
111 // it is a string
112 return m_sExpand.copy(1, m_sExpand.getLength()-2);
115 return m_sExpand;
118 sal_uInt16 SwTableField::GetSubType() const
120 return m_nSubType;
123 void SwTableField::SetSubType(sal_uInt16 nType)
125 m_nSubType = nType;
128 void SwTableField::SetValue( const double& rVal )
130 SwValueField::SetValue(rVal);
131 m_sExpand = static_cast<SwValueFieldType*>(GetTyp())->ExpandValue(rVal, GetFormat(), GetLanguage());
134 OUString SwTableField::GetPar2() const
136 return SwTableFormula::GetFormula();
139 void SwTableField::SetPar2(const OUString& rStr)
141 SetFormula( rStr );
144 bool SwTableField::QueryValue( uno::Any& rAny, sal_uInt16 nWhichId ) const
146 bool bRet = true;
147 switch ( nWhichId )
149 case FIELD_PROP_PAR2:
151 sal_uInt16 nOldSubType = m_nSubType;
152 SwTableField* pThis = const_cast<SwTableField*>(this);
153 pThis->m_nSubType |= nsSwExtendedSubType::SUB_CMD;
154 rAny <<= ExpandImpl(nullptr);
155 pThis->m_nSubType = nOldSubType;
157 break;
158 case FIELD_PROP_BOOL1:
159 rAny <<= 0 != (nsSwExtendedSubType::SUB_CMD & m_nSubType);
160 break;
161 case FIELD_PROP_PAR1:
162 rAny <<= m_sExpand;
163 break;
164 case FIELD_PROP_FORMAT:
165 rAny <<= static_cast<sal_Int32>(GetFormat());
166 break;
167 default:
168 bRet = false;
170 return bRet;
173 bool SwTableField::PutValue( const uno::Any& rAny, sal_uInt16 nWhichId )
175 bool bRet = true;
176 switch ( nWhichId )
178 case FIELD_PROP_PAR2:
180 OUString sTmp;
181 rAny >>= sTmp;
182 SetFormula( sTmp );
184 break;
185 case FIELD_PROP_BOOL1:
186 if(*o3tl::doAccess<bool>(rAny))
187 m_nSubType = nsSwGetSetExpType::GSE_FORMULA|nsSwExtendedSubType::SUB_CMD;
188 else
189 m_nSubType = nsSwGetSetExpType::GSE_FORMULA;
190 break;
191 case FIELD_PROP_PAR1:
193 OUString sTmp;
194 rAny >>= sTmp;
195 ChgExpStr( sTmp );
197 break;
198 case FIELD_PROP_FORMAT:
200 sal_Int32 nTmp = 0;
201 rAny >>= nTmp;
202 SetFormat(nTmp);
204 break;
205 default:
206 bRet = false;
208 return bRet;
211 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */