LCOV - code coverage report
Current view: top level - sw/source/ui/vba - vbarangehelper.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 0 81 0.0 %
Date: 2015-06-13 12:38:46 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 position; 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 :         bool bCanGo = 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, 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,
      92             :                                                                   const uno::Reference< text::XText >& rText )
      93             :     throw ( uno::RuntimeException, script::BasicErrorException )
      94             : {
      95           0 :     uno::Reference< text::XTextCursor > xTextCursor;
      96           0 :     bool bGotTextCursor = false;
      97             : 
      98             :     try
      99             :     {
     100           0 :         xTextCursor = rText->createTextCursorByRange( rTextRange );
     101           0 :         bGotTextCursor = true;
     102             :     }
     103           0 :     catch (const uno::Exception& e)
     104             :     {
     105           0 :         DebugHelper::basicexception(e);
     106             :     }
     107             : 
     108           0 :     if( !bGotTextCursor || !xTextCursor.is() )
     109             :     {
     110             :         try
     111             :         {
     112           0 :             uno::Reference< text::XText > xText = rTextRange->getText();
     113           0 :             xTextCursor = xText->createTextCursor();
     114           0 :             bGotTextCursor = true;
     115             :         }
     116           0 :         catch (const uno::Exception& e)
     117             :         {
     118           0 :             DebugHelper::basicexception(e);
     119             :         }
     120             :     }
     121             : 
     122           0 :     if( !bGotTextCursor || !xTextCursor.is() )
     123             :     {
     124             :         try
     125             :         {
     126           0 :             xTextCursor = rText->createTextCursor();
     127           0 :             bGotTextCursor = true;
     128             :         }
     129           0 :         catch (const uno::Exception& e)
     130             :         {
     131           0 :             DebugHelper::basicexception(e);
     132             :         }
     133             :     }
     134           0 :     return xTextCursor;
     135             : }
     136             : 
     137           0 : sal_Int32 SwVbaRangeHelper::getPosition( const uno::Reference< text::XText >& rText, const uno::Reference< text::XTextRange >& rTextRange ) throw ( uno::RuntimeException )
     138             : {
     139           0 :     sal_Int32 nPosition = -1;
     140           0 :     if( rText.is() && rTextRange.is() )
     141             :     {
     142           0 :         nPosition = 0;
     143           0 :         uno::Reference< text::XTextCursor > xCursor = rText->createTextCursor();
     144           0 :         xCursor->collapseToStart();
     145           0 :         uno::Reference< text::XTextRangeCompare > xCompare( rText, uno::UNO_QUERY_THROW );
     146             :         // compareValue is 0 if the ranges are equal
     147           0 :         sal_Int32 nCompareValue = xCompare->compareRegionStarts( xCursor->getStart(), rTextRange );
     148           0 :         bool canGo = true;
     149             : 
     150           0 :         while( nCompareValue !=0 && canGo )
     151             :         {
     152           0 :             canGo = xCursor->goRight( 1, sal_False );
     153           0 :             nCompareValue = xCompare->compareRegionStarts( xCursor->getStart(), rTextRange );
     154           0 :             nPosition++;
     155             :         }
     156             : 
     157             :         // check fails: no correct position found
     158           0 :         if( !canGo && nCompareValue != 0 )
     159             :         {
     160           0 :             nPosition = -1;
     161           0 :         }
     162             :     }
     163             : 
     164           0 :     return nPosition;
     165             : }
     166             : 
     167           0 : uno::Reference< text::XTextContent > SwVbaRangeHelper::findBookmarkByPosition( const uno::Reference< text::XTextDocument >& xTextDoc, const uno::Reference< text::XTextRange >& xTextRange ) throw ( css::uno::RuntimeException )
     168             : {
     169           0 :     uno::Reference< text::XBookmarksSupplier > xBookmarksSupplier( xTextDoc, uno::UNO_QUERY_THROW );
     170           0 :     uno::Reference< container::XIndexAccess > xIndexAccess( xBookmarksSupplier->getBookmarks(), uno::UNO_QUERY_THROW );
     171           0 :     for( sal_Int32 index = 0; index < xIndexAccess->getCount(); index++ )
     172             :     {
     173           0 :         uno::Reference< text::XTextContent > xBookmark( xIndexAccess->getByIndex( index ), uno::UNO_QUERY_THROW );
     174           0 :         uno::Reference< text::XTextRange > xBkAnchor = xBookmark->getAnchor();
     175           0 :         uno::Reference< text::XTextRangeCompare > xCompare( xBkAnchor->getText(), uno::UNO_QUERY_THROW );
     176           0 :         if( xCompare->compareRegionStarts( xBkAnchor->getStart(), xBkAnchor->getEnd() ) == 0 )
     177             :         {
     178             :             try
     179             :             {
     180           0 :                 if( xCompare->compareRegionStarts( xTextRange, xBkAnchor->getStart() ) == 0 )
     181           0 :                     return xBookmark;
     182             :             }
     183           0 :             catch (const uno::Exception&)
     184             :             {
     185           0 :                 continue;
     186             :             }
     187             :         }
     188           0 :     }
     189           0 :     return uno::Reference< text::XTextContent >();
     190             : }
     191             : 
     192             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11