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