Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <switerator.hxx>
21 : #include <cntfrm.hxx>
22 : #include <doc.hxx>
23 : #include <pam.hxx> // for GetBodyTxtNode
24 : #include <ndtxt.hxx>
25 : #include <fmtfld.hxx>
26 : #include <txtfld.hxx>
27 : #include <expfld.hxx>
28 : #include <docfld.hxx> // for _SetGetExpFld
29 : #include <unofldmid.h>
30 :
31 : using namespace ::com::sun::star;
32 :
33 898 : SwTblFieldType::SwTblFieldType(SwDoc* pDocPtr)
34 898 : : SwValueFieldType( pDocPtr, RES_TABLEFLD )
35 898 : {}
36 :
37 0 : SwFieldType* SwTblFieldType::Copy() const
38 : {
39 0 : return new SwTblFieldType(GetDoc());
40 : }
41 :
42 0 : void SwTblField::CalcField( SwTblCalcPara& rCalcPara )
43 : {
44 0 : if( rCalcPara.rCalc.IsCalcError() ) // stop if there is already an error set
45 0 : return;
46 :
47 : // create pointers from box name
48 0 : BoxNmToPtr( rCalcPara.pTbl );
49 0 : String sFml( MakeFormula( rCalcPara ));
50 0 : SetValue( rCalcPara.rCalc.Calculate( sFml ).GetDouble() );
51 0 : ChgValid( !rCalcPara.IsStackOverFlow() ); // is the value again valid?
52 : }
53 :
54 0 : SwTblField::SwTblField( SwTblFieldType* pInitType, const String& rFormel,
55 : sal_uInt16 nType, sal_uLong nFmt )
56 : : SwValueField( pInitType, nFmt ), SwTableFormula( rFormel ),
57 0 : nSubType(nType)
58 : {
59 0 : sExpand = OUString('0');
60 0 : }
61 :
62 0 : SwField* SwTblField::Copy() const
63 : {
64 0 : SwTblField* pTmp = new SwTblField( (SwTblFieldType*)GetTyp(),
65 0 : SwTableFormula::GetFormula(), nSubType, GetFormat() );
66 0 : pTmp->sExpand = sExpand;
67 0 : pTmp->SwValueField::SetValue(GetValue());
68 0 : pTmp->SwTableFormula::operator=( *this );
69 0 : pTmp->SetAutomaticLanguage(IsAutomaticLanguage());
70 0 : return pTmp;
71 : }
72 :
73 0 : String SwTblField::GetFieldName() const
74 : {
75 0 : String aStr(GetTyp()->GetName());
76 0 : aStr += ' ';
77 0 : aStr += const_cast<SwTblField *>(this)->GetCommand();
78 0 : return aStr;
79 : }
80 :
81 : /// search TextNode containing this field
82 0 : const SwNode* SwTblField::GetNodeOfFormula() const
83 : {
84 0 : if( !GetTyp()->GetDepends() )
85 0 : return 0;
86 :
87 0 : SwIterator<SwFmtFld,SwFieldType> aIter( *GetTyp() );
88 0 : for( SwFmtFld* pFmtFld = aIter.First(); pFmtFld; pFmtFld = aIter.Next() )
89 0 : if( this == pFmtFld->GetFld() )
90 0 : return (SwTxtNode*)&pFmtFld->GetTxtFld()->GetTxtNode();
91 0 : return 0;
92 : }
93 :
94 0 : String SwTblField::GetCommand()
95 : {
96 0 : if (EXTRNL_NAME != GetNameType())
97 : {
98 0 : SwNode const*const pNd = GetNodeOfFormula();
99 0 : SwTableNode const*const pTblNd = (pNd) ? pNd->FindTableNode() : 0;
100 0 : if (pTblNd)
101 : {
102 0 : PtrToBoxNm( &pTblNd->GetTable() );
103 : }
104 : }
105 0 : return (EXTRNL_NAME == GetNameType())
106 0 : ? SwTableFormula::GetFormula()
107 0 : : String();
108 : }
109 :
110 0 : String SwTblField::Expand() const
111 : {
112 0 : String aStr;
113 0 : if (nSubType & nsSwExtendedSubType::SUB_CMD)
114 : {
115 0 : aStr = const_cast<SwTblField *>(this)->GetCommand();
116 : }
117 : else
118 : {
119 0 : aStr = sExpand;
120 0 : if(nSubType & nsSwGetSetExpType::GSE_STRING)
121 : {
122 : // es ist ein String
123 0 : aStr = sExpand;
124 0 : aStr.Erase( 0,1 );
125 0 : aStr.Erase( aStr.Len()-1, 1 );
126 : }
127 : }
128 0 : return aStr;
129 : }
130 :
131 0 : sal_uInt16 SwTblField::GetSubType() const
132 : {
133 0 : return nSubType;
134 : }
135 :
136 0 : void SwTblField::SetSubType(sal_uInt16 nType)
137 : {
138 0 : nSubType = nType;
139 0 : }
140 :
141 :
142 0 : void SwTblField::SetValue( const double& rVal )
143 : {
144 0 : SwValueField::SetValue(rVal);
145 0 : sExpand = ((SwValueFieldType*)GetTyp())->ExpandValue(rVal, GetFormat(), GetLanguage());
146 0 : }
147 :
148 0 : OUString SwTblField::GetPar2() const
149 : {
150 0 : return SwTableFormula::GetFormula();
151 : }
152 :
153 0 : void SwTblField::SetPar2(const OUString& rStr)
154 : {
155 0 : SetFormula( rStr );
156 0 : }
157 :
158 0 : bool SwTblField::QueryValue( uno::Any& rAny, sal_uInt16 nWhichId ) const
159 : {
160 0 : bool bRet = true;
161 0 : switch ( nWhichId )
162 : {
163 : case FIELD_PROP_PAR2:
164 : {
165 0 : sal_uInt16 nOldSubType = nSubType;
166 0 : SwTblField* pThis = (SwTblField*)this;
167 0 : pThis->nSubType |= nsSwExtendedSubType::SUB_CMD;
168 0 : rAny <<= OUString( Expand() );
169 0 : pThis->nSubType = nOldSubType;
170 : }
171 0 : break;
172 : case FIELD_PROP_BOOL1:
173 : {
174 0 : sal_Bool bFormula = 0 != (nsSwExtendedSubType::SUB_CMD & nSubType);
175 0 : rAny.setValue(&bFormula, ::getBooleanCppuType());
176 : }
177 0 : break;
178 : case FIELD_PROP_PAR1:
179 0 : rAny <<= OUString(GetExpStr());
180 0 : break;
181 : case FIELD_PROP_FORMAT:
182 0 : rAny <<= (sal_Int32)GetFormat();
183 0 : break;
184 : default:
185 0 : bRet = false;
186 : }
187 0 : return bRet;
188 : }
189 :
190 0 : bool SwTblField::PutValue( const uno::Any& rAny, sal_uInt16 nWhichId )
191 : {
192 0 : bool bRet = true;
193 0 : String sTmp;
194 0 : switch ( nWhichId )
195 : {
196 : case FIELD_PROP_PAR2:
197 0 : SetFormula( ::GetString( rAny, sTmp ));
198 0 : break;
199 : case FIELD_PROP_BOOL1:
200 0 : if(*(sal_Bool*)rAny.getValue())
201 0 : nSubType = nsSwGetSetExpType::GSE_FORMULA|nsSwExtendedSubType::SUB_CMD;
202 : else
203 0 : nSubType = nsSwGetSetExpType::GSE_FORMULA;
204 0 : break;
205 : case FIELD_PROP_PAR1:
206 0 : ChgExpStr( ::GetString( rAny, sTmp ));
207 0 : break;
208 : case FIELD_PROP_FORMAT:
209 : {
210 0 : sal_Int32 nTmp = 0;
211 0 : rAny >>= nTmp;
212 0 : SetFormat(nTmp);
213 : }
214 0 : break;
215 : default:
216 0 : bRet = false;
217 : }
218 0 : return bRet;
219 99 : }
220 :
221 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|