xref: /aoo41x/main/sw/source/core/attr/cellatr.cxx (revision efeef26f)
1*efeef26fSAndrew Rist /**************************************************************
2*efeef26fSAndrew Rist  *
3*efeef26fSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*efeef26fSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*efeef26fSAndrew Rist  * distributed with this work for additional information
6*efeef26fSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*efeef26fSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*efeef26fSAndrew Rist  * "License"); you may not use this file except in compliance
9*efeef26fSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*efeef26fSAndrew Rist  *
11*efeef26fSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*efeef26fSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*efeef26fSAndrew Rist  * software distributed under the License is distributed on an
15*efeef26fSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*efeef26fSAndrew Rist  * KIND, either express or implied.  See the License for the
17*efeef26fSAndrew Rist  * specific language governing permissions and limitations
18*efeef26fSAndrew Rist  * under the License.
19*efeef26fSAndrew Rist  *
20*efeef26fSAndrew Rist  *************************************************************/
21*efeef26fSAndrew Rist 
22*efeef26fSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sw.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <float.h>
28cdf0e10cSrcweir #include <rtl/math.hxx>
29cdf0e10cSrcweir #include <hintids.hxx> 			// fuer RES_..
30cdf0e10cSrcweir #include <cellatr.hxx>
31cdf0e10cSrcweir #include <calc.hxx>
32cdf0e10cSrcweir #include <format.hxx>
33cdf0e10cSrcweir #include <doc.hxx>
34cdf0e10cSrcweir #include <swtable.hxx>
35cdf0e10cSrcweir #include <node.hxx>
36cdf0e10cSrcweir #include <hints.hxx>
37cdf0e10cSrcweir #include <rolbck.hxx>
38cdf0e10cSrcweir #include <switerator.hxx>
39cdf0e10cSrcweir 
40cdf0e10cSrcweir 
41cdf0e10cSrcweir //TYPEINIT1( SwFmt, SwClient );	//rtti fuer SwFmt
42cdf0e10cSrcweir 
43cdf0e10cSrcweir /*************************************************************************
44cdf0e10cSrcweir |*
45cdf0e10cSrcweir *************************************************************************/
46cdf0e10cSrcweir 
47cdf0e10cSrcweir 
SwTblBoxNumFormat(sal_uInt32 nFormat,sal_Bool bFlag)48cdf0e10cSrcweir SwTblBoxNumFormat::SwTblBoxNumFormat( sal_uInt32 nFormat, sal_Bool bFlag )
49cdf0e10cSrcweir 	: SfxUInt32Item( RES_BOXATR_FORMAT, nFormat ), bAuto( bFlag )
50cdf0e10cSrcweir {
51cdf0e10cSrcweir }
52cdf0e10cSrcweir 
53cdf0e10cSrcweir 
operator ==(const SfxPoolItem & rAttr) const54cdf0e10cSrcweir int SwTblBoxNumFormat::operator==( const SfxPoolItem& rAttr ) const
55cdf0e10cSrcweir {
56cdf0e10cSrcweir 	ASSERT( SfxPoolItem::operator==( rAttr ), "keine gleichen Attribute" );
57cdf0e10cSrcweir 	return GetValue() == ((SwTblBoxNumFormat&)rAttr).GetValue() &&
58cdf0e10cSrcweir 			bAuto == ((SwTblBoxNumFormat&)rAttr).bAuto;
59cdf0e10cSrcweir }
60cdf0e10cSrcweir 
61cdf0e10cSrcweir 
Clone(SfxItemPool *) const62cdf0e10cSrcweir SfxPoolItem* SwTblBoxNumFormat::Clone( SfxItemPool* ) const
63cdf0e10cSrcweir {
64cdf0e10cSrcweir 	return new SwTblBoxNumFormat( GetValue(), bAuto );
65cdf0e10cSrcweir }
66cdf0e10cSrcweir 
67cdf0e10cSrcweir 
68cdf0e10cSrcweir /*************************************************************************
69cdf0e10cSrcweir |*
70cdf0e10cSrcweir *************************************************************************/
71cdf0e10cSrcweir 
72cdf0e10cSrcweir 
73cdf0e10cSrcweir 
SwTblBoxFormula(const String & rFormula)74cdf0e10cSrcweir SwTblBoxFormula::SwTblBoxFormula( const String& rFormula )
75cdf0e10cSrcweir 	: SfxPoolItem( RES_BOXATR_FORMULA ),
76cdf0e10cSrcweir 	SwTableFormula( rFormula ),
77cdf0e10cSrcweir 	pDefinedIn( 0 )
78cdf0e10cSrcweir {
79cdf0e10cSrcweir }
80cdf0e10cSrcweir 
81cdf0e10cSrcweir 
operator ==(const SfxPoolItem & rAttr) const82cdf0e10cSrcweir int SwTblBoxFormula::operator==( const SfxPoolItem& rAttr ) const
83cdf0e10cSrcweir {
84cdf0e10cSrcweir 	ASSERT( SfxPoolItem::operator==( rAttr ), "keine gleichen Attribute" );
85cdf0e10cSrcweir 	return GetFormula() == ((SwTblBoxFormula&)rAttr).GetFormula() &&
86cdf0e10cSrcweir 			pDefinedIn == ((SwTblBoxFormula&)rAttr).pDefinedIn;
87cdf0e10cSrcweir }
88cdf0e10cSrcweir 
89cdf0e10cSrcweir 
Clone(SfxItemPool *) const90cdf0e10cSrcweir SfxPoolItem* SwTblBoxFormula::Clone( SfxItemPool* ) const
91cdf0e10cSrcweir {
92cdf0e10cSrcweir // auf externe Darstellung umschalten!!
93cdf0e10cSrcweir 	SwTblBoxFormula* pNew = new SwTblBoxFormula( GetFormula() );
94cdf0e10cSrcweir 	pNew->SwTableFormula::operator=( *this );
95cdf0e10cSrcweir 	return pNew;
96cdf0e10cSrcweir }
97cdf0e10cSrcweir 
98cdf0e10cSrcweir 
99cdf0e10cSrcweir 
100cdf0e10cSrcweir 	// suche den Node, in dem die Formel steht:
101cdf0e10cSrcweir 	//	TextFeld	-> TextNode,
102cdf0e10cSrcweir 	//	BoxAttribut	-> BoxStartNode
103cdf0e10cSrcweir 	// !!! MUSS VON JEDER ABLEITUNG UEBERLADEN WERDEN !!!
GetNodeOfFormula() const104cdf0e10cSrcweir const SwNode* SwTblBoxFormula::GetNodeOfFormula() const
105cdf0e10cSrcweir {
106cdf0e10cSrcweir 	const SwNode* pRet = 0;
107cdf0e10cSrcweir 	if( pDefinedIn )
108cdf0e10cSrcweir 	{
109cdf0e10cSrcweir 		SwTableBox* pBox = SwIterator<SwTableBox,SwModify>::FirstElement( *pDefinedIn );
110cdf0e10cSrcweir 		if( pBox )
111cdf0e10cSrcweir 			pRet = pBox->GetSttNd();
112cdf0e10cSrcweir 	}
113cdf0e10cSrcweir 	return pRet;
114cdf0e10cSrcweir }
115cdf0e10cSrcweir 
116cdf0e10cSrcweir 
GetTableBox()117cdf0e10cSrcweir SwTableBox* SwTblBoxFormula::GetTableBox()
118cdf0e10cSrcweir {
119cdf0e10cSrcweir 	SwTableBox* pBox = 0;
120cdf0e10cSrcweir 	if( pDefinedIn )
121cdf0e10cSrcweir 		pBox = SwIterator<SwTableBox,SwModify>::FirstElement( *pDefinedIn );
122cdf0e10cSrcweir 	return pBox;
123cdf0e10cSrcweir }
124cdf0e10cSrcweir 
125cdf0e10cSrcweir 
ChangeState(const SfxPoolItem * pItem)126cdf0e10cSrcweir void SwTblBoxFormula::ChangeState( const SfxPoolItem* pItem )
127cdf0e10cSrcweir {
128cdf0e10cSrcweir 	if( !pDefinedIn )
129cdf0e10cSrcweir 		return ;
130cdf0e10cSrcweir 
131cdf0e10cSrcweir 	SwTableFmlUpdate* pUpdtFld;
132cdf0e10cSrcweir 	if( !pItem || RES_TABLEFML_UPDATE != pItem->Which() )
133cdf0e10cSrcweir 	{
134cdf0e10cSrcweir 		// setze bei allen das Value-Flag zurueck
135cdf0e10cSrcweir 		ChgValid( sal_False );
136cdf0e10cSrcweir 		return ;
137cdf0e10cSrcweir 	}
138cdf0e10cSrcweir 
139cdf0e10cSrcweir 	pUpdtFld = (SwTableFmlUpdate*)pItem;
140cdf0e10cSrcweir 
141cdf0e10cSrcweir 	// bestimme die Tabelle, in der das Attribut steht
142cdf0e10cSrcweir 	const SwTableNode* pTblNd;
143cdf0e10cSrcweir 	const SwNode* pNd = GetNodeOfFormula();
144cdf0e10cSrcweir 	if( pNd && &pNd->GetNodes() == &pNd->GetDoc()->GetNodes() &&
145cdf0e10cSrcweir 		0 != ( pTblNd = pNd->FindTableNode() ))
146cdf0e10cSrcweir 	{
147cdf0e10cSrcweir 		switch( pUpdtFld->eFlags )
148cdf0e10cSrcweir 		{
149cdf0e10cSrcweir 		case TBL_CALC:
150cdf0e10cSrcweir 			// setze das Value-Flag zurueck
151cdf0e10cSrcweir 			// JP 17.06.96: interne Darstellung auf alle Formeln
152cdf0e10cSrcweir 			//				(Referenzen auf andere Tabellen!!!)
153cdf0e10cSrcweir //			if( VF_CMD & pFld->GetFormat() )
154cdf0e10cSrcweir //				pFld->PtrToBoxNm( pUpdtFld->pTbl );
155cdf0e10cSrcweir //			else
156cdf0e10cSrcweir 				ChgValid( sal_False );
157cdf0e10cSrcweir 			break;
158cdf0e10cSrcweir 		case TBL_BOXNAME:
159cdf0e10cSrcweir 			// ist es die gesuchte Tabelle ??
160cdf0e10cSrcweir 			if( &pTblNd->GetTable() == pUpdtFld->pTbl )
161cdf0e10cSrcweir 				// zur externen Darstellung
162cdf0e10cSrcweir 				PtrToBoxNm( pUpdtFld->pTbl );
163cdf0e10cSrcweir 			break;
164cdf0e10cSrcweir 		case TBL_BOXPTR:
165cdf0e10cSrcweir 			// zur internen Darstellung
166cdf0e10cSrcweir 			// JP 17.06.96: interne Darstellung auf alle Formeln
167cdf0e10cSrcweir 			//				(Referenzen auf andere Tabellen!!!)
168cdf0e10cSrcweir 			BoxNmToPtr( &pTblNd->GetTable() );
169cdf0e10cSrcweir 			break;
170cdf0e10cSrcweir 		case TBL_RELBOXNAME:
171cdf0e10cSrcweir 			// ist es die gesuchte Tabelle ??
172cdf0e10cSrcweir 			if( &pTblNd->GetTable() == pUpdtFld->pTbl )
173cdf0e10cSrcweir 				// zur relativen Darstellung
174cdf0e10cSrcweir 				ToRelBoxNm( pUpdtFld->pTbl );
175cdf0e10cSrcweir 			break;
176cdf0e10cSrcweir 
177cdf0e10cSrcweir 		case TBL_SPLITTBL:
178cdf0e10cSrcweir 			if( &pTblNd->GetTable() == pUpdtFld->pTbl )
179cdf0e10cSrcweir 			{
180cdf0e10cSrcweir 				sal_uInt16 nLnPos = SwTableFormula::GetLnPosInTbl(
181cdf0e10cSrcweir 										pTblNd->GetTable(), GetTableBox() );
182cdf0e10cSrcweir 				pUpdtFld->bBehindSplitLine = USHRT_MAX != nLnPos &&
183cdf0e10cSrcweir 											pUpdtFld->nSplitLine <= nLnPos;
184cdf0e10cSrcweir 			}
185cdf0e10cSrcweir 			else
186cdf0e10cSrcweir 				pUpdtFld->bBehindSplitLine = sal_False;
187cdf0e10cSrcweir 			// kein break
188cdf0e10cSrcweir 		case TBL_MERGETBL:
189cdf0e10cSrcweir 			if( pUpdtFld->pHistory )
190cdf0e10cSrcweir 			{
191cdf0e10cSrcweir 				// fuer die History brauche ich aber die unveraenderte Formel
192cdf0e10cSrcweir 				SwTblBoxFormula aCopy( *this );
193cdf0e10cSrcweir 				pUpdtFld->bModified = sal_False;
194cdf0e10cSrcweir 				ToSplitMergeBoxNm( *pUpdtFld );
195cdf0e10cSrcweir 
196cdf0e10cSrcweir 				if( pUpdtFld->bModified )
197cdf0e10cSrcweir 				{
198cdf0e10cSrcweir 					// und dann in der externen Darstellung
199cdf0e10cSrcweir 					aCopy.PtrToBoxNm( &pTblNd->GetTable() );
200cdf0e10cSrcweir 					pUpdtFld->pHistory->Add( &aCopy, &aCopy,
201cdf0e10cSrcweir                                 pNd->FindTableBoxStartNode()->GetIndex() );
202cdf0e10cSrcweir 				}
203cdf0e10cSrcweir 			}
204cdf0e10cSrcweir 			else
205cdf0e10cSrcweir 				ToSplitMergeBoxNm( *pUpdtFld );
206cdf0e10cSrcweir 			break;
207cdf0e10cSrcweir 		}
208cdf0e10cSrcweir 	}
209cdf0e10cSrcweir }
210cdf0e10cSrcweir 
211cdf0e10cSrcweir 
Calc(SwTblCalcPara & rCalcPara,double & rValue)212cdf0e10cSrcweir void SwTblBoxFormula::Calc( SwTblCalcPara& rCalcPara, double& rValue )
213cdf0e10cSrcweir {
214cdf0e10cSrcweir 	if( !rCalcPara.rCalc.IsCalcError() )		// ist schon Fehler gesetzt ?
215cdf0e10cSrcweir 	{
216cdf0e10cSrcweir 		// erzeuge aus den BoxNamen die Pointer
217cdf0e10cSrcweir 		BoxNmToPtr( rCalcPara.pTbl );
218cdf0e10cSrcweir 		String sFml( MakeFormel( rCalcPara ));
219cdf0e10cSrcweir 		if( !rCalcPara.rCalc.IsCalcError() )
220cdf0e10cSrcweir 			rValue = rCalcPara.rCalc.Calculate( sFml ).GetDouble();
221cdf0e10cSrcweir 		else
222cdf0e10cSrcweir 			rValue = DBL_MAX;
223cdf0e10cSrcweir 		ChgValid( !rCalcPara.IsStackOverFlow() );		// der Wert ist wieder gueltig
224cdf0e10cSrcweir 	}
225cdf0e10cSrcweir }
226cdf0e10cSrcweir 
227cdf0e10cSrcweir /*************************************************************************
228cdf0e10cSrcweir |*
229cdf0e10cSrcweir *************************************************************************/
230cdf0e10cSrcweir 
231cdf0e10cSrcweir 
SwTblBoxValue()232cdf0e10cSrcweir SwTblBoxValue::SwTblBoxValue()
233cdf0e10cSrcweir 	: SfxPoolItem( RES_BOXATR_VALUE ), nValue( 0 )
234cdf0e10cSrcweir {
235cdf0e10cSrcweir }
236cdf0e10cSrcweir 
237cdf0e10cSrcweir 
SwTblBoxValue(const double nVal)238cdf0e10cSrcweir SwTblBoxValue::SwTblBoxValue( const double nVal )
239cdf0e10cSrcweir 	: SfxPoolItem( RES_BOXATR_VALUE ), nValue( nVal )
240cdf0e10cSrcweir {
241cdf0e10cSrcweir }
242cdf0e10cSrcweir 
243cdf0e10cSrcweir 
operator ==(const SfxPoolItem & rAttr) const244cdf0e10cSrcweir int SwTblBoxValue::operator==( const SfxPoolItem& rAttr ) const
245cdf0e10cSrcweir {
246cdf0e10cSrcweir     ASSERT(SfxPoolItem::operator==(rAttr), "SwTblBoxValue: item not equal");
247cdf0e10cSrcweir     SwTblBoxValue const& rOther( static_cast<SwTblBoxValue const&>(rAttr) );
248cdf0e10cSrcweir     // items with NaN should be equal to enable pooling
249cdf0e10cSrcweir     return ::rtl::math::isNan(nValue)
250cdf0e10cSrcweir         ?   ::rtl::math::isNan(rOther.nValue)
251cdf0e10cSrcweir         :   (nValue == rOther.nValue);
252cdf0e10cSrcweir }
253cdf0e10cSrcweir 
254cdf0e10cSrcweir 
Clone(SfxItemPool *) const255cdf0e10cSrcweir SfxPoolItem* SwTblBoxValue::Clone( SfxItemPool* ) const
256cdf0e10cSrcweir {
257cdf0e10cSrcweir 	return new SwTblBoxValue( nValue );
258cdf0e10cSrcweir }
259cdf0e10cSrcweir 
260cdf0e10cSrcweir 
261cdf0e10cSrcweir 
262cdf0e10cSrcweir 
263