1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: atrfld.cxx,v $
10 * $Revision: 1.16.190.1 $
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"
34 #include "fldbas.hxx" // fuer FieldType
37 #include <docufld.hxx>
43 #include "swfont.hxx" // fuer GetFldsColor
44 #include "ndtxt.hxx" // SwTxtNode
45 #include "calc.hxx" // Update fuer UserFields
47 #include <IDocumentFieldsAccess.hxx>
49 #include <svtools/smplhint.hxx>
51 TYPEINIT3( SwFmtFld
, SfxPoolItem
, SwClient
,SfxBroadcaster
)
52 TYPEINIT1(SwFmtFldHint
, SfxHint
);
54 /****************************************************************************
58 ****************************************************************************/
60 // Konstruktor fuers Default vom Attribut-Pool
62 : SfxPoolItem( RES_TXTATR_FIELD
),
69 SwFmtFld::SwFmtFld( const SwField
&rFld
)
70 : SfxPoolItem( RES_TXTATR_FIELD
),
71 SwClient( rFld
.GetTyp() ),
78 // Since Items are used in ItemPool and in default constructed ItemSets with
79 // full pool range, all items need to be clonable. Thus, this one needed to be
81 SwFmtFld::SwFmtFld( const SwFmtFld
& rAttr
)
82 : SfxPoolItem( RES_TXTATR_FIELD
), SwClient(), SfxBroadcaster(),
88 rAttr
.GetFld()->GetTyp()->Add(this);
89 pField
= rAttr
.GetFld()->Copy();
95 SwFieldType
* pType
= pField
? pField
->GetTyp() : 0;
97 if (pType
&& pType
->Which() == RES_DBFLD
)
98 pType
= 0; // DB-Feldtypen zerstoeren sich selbst
100 Broadcast( SwFmtFldHint( this, SWFMTFLD_REMOVED
) );
103 // bei einige FeldTypen muessen wir den FeldTypen noch loeschen
104 if( pType
&& pType
->IsLastDepend() )
107 switch( pType
->Which() )
110 bDel
= ((SwUserFieldType
*)pType
)->IsDeleted();
114 bDel
= ((SwSetExpFieldType
*)pType
)->IsDeleted();
118 bDel
= ((SwDDEFieldType
*)pType
)->IsDeleted();
124 // vorm loeschen erstmal austragen
125 pType
->Remove( this );
132 void SwFmtFld::SetFld(SwField
* _pField
)
138 Broadcast( SwFmtFldHint( this, SWFMTFLD_CHANGED
) );
141 int SwFmtFld::operator==( const SfxPoolItem
& rAttr
) const
143 ASSERT( SfxPoolItem::operator==( rAttr
), "keine gleichen Attribute" );
144 // OD 2004-05-14 #i29146# - correction: check, if <pField> and
145 // <((SwFmtFld&)rAttr).GetFld()> are set.
146 // OD 2004-05-14 #i29146# - items are equal, if both fields aren't set.
147 return ( pField
&& ((SwFmtFld
&)rAttr
).GetFld() &&
148 pField
->GetTyp() == ((SwFmtFld
&)rAttr
).GetFld()->GetTyp() &&
149 pField
->GetFormat() == ((SwFmtFld
&)rAttr
).GetFld()->GetFormat() ) ||
150 ( !pField
&& !((SwFmtFld
&)rAttr
).GetFld() );
153 SfxPoolItem
* SwFmtFld::Clone( SfxItemPool
* ) const
155 return new SwFmtFld( *this );
158 void SwFmtFld::Modify( SfxPoolItem
* pOld
, SfxPoolItem
* pNew
)
163 SwTxtNode
* pTxtNd
= (SwTxtNode
*)&pTxtAttr
->GetTxtNode();
164 ASSERT( pTxtNd
, "wo ist denn mein Node?" );
167 switch( pNew
->Which() )
169 case RES_TXTATR_FLDCHG
:
170 // "Farbe hat sich geaendert !"
171 // this, this fuer "nur Painten"
172 pTxtNd
->Modify( this, this );
174 case RES_REFMARKFLD_UPDATE
:
175 // GetReferenz-Felder aktualisieren
176 if( RES_GETREFFLD
== GetFld()->GetTyp()->Which() )
178 // --> OD 2007-09-06 #i81002#
179 // ((SwGetRefField*)GetFld())->UpdateField();
180 dynamic_cast<SwGetRefField
*>(GetFld())->UpdateField( pTxtAttr
);
184 case RES_DOCPOS_UPDATE
:
185 // Je nach DocPos aktualisieren (SwTxtFrm::Modify())
186 pTxtNd
->Modify( pNew
, this );
189 case RES_ATTRSET_CHG
:
191 pTxtNd
->Modify( pOld
, pNew
);
196 switch (GetFld()->GetTyp()->Which())
198 case RES_HIDDENPARAFLD
:
199 if( !pOld
|| RES_HIDDENPARA_PRINT
!= pOld
->Which() )
201 case RES_DBSETNUMBERFLD
:
202 case RES_DBNUMSETFLD
:
203 case RES_DBNEXTSETFLD
:
205 pTxtNd
->Modify( 0, pNew
);
209 if( RES_USERFLD
== GetFld()->GetTyp()->Which() )
211 SwUserFieldType
* pType
= (SwUserFieldType
*)GetFld()->GetTyp();
212 if(!pType
->IsValid())
214 SwCalc
aCalc( *pTxtNd
->GetDoc() );
215 pType
->GetValue( aCalc
);
221 BOOL
SwFmtFld::GetInfo( SfxPoolItem
& rInfo
) const
223 const SwTxtNode
* pTxtNd
;
224 if( RES_AUTOFMT_DOCNODE
!= rInfo
.Which() ||
225 !pTxtAttr
|| 0 == ( pTxtNd
= pTxtAttr
->GetpTxtNode() ) ||
226 &pTxtNd
->GetNodes() != ((SwAutoFmtGetDocNode
&)rInfo
).pNodes
)
229 ((SwAutoFmtGetDocNode
&)rInfo
).pCntntNode
= pTxtNd
;
234 BOOL
SwFmtFld::IsFldInDoc() const
236 const SwTxtNode
* pTxtNd
;
237 return pTxtAttr
&& 0 != ( pTxtNd
= pTxtAttr
->GetpTxtNode() ) &&
238 pTxtNd
->GetNodes().IsDocNodes();
241 BOOL
SwFmtFld::IsProtect() const
243 const SwTxtNode
* pTxtNd
;
244 return pTxtAttr
&& 0 != ( pTxtNd
= pTxtAttr
->GetpTxtNode() ) &&
248 /*************************************************************************
250 |* SwTxtFld::SwTxtFld()
252 |* Beschreibung Attribut fuer automatischen Text, Ctor
253 |* Ersterstellung BP 30.04.92
254 |* Letzte Aenderung JP 15.08.94
256 *************************************************************************/
258 SwTxtFld::SwTxtFld( SwFmtFld
& rAttr
, xub_StrLen nStartPos
)
259 : SwTxtAttr( rAttr
, nStartPos
)
260 , m_aExpand( rAttr
.GetFld()->Expand() )
263 rAttr
.pTxtAttr
= this;
266 SwTxtFld::~SwTxtFld( )
270 /*************************************************************************
272 |* SwTxtFld::Expand()
274 |* Beschreibung exandiert das Feld und tauscht den Text im Node
275 |* Ersterstellung BP 30.04.92
276 |* Letzte Aenderung JP 15.08.94
278 *************************************************************************/
280 void SwTxtFld::Expand() const
282 // Wenn das expandierte Feld sich nicht veraendert hat, wird returnt
283 ASSERT( m_pTxtNode
, "SwTxtFld: where is my TxtNode?" );
285 const SwField
* pFld
= GetFld().GetFld();
286 XubString
aNewExpand( pFld
->Expand() );
288 if( aNewExpand
== m_aExpand
)
290 // Bei Seitennummernfeldern
291 const USHORT nWhich
= pFld
->GetTyp()->Which();
292 if( RES_CHAPTERFLD
!= nWhich
&& RES_PAGENUMBERFLD
!= nWhich
&&
293 RES_REFPAGEGETFLD
!= nWhich
&&
294 // --> FME 2005-05-23 #122919# Page count fields to not use aExpand
295 // during formatting, therefore an invalidation of the text frame
296 // has to be triggered even if aNewExpand == aExpand:
297 ( RES_DOCSTATFLD
!= nWhich
|| DS_PAGE
!= static_cast<const SwDocStatField
*>(pFld
)->GetSubType() ) &&
299 ( RES_GETEXPFLD
!= nWhich
|| ((SwGetExpField
*)pFld
)->IsInBodyTxt() ) )
301 // BP: das muesste man noch optimieren!
302 //JP 12.06.97: stimmt, man sollte auf jedenfall eine Status-
303 // aenderung an die Frames posten
304 if( m_pTxtNode
->CalcHiddenParaField() )
306 m_pTxtNode
->Modify( 0, 0 );
312 m_aExpand
= aNewExpand
;
314 // 0, this for formatting
315 m_pTxtNode
->Modify( 0, const_cast<SwFmtFld
*>( &GetFld() ) );
318 /*************************************************************************
319 * SwTxtFld::CopyFld()
320 *************************************************************************/
322 void SwTxtFld::CopyFld( SwTxtFld
*pDest
) const
324 ASSERT( m_pTxtNode
, "SwTxtFld: where is my TxtNode?" );
325 ASSERT( pDest
->m_pTxtNode
, "SwTxtFld: where is pDest's TxtNode?" );
327 IDocumentFieldsAccess
* pIDFA
= m_pTxtNode
->getIDocumentFieldsAccess();
328 IDocumentFieldsAccess
* pDestIDFA
= pDest
->m_pTxtNode
->getIDocumentFieldsAccess();
330 SwFmtFld
& rFmtFld
= (SwFmtFld
&)pDest
->GetFld();
331 const USHORT nFldWhich
= rFmtFld
.GetFld()->GetTyp()->Which();
333 if( pIDFA
!= pDestIDFA
)
335 // Die Hints stehen in unterschiedlichen Dokumenten,
336 // der Feldtyp muss im neuen Dokument angemeldet werden.
337 // Z.B: Kopieren ins ClipBoard.
338 SwFieldType
* pFldType
;
339 if( nFldWhich
!= RES_DBFLD
&& nFldWhich
!= RES_USERFLD
&&
340 nFldWhich
!= RES_SETEXPFLD
&& nFldWhich
!= RES_DDEFLD
&&
341 RES_AUTHORITY
!= nFldWhich
)
342 pFldType
= pDestIDFA
->GetSysFldType( nFldWhich
);
344 pFldType
= pDestIDFA
->InsertFldType( *rFmtFld
.GetFld()->GetTyp() );
346 // Sonderbehandlung fuer DDE-Felder
347 if( RES_DDEFLD
== nFldWhich
)
349 if( rFmtFld
.GetTxtFld() )
350 ((SwDDEFieldType
*)rFmtFld
.GetFld()->GetTyp())->DecRefCnt();
351 ((SwDDEFieldType
*)pFldType
)->IncRefCnt();
354 ASSERT( pFldType
, "unbekannter FieldType" );
355 pFldType
->Add( &rFmtFld
); // ummelden
356 rFmtFld
.GetFld()->ChgTyp( pFldType
);
359 // Expressionfelder Updaten
360 if( nFldWhich
== RES_SETEXPFLD
|| nFldWhich
== RES_GETEXPFLD
||
361 nFldWhich
== RES_HIDDENTXTFLD
)
363 SwTxtFld
* pFld
= (SwTxtFld
*)this;
364 pDestIDFA
->UpdateExpFlds( pFld
, true );
366 // Tabellenfelder auf externe Darstellung
367 else if( RES_TABLEFLD
== nFldWhich
&&
368 ((SwTblField
*)rFmtFld
.GetFld())->IsIntrnlName() )
370 // erzeuge aus der internen (fuer CORE) die externe (fuer UI) Formel
371 const SwTableNode
* pTblNd
= m_pTxtNode
->FindTableNode();
372 if( pTblNd
) // steht in einer Tabelle
373 ((SwTblField
*)rFmtFld
.GetFld())->PtrToBoxNm( &pTblNd
->GetTable() );
377 /* -----------------26.06.2003 13:54-----------------
379 --------------------------------------------------*/
380 void SwTxtFld::NotifyContentChange(SwFmtFld
& rFmtFld
)
382 //if not in undo section notify the change
383 if (m_pTxtNode
&& m_pTxtNode
->GetNodes().IsDocNodes())
385 m_pTxtNode
->Modify(0, &rFmtFld
);