LCOV - code coverage report
Current view: top level - sw/inc - pam.hxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 65 66 98.5 %
Date: 2015-06-13 12:38:46 Functions: 26 27 96.3 %
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             : #ifndef INCLUDED_SW_INC_PAM_HXX
      20             : #define INCLUDED_SW_INC_PAM_HXX
      21             : 
      22             : #include <stddef.h>
      23             : #include <sal/types.h>
      24             : #include <tools/mempool.hxx>
      25             : #include <cshtyp.hxx>
      26             : #include <ring.hxx>
      27             : #include <index.hxx>
      28             : #include <ndindex.hxx>
      29             : #include "swdllapi.h"
      30             : 
      31             : #include <iostream>
      32             : 
      33             : class SwFormat;
      34             : class SfxPoolItem;
      35             : class SfxItemSet;
      36             : class SwDoc;
      37             : class SwNode;
      38             : class SwContentNode;
      39             : class SwPaM;
      40             : class Point;
      41             : 
      42             : namespace com { namespace sun { namespace star { namespace util {
      43             :     struct SearchOptions;
      44             : } } } }
      45             : 
      46             : namespace utl {
      47             :     class TextSearch;
      48             : }
      49             : 
      50             : /// Marks a position in the document model.
      51    61057578 : struct SW_DLLPUBLIC SwPosition
      52             : {
      53             :     SwNodeIndex nNode;
      54             :     SwIndex nContent;
      55             : 
      56             :     SwPosition( const SwNodeIndex &rNode, const SwIndex &rContent );
      57             :     explicit SwPosition( const SwNodeIndex &rNode );
      58             :     explicit SwPosition( const SwNode& rNode );
      59             :     explicit SwPosition( SwContentNode& rNode, const sal_Int32 nOffset = 0 );
      60             : 
      61             :     SwPosition( const SwPosition & );
      62             :     SwPosition &operator=(const SwPosition &);
      63             : 
      64             :     /**
      65             :        Returns the document this position is in.
      66             : 
      67             :        @return the document this position is in.
      68             :     */
      69             :     SwDoc * GetDoc() const;
      70             : 
      71             :     bool operator < (const SwPosition &) const;
      72             :     bool operator > (const SwPosition &) const;
      73             :     bool operator <=(const SwPosition &) const;
      74             :     bool operator >=(const SwPosition &) const;
      75             :     bool operator ==(const SwPosition &) const;
      76             :     bool operator !=(const SwPosition &) const;
      77             :     void dumpAsXml(struct _xmlTextWriter* pWriter) const;
      78             : };
      79             : 
      80             : SW_DLLPUBLIC std::ostream &operator <<(std::ostream& s, const SwPosition& position);
      81             : 
      82             : // Result of comparing positions.
      83             : enum SwComparePosition {
      84             :     POS_BEFORE,             ///< Pos1 before Pos2.
      85             :     POS_BEHIND,             ///< Pos1 behind Pos2.
      86             :     POS_INSIDE,             ///< Pos1 completely contained in Pos2.
      87             :     POS_OUTSIDE,            ///< Pos2 completely contained in Pos1.
      88             :     POS_EQUAL,              ///< Pos1 is as large as Pos2.
      89             :     POS_OVERLAP_BEFORE,     ///< Pos1 overlaps Pos2 at the beginning.
      90             :     POS_OVERLAP_BEHIND,     ///< Pos1 overlaps Pos2 at the end.
      91             :     POS_COLLIDE_START,      ///< Pos1 start touches at Pos2 end.
      92             :     POS_COLLIDE_END         ///< Pos1 end touches at Pos2 start.
      93             : };
      94             : 
      95             : template<typename T>
      96       83397 : SwComparePosition ComparePosition(
      97             :             const T& rStt1, const T& rEnd1,
      98             :             const T& rStt2, const T& rEnd2 )
      99             : {
     100             :     SwComparePosition nRet;
     101       83397 :     if( rStt1 < rStt2 )
     102             :     {
     103        4011 :         if( rEnd1 > rStt2 )
     104             :         {
     105        3409 :             if( rEnd1 >= rEnd2 )
     106        3404 :                 nRet = POS_OUTSIDE;
     107             :             else
     108           5 :                 nRet = POS_OVERLAP_BEFORE;
     109             : 
     110             :         }
     111         602 :         else if( rEnd1 == rStt2 )
     112           6 :             nRet = POS_COLLIDE_END;
     113             :         else
     114         596 :             nRet = POS_BEFORE;
     115             :     }
     116       79386 :     else if( rEnd2 > rStt1 )
     117             :     {
     118         899 :         if( rEnd2 >= rEnd1 )
     119             :         {
     120         867 :             if( rEnd2 == rEnd1 && rStt2 == rStt1 )
     121         865 :                 nRet = POS_EQUAL;
     122             :             else
     123           2 :                 nRet = POS_INSIDE;
     124             :         }
     125             :         else
     126             :         {
     127          32 :             if (rStt1 == rStt2)
     128          27 :                 nRet = POS_OUTSIDE;
     129             :             else
     130           5 :                 nRet = POS_OVERLAP_BEHIND;
     131             :         }
     132             :     }
     133       78487 :     else if( rEnd2 == rStt1 )
     134        1489 :         nRet = POS_COLLIDE_START;
     135             :     else
     136       76998 :         nRet = POS_BEHIND;
     137       83397 :     return nRet;
     138             : }
     139             : 
     140             : /// SwPointAndMark / SwPaM
     141             : struct SwMoveFnCollection;
     142             : typedef SwMoveFnCollection* SwMoveFn;
     143             : SW_DLLPUBLIC extern SwMoveFn fnMoveForward; ///< SwPam::Move()/Find() default argument.
     144             : SW_DLLPUBLIC extern SwMoveFn fnMoveBackward;
     145             : 
     146             : typedef bool (*SwGoInDoc)( SwPaM& rPam, SwMoveFn fnMove );
     147             : SW_DLLPUBLIC extern SwGoInDoc fnGoDoc;
     148             : extern SwGoInDoc fnGoSection;
     149             : SW_DLLPUBLIC extern SwGoInDoc fnGoNode;
     150             : SW_DLLPUBLIC extern SwGoInDoc fnGoContent; ///< SwPam::Move() default argument.
     151             : extern SwGoInDoc fnGoContentCells;
     152             : extern SwGoInDoc fnGoContentSkipHidden;
     153             : extern SwGoInDoc fnGoContentCellsSkipHidden;
     154             : 
     155             : void _InitPam();
     156             : 
     157             : class SwPaM;
     158             : /// PaM is Point and Mark: a selection of the document model.
     159             : class SW_DLLPUBLIC SwPaM : public sw::Ring<SwPaM>
     160             : {
     161             :     SwPosition   m_Bound1;
     162             :     SwPosition   m_Bound2;
     163             :     SwPosition * m_pPoint; ///< points at either m_Bound1 or m_Bound2
     164             :     SwPosition * m_pMark;  ///< points at either m_Bound1 or m_Bound2
     165             :     bool m_bIsInFrontOfLabel;
     166             : 
     167             :     SwPaM* MakeRegion( SwMoveFn fnMove, const SwPaM * pOrigRg = 0 );
     168             : 
     169             :     SwPaM(SwPaM const& rPaM) SAL_DELETED_FUNCTION;
     170             : 
     171             : public:
     172             :     explicit SwPaM( const SwPosition& rPos, SwPaM* pRing = 0 );
     173             :     SwPaM( const SwPosition& rMk, const SwPosition& rPt, SwPaM* pRing = 0 );
     174             :     SwPaM( const SwNodeIndex& rMk, const SwNodeIndex& rPt,
     175             :            long nMkOffset = 0, long nPtOffset = 0, SwPaM* pRing = 0 );
     176             :     SwPaM( const SwNode& rMk, const SwNode& rPt,
     177             :            long nMkOffset = 0, long nPtOffset = 0, SwPaM* pRing = 0 );
     178             :     SwPaM(  const SwNodeIndex& rMk, sal_Int32 nMkContent,
     179             :             const SwNodeIndex& rPt, sal_Int32 nPtContent, SwPaM* pRing = 0 );
     180             :     SwPaM(  const SwNode& rMk, sal_Int32 nMkContent,
     181             :             const SwNode& rPt, sal_Int32 nPtContent, SwPaM* pRing = 0 );
     182             :     SwPaM( const SwNode& rNd, sal_Int32 nContent = 0, SwPaM* pRing = 0 );
     183             :     SwPaM( const SwNodeIndex& rNd, sal_Int32 nContent = 0, SwPaM* pRing = 0 );
     184             :     virtual ~SwPaM();
     185             : 
     186             :     /// this takes a second parameter, which indicates the Ring that
     187             :     /// the new PaM should be part of (may be null)
     188             :     SwPaM(SwPaM const& rPaM, SwPaM * pRing);
     189             :     /// @@@ semantic: no copy assignment for super class Ring.
     190             :     SwPaM& operator=( const SwPaM & );
     191             : 
     192             :     /// Movement of cursor.
     193             :     bool Move( SwMoveFn fnMove = fnMoveForward,
     194             :                 SwGoInDoc fnGo = fnGoContent );
     195             : 
     196             :     /// Search.
     197             :     bool Find(  const com::sun::star::util::SearchOptions& rSearchOpt,
     198             :                 bool bSearchInNotes,
     199             :                 utl::TextSearch& rSText,
     200             :                 SwMoveFn fnMove = fnMoveForward,
     201             :                 const SwPaM *pPam =0, bool bInReadOnly = false);
     202             :     bool Find(  const SwFormat& rFormat,
     203             :                 SwMoveFn fnMove = fnMoveForward,
     204             :                 const SwPaM *pPam =0, bool bInReadOnly = false);
     205             :     bool Find(  const SfxPoolItem& rAttr, bool bValue = true,
     206             :                 SwMoveFn fnMove = fnMoveForward,
     207             :                 const SwPaM *pPam =0, bool bInReadOnly = false );
     208             :     bool Find(  const SfxItemSet& rAttr, bool bNoColls,
     209             :                 SwMoveFn fnMove,
     210             :                 const SwPaM *pPam, bool bInReadOnly, bool bMoveFirst );
     211             : 
     212             :     bool DoSearch( const com::sun::star::util::SearchOptions& rSearchOpt, utl::TextSearch& rSText,
     213             :                    SwMoveFn fnMove, bool bSrchForward, bool bRegSearch, bool bChkEmptyPara, bool bChkParaEnd,
     214             :                    sal_Int32 &nStart, sal_Int32 &nEnd, sal_Int32 nTextLen, SwNode* pNode, SwPaM* pPam);
     215             : 
     216      247046 :     inline bool IsInFrontOfLabel() const        { return m_bIsInFrontOfLabel; }
     217           0 :     inline void _SetInFrontOfLabel( bool bNew ) { m_bIsInFrontOfLabel = bNew; }
     218             : 
     219             :     /// Unless this is called, the getter method of Mark will return Point.
     220             :     virtual void SetMark();
     221             : 
     222      164119 :     void DeleteMark()
     223             :     {
     224      164119 :         if (m_pMark != m_pPoint)
     225             :         {
     226             :             /** clear the mark position; this helps if mark's SwIndex is
     227             :                registered at some node, and that node is then deleted */
     228       38955 :             *m_pMark = SwPosition( SwNodeIndex( GetNode().GetNodes() ) );
     229       38955 :             m_pMark = m_pPoint;
     230             :         }
     231      164119 :     }
     232             : #ifdef DBG_UTIL
     233             :     void Exchange();
     234             : 
     235             : #else
     236        7161 :     void Exchange()
     237             :     {
     238        7161 :         if (m_pPoint != m_pMark)
     239             :         {
     240        5312 :             SwPosition *pTmp = m_pPoint;
     241        5312 :             m_pPoint = m_pMark;
     242        5312 :             m_pMark = pTmp;
     243             :         }
     244        7161 :     }
     245             : #endif
     246             : 
     247             :     /** A PaM marks a selection if Point and Mark are distinct positions.
     248             :         @return     true if the PaM spans a selection
     249             :      */
     250    60373219 :     bool HasMark() const { return m_pPoint != m_pMark; }
     251             : 
     252     1920963 :     const SwPosition *GetPoint() const { return m_pPoint; }
     253    63792003 :           SwPosition *GetPoint()       { return m_pPoint; }
     254      357645 :     const SwPosition *GetMark()  const { return m_pMark; }
     255      768118 :           SwPosition *GetMark()        { return m_pMark; }
     256             : 
     257    11901804 :     const SwPosition *Start() const
     258    11901804 :                 { return (*m_pPoint) <= (*m_pMark) ? m_pPoint : m_pMark; }
     259      820348 :           SwPosition *Start()
     260      820348 :                 { return (*m_pPoint) <= (*m_pMark) ? m_pPoint : m_pMark; }
     261             : 
     262    10678417 :     const SwPosition *End()   const
     263    10678417 :                 { return (*m_pPoint) >  (*m_pMark) ? m_pPoint : m_pMark; }
     264      561605 :           SwPosition *End()
     265      561605 :                 { return (*m_pPoint) >  (*m_pMark) ? m_pPoint : m_pMark; }
     266             : 
     267             :     /// @return current Node at Point/Mark
     268    59974217 :     SwNode    & GetNode      ( bool bPoint = true ) const
     269             :     {
     270    59974217 :         return ( bPoint ? m_pPoint->nNode : m_pMark->nNode ).GetNode();
     271             :     }
     272             : 
     273             :     /// @return current ContentNode at Point/Mark
     274    59261518 :     SwContentNode* GetContentNode( bool bPoint = true ) const
     275             :     {
     276    59261518 :         return GetNode(bPoint).GetContentNode();
     277             :     }
     278             : 
     279             :     /**
     280             :        Normalizes PaM, i.e. sort point and mark.
     281             : 
     282             :        @param bPointFirst true: If the point is behind the mark then swap.
     283             :                           false: If the mark is behind the point then swap.
     284             :     */
     285             :     SwPaM & Normalize(bool bPointFirst = true);
     286             : 
     287             :     /// @return the document (SwDoc) at which the PaM is registered
     288     1533918 :     SwDoc* GetDoc() const   { return m_pPoint->nNode.GetNode().GetDoc(); }
     289             : 
     290     2177549 :           SwPosition& GetBound( bool bOne = true )
     291     2177549 :                             { return bOne ? m_Bound1 : m_Bound2; }
     292             :     const SwPosition& GetBound( bool bOne = true ) const
     293             :                             { return bOne ? m_Bound1 : m_Bound2; }
     294             : 
     295             :     /// Get number of page which contains cursor.
     296             :     sal_uInt16 GetPageNum( bool bAtPoint = true, const Point* pLayPos = 0 );
     297             : 
     298             :     /** Is in something protected (readonly) or selection contains
     299             :        something protected. */
     300             :     bool HasReadonlySel( bool bFormView, bool bAnnotationMode = false ) const;
     301             : 
     302    11478414 :     bool ContainsPosition(const SwPosition & rPos) const
     303             :     {
     304    11478414 :         return *Start() <= rPos && rPos <= *End();
     305             :     }
     306             : 
     307      316647 :     DECL_FIXEDMEMPOOL_NEWDEL(SwPaM);
     308             : 
     309             :     OUString GetText() const;
     310             :     void InvalidatePaM();
     311      638181 :     SwPaM* GetNext()
     312      638181 :         { return GetNextInRing(); }
     313         153 :     const SwPaM* GetNext() const
     314         153 :         { return GetNextInRing(); }
     315         131 :     SwPaM* GetPrev()
     316         131 :         { return GetPrevInRing(); }
     317             :     const SwPaM* GetPrev() const
     318             :         { return GetPrevInRing(); }
     319    58897396 :     bool IsMultiSelection() const
     320    58897396 :         { return !unique(); }
     321             : 
     322             :     void dumpAsXml(struct _xmlTextWriter* pWriter) const;
     323             : };
     324             : 
     325             : SW_DLLPUBLIC std::ostream &operator <<(std::ostream& s, const SwPaM& pam);
     326             : 
     327             : bool CheckNodesRange( const SwNodeIndex&, const SwNodeIndex&, bool bChkSection );
     328             : 
     329             : #endif // INCLUDED_SW_INC_PAM_HXX
     330             : 
     331             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11