LCOV - code coverage report
Current view: top level - libreoffice/sw/source/core/SwNumberTree - SwNodeNum.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 123 158 77.8 %
Date: 2012-12-27 Functions: 23 25 92.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             : 
      21             : #include <editeng/svxenum.hxx>
      22             : #include <numrule.hxx>
      23             : #include <SwNodeNum.hxx>
      24             : #include <ndtxt.hxx>
      25             : #include <pam.hxx>
      26             : #include <stdio.h>
      27             : // #i83479#
      28             : #include <IDocumentListItems.hxx>
      29             : #include <doc.hxx>
      30             : 
      31        1339 : SwNodeNum::SwNodeNum( SwTxtNode* pTxtNode )
      32             :     : SwNumberTreeNode(),
      33             :       mpTxtNode( pTxtNode ),
      34        1339 :       mpNumRule( 0 )
      35             : {
      36        1339 : }
      37             : 
      38        3275 : SwNodeNum::SwNodeNum( SwNumRule* pNumRule )
      39             :     : SwNumberTreeNode(),
      40             :       mpTxtNode( 0 ),
      41        3275 :       mpNumRule( pNumRule )
      42             : {
      43        3275 : }
      44             : 
      45        6694 : SwNodeNum::~SwNodeNum()
      46             : {
      47        6694 : }
      48             : 
      49       52481 : SwTxtNode * SwNodeNum::GetTxtNode() const
      50             : {
      51       52481 :     return mpTxtNode;
      52             : }
      53             : 
      54       20678 : SwNumRule * SwNodeNum::GetNumRule() const
      55             : {
      56       20678 :     return mpNumRule;
      57             : }
      58             : 
      59           0 : void SwNodeNum::ChangeNumRule( SwNumRule& rNumRule )
      60             : {
      61             :     OSL_ENSURE( GetNumRule() && GetTxtNode(),
      62             :             "<SwNodeNum::ChangeNumRule(..)> - missing list style and/or text node. Serious defect -> please informm OD." );
      63           0 :     if ( GetNumRule() && GetTxtNode() )
      64             :     {
      65           0 :         GetNumRule()->RemoveTxtNode( *(GetTxtNode()) );
      66             :     }
      67             : 
      68           0 :     mpNumRule = &rNumRule;
      69             : 
      70           0 :     if ( GetNumRule() && GetTxtNode() )
      71             :     {
      72           0 :         GetNumRule()->AddTxtNode( *(GetTxtNode()) );
      73             :     }
      74           0 : }
      75             : 
      76        1339 : SwPosition SwNodeNum::GetPosition() const
      77             : {
      78             :     OSL_ENSURE( GetTxtNode(),
      79             :             "<SwNodeNum::GetPosition()> - no text node set at <SwNodeNum> instance" );
      80        1339 :     return SwPosition(*mpTxtNode);
      81             : }
      82             : 
      83         320 : SwNumberTreeNode * SwNodeNum::Create() const
      84             : {
      85         320 :     SwNodeNum * pResult = new SwNodeNum( GetNumRule() );
      86             : 
      87         320 :     return pResult;
      88             : }
      89             : 
      90        1360 : void SwNodeNum::PreAdd()
      91             : {
      92             :     OSL_ENSURE( GetTxtNode(),
      93             :             "<SwNodeNum::PreAdd()> - no text node set at <SwNodeNum> instance" );
      94        1360 :     if ( !GetNumRule() && GetTxtNode() )
      95             :     {
      96        1360 :         mpNumRule = GetTxtNode()->GetNumRule();
      97             :     }
      98             :     OSL_ENSURE( GetNumRule(),
      99             :             "<SwNodeNum::PreAdd()> - no list style set at <SwNodeNum> instance" );
     100        1360 :     if ( GetNumRule() && GetTxtNode() )
     101             :     {
     102        1360 :         GetNumRule()->AddTxtNode( *(GetTxtNode()) );
     103             :     }
     104             : 
     105             : 
     106             :     {
     107        2720 :         if ( GetTxtNode() &&
     108        1360 :              GetTxtNode()->GetNodes().IsDocNodes() )
     109             :         {
     110        1360 :             GetTxtNode()->getIDocumentListItems().addListItem( *this );
     111             :         }
     112             :     }
     113        1360 : }
     114             : 
     115        1329 : void SwNodeNum::PostRemove()
     116             : {
     117             :     OSL_ENSURE( GetTxtNode(),
     118             :             "<SwNodeNum::PostRemove()> - no text node set at <SwNodeNum> instance" );
     119             :     OSL_ENSURE( GetNumRule(),
     120             :             "<SwNodeNum::PostRemove()> - no list style set at <SwNodeNum> instance" );
     121             : 
     122        1329 :     if ( GetTxtNode() )
     123             :     {
     124        1329 :         GetTxtNode()->getIDocumentListItems().removeListItem( *this );
     125             :     }
     126             : 
     127        1329 :     if ( GetNumRule() )
     128             :     {
     129        1329 :         if ( GetTxtNode() )
     130             :         {
     131        1329 :             GetNumRule()->RemoveTxtNode( *(GetTxtNode()) );
     132             :         }
     133        1329 :         mpNumRule = 0;
     134             :     }
     135        1329 : }
     136             : 
     137       14756 : bool SwNodeNum::IsNotifiable() const
     138             : {
     139       14756 :     bool aResult = true;
     140             : 
     141       14756 :     if ( GetTxtNode() )
     142       12792 :         aResult = GetTxtNode()->IsNotifiable();
     143             : 
     144       14756 :     return aResult;
     145             : }
     146             : 
     147        1360 : bool SwNodeNum::IsNotificationEnabled() const
     148             : {
     149        1360 :     bool aResult = true;
     150             : 
     151        1360 :     if ( GetTxtNode() )
     152        1360 :         aResult = GetTxtNode()->IsNotificationEnabled();
     153             : 
     154        1360 :     return aResult;
     155             : }
     156             : 
     157        4717 : bool SwNodeNum::IsContinuous() const
     158             : {
     159        4717 :     bool aResult = false;
     160             : 
     161             :     // #i64311#
     162        4717 :     if ( GetNumRule() )
     163             :     {
     164        4717 :         aResult = mpNumRule->IsContinusNum();
     165             :     }
     166           0 :     else if ( GetParent() )
     167             :     {
     168           0 :         aResult = GetParent()->IsContinuous();
     169             :     }
     170             :     else
     171             :     {
     172             :         OSL_FAIL( "<SwNodeNum::IsContinuous()> - OD debug" );
     173             :     }
     174             : 
     175        4717 :     return aResult;
     176             : }
     177             : 
     178        1675 : bool SwNodeNum::IsCounted() const
     179             : {
     180        1675 :     bool aResult = false;
     181             : 
     182        1675 :     if ( GetTxtNode() )
     183             :     {
     184             :         // #i59559#
     185             :         // <SwTxtNode::IsCounted()> determines, if a text node is counted for numbering
     186        1355 :         aResult = GetTxtNode()->IsCountedInList();
     187             :     }
     188             :     else
     189         320 :         aResult = SwNumberTreeNode::IsCounted();
     190             : 
     191        1675 :     return aResult;
     192             : }
     193             : 
     194             : // #i64010#
     195         933 : bool SwNodeNum::HasCountedChildren() const
     196             : {
     197         933 :     bool bResult = false;
     198             : 
     199         933 :     tSwNumberTreeChildren::const_iterator aIt;
     200             : 
     201        1553 :     for (aIt = mChildren.begin(); aIt != mChildren.end(); ++aIt)
     202             :     {
     203         679 :         SwNodeNum* pChild( dynamic_cast<SwNodeNum*>(*aIt) );
     204             :         OSL_ENSURE( pChild,
     205             :                 "<SwNodeNum::HasCountedChildren()> - unexcepted type of child -> please inform OD" );
     206        1978 :         if ( pChild &&
     207         679 :              ( pChild->IsCountedForNumbering() ||
     208         620 :                pChild->HasCountedChildren() ) )
     209             :         {
     210          59 :             bResult = true;
     211             : 
     212          59 :             break;
     213             :         }
     214             :     }
     215             : 
     216         933 :     return bResult;
     217             : }
     218             : // #i64010#
     219         679 : bool SwNodeNum::IsCountedForNumbering() const
     220             : {
     221         679 :     return IsCounted() &&
     222         500 :            ( IsPhantom() ||                 // phantoms
     223         488 :              !GetTxtNode() ||               // root node
     224         488 :              GetTxtNode()->HasNumber() ||   // text node
     225        2155 :              GetTxtNode()->HasBullet() );   // text node
     226             : }
     227             : 
     228             : 
     229         528 : void SwNodeNum::NotifyNode()
     230             : {
     231         528 :     ValidateMe();
     232             : 
     233         528 :     if (mpTxtNode)
     234             :     {
     235         479 :         mpTxtNode->NumRuleChgd();
     236             :     }
     237         528 : }
     238             : 
     239       46578 : bool SwNodeNum::LessThan(const SwNumberTreeNode & rNode) const
     240             : {
     241       46578 :     bool bResult = false;
     242       46578 :     const SwNodeNum & rTmpNode = static_cast<const SwNodeNum &>(rNode);
     243             : 
     244       46578 :     if (mpTxtNode == NULL && rTmpNode.mpTxtNode != NULL)
     245         236 :         bResult = true;
     246       46342 :     else if (mpTxtNode != NULL && rTmpNode.mpTxtNode != NULL)
     247             :     {
     248             :         // #i83479# - refactoring
     249             :         // simplify comparison by comparing the indexes of the text nodes
     250       42106 :         bResult = ( mpTxtNode->GetIndex() < rTmpNode.mpTxtNode->GetIndex() ) ? true : false;
     251             :     }
     252             : 
     253       46578 :     return bResult;
     254             : }
     255             : 
     256         626 : bool SwNodeNum::IsRestart() const
     257             : {
     258         626 :     bool bIsRestart = false;
     259             : 
     260         626 :     if ( GetTxtNode() )
     261             :     {
     262         584 :         bIsRestart = GetTxtNode()->IsListRestart();
     263             :     }
     264             : 
     265         626 :     return bIsRestart;
     266             : }
     267             : 
     268         299 : bool SwNodeNum::IsCountPhantoms() const
     269             : {
     270         299 :     bool bResult = true;
     271             : 
     272             :     // #i64311#
     273             :     // phantoms aren't counted in consecutive numbering rules
     274         299 :     if ( mpNumRule )
     275         299 :         bResult = !mpNumRule->IsContinusNum() &&
     276         299 :                   mpNumRule->IsCountPhantoms();
     277             :     else
     278             :     {
     279             :         OSL_FAIL( "<SwNodeNum::IsCountPhantoms(): missing numbering rule - please inform OD" );
     280             :     }
     281             : 
     282         299 :     return bResult;
     283             : }
     284             : 
     285         153 : SwNumberTree::tSwNumTreeNumber SwNodeNum::GetStartValue() const
     286             : {
     287         153 :     SwNumberTree::tSwNumTreeNumber aResult = 1;
     288             : 
     289         153 :     if ( IsRestart() && GetTxtNode() )
     290             :     {
     291           0 :         aResult = GetTxtNode()->GetActualListStartValue();
     292             :     }
     293             :     else
     294             :     {
     295         153 :         SwNumRule * pRule = GetNumRule();
     296             : 
     297         153 :         if (pRule)
     298             :         {
     299         153 :             int nLevel = GetParent() ? GetLevelInListTree() : 0;
     300             : 
     301         153 :             if (nLevel >= 0 && nLevel < MAXLEVEL)
     302             :             {
     303         153 :                 const SwNumFmt * pFmt = pRule->GetNumFmt( static_cast<sal_uInt16>(nLevel));
     304             : 
     305         153 :                 if (pFmt)
     306          89 :                     aResult = pFmt->GetStart();
     307             :             }
     308             :         }
     309             :     }
     310             : 
     311         153 :     return aResult;
     312             : }
     313             : 
     314        1720 : void SwNodeNum::HandleNumberTreeRootNodeDelete( SwNodeNum& rNodeNum )
     315             : {
     316        1720 :     SwNodeNum* pRootNode = rNodeNum.GetParent()
     317           0 :                            ? dynamic_cast<SwNodeNum*>(rNodeNum.GetRoot())
     318        1720 :                            : &rNodeNum;
     319        1720 :     if ( !pRootNode )
     320             :     {
     321             :         // no root node -> nothing do.
     322        1720 :         return;
     323             :     }
     324             : 
     325             :     // unregister all number tree node entries, which correspond to a text node,
     326             :     // about the deletion of the number tree root node.
     327        1720 :     _UnregisterMeAndChildrenDueToRootDelete( *pRootNode );
     328             : }
     329             : 
     330        1720 : void SwNodeNum::_UnregisterMeAndChildrenDueToRootDelete( SwNodeNum& rNodeNum )
     331             : {
     332        1720 :     const bool bIsPhantom( rNodeNum.IsPhantom() );
     333        1720 :     tSwNumberTreeChildren::size_type nAllowedChildCount( 0 );
     334        1720 :     bool bDone( false );
     335        5160 :     while ( !bDone &&
     336        1720 :             rNodeNum.GetChildCount() > nAllowedChildCount )
     337             :     {
     338           0 :         SwNodeNum* pChildNode( dynamic_cast<SwNodeNum*>((*rNodeNum.mChildren.begin())) );
     339           0 :         if ( !pChildNode )
     340             :         {
     341             :             OSL_FAIL( "<SwNodeNum::_UnregisterMeAndChildrenDueToRootDelete(..)> - unknown number tree node child" );
     342           0 :             ++nAllowedChildCount;
     343           0 :             continue;
     344             :         }
     345             : 
     346             :         // Unregistering the last child of a phantom will destroy the phantom.
     347             :         // Thus <rNodeNum> will be destroyed and access on <rNodeNum> has to
     348             :         // be suppressed.
     349           0 :         if ( bIsPhantom && rNodeNum.GetChildCount() == 1 )
     350             :         {
     351           0 :             bDone = true;
     352             :         }
     353             : 
     354           0 :         _UnregisterMeAndChildrenDueToRootDelete( *pChildNode );
     355             :     }
     356             : 
     357        1720 :     if ( !bIsPhantom )
     358             :     {
     359        1720 :         SwTxtNode* pTxtNode( rNodeNum.GetTxtNode() );
     360        1720 :         if ( pTxtNode )
     361             :         {
     362           0 :             pTxtNode->RemoveFromList();
     363             :             // --> clear all list attributes and the list style
     364           0 :             std::set<sal_uInt16> aResetAttrsArray;
     365           0 :             aResetAttrsArray.insert( aResetAttrsArray.end(), RES_PARATR_LIST_ID );
     366           0 :             aResetAttrsArray.insert( aResetAttrsArray.end(), RES_PARATR_LIST_LEVEL );
     367           0 :             aResetAttrsArray.insert( aResetAttrsArray.end(), RES_PARATR_LIST_ISRESTART );
     368           0 :             aResetAttrsArray.insert( aResetAttrsArray.end(), RES_PARATR_LIST_RESTARTVALUE );
     369           0 :             aResetAttrsArray.insert( aResetAttrsArray.end(), RES_PARATR_LIST_ISCOUNTED );
     370           0 :             aResetAttrsArray.insert( aResetAttrsArray.end(), RES_PARATR_NUMRULE );
     371           0 :             SwPaM aPam( *pTxtNode );
     372             :             pTxtNode->GetDoc()->ResetAttrs( aPam, false,
     373             :                                             aResetAttrsArray,
     374           0 :                                             false );
     375             :         }
     376             :     }
     377        1720 : }
     378             : 
     379             : // #i81002#
     380           0 : const SwNodeNum* SwNodeNum::GetPrecedingNodeNumOf( const SwTxtNode& rTxtNode ) const
     381             : {
     382           0 :     const SwNodeNum* pPrecedingNodeNum( 0 );
     383             : 
     384             :     // #i83479#
     385           0 :     SwNodeNum aNodeNumForTxtNode( const_cast<SwTxtNode*>(&rTxtNode) );
     386             : 
     387             :     pPrecedingNodeNum = dynamic_cast<const SwNodeNum*>(
     388           0 :                             GetRoot()
     389           0 :                             ? GetRoot()->GetPrecedingNodeOf( aNodeNumForTxtNode )
     390           0 :                             : GetPrecedingNodeOf( aNodeNumForTxtNode ) );
     391             : 
     392           0 :     return pPrecedingNodeNum;
     393             : }
     394             : 
     395             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10