1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 // MARKER(update_precomp.py): autogen include statement, do not remove 25 #include "precompiled_sw.hxx" 26 27 #include "fldbas.hxx" // fuer FieldType 28 #include <fmtfld.hxx> 29 #include <txtfld.hxx> 30 #include <txtannotationfld.hxx> 31 #include <docufld.hxx> 32 #include <doc.hxx> 33 34 #include "reffld.hxx" 35 #include "ddefld.hxx" 36 #include "usrfld.hxx" 37 #include "expfld.hxx" 38 #include "swfont.hxx" // fuer GetFldsColor 39 #include "ndtxt.hxx" // SwTxtNode 40 #include "calc.hxx" // Update fuer UserFields 41 #include "hints.hxx" 42 #include <IDocumentFieldsAccess.hxx> 43 #include <fieldhint.hxx> 44 #include <svl/smplhint.hxx> 45 46 TYPEINIT3( SwFmtFld, SfxPoolItem, SwClient,SfxBroadcaster) 47 TYPEINIT1(SwFmtFldHint, SfxHint); 48 49 /**************************************************************************** 50 * 51 * class SwFmtFld 52 * 53 ****************************************************************************/ 54 55 // constructor for default item in attribute-pool 56 SwFmtFld::SwFmtFld( sal_uInt16 nWhich ) 57 : SfxPoolItem( nWhich ) 58 , SwClient() 59 , SfxBroadcaster() 60 , mpField( NULL ) 61 , mpTxtFld( NULL ) 62 { 63 } 64 65 SwFmtFld::SwFmtFld( const SwField &rFld ) 66 : SfxPoolItem( RES_TXTATR_FIELD ) 67 , SwClient( rFld.GetTyp() ) 68 , SfxBroadcaster() 69 , mpField( rFld.CopyField() ) 70 , mpTxtFld( NULL ) 71 { 72 if ( GetField()->GetTyp()->Which() == RES_INPUTFLD ) 73 { 74 // input field in-place editing 75 SetWhich( RES_TXTATR_INPUTFIELD ); 76 dynamic_cast<SwInputField*>(GetField())->SetFmtFld( *this ); 77 } 78 else if ( GetField()->GetTyp()->Which() == RES_POSTITFLD ) 79 { 80 // text annotation field 81 SetWhich( RES_TXTATR_ANNOTATION ); 82 } 83 } 84 85 // #i24434# 86 // Since Items are used in ItemPool and in default constructed ItemSets with 87 // full pool range, all items need to be clonable. Thus, this one needed to be 88 // corrected 89 SwFmtFld::SwFmtFld( const SwFmtFld& rAttr ) 90 : SfxPoolItem( RES_TXTATR_FIELD ) 91 , SwClient() 92 , SfxBroadcaster() 93 , mpField( NULL ) 94 , mpTxtFld( NULL ) 95 { 96 if ( rAttr.GetField() ) 97 { 98 rAttr.GetField()->GetTyp()->Add(this); 99 mpField = rAttr.GetField()->CopyField(); 100 if ( GetField()->GetTyp()->Which() == RES_INPUTFLD ) 101 { 102 // input field in-place editing 103 SetWhich( RES_TXTATR_INPUTFIELD ); 104 dynamic_cast<SwInputField*>(GetField())->SetFmtFld( *this ); 105 } 106 else if ( GetField()->GetTyp()->Which() == RES_POSTITFLD ) 107 { 108 // text annotation field 109 SetWhich( RES_TXTATR_ANNOTATION ); 110 } 111 } 112 } 113 114 SwFmtFld::~SwFmtFld() 115 { 116 SwFieldType* pType = mpField ? mpField->GetTyp() : 0; 117 118 if (pType && pType->Which() == RES_DBFLD) 119 pType = 0; // DB-Feldtypen zerstoeren sich selbst 120 121 Broadcast( SwFmtFldHint( this, SWFMTFLD_REMOVED ) ); 122 delete mpField; 123 124 // bei einige FeldTypen muessen wir den FeldTypen noch loeschen 125 if( pType && pType->IsLastDepend() ) 126 { 127 sal_Bool bDel = sal_False; 128 switch( pType->Which() ) 129 { 130 case RES_USERFLD: 131 bDel = ((SwUserFieldType*)pType)->IsDeleted(); 132 break; 133 134 case RES_SETEXPFLD: 135 bDel = ((SwSetExpFieldType*)pType)->IsDeleted(); 136 break; 137 138 case RES_DDEFLD: 139 bDel = ((SwDDEFieldType*)pType)->IsDeleted(); 140 break; 141 } 142 143 if( bDel ) 144 { 145 // vorm loeschen erstmal austragen 146 pType->Remove( this ); 147 delete pType; 148 } 149 } 150 } 151 152 void SwFmtFld::RegisterToFieldType( SwFieldType& rType ) 153 { 154 rType.Add(this); 155 } 156 157 158 // #111840# 159 void SwFmtFld::SetField(SwField * _pField) 160 { 161 if (NULL != mpField) 162 delete mpField; 163 164 mpField = _pField; 165 if ( GetField()->GetTyp()->Which() == RES_INPUTFLD ) 166 { 167 dynamic_cast<SwInputField* >(GetField())->SetFmtFld( *this ); 168 } 169 Broadcast( SwFmtFldHint( this, SWFMTFLD_CHANGED ) ); 170 } 171 172 void SwFmtFld::SetTxtFld( SwTxtFld& rTxtFld ) 173 { 174 mpTxtFld = &rTxtFld; 175 } 176 177 void SwFmtFld::ClearTxtFld() 178 { 179 mpTxtFld = NULL; 180 } 181 182 int SwFmtFld::operator==( const SfxPoolItem& rAttr ) const 183 { 184 ASSERT( SfxPoolItem::operator==( rAttr ), "keine gleichen Attribute" ); 185 return ( ( mpField && ((SwFmtFld&)rAttr).GetField() 186 && mpField->GetTyp() == ((SwFmtFld&)rAttr).GetField()->GetTyp() 187 && mpField->GetFormat() == ((SwFmtFld&)rAttr).GetField()->GetFormat() ) ) 188 || ( !mpField && !((SwFmtFld&)rAttr).GetField() ); 189 } 190 191 SfxPoolItem* SwFmtFld::Clone( SfxItemPool* ) const 192 { 193 return new SwFmtFld( *this ); 194 } 195 196 void SwFmtFld::SwClientNotify( const SwModify&, const SfxHint& rHint ) 197 { 198 if( !mpTxtFld ) 199 return; 200 201 const SwFieldHint* pHint = dynamic_cast<const SwFieldHint*>( &rHint ); 202 if ( pHint ) 203 { 204 // replace field content by text 205 SwPaM* pPaM = pHint->GetPaM(); 206 SwDoc* pDoc = pPaM->GetDoc(); 207 const SwTxtNode& rTxtNode = mpTxtFld->GetTxtNode(); 208 pPaM->GetPoint()->nNode = rTxtNode; 209 pPaM->GetPoint()->nContent.Assign( (SwTxtNode*)&rTxtNode, *mpTxtFld->GetStart() ); 210 211 String const aEntry( GetField()->ExpandField( pDoc->IsClipBoard() ) ); 212 pPaM->SetMark(); 213 pPaM->Move( fnMoveForward ); 214 pDoc->DeleteRange( *pPaM ); 215 pDoc->InsertString( *pPaM, aEntry ); 216 } 217 } 218 219 void SwFmtFld::Modify( const SfxPoolItem* pOld, const SfxPoolItem* pNew ) 220 { 221 if( !mpTxtFld ) 222 return; 223 224 // don't do anything, especially not expand! 225 if( pNew && pNew->Which() == RES_OBJECTDYING ) 226 return; 227 228 SwTxtNode* pTxtNd = (SwTxtNode*)&mpTxtFld->GetTxtNode(); 229 ASSERT( pTxtNd, "wo ist denn mein Node?" ); 230 if( pNew ) 231 { 232 switch( pNew->Which() ) 233 { 234 case RES_TXTATR_FLDCHG: 235 // "Farbe hat sich geaendert !" 236 // this, this fuer "nur Painten" 237 pTxtNd->ModifyNotification( this, this ); 238 return; 239 case RES_REFMARKFLD_UPDATE: 240 // GetReferenz-Felder aktualisieren 241 if( RES_GETREFFLD == GetField()->GetTyp()->Which() ) 242 { 243 // --> OD 2007-09-06 #i81002# 244 // ((SwGetRefField*)GetFld())->UpdateField(); 245 dynamic_cast<SwGetRefField*>(GetField())->UpdateField( mpTxtFld ); 246 // <-- 247 } 248 break; 249 case RES_DOCPOS_UPDATE: 250 // Je nach DocPos aktualisieren (SwTxtFrm::Modify()) 251 pTxtNd->ModifyNotification( pNew, this ); 252 return; 253 254 case RES_ATTRSET_CHG: 255 case RES_FMT_CHG: 256 pTxtNd->ModifyNotification( pOld, pNew ); 257 return; 258 default: 259 break; 260 } 261 } 262 263 switch (GetField()->GetTyp()->Which()) 264 { 265 case RES_HIDDENPARAFLD: 266 if( !pOld || RES_HIDDENPARA_PRINT != pOld->Which() ) 267 break; 268 case RES_DBSETNUMBERFLD: 269 case RES_DBNUMSETFLD: 270 case RES_DBNEXTSETFLD: 271 case RES_DBNAMEFLD: 272 pTxtNd->ModifyNotification( 0, pNew); 273 return; 274 } 275 276 if( RES_USERFLD == GetField()->GetTyp()->Which() ) 277 { 278 SwUserFieldType* pType = (SwUserFieldType*)GetField()->GetTyp(); 279 if(!pType->IsValid()) 280 { 281 SwCalc aCalc( *pTxtNd->GetDoc() ); 282 pType->GetValue( aCalc ); 283 } 284 } 285 mpTxtFld->ExpandTxtFld(); 286 } 287 288 sal_Bool SwFmtFld::GetInfo( SfxPoolItem& rInfo ) const 289 { 290 const SwTxtNode* pTxtNd; 291 if( RES_AUTOFMT_DOCNODE != rInfo.Which() || 292 !mpTxtFld || 0 == ( pTxtNd = mpTxtFld->GetpTxtNode() ) || 293 &pTxtNd->GetNodes() != ((SwAutoFmtGetDocNode&)rInfo).pNodes ) 294 return sal_True; 295 296 ((SwAutoFmtGetDocNode&)rInfo).pCntntNode = pTxtNd; 297 return sal_False; 298 } 299 300 301 bool SwFmtFld::IsFldInDoc() const 302 { 303 return mpTxtFld != NULL 304 && mpTxtFld->IsFldInDoc(); 305 } 306 307 sal_Bool SwFmtFld::IsProtect() const 308 { 309 return mpTxtFld != NULL 310 && mpTxtFld->GetpTxtNode() != NULL 311 && mpTxtFld->GetpTxtNode()->IsProtect(); 312 } 313 314 315 316 317 SwTxtFld::SwTxtFld( 318 SwFmtFld & rAttr, 319 xub_StrLen const nStartPos ) 320 : SwTxtAttr( rAttr, nStartPos ) 321 , m_aExpand( rAttr.GetField()->ExpandField(true) ) 322 , m_pTxtNode( NULL ) 323 { 324 rAttr.SetTxtFld( *this ); 325 SetHasDummyChar(true); 326 } 327 328 SwTxtFld::~SwTxtFld( ) 329 { 330 SwFmtFld & rFmtFld( static_cast<SwFmtFld &>(GetAttr()) ); 331 if ( this == rFmtFld.GetTxtFld() ) 332 { 333 rFmtFld.ClearTxtFld(); 334 } 335 } 336 337 338 bool SwTxtFld::IsFldInDoc() const 339 { 340 return GetpTxtNode() != NULL 341 && GetpTxtNode()->GetNodes().IsDocNodes(); 342 } 343 344 void SwTxtFld::ExpandTxtFld() const 345 { 346 ASSERT( m_pTxtNode, "SwTxtFld: where is my TxtNode?" ); 347 348 const SwField* pFld = GetFmtFld().GetField(); 349 const XubString aNewExpand( pFld->ExpandField(m_pTxtNode->GetDoc()->IsClipBoard()) ); 350 351 if( aNewExpand == m_aExpand ) 352 { 353 // Bei Seitennummernfeldern 354 const sal_uInt16 nWhich = pFld->GetTyp()->Which(); 355 if ( RES_CHAPTERFLD != nWhich 356 && RES_PAGENUMBERFLD != nWhich 357 && RES_REFPAGEGETFLD != nWhich 358 // Page count fields to not use aExpand during formatting, 359 // therefore an invalidation of the text frame has to be triggered even if aNewExpand == aExpand: 360 && ( RES_DOCSTATFLD != nWhich || DS_PAGE != static_cast<const SwDocStatField*>(pFld)->GetSubType() ) 361 && ( RES_GETEXPFLD != nWhich || ((SwGetExpField*)pFld)->IsInBodyTxt() ) ) 362 { 363 if( m_pTxtNode->CalcHiddenParaField() ) 364 { 365 m_pTxtNode->ModifyNotification( 0, 0 ); 366 } 367 return; 368 } 369 } 370 371 m_aExpand = aNewExpand; 372 373 const_cast<SwTxtFld*>(this)->NotifyContentChange( const_cast<SwFmtFld&>(GetFmtFld()) ); 374 } 375 376 377 void SwTxtFld::CopyTxtFld( SwTxtFld *pDest ) const 378 { 379 ASSERT( m_pTxtNode, "SwTxtFld: where is my TxtNode?" ); 380 ASSERT( pDest->m_pTxtNode, "SwTxtFld: where is pDest's TxtNode?" ); 381 382 IDocumentFieldsAccess* pIDFA = m_pTxtNode->getIDocumentFieldsAccess(); 383 IDocumentFieldsAccess* pDestIDFA = pDest->m_pTxtNode->getIDocumentFieldsAccess(); 384 385 SwFmtFld& rDestFmtFld = (SwFmtFld&)pDest->GetFmtFld(); 386 const sal_uInt16 nFldWhich = rDestFmtFld.GetField()->GetTyp()->Which(); 387 388 if( pIDFA != pDestIDFA ) 389 { 390 // Die Hints stehen in unterschiedlichen Dokumenten, 391 // der Feldtyp muss im neuen Dokument angemeldet werden. 392 // Z.B: Kopieren ins ClipBoard. 393 SwFieldType* pFldType; 394 if( nFldWhich != RES_DBFLD 395 && nFldWhich != RES_USERFLD 396 && nFldWhich != RES_SETEXPFLD 397 && nFldWhich != RES_DDEFLD 398 && RES_AUTHORITY != nFldWhich ) 399 { 400 pFldType = pDestIDFA->GetSysFldType( nFldWhich ); 401 } 402 else 403 { 404 pFldType = pDestIDFA->InsertFldType( *rDestFmtFld.GetField()->GetTyp() ); 405 } 406 407 // Sonderbehandlung fuer DDE-Felder 408 if( RES_DDEFLD == nFldWhich ) 409 { 410 if( rDestFmtFld.GetTxtFld() ) 411 { 412 ((SwDDEFieldType*)rDestFmtFld.GetField()->GetTyp())->DecRefCnt(); 413 } 414 ((SwDDEFieldType*)pFldType)->IncRefCnt(); 415 } 416 417 ASSERT( pFldType, "unbekannter FieldType" ); 418 pFldType->Add( &rDestFmtFld ); // ummelden 419 rDestFmtFld.GetField()->ChgTyp( pFldType ); 420 } 421 422 // Expressionfelder Updaten 423 if( nFldWhich == RES_SETEXPFLD 424 || nFldWhich == RES_GETEXPFLD 425 || nFldWhich == RES_HIDDENTXTFLD ) 426 { 427 SwTxtFld* pFld = (SwTxtFld*)this; 428 pDestIDFA->UpdateExpFlds( pFld, true ); 429 } 430 // Tabellenfelder auf externe Darstellung 431 else if( RES_TABLEFLD == nFldWhich 432 && ((SwTblField*)rDestFmtFld.GetField())->IsIntrnlName() ) 433 { 434 // erzeuge aus der internen (fuer CORE) die externe (fuer UI) Formel 435 const SwTableNode* pTblNd = m_pTxtNode->FindTableNode(); 436 if( pTblNd ) // steht in einer Tabelle 437 ((SwTblField*)rDestFmtFld.GetField())->PtrToBoxNm( &pTblNd->GetTable() ); 438 } 439 } 440 441 442 void SwTxtFld::NotifyContentChange(SwFmtFld& rFmtFld) 443 { 444 //if not in undo section notify the change 445 if (m_pTxtNode && m_pTxtNode->GetNodes().IsDocNodes()) 446 { 447 m_pTxtNode->ModifyNotification(0, &rFmtFld); 448 } 449 } 450 451 452 /*static*/ 453 void SwTxtFld::GetPamForTxtFld( 454 const SwTxtFld& rTxtFld, 455 boost::shared_ptr< SwPaM >& rPamForTxtFld ) 456 { 457 if ( rTxtFld.GetpTxtNode() == NULL ) 458 { 459 ASSERT( false, "<SwTxtFld::GetPamForField> - missing <SwTxtNode>" ); 460 return; 461 } 462 463 const SwTxtNode& rTxtNode = rTxtFld.GetTxtNode(); 464 465 rPamForTxtFld.reset( new SwPaM( rTxtNode, 466 ( (rTxtFld.End() != NULL) ? *(rTxtFld.End()) : ( *(rTxtFld.GetStart()) + 1 ) ), 467 rTxtNode, 468 *(rTxtFld.GetStart()) ) ); 469 470 } 471 472 473 /*static*/ 474 void SwTxtFld::DeleteTxtFld( const SwTxtFld& rTxtFld ) 475 { 476 if ( rTxtFld.GetpTxtNode() != NULL ) 477 { 478 boost::shared_ptr< SwPaM > pPamForTxtFld; 479 GetPamForTxtFld( rTxtFld, pPamForTxtFld ); 480 if ( pPamForTxtFld.get() != NULL ) 481 { 482 rTxtFld.GetTxtNode().GetDoc()->DeleteAndJoin( *pPamForTxtFld ); 483 } 484 } 485 } 486 487 488 489 // input field in-place editing 490 SwTxtInputFld::SwTxtInputFld( 491 SwFmtFld & rAttr, 492 xub_StrLen const nStart, 493 xub_StrLen const nEnd ) 494 495 : SwTxtFld( rAttr, nStart ) 496 , m_nEnd( nEnd ) 497 , m_bLockNotifyContentChange( false ) 498 { 499 SetHasDummyChar( false ); 500 SetHasContent( true ); 501 502 SetDontExpand( true ); 503 SetLockExpandFlag( true ); 504 SetDontExpandStartAttr( true ); 505 506 SetNesting( true ); 507 } 508 509 SwTxtInputFld::~SwTxtInputFld() 510 { 511 } 512 513 xub_StrLen* SwTxtInputFld::GetEnd() 514 { 515 return &m_nEnd; 516 } 517 518 519 void SwTxtInputFld::LockNotifyContentChange() 520 { 521 m_bLockNotifyContentChange = true; 522 } 523 524 525 void SwTxtInputFld::UnlockNotifyContentChange() 526 { 527 m_bLockNotifyContentChange = false; 528 } 529 530 531 void SwTxtInputFld::NotifyContentChange( SwFmtFld& rFmtFld ) 532 { 533 if ( !m_bLockNotifyContentChange ) 534 { 535 LockNotifyContentChange(); 536 537 SwTxtFld::NotifyContentChange( rFmtFld ); 538 UpdateTextNodeContent( GetFieldContent() ); 539 540 UnlockNotifyContentChange(); 541 } 542 } 543 544 const String SwTxtInputFld::GetFieldContent() const 545 { 546 return GetFmtFld().GetField()->ExpandField(false); 547 } 548 549 void SwTxtInputFld::UpdateFieldContent() 550 { 551 if ( IsFldInDoc() 552 && (*GetStart()) != (*End()) ) 553 { 554 ASSERT( (*End()) - (*GetStart()) >= 2, 555 "<SwTxtInputFld::UpdateFieldContent()> - Are CH_TXT_ATR_INPUTFIELDSTART and/or CH_TXT_ATR_INPUTFIELDEND missing?" ); 556 // skip CH_TXT_ATR_INPUTFIELDSTART character 557 const xub_StrLen nIdx = (*GetStart()) + 1; 558 // skip CH_TXT_ATR_INPUTFIELDEND character 559 const xub_StrLen nLen = static_cast<xub_StrLen>(std::max( 0, ( (*End()) - 1 - nIdx ) )); 560 const String aNewFieldContent = GetTxtNode().GetExpandTxt( nIdx, nLen ); 561 562 const SwInputField* pInputFld = dynamic_cast<const SwInputField*>(GetFmtFld().GetField()); 563 ASSERT( pInputFld != NULL, 564 "<SwTxtInputFld::GetContent()> - Missing <SwInputFld> instance!" ); 565 if ( pInputFld != NULL ) 566 { 567 const_cast<SwInputField*>(pInputFld)->applyFieldContent( aNewFieldContent ); 568 } 569 } 570 } 571 572 void SwTxtInputFld::UpdateTextNodeContent( const String& rNewContent ) 573 { 574 if ( !IsFldInDoc() ) 575 { 576 ASSERT( false, "<SwTxtInputFld::UpdateTextNodeContent(..)> - misusage as Input Field is not in document content." ); 577 return; 578 } 579 580 ASSERT( (*End()) - (*GetStart()) >= 2, 581 "<SwTxtInputFld::UpdateTextNodeContent(..)> - Are CH_TXT_ATR_INPUTFIELDSTART and/or CH_TXT_ATR_INPUTFIELDEND missing?" ); 582 // skip CH_TXT_ATR_INPUTFIELDSTART character 583 const xub_StrLen nIdx = (*GetStart()) + 1; 584 // skip CH_TXT_ATR_INPUTFIELDEND character 585 const xub_StrLen nDelLen = static_cast<xub_StrLen>(std::max( 0, ( (*End()) - 1 - nIdx ) )); 586 SwIndex aIdx( &GetTxtNode(), nIdx ); 587 GetTxtNode().ReplaceText( aIdx, nDelLen, rNewContent ); 588 } 589 590 591 592 593 // text annotation field 594 SwTxtAnnotationFld::SwTxtAnnotationFld( 595 SwFmtFld & rAttr, 596 xub_StrLen const nStart ) 597 : SwTxtFld( rAttr, nStart ) 598 { 599 } 600 601 SwTxtAnnotationFld::~SwTxtAnnotationFld() 602 { 603 } 604 605 606 ::sw::mark::IMark* SwTxtAnnotationFld::GetAnnotationMark( 607 SwDoc* pDoc ) const 608 { 609 const SwPostItField* pPostItField = dynamic_cast<const SwPostItField*>(GetFmtFld().GetField()); 610 ASSERT( pPostItField != NULL, "<SwTxtAnnotationFld::GetAnnotationMark()> - field missing" ); 611 if ( pPostItField == NULL ) 612 { 613 return NULL; 614 } 615 616 if ( pDoc == NULL ) 617 { 618 pDoc = static_cast<const SwPostItFieldType*>(pPostItField->GetTyp())->GetDoc(); 619 } 620 ASSERT( pDoc != NULL, "<SwTxtAnnotationFld::GetAnnotationMark()> - missing document" ); 621 if ( pDoc == NULL ) 622 { 623 return NULL; 624 } 625 626 IDocumentMarkAccess* pMarksAccess = pDoc->getIDocumentMarkAccess(); 627 IDocumentMarkAccess::const_iterator_t pMark = pMarksAccess->findAnnotationMark( pPostItField->GetName() ); 628 return pMark != pMarksAccess->getAnnotationMarksEnd() 629 ? pMark->get() 630 : NULL; 631 } 632 633