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 :
21 : #include "fldbas.hxx" // fuer FieldType
22 : #include <fmtfld.hxx>
23 : #include <txtfld.hxx>
24 : #include <docufld.hxx>
25 : #include <doc.hxx>
26 :
27 : #include "reffld.hxx"
28 : #include "ddefld.hxx"
29 : #include "usrfld.hxx"
30 : #include "expfld.hxx"
31 : #include "swfont.hxx" // fuer GetFldsColor
32 : #include "ndtxt.hxx" // SwTxtNode
33 : #include "calc.hxx" // Update fuer UserFields
34 : #include "hints.hxx"
35 : #include <IDocumentFieldsAccess.hxx>
36 : #include <fieldhint.hxx>
37 : #include <svl/smplhint.hxx>
38 :
39 9032 : TYPEINIT3( SwFmtFld, SfxPoolItem, SwClient,SfxBroadcaster)
40 243 : TYPEINIT1(SwFmtFldHint, SfxHint);
41 :
42 : /****************************************************************************
43 : *
44 : * class SwFmtFld
45 : *
46 : ****************************************************************************/
47 :
48 : // Konstruktor fuers Default vom Attribut-Pool
49 10 : SwFmtFld::SwFmtFld()
50 : : SfxPoolItem( RES_TXTATR_FIELD ),
51 : SwClient( 0 ),
52 : pField( 0 ),
53 10 : pTxtAttr( 0 )
54 : {
55 10 : }
56 :
57 491 : SwFmtFld::SwFmtFld( const SwField &rFld )
58 : : SfxPoolItem( RES_TXTATR_FIELD ),
59 491 : SwClient( rFld.GetTyp() ),
60 982 : pTxtAttr( 0 )
61 : {
62 491 : pField = rFld.CopyField();
63 491 : }
64 :
65 : // #i24434#
66 : // Since Items are used in ItemPool and in default constructed ItemSets with
67 : // full pool range, all items need to be clonable. Thus, this one needed to be
68 : // corrected
69 1479 : SwFmtFld::SwFmtFld( const SwFmtFld& rAttr )
70 : : SfxPoolItem( RES_TXTATR_FIELD ), SwClient(), SfxBroadcaster(),
71 : pField( 0 ),
72 1479 : pTxtAttr( 0 )
73 : {
74 1479 : if(rAttr.GetFld())
75 : {
76 1479 : rAttr.GetFld()->GetTyp()->Add(this);
77 1479 : pField = rAttr.GetFld()->CopyField();
78 : }
79 1479 : }
80 :
81 5419 : SwFmtFld::~SwFmtFld()
82 : {
83 1970 : SwFieldType* pType = pField ? pField->GetTyp() : 0;
84 :
85 1970 : if (pType && pType->Which() == RES_DBFLD)
86 0 : pType = 0; // DB-Feldtypen zerstoeren sich selbst
87 :
88 1970 : Broadcast( SwFmtFldHint( this, SWFMTFLD_REMOVED ) );
89 1970 : delete pField;
90 :
91 : // bei einige FeldTypen muessen wir den FeldTypen noch loeschen
92 1970 : if( pType && pType->IsLastDepend() )
93 : {
94 9 : sal_Bool bDel = sal_False;
95 9 : switch( pType->Which() )
96 : {
97 : case RES_USERFLD:
98 0 : bDel = ((SwUserFieldType*)pType)->IsDeleted();
99 0 : break;
100 :
101 : case RES_SETEXPFLD:
102 0 : bDel = ((SwSetExpFieldType*)pType)->IsDeleted();
103 0 : break;
104 :
105 : case RES_DDEFLD:
106 0 : bDel = ((SwDDEFieldType*)pType)->IsDeleted();
107 0 : break;
108 : }
109 :
110 9 : if( bDel )
111 : {
112 : // vorm loeschen erstmal austragen
113 0 : pType->Remove( this );
114 0 : delete pType;
115 : }
116 : }
117 3449 : }
118 :
119 0 : void SwFmtFld::RegisterToFieldType( SwFieldType& rType )
120 : {
121 0 : rType.Add(this);
122 0 : }
123 :
124 :
125 : // #111840#
126 0 : void SwFmtFld::SetFld(SwField * _pField)
127 : {
128 0 : delete pField;
129 :
130 0 : pField = _pField;
131 0 : Broadcast( SwFmtFldHint( this, SWFMTFLD_CHANGED ) );
132 0 : }
133 :
134 0 : int SwFmtFld::operator==( const SfxPoolItem& rAttr ) const
135 : {
136 : OSL_ENSURE( SfxPoolItem::operator==( rAttr ), "keine gleichen Attribute" );
137 : // OD 2004-05-14 #i29146# - correction: check, if <pField> and
138 : // <((SwFmtFld&)rAttr).GetFld()> are set.
139 : // OD 2004-05-14 #i29146# - items are equal, if both fields aren't set.
140 0 : return ( pField && ((SwFmtFld&)rAttr).GetFld() &&
141 0 : pField->GetTyp() == ((SwFmtFld&)rAttr).GetFld()->GetTyp() &&
142 0 : pField->GetFormat() == ((SwFmtFld&)rAttr).GetFld()->GetFormat() ) ||
143 0 : ( !pField && !((SwFmtFld&)rAttr).GetFld() );
144 : }
145 :
146 1479 : SfxPoolItem* SwFmtFld::Clone( SfxItemPool* ) const
147 : {
148 1479 : return new SwFmtFld( *this );
149 : }
150 :
151 0 : void SwFmtFld::SwClientNotify( const SwModify&, const SfxHint& rHint )
152 : {
153 0 : if( !pTxtAttr )
154 0 : return;
155 :
156 0 : const SwFieldHint* pHint = dynamic_cast<const SwFieldHint*>( &rHint );
157 0 : if ( pHint )
158 : {
159 : // replace field content by text
160 0 : SwPaM* pPaM = pHint->GetPaM();
161 0 : SwDoc* pDoc = pPaM->GetDoc();
162 0 : const SwTxtNode& rTxtNode = pTxtAttr->GetTxtNode();
163 0 : pPaM->GetPoint()->nNode = rTxtNode;
164 0 : pPaM->GetPoint()->nContent.Assign( (SwTxtNode*)&rTxtNode, *pTxtAttr->GetStart() );
165 :
166 0 : String const aEntry( GetFld()->ExpandField( pDoc->IsClipBoard() ) );
167 0 : pPaM->SetMark();
168 0 : pPaM->Move( fnMoveForward );
169 0 : pDoc->DeleteRange( *pPaM );
170 0 : pDoc->InsertString( *pPaM, aEntry );
171 : }
172 : }
173 :
174 5 : void SwFmtFld::Modify( const SfxPoolItem* pOld, const SfxPoolItem* pNew )
175 : {
176 5 : if( !pTxtAttr )
177 0 : return;
178 :
179 : // don't do anything, especially not expand!
180 5 : if( pNew && pNew->Which() == RES_OBJECTDYING )
181 0 : return;
182 :
183 5 : SwTxtNode* pTxtNd = (SwTxtNode*)&pTxtAttr->GetTxtNode();
184 : OSL_ENSURE( pTxtNd, "wo ist denn mein Node?" );
185 5 : if( pNew )
186 : {
187 5 : switch( pNew->Which() )
188 : {
189 : case RES_TXTATR_FLDCHG:
190 : // "Farbe hat sich geaendert !"
191 : // this, this fuer "nur Painten"
192 0 : pTxtNd->ModifyNotification( this, this );
193 0 : return;
194 : case RES_REFMARKFLD_UPDATE:
195 : // GetReferenz-Felder aktualisieren
196 0 : if( RES_GETREFFLD == GetFld()->GetTyp()->Which() )
197 : {
198 : // #i81002#
199 : // ((SwGetRefField*)GetFld())->UpdateField();
200 0 : dynamic_cast<SwGetRefField*>(GetFld())->UpdateField( pTxtAttr );
201 : }
202 0 : break;
203 : case RES_DOCPOS_UPDATE:
204 : // Je nach DocPos aktualisieren (SwTxtFrm::Modify())
205 5 : pTxtNd->ModifyNotification( pNew, this );
206 5 : return;
207 :
208 : case RES_ATTRSET_CHG:
209 : case RES_FMT_CHG:
210 0 : pTxtNd->ModifyNotification( pOld, pNew );
211 0 : return;
212 : default:
213 0 : break;
214 : }
215 : }
216 :
217 0 : switch (GetFld()->GetTyp()->Which())
218 : {
219 : case RES_HIDDENPARAFLD:
220 0 : if( !pOld || RES_HIDDENPARA_PRINT != pOld->Which() )
221 0 : break;
222 : case RES_DBSETNUMBERFLD:
223 : case RES_DBNUMSETFLD:
224 : case RES_DBNEXTSETFLD:
225 : case RES_DBNAMEFLD:
226 0 : pTxtNd->ModifyNotification( 0, pNew);
227 0 : return;
228 : }
229 :
230 0 : if( RES_USERFLD == GetFld()->GetTyp()->Which() )
231 : {
232 0 : SwUserFieldType* pType = (SwUserFieldType*)GetFld()->GetTyp();
233 0 : if(!pType->IsValid())
234 : {
235 0 : SwCalc aCalc( *pTxtNd->GetDoc() );
236 0 : pType->GetValue( aCalc );
237 : }
238 : }
239 0 : pTxtAttr->Expand();
240 : }
241 :
242 3 : bool SwFmtFld::GetInfo( SfxPoolItem& rInfo ) const
243 : {
244 : const SwTxtNode* pTxtNd;
245 12 : if( RES_AUTOFMT_DOCNODE != rInfo.Which() ||
246 6 : !pTxtAttr || 0 == ( pTxtNd = pTxtAttr->GetpTxtNode() ) ||
247 3 : &pTxtNd->GetNodes() != ((SwAutoFmtGetDocNode&)rInfo).pNodes )
248 0 : return true;
249 :
250 3 : ((SwAutoFmtGetDocNode&)rInfo).pCntntNode = pTxtNd;
251 3 : return false;
252 : }
253 :
254 :
255 42 : sal_Bool SwFmtFld::IsFldInDoc() const
256 : {
257 : const SwTxtNode* pTxtNd;
258 42 : return pTxtAttr && 0 != ( pTxtNd = pTxtAttr->GetpTxtNode() ) &&
259 84 : pTxtNd->GetNodes().IsDocNodes();
260 : }
261 :
262 0 : sal_Bool SwFmtFld::IsProtect() const
263 : {
264 : const SwTxtNode* pTxtNd;
265 0 : return pTxtAttr && 0 != ( pTxtNd = pTxtAttr->GetpTxtNode() ) &&
266 0 : pTxtNd->IsProtect();
267 : }
268 :
269 : /*************************************************************************
270 : |*
271 : |* SwTxtFld::SwTxtFld()
272 : |*
273 : |* Beschreibung Attribut fuer automatischen Text, Ctor
274 : |*
275 : *************************************************************************/
276 :
277 491 : SwTxtFld::SwTxtFld(SwFmtFld & rAttr, xub_StrLen const nStartPos,
278 : bool const bInClipboard)
279 : : SwTxtAttr( rAttr, nStartPos )
280 : // fdo#39694 the ExpandField here may not give the correct result in all cases,
281 : // but is better than nothing
282 : , m_aExpand( rAttr.GetFld()->ExpandField(bInClipboard) )
283 491 : , m_pTxtNode( 0 )
284 : {
285 491 : rAttr.pTxtAttr = this;
286 491 : SetHasDummyChar(true);
287 491 : }
288 :
289 1443 : SwTxtFld::~SwTxtFld( )
290 : {
291 481 : SwFmtFld & rFmtFld( static_cast<SwFmtFld &>(GetAttr()) );
292 481 : if (this == rFmtFld.pTxtAttr)
293 : {
294 481 : rFmtFld.pTxtAttr = 0; // #i110140# invalidate!
295 : }
296 962 : }
297 :
298 : /*************************************************************************
299 : |*
300 : |* SwTxtFld::Expand()
301 : |*
302 : |* Beschreibung exandiert das Feld und tauscht den Text im Node
303 : |*
304 : *************************************************************************/
305 :
306 0 : void SwTxtFld::Expand() const
307 : {
308 : // Wenn das expandierte Feld sich nicht veraendert hat, wird returnt
309 : OSL_ENSURE( m_pTxtNode, "SwTxtFld: where is my TxtNode?" );
310 :
311 0 : const SwField* pFld = GetFld().GetFld();
312 : XubString aNewExpand(
313 0 : pFld->ExpandField(m_pTxtNode->GetDoc()->IsClipBoard()) );
314 :
315 0 : if( aNewExpand == m_aExpand )
316 : {
317 : // Bei Seitennummernfeldern
318 0 : const sal_uInt16 nWhich = pFld->GetTyp()->Which();
319 0 : if( RES_CHAPTERFLD != nWhich && RES_PAGENUMBERFLD != nWhich &&
320 : RES_REFPAGEGETFLD != nWhich &&
321 : // #122919# Page count fields to not use aExpand
322 : // during formatting, therefore an invalidation of the text frame
323 : // has to be triggered even if aNewExpand == aExpand:
324 0 : ( RES_DOCSTATFLD != nWhich || DS_PAGE != static_cast<const SwDocStatField*>(pFld)->GetSubType() ) &&
325 0 : ( RES_GETEXPFLD != nWhich || ((SwGetExpField*)pFld)->IsInBodyTxt() ) )
326 : {
327 : // BP: das muesste man noch optimieren!
328 : //JP 12.06.97: stimmt, man sollte auf jedenfall eine Status-
329 : // aenderung an die Frames posten
330 0 : if( m_pTxtNode->CalcHiddenParaField() )
331 : {
332 0 : m_pTxtNode->ModifyNotification( 0, 0 );
333 : }
334 0 : return;
335 : }
336 : }
337 :
338 0 : m_aExpand = aNewExpand;
339 :
340 : // 0, this for formatting
341 0 : m_pTxtNode->ModifyNotification( 0, const_cast<SwFmtFld*>( &GetFld() ) );
342 : }
343 :
344 : /*************************************************************************
345 : * SwTxtFld::CopyFld()
346 : *************************************************************************/
347 :
348 0 : void SwTxtFld::CopyFld( SwTxtFld *pDest ) const
349 : {
350 : OSL_ENSURE( m_pTxtNode, "SwTxtFld: where is my TxtNode?" );
351 : OSL_ENSURE( pDest->m_pTxtNode, "SwTxtFld: where is pDest's TxtNode?" );
352 :
353 0 : IDocumentFieldsAccess* pIDFA = m_pTxtNode->getIDocumentFieldsAccess();
354 0 : IDocumentFieldsAccess* pDestIDFA = pDest->m_pTxtNode->getIDocumentFieldsAccess();
355 :
356 0 : SwFmtFld& rFmtFld = (SwFmtFld&)pDest->GetFld();
357 0 : const sal_uInt16 nFldWhich = rFmtFld.GetFld()->GetTyp()->Which();
358 :
359 0 : if( pIDFA != pDestIDFA )
360 : {
361 : // Die Hints stehen in unterschiedlichen Dokumenten,
362 : // der Feldtyp muss im neuen Dokument angemeldet werden.
363 : // Z.B: Kopieren ins ClipBoard.
364 : SwFieldType* pFldType;
365 0 : if( nFldWhich != RES_DBFLD && nFldWhich != RES_USERFLD &&
366 : nFldWhich != RES_SETEXPFLD && nFldWhich != RES_DDEFLD &&
367 : RES_AUTHORITY != nFldWhich )
368 0 : pFldType = pDestIDFA->GetSysFldType( nFldWhich );
369 : else
370 0 : pFldType = pDestIDFA->InsertFldType( *rFmtFld.GetFld()->GetTyp() );
371 :
372 : // Sonderbehandlung fuer DDE-Felder
373 0 : if( RES_DDEFLD == nFldWhich )
374 : {
375 0 : if( rFmtFld.GetTxtFld() )
376 0 : ((SwDDEFieldType*)rFmtFld.GetFld()->GetTyp())->DecRefCnt();
377 0 : ((SwDDEFieldType*)pFldType)->IncRefCnt();
378 : }
379 :
380 : OSL_ENSURE( pFldType, "unbekannter FieldType" );
381 0 : pFldType->Add( &rFmtFld ); // ummelden
382 0 : rFmtFld.GetFld()->ChgTyp( pFldType );
383 : }
384 :
385 : // Expressionfelder Updaten
386 0 : if( nFldWhich == RES_SETEXPFLD || nFldWhich == RES_GETEXPFLD ||
387 : nFldWhich == RES_HIDDENTXTFLD )
388 : {
389 0 : SwTxtFld* pFld = (SwTxtFld*)this;
390 0 : pDestIDFA->UpdateExpFlds( pFld, true );
391 : }
392 : // Tabellenfelder auf externe Darstellung
393 0 : else if( RES_TABLEFLD == nFldWhich &&
394 0 : ((SwTblField*)rFmtFld.GetFld())->IsIntrnlName() )
395 : {
396 : // erzeuge aus der internen (fuer CORE) die externe (fuer UI) Formel
397 0 : const SwTableNode* pTblNd = m_pTxtNode->FindTableNode();
398 0 : if( pTblNd ) // steht in einer Tabelle
399 0 : ((SwTblField*)rFmtFld.GetFld())->PtrToBoxNm( &pTblNd->GetTable() );
400 : }
401 0 : }
402 :
403 0 : void SwTxtFld::NotifyContentChange(SwFmtFld& rFmtFld)
404 : {
405 : //if not in undo section notify the change
406 0 : if (m_pTxtNode && m_pTxtNode->GetNodes().IsDocNodes())
407 : {
408 0 : m_pTxtNode->ModifyNotification(0, &rFmtFld);
409 : }
410 0 : }
411 :
412 :
413 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|