merge the formfield patch from ooo-build
[ooovba.git] / sw / source / core / fields / tblcalc.cxx
blob2352fb9e1b7db40796ead6069091ae785ada7584
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: tblcalc.cxx,v $
10 * $Revision: 1.17 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sw.hxx"
35 #include <cntfrm.hxx>
36 #include <doc.hxx>
37 #include <pam.hxx> // fuer GetBodyTxtNode
38 #include <ndtxt.hxx>
39 #include <fmtfld.hxx>
40 #include <txtfld.hxx>
41 #include <expfld.hxx>
42 #include <docfld.hxx> // fuer _SetGetExpFld
43 #ifndef _UNOFLDMID_H
44 #include <unofldmid.h>
45 #endif
47 using namespace ::com::sun::star;
48 using ::rtl::OUString;
51 SwTblFieldType::SwTblFieldType(SwDoc* pDocPtr)
52 : SwValueFieldType( pDocPtr, RES_TABLEFLD )
56 SwFieldType* SwTblFieldType::Copy() const
58 return new SwTblFieldType(GetDoc());
63 void SwTblField::CalcField( SwTblCalcPara& rCalcPara )
65 if( rCalcPara.rCalc.IsCalcError() ) // ist schon Fehler gesetzt ?
66 return;
68 // erzeuge aus den BoxNamen die Pointer
69 BoxNmToPtr( rCalcPara.pTbl );
70 String sFml( MakeFormel( rCalcPara ));
71 SetValue( rCalcPara.rCalc.Calculate( sFml ).GetDouble() );
72 ChgValid( !rCalcPara.IsStackOverFlow() ); // ist der Wert wieder gueltig?
77 SwTblField::SwTblField( SwTblFieldType* pInitType, const String& rFormel,
78 USHORT nType, ULONG nFmt )
79 : SwValueField( pInitType, nFmt ), SwTableFormula( rFormel ),
80 sExpand( '0' ), nSubType(nType)
85 SwField* SwTblField::Copy() const
87 SwTblField* pTmp = new SwTblField( (SwTblFieldType*)GetTyp(),
88 SwTableFormula::GetFormula(), nSubType, GetFormat() );
89 pTmp->sExpand = sExpand;
90 pTmp->SwValueField::SetValue(GetValue());
91 pTmp->SwTableFormula::operator=( *this );
92 pTmp->SetAutomaticLanguage(IsAutomaticLanguage());
93 return pTmp;
97 String SwTblField::GetCntnt(BOOL bName) const
99 if( bName )
101 String aStr(GetTyp()->GetName());
102 aStr += ' ';
104 USHORT nOldSubType = nSubType;
105 SwTblField* pThis = (SwTblField*)this;
106 pThis->nSubType |= nsSwExtendedSubType::SUB_CMD;
107 aStr += Expand();
108 pThis->nSubType = nOldSubType;
110 return aStr;
112 return Expand();
115 // suche den TextNode, in dem das Feld steht
116 const SwNode* SwTblField::GetNodeOfFormula() const
118 if( !GetTyp()->GetDepends() )
119 return 0;
121 SwClientIter aIter( *GetTyp() );
122 SwClient * pLast = aIter.GoStart();
123 if( pLast ) // konnte zum Anfang gesprungen werden ??
124 do {
125 const SwFmtFld* pFmtFld = (SwFmtFld*)pLast;
126 if( this == pFmtFld->GetFld() )
127 return (SwTxtNode*)&pFmtFld->GetTxtFld()->GetTxtNode();
129 } while( 0 != ( pLast = aIter++ ));
130 return 0;
134 String SwTblField::Expand() const
136 String aStr;
137 if (nSubType & nsSwExtendedSubType::SUB_CMD)
139 if( EXTRNL_NAME != GetNameType() )
141 const SwNode* pNd = GetNodeOfFormula();
142 const SwTableNode* pTblNd = pNd ? pNd->FindTableNode() : 0;
143 if( pTblNd )
144 ((SwTblField*)this)->PtrToBoxNm( &pTblNd->GetTable() );
146 if( EXTRNL_NAME == GetNameType() )
147 aStr = SwTableFormula::GetFormula();
149 else
151 aStr = sExpand;
152 if(nSubType & nsSwGetSetExpType::GSE_STRING)
154 // es ist ein String
155 aStr = sExpand;
156 aStr.Erase( 0,1 );
157 aStr.Erase( aStr.Len()-1, 1 );
160 return aStr;
163 USHORT SwTblField::GetSubType() const
165 return nSubType;
168 void SwTblField::SetSubType(USHORT nType)
170 nSubType = nType;
174 void SwTblField::SetValue( const double& rVal )
176 SwValueField::SetValue(rVal);
177 sExpand = ((SwValueFieldType*)GetTyp())->ExpandValue(rVal, GetFormat(), GetLanguage());
180 /*--------------------------------------------------------------------
181 Beschreibung: Parameter setzen
182 --------------------------------------------------------------------*/
185 String SwTblField::GetPar2() const
187 return SwTableFormula::GetFormula();
191 void SwTblField::SetPar2(const String& rStr)
193 SetFormula( rStr );
197 /*-----------------04.03.98 10:33-------------------
199 --------------------------------------------------*/
200 BOOL SwTblField::QueryValue( uno::Any& rAny, USHORT nWhichId ) const
202 BOOL bRet = TRUE;
203 switch ( nWhichId )
205 case FIELD_PROP_PAR2:
207 USHORT nOldSubType = nSubType;
208 SwTblField* pThis = (SwTblField*)this;
209 pThis->nSubType |= nsSwExtendedSubType::SUB_CMD;
210 rAny <<= rtl::OUString( Expand() );
211 pThis->nSubType = nOldSubType;
213 break;
214 case FIELD_PROP_BOOL1:
216 BOOL bFormula = 0 != (nsSwExtendedSubType::SUB_CMD & nSubType);
217 rAny.setValue(&bFormula, ::getBooleanCppuType());
219 break;
220 case FIELD_PROP_PAR1:
221 rAny <<= rtl::OUString(GetExpStr());
222 break;
223 case FIELD_PROP_FORMAT:
224 rAny <<= (sal_Int32)GetFormat();
225 break;
226 default:
227 bRet = sal_False;
229 return bRet;
231 /*-----------------04.03.98 10:33-------------------
233 --------------------------------------------------*/
234 BOOL SwTblField::PutValue( const uno::Any& rAny, USHORT nWhichId )
236 BOOL bRet = TRUE;
237 String sTmp;
238 switch ( nWhichId )
240 case FIELD_PROP_PAR2:
241 SetFormula( ::GetString( rAny, sTmp ));
242 break;
243 case FIELD_PROP_BOOL1:
244 if(*(sal_Bool*)rAny.getValue())
245 nSubType = nsSwGetSetExpType::GSE_FORMULA|nsSwExtendedSubType::SUB_CMD;
246 else
247 nSubType = nsSwGetSetExpType::GSE_FORMULA;
248 break;
249 case FIELD_PROP_PAR1:
250 ChgExpStr( ::GetString( rAny, sTmp ));
251 break;
252 case FIELD_PROP_FORMAT:
254 sal_Int32 nTmp = 0;
255 rAny >>= nTmp;
256 SetFormat(nTmp);
258 break;
259 default:
260 bRet = sal_False;
262 return bRet;