LCOV - code coverage report
Current view: top level - sw/source/ui/vba - vbarangehelper.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 81 0.0 %
Date: 2014-04-14 Functions: 0 5 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             : #include "vbarangehelper.hxx"
      20             : #include <com/sun/star/text/ControlCharacter.hpp>
      21             : #include <com/sun/star/text/XTextRangeCompare.hpp>
      22             : #include <com/sun/star/text/XBookmarksSupplier.hpp>
      23             : 
      24             : using namespace ::ooo::vba;
      25             : using namespace ::com::sun::star;
      26             : 
      27             : /**
      28             :  * get a range in a xText by creating
      29             :  * a cursor that iterates over the text. If the iterating cursor is
      30             :  * equal to the desired position, the range equivalent is returned.
      31             :  * Some special cases are tables that are inside of the text, because the
      32             :  * position has to be adjusted.
      33             :  * @param xText a text where a range position is searched
      34             :  * @param position a position inside o the text
      35             :  * @return a range for the postion; null is returned if no range can be
      36             :  * constructed.
      37             :  */
      38           0 : uno::Reference< text::XTextRange > SwVbaRangeHelper::getRangeByPosition( const uno::Reference< text::XText >& rText, sal_Int32 _position ) throw ( uno::RuntimeException )
      39             : {
      40           0 :     uno::Reference< text::XTextRange > xRange;
      41           0 :     if( rText.is() )
      42             :     {
      43           0 :         sal_Int32 nPos = 0;
      44           0 :         uno::Reference< text::XTextCursor > xCursor = rText->createTextCursor();
      45           0 :         xCursor->collapseToStart();
      46           0 :         sal_Bool bCanGo = sal_True;
      47           0 :         while( !xRange.is() && bCanGo )
      48             :         {
      49           0 :             if( _position == nPos )
      50             :             {
      51           0 :                 xRange = xCursor->getStart();
      52             :             }
      53             :             else
      54             :             {
      55           0 :                 bCanGo = xCursor->goRight( 1, sal_False );
      56           0 :                 nPos++;
      57             :             }
      58           0 :         }
      59             :     }
      60           0 :     return xRange;
      61             : }
      62             : 
      63           0 : void SwVbaRangeHelper::insertString( uno::Reference< text::XTextRange >& rTextRange, uno::Reference< text::XText >& rText, const OUString& rStr, sal_Bool _bAbsorb ) throw ( uno::RuntimeException )
      64             : {
      65           0 :     sal_Int32 nlastIndex = 0;
      66           0 :     sal_Int32 nIndex = 0;
      67           0 :     uno::Reference< text::XTextRange > xRange = rTextRange;
      68             : 
      69           0 :     while(( nIndex = rStr.indexOf('\n', nlastIndex)) >= 0  )
      70             :     {
      71           0 :         xRange = xRange->getEnd();
      72           0 :         if( nlastIndex < ( nIndex - 1 ) )
      73             :         {
      74           0 :             rText->insertString( xRange, rStr.copy( nlastIndex, ( nIndex - 1 - nlastIndex ) ), _bAbsorb );
      75           0 :             xRange = xRange->getEnd();
      76             :         }
      77             : 
      78           0 :         rText->insertControlCharacter( xRange, text::ControlCharacter::PARAGRAPH_BREAK, _bAbsorb );
      79           0 :         nlastIndex = nIndex + 1;
      80             :     }
      81             : 
      82           0 :     if( nlastIndex < rStr.getLength() )
      83             :     {
      84           0 :         xRange = xRange->getEnd();
      85             : 
      86           0 :         OUString aWatt = rStr.copy( nlastIndex );
      87           0 :         rText->insertString( xRange, aWatt, _bAbsorb );
      88           0 :     }
      89           0 : }
      90             : 
      91           0 : uno::Reference< text::XTextCursor > SwVbaRangeHelper::initCursor( const uno::Reference< text::XTextRange >& rTextRange, const uno::Reference< text::XText >& rText ) throw ( uno::RuntimeException )
      92             : {
      93           0 :     uno::Reference< text::XTextCursor > xTextCursor;
      94           0 :     bool bGotTextCursor = false;
      95             : 
      96             :     try
      97             :     {
      98           0 :         xTextCursor = rText->createTextCursorByRange( rTextRange );
      99           0 :         bGotTextCursor = true;
     100             :     }
     101           0 :     catch (const uno::Exception& e)
     102             :     {
     103           0 :         DebugHelper::exception(e);
     104             :     }
     105             : 
     106           0 :     if( !bGotTextCursor || !xTextCursor.is() )
     107             :     {
     108             :         try
     109             :         {
     110           0 :             uno::Reference< text::XText > xText = rTextRange->getText();
     111           0 :             xTextCursor = xText->createTextCursor();
     112           0 :             bGotTextCursor = true;
     113             :         }
     114           0 :         catch (const uno::Exception& e)
     115             :         {
     116           0 :             DebugHelper::exception(e);
     117             :         }
     118             :     }
     119             : 
     120           0 :     if( !bGotTextCursor || !xTextCursor.is() )
     121             :     {
     122             :         try
     123             :         {
     124           0 :             xTextCursor = rText->createTextCursor();
     125           0 :             bGotTextCursor = true;
     126             :         }
     127           0 :         catch (const uno::Exception& e)
     128             :         {
     129           0 :             DebugHelper::exception(e);
     130             :         }
     131             :     }
     132           0 :     return xTextCursor;
     133             : }
     134             : 
     135           0 : sal_Int32 SwVbaRangeHelper::getPosition( const uno::Reference< text::XText >& rText, const uno::Reference< text::XTextRange >& rTextRange ) throw ( uno::RuntimeException )
     136             : {
     137           0 :     sal_Int32 nPosition = -1;
     138           0 :     if( rText.is() && rTextRange.is() )
     139             :     {
     140           0 :         nPosition = 0;
     141           0 :         uno::Reference< text::XTextCursor > xCursor = rText->createTextCursor();
     142           0 :         xCursor->collapseToStart();
     143           0 :         uno::Reference< text::XTextRangeCompare > xCompare( rText, uno::UNO_QUERY_THROW );
     144             :         // compareValue is 0 if the ranges are equal
     145           0 :         sal_Int32 nCompareValue = xCompare->compareRegionStarts( xCursor->getStart(), rTextRange );
     146           0 :         sal_Bool canGo = sal_True;
     147             : 
     148           0 :         while( nCompareValue !=0 && canGo )
     149             :         {
     150           0 :             canGo = xCursor->goRight( 1, sal_False );
     151           0 :             nCompareValue = xCompare->compareRegionStarts( xCursor->getStart(), rTextRange );
     152           0 :             nPosition++;
     153             :         }
     154             : 
     155             :         // check fails: no correct position found
     156           0 :         if( !canGo && nCompareValue != 0 )
     157             :         {
     158           0 :             nPosition = -1;
     159           0 :         }
     160             :     }
     161             : 
     162           0 :     return nPosition;
     163             : }
     164             : 
     165           0 : uno::Reference< text::XTextContent > SwVbaRangeHelper::findBookmarkByPosition( const uno::Reference< text::XTextDocument >& xTextDoc, const uno::Reference< text::XTextRange >& xTextRange ) throw ( css::uno::RuntimeException )
     166             : {
     167           0 :     uno::Reference< text::XBookmarksSupplier > xBookmarksSupplier( xTextDoc, uno::UNO_QUERY_THROW );
     168           0 :     uno::Reference< container::XIndexAccess > xIndexAccess( xBookmarksSupplier->getBookmarks(), uno::UNO_QUERY_THROW );
     169           0 :     for( sal_Int32 index = 0; index < xIndexAccess->getCount(); index++ )
     170             :     {
     171           0 :         uno::Reference< text::XTextContent > xBookmark( xIndexAccess->getByIndex( index ), uno::UNO_QUERY_THROW );
     172           0 :         uno::Reference< text::XTextRange > xBkAnchor = xBookmark->getAnchor();
     173           0 :         uno::Reference< text::XTextRangeCompare > xCompare( xBkAnchor->getText(), uno::UNO_QUERY_THROW );
     174           0 :         if( xCompare->compareRegionStarts( xBkAnchor->getStart(), xBkAnchor->getEnd() ) == 0 )
     175             :         {
     176             :             try
     177             :             {
     178           0 :                 if( xCompare->compareRegionStarts( xTextRange, xBkAnchor->getStart() ) == 0 )
     179           0 :                     return xBookmark;
     180             :             }
     181           0 :             catch (const uno::Exception&)
     182             :             {
     183           0 :                 continue;
     184             :             }
     185             :         }
     186           0 :     }
     187           0 :     return uno::Reference< text::XTextContent >();
     188             : }
     189             : 
     190             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10