LCOV - code coverage report
Current view: top level - sw/source/core/edit - edfld.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 221 0.0 %
Date: 2014-04-14 Functions: 0 25 0.0 %
Legend: Lines: hit not hit

          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 <unotools/charclass.hxx>
      21             : #include <editsh.hxx>
      22             : #include <fldbas.hxx>
      23             : #include <doc.hxx>
      24             : #include <docary.hxx>
      25             : #include <fmtfld.hxx>
      26             : #include <txtfld.hxx>
      27             : #include <edimp.hxx>
      28             : #include <dbfld.hxx>
      29             : #include <expfld.hxx>
      30             : #include <flddat.hxx>
      31             : #include <swundo.hxx>
      32             : #include <dbmgr.hxx>
      33             : #include <swddetbl.hxx>
      34             : #include <hints.hxx>
      35             : #include <switerator.hxx>
      36             : #include <fieldhint.hxx>
      37             : 
      38             : /// count field types with a ResId, if 0 count all
      39           0 : sal_uInt16 SwEditShell::GetFldTypeCount(sal_uInt16 nResId, sal_Bool bUsed ) const
      40             : {
      41           0 :     const SwFldTypes* pFldTypes = GetDoc()->GetFldTypes();
      42           0 :     const sal_uInt16 nSize = pFldTypes->size();
      43             : 
      44           0 :     if(nResId == USHRT_MAX)
      45             :     {
      46           0 :         if(!bUsed)
      47           0 :             return nSize;
      48             :         else
      49             :         {
      50           0 :             sal_uInt16 nUsed = 0;
      51           0 :             for ( sal_uInt16 i = 0; i < nSize; i++ )
      52             :             {
      53           0 :                 if(IsUsed(*(*pFldTypes)[i]))
      54           0 :                     nUsed++;
      55             :             }
      56           0 :             return nUsed;
      57             :         }
      58             :     }
      59             : 
      60             :     // all types with the same ResId
      61           0 :     sal_uInt16 nIdx  = 0;
      62           0 :     for(sal_uInt16 i = 0; i < nSize; ++i)
      63             :     {   // same ResId -> increment index
      64           0 :         SwFieldType& rFldType = *((*pFldTypes)[i]);
      65           0 :         if(rFldType.Which() == nResId)
      66           0 :             nIdx++;
      67             :     }
      68           0 :     return nIdx;
      69             : }
      70             : 
      71             : /// get field types with a ResId, if 0 get all
      72           0 : SwFieldType* SwEditShell::GetFldType(sal_uInt16 nFld, sal_uInt16 nResId, sal_Bool bUsed ) const
      73             : {
      74           0 :     const SwFldTypes* pFldTypes = GetDoc()->GetFldTypes();
      75           0 :     const sal_uInt16 nSize = pFldTypes->size();
      76             : 
      77           0 :     if(nResId == USHRT_MAX && nFld < nSize)
      78             :     {
      79           0 :         if(!bUsed)
      80           0 :             return (*pFldTypes)[nFld];
      81             :         else
      82             :         {
      83           0 :             sal_uInt16 i, nUsed = 0;
      84           0 :             for ( i = 0; i < nSize; i++ )
      85             :             {
      86           0 :                 if(IsUsed(*(*pFldTypes)[i]))
      87             :                 {
      88           0 :                     if(nUsed == nFld)
      89           0 :                         break;
      90           0 :                     nUsed++;
      91             :                 }
      92             :             }
      93           0 :             return i < nSize ? (*pFldTypes)[i] : 0;
      94             :         }
      95             :     }
      96             : 
      97           0 :     sal_uInt16 nIdx = 0;
      98           0 :     for(sal_uInt16 i = 0; i < nSize; ++i)
      99             :     {   // same ResId -> increment index
     100           0 :         SwFieldType* pFldType = (*pFldTypes)[i];
     101           0 :         if(pFldType->Which() == nResId)
     102             :         {
     103           0 :             if (!bUsed || IsUsed(*pFldType))
     104             :             {
     105           0 :                 if(nIdx == nFld)
     106           0 :                     return pFldType;
     107           0 :                 nIdx++;
     108             :             }
     109             :         }
     110             :     }
     111           0 :     return 0;
     112             : }
     113             : 
     114             : /// get first type with given ResId and name
     115           0 : SwFieldType* SwEditShell::GetFldType(sal_uInt16 nResId, const OUString& rName) const
     116             : {
     117           0 :     return GetDoc()->GetFldType( nResId, rName, false );
     118             : }
     119             : 
     120             : /// delete field type
     121           0 : void SwEditShell::RemoveFldType(sal_uInt16 nFld, sal_uInt16 nResId)
     122             : {
     123           0 :     if( USHRT_MAX == nResId )
     124             :     {
     125           0 :         GetDoc()->RemoveFldType(nFld);
     126           0 :         return;
     127             :     }
     128             : 
     129           0 :     const SwFldTypes* pFldTypes = GetDoc()->GetFldTypes();
     130           0 :     const sal_uInt16 nSize = pFldTypes->size();
     131           0 :     sal_uInt16 nIdx = 0;
     132           0 :     for( sal_uInt16 i = 0; i < nSize; ++i )
     133             :         // Gleiche ResId -> Index erhoehen
     134           0 :         if( (*pFldTypes)[i]->Which() == nResId &&
     135           0 :             nIdx++ == nFld )
     136             :         {
     137           0 :             GetDoc()->RemoveFldType( i );
     138           0 :             return;
     139             :         }
     140             : }
     141             : 
     142             : /// delete field type based on its name
     143           0 : void SwEditShell::RemoveFldType(sal_uInt16 nResId, const OUString& rStr)
     144             : {
     145           0 :     const SwFldTypes* pFldTypes = GetDoc()->GetFldTypes();
     146           0 :     const sal_uInt16 nSize = pFldTypes->size();
     147           0 :     const CharClass& rCC = GetAppCharClass();
     148             : 
     149           0 :     OUString aTmp( rCC.lowercase( rStr ));
     150             : 
     151           0 :     for(sal_uInt16 i = 0; i < nSize; ++i)
     152             :     {
     153             :         // same ResId -> increment index
     154           0 :         SwFieldType* pFldType = (*pFldTypes)[i];
     155           0 :         if( pFldType->Which() == nResId )
     156             :         {
     157           0 :             if( aTmp == rCC.lowercase( pFldType->GetName() ) )
     158             :             {
     159           0 :                 GetDoc()->RemoveFldType(i);
     160           0 :                 return;
     161             :             }
     162             :         }
     163           0 :     }
     164             : }
     165             : 
     166           0 : void SwEditShell::FieldToText( SwFieldType* pType )
     167             : {
     168           0 :     if( !pType->GetDepends() )
     169           0 :         return;
     170             : 
     171           0 :     SET_CURR_SHELL( this );
     172           0 :     StartAllAction();
     173           0 :     StartUndo( UNDO_DELETE );
     174           0 :     Push();
     175           0 :     SwPaM* pPaM = GetCrsr();
     176             : 
     177           0 :     SwFieldHint aHint( pPaM );
     178           0 :     SwClientIter aIter( *pType );
     179           0 :     for ( SwClient* pClient = aIter.GoStart(); pClient; pClient = ++aIter )
     180             :     {
     181           0 :         pPaM->DeleteMark();
     182           0 :         pClient->SwClientNotifyCall( *pType, aHint );
     183             :      }
     184             : 
     185           0 :     Pop( sal_False );
     186           0 :     EndAllAction();
     187           0 :     EndUndo( UNDO_DELETE );
     188             : }
     189             : 
     190             : /// add a field at the cursor position
     191           0 : void SwEditShell::Insert2(SwField& rFld, const bool bForceExpandHints)
     192             : {
     193           0 :     SET_CURR_SHELL( this );
     194           0 :     StartAllAction();
     195           0 :     SwFmtFld aFld( rFld );
     196             : 
     197             :     const SetAttrMode nInsertFlags = (bForceExpandHints)
     198             :         ? nsSetAttrMode::SETATTR_FORCEHINTEXPAND
     199           0 :         : nsSetAttrMode::SETATTR_DEFAULT;
     200             : 
     201           0 :     FOREACHPAM_START(GetCrsr()) // for each PaM
     202           0 :         const bool bSuccess(GetDoc()->InsertPoolItem(*PCURCRSR, aFld, nInsertFlags));
     203             :         OSL_ENSURE( bSuccess, "Doc->Insert(Field) failed");
     204             :         (void) bSuccess;
     205           0 :     FOREACHPAM_END()
     206             : 
     207           0 :     EndAllAction();
     208           0 : }
     209             : 
     210             : /// Are the PaMs positioned on fields?
     211           0 : static SwTxtFld* lcl_FindInputFld( SwDoc* pDoc, SwField& rFld )
     212             : {
     213             :     // Search field via its address. For input fields this needs to be done in protected fields.
     214           0 :     SwTxtFld* pTFld = 0;
     215           0 :     if( RES_INPUTFLD == rFld.Which() )
     216             :     {
     217           0 :         const SfxPoolItem* pItem = NULL;
     218             :         const sal_uInt32 nMaxItems =
     219           0 :             pDoc->GetAttrPool().GetItemCount2( RES_TXTATR_INPUTFIELD );
     220           0 :         for( sal_uInt32 n = 0; n < nMaxItems; ++n )
     221           0 :             if( 0 != (pItem = pDoc->GetAttrPool().GetItem2( RES_TXTATR_INPUTFIELD, n ) )
     222           0 :                 && ((SwFmtFld*)pItem)->GetField() == &rFld )
     223             :             {
     224           0 :                 pTFld = ((SwFmtFld*)pItem)->GetTxtFld();
     225           0 :                 break;
     226             :             }
     227             :     }
     228           0 :     else if( RES_SETEXPFLD == rFld.Which()
     229           0 :         && ((SwSetExpField&)rFld).GetInputFlag() )
     230             :     {
     231           0 :         const SfxPoolItem* pItem = NULL;
     232             :         const sal_uInt32 nMaxItems =
     233           0 :             pDoc->GetAttrPool().GetItemCount2( RES_TXTATR_FIELD );
     234           0 :         for( sal_uInt32 n = 0; n < nMaxItems; ++n )
     235           0 :             if( 0 != (pItem = pDoc->GetAttrPool().GetItem2( RES_TXTATR_FIELD, n ) )
     236           0 :                 && ((SwFmtFld*)pItem)->GetField() == &rFld )
     237             :             {
     238           0 :                 pTFld = ((SwFmtFld*)pItem)->GetTxtFld();
     239           0 :                 break;
     240             :             }
     241             :     }
     242           0 :     return pTFld;
     243             : }
     244             : 
     245           0 : void SwEditShell::UpdateFlds( SwField &rFld )
     246             : {
     247           0 :     SET_CURR_SHELL( this );
     248           0 :     StartAllAction();
     249             :     {
     250             :         // // If there are no selections so take the value of the current cursor position.
     251           0 :         SwMsgPoolItem* pMsgHnt = 0;
     252           0 :         SwRefMarkFldUpdate aRefMkHt( GetOut() );
     253           0 :         sal_uInt16 nFldWhich = rFld.GetTyp()->Which();
     254           0 :         if( RES_GETREFFLD == nFldWhich )
     255           0 :             pMsgHnt = &aRefMkHt;
     256             : 
     257           0 :         SwPaM* pCrsr = GetCrsr();
     258             :         SwTxtFld *pTxtFld;
     259             :         SwFmtFld *pFmtFld;
     260             : 
     261           0 :         if ( pCrsr->GetNext() == pCrsr && !pCrsr->HasMark())
     262             :         {
     263           0 :             pTxtFld = GetTxtFldAtPos( pCrsr->Start(), true );
     264             : 
     265           0 :             if (!pTxtFld) // #i30221#
     266           0 :                 pTxtFld = lcl_FindInputFld( GetDoc(), rFld);
     267             : 
     268           0 :             if (pTxtFld != 0)
     269           0 :                 GetDoc()->UpdateFld(pTxtFld, rFld, pMsgHnt, true);
     270             :         }
     271             : 
     272             :         // bOkay (instead of return because of EndAllAction) becomes false,
     273             :         // 1) if only one PaM has more than one field or
     274             :         // 2) if there are mixed field types
     275           0 :         bool bOkay = true;
     276           0 :         sal_Bool bTblSelBreak = sal_False;
     277             : 
     278           0 :         SwMsgPoolItem aFldHint( RES_TXTATR_FIELD );  // Search-Hint
     279           0 :         SwMsgPoolItem aAnnotationFldHint( RES_TXTATR_ANNOTATION );
     280           0 :         SwMsgPoolItem aInputFldHint( RES_TXTATR_INPUTFIELD );
     281           0 :         FOREACHPAM_START(GetCrsr())               // for each PaM
     282           0 :             if( PCURCRSR->HasMark() && bOkay )    // ... with selection
     283             :             {
     284             :                 // copy of the PaM
     285           0 :                 SwPaM aCurPam( *PCURCRSR->GetMark(), *PCURCRSR->GetPoint() );
     286           0 :                 SwPaM aPam( *PCURCRSR->GetPoint() );
     287             : 
     288           0 :                 SwPosition *pCurStt = aCurPam.Start(), *pCurEnd =
     289           0 :                     aCurPam.End();
     290             :                 /*
     291             :                  * In case that there are two contiguous fields in a PaM, the aPam goes step by step
     292             :                  * to the end. aCurPam is reduced in each loop. If aCurPam was searched completely,
     293             :                  * the loop terminates because Start = End.
     294             :                  */
     295             : 
     296             :                 // Search for SwTxtFld ...
     297           0 :                 while(  bOkay
     298           0 :                      && pCurStt->nContent != pCurEnd->nContent
     299           0 :                      && ( aPam.Find( aFldHint, false, fnMoveForward, &aCurPam )
     300           0 :                           || aPam.Find( aAnnotationFldHint, false, fnMoveForward, &aCurPam )
     301           0 :                           || aPam.Find( aInputFldHint, false, fnMoveForward, &aCurPam ) ) )
     302             :                 {
     303             :                     // if only one PaM has more than one field  ...
     304           0 :                     if( aPam.Start()->nContent != pCurStt->nContent )
     305           0 :                         bOkay = false;
     306             : 
     307           0 :                     if( 0 != (pTxtFld = GetTxtFldAtPos( pCurStt, true )) )
     308             :                     {
     309           0 :                         pFmtFld = (SwFmtFld*)&pTxtFld->GetFmtFld();
     310           0 :                         SwField *pCurFld = pFmtFld->GetField();
     311             : 
     312             :                         // if there are mixed field types
     313           0 :                         if( pCurFld->GetTyp()->Which() !=
     314           0 :                             rFld.GetTyp()->Which() )
     315           0 :                             bOkay = false;
     316             : 
     317           0 :                         bTblSelBreak = GetDoc()->UpdateFld(pTxtFld, rFld,
     318           0 :                                                            pMsgHnt, false);
     319             :                     }
     320             :                     // The search area is reduced by the found area:
     321           0 :                     pCurStt->nContent++;
     322           0 :                 }
     323             :             }
     324             : 
     325           0 :             if( bTblSelBreak ) // If table section and table formula are updated -> finish
     326           0 :                 break;
     327             : 
     328           0 :         FOREACHPAM_END()
     329             :     }
     330           0 :     GetDoc()->SetModified();
     331           0 :     EndAllAction();
     332           0 : }
     333             : 
     334           0 : SwDBData SwEditShell::GetDBData() const
     335             : {
     336           0 :     return GetDoc()->GetDBData();
     337             : }
     338             : 
     339           0 : const SwDBData& SwEditShell::GetDBDesc() const
     340             : {
     341           0 :     return GetDoc()->GetDBDesc();
     342             : }
     343             : 
     344           0 : void SwEditShell::ChgDBData(const SwDBData& rNewData)
     345             : {
     346           0 :     GetDoc()->ChgDBData(rNewData);
     347           0 : }
     348             : 
     349           0 : void SwEditShell::GetAllUsedDB( std::vector<OUString>& rDBNameList,
     350             :                                 std::vector<OUString>* pAllDBNames )
     351             : {
     352           0 :     GetDoc()->GetAllUsedDB( rDBNameList, pAllDBNames );
     353           0 : }
     354             : 
     355           0 : void SwEditShell::ChangeDBFields( const std::vector<OUString>& rOldNames,
     356             :                                   const OUString& rNewName )
     357             : {
     358           0 :     GetDoc()->ChangeDBFields( rOldNames, rNewName );
     359           0 : }
     360             : 
     361             : /// Update all expression fields
     362           0 : void SwEditShell::UpdateExpFlds(sal_Bool bCloseDB)
     363             : {
     364           0 :     SET_CURR_SHELL( this );
     365           0 :     StartAllAction();
     366           0 :     GetDoc()->UpdateExpFlds(NULL, true);
     367           0 :     if (bCloseDB)
     368           0 :         GetDoc()->GetNewDBMgr()->CloseAll(); // close all database connections
     369           0 :     EndAllAction();
     370           0 : }
     371             : 
     372           0 : SwNewDBMgr* SwEditShell::GetNewDBMgr() const
     373             : {
     374           0 :     return GetDoc()->GetNewDBMgr();
     375             : }
     376             : 
     377             : /// insert field type
     378           0 : SwFieldType* SwEditShell::InsertFldType(const SwFieldType& rFldType)
     379             : {
     380           0 :     return GetDoc()->InsertFldType(rFldType);
     381             : }
     382             : 
     383           0 : void SwEditShell::LockExpFlds()
     384             : {
     385           0 :     GetDoc()->LockExpFlds();
     386           0 : }
     387             : 
     388           0 : void SwEditShell::UnlockExpFlds()
     389             : {
     390           0 :     GetDoc()->UnlockExpFlds();
     391           0 : }
     392             : 
     393           0 : void SwEditShell::SetFldUpdateFlags( SwFldUpdateFlags eFlags )
     394             : {
     395           0 :     getIDocumentSettingAccess()->setFieldUpdateFlags( eFlags );
     396           0 : }
     397             : 
     398           0 : SwFldUpdateFlags SwEditShell::GetFldUpdateFlags(sal_Bool bDocSettings) const
     399             : {
     400           0 :     return getIDocumentSettingAccess()->getFieldUpdateFlags( !bDocSettings );
     401             : }
     402             : 
     403           0 : void SwEditShell::SetLabelDoc( sal_Bool bFlag )
     404             : {
     405           0 :     GetDoc()->set(IDocumentSettingAccess::LABEL_DOCUMENT, bFlag );
     406           0 : }
     407             : 
     408           0 : sal_Bool SwEditShell::IsLabelDoc() const
     409             : {
     410           0 :     return getIDocumentSettingAccess()->get(IDocumentSettingAccess::LABEL_DOCUMENT);
     411             : }
     412             : 
     413           0 : void SwEditShell::ChangeAuthorityData(const SwAuthEntry* pNewData)
     414             : {
     415           0 :     GetDoc()->ChangeAuthorityData(pNewData);
     416           0 : }
     417             : 
     418           0 : sal_Bool SwEditShell::IsAnyDatabaseFieldInDoc()const
     419             : {
     420           0 :     const SwFldTypes * pFldTypes = GetDoc()->GetFldTypes();
     421           0 :     const sal_uInt16 nSize = pFldTypes->size();
     422           0 :     for(sal_uInt16 i = 0; i < nSize; ++i)
     423             :     {
     424           0 :         SwFieldType& rFldType = *((*pFldTypes)[i]);
     425           0 :         sal_uInt16 nWhich = rFldType.Which();
     426           0 :         if(IsUsed(rFldType))
     427             :         {
     428           0 :             switch(nWhich)
     429             :             {
     430             :                 case RES_DBFLD:
     431             :                 case RES_DBNEXTSETFLD:
     432             :                 case RES_DBNUMSETFLD:
     433             :                 case RES_DBSETNUMBERFLD:
     434             :                 {
     435           0 :                     SwIterator<SwFmtFld,SwFieldType> aIter( rFldType );
     436           0 :                     SwFmtFld* pFld = aIter.First();
     437           0 :                     while(pFld)
     438             :                     {
     439           0 :                         if(pFld->IsFldInDoc())
     440           0 :                             return sal_True;
     441           0 :                         pFld = aIter.Next();
     442           0 :                     }
     443             :                 }
     444           0 :                 break;
     445             :             }
     446             :         }
     447             :     }
     448           0 :     return sal_False;
     449             : }
     450             : 
     451             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10