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