xref: /aoo41x/main/sw/source/core/fields/tblcalc.cxx (revision c0286415)
1efeef26fSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3efeef26fSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4efeef26fSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5efeef26fSAndrew Rist  * distributed with this work for additional information
6efeef26fSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7efeef26fSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8efeef26fSAndrew Rist  * "License"); you may not use this file except in compliance
9efeef26fSAndrew Rist  * with the License.  You may obtain a copy of the License at
10efeef26fSAndrew Rist  *
11efeef26fSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12efeef26fSAndrew Rist  *
13efeef26fSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14efeef26fSAndrew Rist  * software distributed under the License is distributed on an
15efeef26fSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16efeef26fSAndrew Rist  * KIND, either express or implied.  See the License for the
17efeef26fSAndrew Rist  * specific language governing permissions and limitations
18efeef26fSAndrew Rist  * under the License.
19efeef26fSAndrew Rist  *
20efeef26fSAndrew Rist  *************************************************************/
21efeef26fSAndrew Rist 
22efeef26fSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sw.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <switerator.hxx>
28cdf0e10cSrcweir #include <cntfrm.hxx>
29cdf0e10cSrcweir #include <doc.hxx>
30cdf0e10cSrcweir #include <pam.hxx> 		// fuer GetBodyTxtNode
31cdf0e10cSrcweir #include <ndtxt.hxx>
32cdf0e10cSrcweir #include <fmtfld.hxx>
33cdf0e10cSrcweir #include <txtfld.hxx>
34cdf0e10cSrcweir #include <expfld.hxx>
35cdf0e10cSrcweir #include <docfld.hxx> 	// fuer _SetGetExpFld
36cdf0e10cSrcweir #include <unofldmid.h>
37cdf0e10cSrcweir 
38cdf0e10cSrcweir using namespace ::com::sun::star;
39cdf0e10cSrcweir using ::rtl::OUString;
40cdf0e10cSrcweir 
41cdf0e10cSrcweir 
SwTblFieldType(SwDoc * pDocPtr)42cdf0e10cSrcweir SwTblFieldType::SwTblFieldType(SwDoc* pDocPtr)
43cdf0e10cSrcweir 	: SwValueFieldType( pDocPtr, RES_TABLEFLD )
44cdf0e10cSrcweir {}
45cdf0e10cSrcweir 
46cdf0e10cSrcweir 
Copy() const47cdf0e10cSrcweir SwFieldType* SwTblFieldType::Copy() const
48cdf0e10cSrcweir {
49cdf0e10cSrcweir 	return new SwTblFieldType(GetDoc());
50cdf0e10cSrcweir }
51cdf0e10cSrcweir 
52cdf0e10cSrcweir 
53cdf0e10cSrcweir 
CalcField(SwTblCalcPara & rCalcPara)54cdf0e10cSrcweir void SwTblField::CalcField( SwTblCalcPara& rCalcPara )
55cdf0e10cSrcweir {
56cdf0e10cSrcweir 	if( rCalcPara.rCalc.IsCalcError() )		// ist schon Fehler gesetzt ?
57cdf0e10cSrcweir 		return;
58cdf0e10cSrcweir 
59cdf0e10cSrcweir 	// erzeuge aus den BoxNamen die Pointer
60cdf0e10cSrcweir 	BoxNmToPtr( rCalcPara.pTbl );
61cdf0e10cSrcweir 	String sFml( MakeFormel( rCalcPara ));
62cdf0e10cSrcweir 	SetValue( rCalcPara.rCalc.Calculate( sFml ).GetDouble() );
63cdf0e10cSrcweir 	ChgValid( !rCalcPara.IsStackOverFlow() );		// ist der Wert wieder gueltig?
64cdf0e10cSrcweir }
65cdf0e10cSrcweir 
66cdf0e10cSrcweir 
67cdf0e10cSrcweir 
SwTblField(SwTblFieldType * pInitType,const String & rFormel,sal_uInt16 nType,sal_uLong nFmt)68cdf0e10cSrcweir SwTblField::SwTblField( SwTblFieldType* pInitType, const String& rFormel,
69cdf0e10cSrcweir 						sal_uInt16 nType, sal_uLong nFmt )
70cdf0e10cSrcweir 	: SwValueField( pInitType, nFmt ), SwTableFormula( rFormel ),
71cdf0e10cSrcweir 	sExpand( '0' ), nSubType(nType)
72cdf0e10cSrcweir {
73cdf0e10cSrcweir }
74cdf0e10cSrcweir 
75cdf0e10cSrcweir 
Copy() const76cdf0e10cSrcweir SwField* SwTblField::Copy() const
77cdf0e10cSrcweir {
78cdf0e10cSrcweir 	SwTblField* pTmp = new SwTblField( (SwTblFieldType*)GetTyp(),
79cdf0e10cSrcweir 										SwTableFormula::GetFormula(), nSubType, GetFormat() );
80cdf0e10cSrcweir 	pTmp->sExpand 	  = sExpand;
81cdf0e10cSrcweir 	pTmp->SwValueField::SetValue(GetValue());
82cdf0e10cSrcweir 	pTmp->SwTableFormula::operator=( *this );
83cdf0e10cSrcweir     pTmp->SetAutomaticLanguage(IsAutomaticLanguage());
84cdf0e10cSrcweir     return pTmp;
85cdf0e10cSrcweir }
86cdf0e10cSrcweir 
87cdf0e10cSrcweir 
GetFieldName() const88cdf0e10cSrcweir String SwTblField::GetFieldName() const
89cdf0e10cSrcweir {
90cdf0e10cSrcweir     String aStr(GetTyp()->GetName());
91cdf0e10cSrcweir     aStr += ' ';
92cdf0e10cSrcweir     aStr += const_cast<SwTblField *>(this)->GetCommand();
93cdf0e10cSrcweir     return aStr;
94cdf0e10cSrcweir }
95cdf0e10cSrcweir 
96cdf0e10cSrcweir // suche den TextNode, in dem das Feld steht
GetNodeOfFormula() const97cdf0e10cSrcweir const SwNode* SwTblField::GetNodeOfFormula() const
98cdf0e10cSrcweir {
99cdf0e10cSrcweir 	if( !GetTyp()->GetDepends() )
100cdf0e10cSrcweir 		return 0;
101cdf0e10cSrcweir 
102cdf0e10cSrcweir 	SwIterator<SwFmtFld,SwFieldType> aIter( *GetTyp() );
103cdf0e10cSrcweir 	for( SwFmtFld* pFmtFld = aIter.First(); pFmtFld; pFmtFld = aIter.Next() )
104*c0286415SOliver-Rainer Wittmann 			if( this == pFmtFld->GetField() )
105cdf0e10cSrcweir 				return (SwTxtNode*)&pFmtFld->GetTxtFld()->GetTxtNode();
106cdf0e10cSrcweir 	return 0;
107cdf0e10cSrcweir }
108cdf0e10cSrcweir 
GetCommand()109cdf0e10cSrcweir String SwTblField::GetCommand()
110cdf0e10cSrcweir {
111cdf0e10cSrcweir     if (EXTRNL_NAME != GetNameType())
112cdf0e10cSrcweir     {
113cdf0e10cSrcweir         SwNode const*const pNd = GetNodeOfFormula();
114cdf0e10cSrcweir         SwTableNode const*const pTblNd = (pNd) ? pNd->FindTableNode() : 0;
115cdf0e10cSrcweir         if (pTblNd)
116cdf0e10cSrcweir         {
117cdf0e10cSrcweir             PtrToBoxNm( &pTblNd->GetTable() );
118cdf0e10cSrcweir         }
119cdf0e10cSrcweir     }
120cdf0e10cSrcweir     return (EXTRNL_NAME == GetNameType())
121cdf0e10cSrcweir         ? SwTableFormula::GetFormula()
122cdf0e10cSrcweir         : String();
123cdf0e10cSrcweir }
124cdf0e10cSrcweir 
Expand() const125cdf0e10cSrcweir String SwTblField::Expand() const
126cdf0e10cSrcweir {
127cdf0e10cSrcweir 	String aStr;
128cdf0e10cSrcweir 	if (nSubType & nsSwExtendedSubType::SUB_CMD)
129cdf0e10cSrcweir 	{
130cdf0e10cSrcweir         aStr = const_cast<SwTblField *>(this)->GetCommand();
131cdf0e10cSrcweir 	}
132cdf0e10cSrcweir 	else
133cdf0e10cSrcweir 	{
134cdf0e10cSrcweir 		aStr = sExpand;
135cdf0e10cSrcweir 		if(nSubType & nsSwGetSetExpType::GSE_STRING)
136cdf0e10cSrcweir 		{
137cdf0e10cSrcweir 			// es ist ein String
138cdf0e10cSrcweir 			aStr = sExpand;
139cdf0e10cSrcweir 			aStr.Erase( 0,1 );
140cdf0e10cSrcweir 			aStr.Erase( aStr.Len()-1, 1 );
141cdf0e10cSrcweir 		}
142cdf0e10cSrcweir 	}
143cdf0e10cSrcweir 	return aStr;
144cdf0e10cSrcweir }
145cdf0e10cSrcweir 
GetSubType() const146cdf0e10cSrcweir sal_uInt16 SwTblField::GetSubType() const
147cdf0e10cSrcweir {
148cdf0e10cSrcweir 	return nSubType;
149cdf0e10cSrcweir }
150cdf0e10cSrcweir 
SetSubType(sal_uInt16 nType)151cdf0e10cSrcweir void SwTblField::SetSubType(sal_uInt16 nType)
152cdf0e10cSrcweir {
153cdf0e10cSrcweir 	nSubType = nType;
154cdf0e10cSrcweir }
155cdf0e10cSrcweir 
156cdf0e10cSrcweir 
SetValue(const double & rVal)157cdf0e10cSrcweir void SwTblField::SetValue( const double& rVal )
158cdf0e10cSrcweir {
159cdf0e10cSrcweir 	SwValueField::SetValue(rVal);
160cdf0e10cSrcweir 	sExpand = ((SwValueFieldType*)GetTyp())->ExpandValue(rVal, GetFormat(), GetLanguage());
161cdf0e10cSrcweir }
162cdf0e10cSrcweir 
163cdf0e10cSrcweir /*--------------------------------------------------------------------
164cdf0e10cSrcweir 	Beschreibung: Parameter setzen
165cdf0e10cSrcweir  --------------------------------------------------------------------*/
166cdf0e10cSrcweir 
167cdf0e10cSrcweir 
GetPar2() const168cdf0e10cSrcweir String SwTblField::GetPar2() const
169cdf0e10cSrcweir {
170cdf0e10cSrcweir 	return SwTableFormula::GetFormula();
171cdf0e10cSrcweir }
172cdf0e10cSrcweir 
173cdf0e10cSrcweir 
SetPar2(const String & rStr)174cdf0e10cSrcweir void SwTblField::SetPar2(const String& rStr)
175cdf0e10cSrcweir {
176cdf0e10cSrcweir 	SetFormula( rStr );
177cdf0e10cSrcweir }
178cdf0e10cSrcweir 
179cdf0e10cSrcweir 
QueryValue(uno::Any & rAny,sal_uInt16 nWhichId) const180cdf0e10cSrcweir sal_Bool SwTblField::QueryValue( uno::Any& rAny, sal_uInt16 nWhichId ) const
181cdf0e10cSrcweir {
182cdf0e10cSrcweir 	sal_Bool bRet = sal_True;
183cdf0e10cSrcweir     switch ( nWhichId )
184cdf0e10cSrcweir 	{
185cdf0e10cSrcweir 	case FIELD_PROP_PAR2:
186cdf0e10cSrcweir 		{
187cdf0e10cSrcweir 			sal_uInt16 nOldSubType = nSubType;
188cdf0e10cSrcweir 			SwTblField* pThis = (SwTblField*)this;
189cdf0e10cSrcweir 			pThis->nSubType |= nsSwExtendedSubType::SUB_CMD;
190cdf0e10cSrcweir 			rAny <<= rtl::OUString( Expand() );
191cdf0e10cSrcweir 			pThis->nSubType = nOldSubType;
192cdf0e10cSrcweir 		}
193cdf0e10cSrcweir 		break;
194cdf0e10cSrcweir 	case FIELD_PROP_BOOL1:
195cdf0e10cSrcweir 		{
196cdf0e10cSrcweir         	sal_Bool bFormula = 0 != (nsSwExtendedSubType::SUB_CMD & nSubType);
197cdf0e10cSrcweir         	rAny.setValue(&bFormula, ::getBooleanCppuType());
198cdf0e10cSrcweir 		}
199cdf0e10cSrcweir 		break;
200cdf0e10cSrcweir 	case FIELD_PROP_PAR1:
201cdf0e10cSrcweir 		rAny <<= rtl::OUString(GetExpStr());
202cdf0e10cSrcweir 		break;
203cdf0e10cSrcweir 	case FIELD_PROP_FORMAT:
204cdf0e10cSrcweir         rAny <<= (sal_Int32)GetFormat();
205cdf0e10cSrcweir 		break;
206cdf0e10cSrcweir 	default:
207cdf0e10cSrcweir         bRet = sal_False;
208cdf0e10cSrcweir 	}
209cdf0e10cSrcweir     return bRet;
210cdf0e10cSrcweir }
211cdf0e10cSrcweir 
PutValue(const uno::Any & rAny,sal_uInt16 nWhichId)212cdf0e10cSrcweir sal_Bool SwTblField::PutValue( const uno::Any& rAny, sal_uInt16 nWhichId )
213cdf0e10cSrcweir {
214cdf0e10cSrcweir 	sal_Bool bRet = sal_True;
215cdf0e10cSrcweir 	String sTmp;
216cdf0e10cSrcweir     switch ( nWhichId )
217cdf0e10cSrcweir 	{
218cdf0e10cSrcweir 	case FIELD_PROP_PAR2:
219cdf0e10cSrcweir 		SetFormula( ::GetString( rAny, sTmp ));
220cdf0e10cSrcweir 		break;
221cdf0e10cSrcweir 	case FIELD_PROP_BOOL1:
222cdf0e10cSrcweir         if(*(sal_Bool*)rAny.getValue())
223cdf0e10cSrcweir             nSubType = nsSwGetSetExpType::GSE_FORMULA|nsSwExtendedSubType::SUB_CMD;
224cdf0e10cSrcweir         else
225cdf0e10cSrcweir             nSubType = nsSwGetSetExpType::GSE_FORMULA;
226cdf0e10cSrcweir 		break;
227cdf0e10cSrcweir 	case FIELD_PROP_PAR1:
228cdf0e10cSrcweir         ChgExpStr( ::GetString( rAny, sTmp ));
229cdf0e10cSrcweir 		break;
230cdf0e10cSrcweir     case FIELD_PROP_FORMAT:
231cdf0e10cSrcweir         {
232cdf0e10cSrcweir             sal_Int32 nTmp = 0;
233cdf0e10cSrcweir             rAny >>= nTmp;
234cdf0e10cSrcweir             SetFormat(nTmp);
235cdf0e10cSrcweir         }
236cdf0e10cSrcweir         break;
237cdf0e10cSrcweir 	default:
238cdf0e10cSrcweir         bRet = sal_False;
239cdf0e10cSrcweir 	}
240cdf0e10cSrcweir     return bRet;
241cdf0e10cSrcweir }
242cdf0e10cSrcweir 
243cdf0e10cSrcweir 
244cdf0e10cSrcweir 
245cdf0e10cSrcweir 
246