LCOV - code coverage report
Current view: top level - libreoffice/sw/source/ui/vba - vbarangehelper.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 81 0.0 %
Date: 2012-12-27 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             : 
      64           0 : void SwVbaRangeHelper::insertString( uno::Reference< text::XTextRange >& rTextRange, uno::Reference< text::XText >& rText, const rtl::OUString& rStr, sal_Bool _bAbsorb ) throw ( uno::RuntimeException )
      65             : {
      66           0 :     sal_Int32 nlastIndex = 0;
      67           0 :     sal_Int32 nIndex = 0;
      68           0 :     uno::Reference< text::XTextRange > xRange = rTextRange;
      69             : 
      70           0 :     while(( nIndex = rStr.indexOf('\n', nlastIndex)) >= 0  )
      71             :     {
      72           0 :         xRange = xRange->getEnd();
      73           0 :         if( nlastIndex < ( nIndex - 1 ) )
      74             :         {
      75           0 :             rText->insertString( xRange, rStr.copy( nlastIndex, ( nIndex - 1 - nlastIndex ) ), _bAbsorb );
      76           0 :             xRange = xRange->getEnd();
      77             :         }
      78             : 
      79           0 :         rText->insertControlCharacter( xRange, text::ControlCharacter::PARAGRAPH_BREAK, _bAbsorb );
      80           0 :         nlastIndex = nIndex + 1;
      81             :     }
      82             : 
      83           0 :     if( nlastIndex < rStr.getLength() )
      84             :     {
      85           0 :         xRange = xRange->getEnd();
      86             : 
      87           0 :         rtl::OUString aWatt = rStr.copy( nlastIndex );
      88           0 :         rText->insertString( xRange, aWatt, _bAbsorb );
      89           0 :     }
      90           0 : }
      91             : 
      92           0 : uno::Reference< text::XTextCursor > SwVbaRangeHelper::initCursor( const uno::Reference< text::XTextRange >& rTextRange, const uno::Reference< text::XText >& rText ) throw ( uno::RuntimeException )
      93             : {
      94           0 :     uno::Reference< text::XTextCursor > xTextCursor;
      95           0 :     sal_Bool bGotTextCursor = sal_False;
      96             : 
      97             :     try
      98             :     {
      99           0 :         xTextCursor = rText->createTextCursorByRange( rTextRange );
     100           0 :         bGotTextCursor = sal_True;
     101             :     }
     102           0 :     catch (const uno::Exception& e)
     103             :     {
     104           0 :         DebugHelper::exception(e);
     105             :     }
     106             : 
     107           0 :     if( !bGotTextCursor || !xTextCursor.is() )
     108             :     {
     109             :         try
     110             :         {
     111           0 :             uno::Reference< text::XText > xText = rTextRange->getText();
     112           0 :             xTextCursor = xText->createTextCursor();
     113           0 :             bGotTextCursor = sal_True;
     114             :         }
     115           0 :         catch (const uno::Exception& e)
     116             :         {
     117           0 :             DebugHelper::exception(e);
     118             :         }
     119             :     }
     120             : 
     121           0 :     if( !bGotTextCursor || !xTextCursor.is() )
     122             :     {
     123             :         try
     124             :         {
     125           0 :             xTextCursor = rText->createTextCursor();
     126           0 :             bGotTextCursor = sal_True;
     127             :         }
     128           0 :         catch (const uno::Exception& e)
     129             :         {
     130           0 :             DebugHelper::exception(e);
     131             :         }
     132             :     }
     133           0 :     return xTextCursor;
     134             : }
     135             : 
     136           0 : sal_Int32 SwVbaRangeHelper::getPosition( const uno::Reference< text::XText >& rText, const uno::Reference< text::XTextRange >& rTextRange ) throw ( uno::RuntimeException )
     137             : {
     138           0 :     sal_Int32 nPosition = -1;
     139           0 :     if( rText.is() && rTextRange.is() )
     140             :     {
     141           0 :         nPosition = 0;
     142           0 :         uno::Reference< text::XTextCursor > xCursor = rText->createTextCursor();
     143           0 :         xCursor->collapseToStart();
     144           0 :         uno::Reference< text::XTextRangeCompare > xCompare( rText, uno::UNO_QUERY_THROW );
     145             :         // compareValue is 0 if the ranges are equal
     146           0 :         sal_Int32 nCompareValue = xCompare->compareRegionStarts( xCursor->getStart(), rTextRange );
     147           0 :         sal_Bool canGo = sal_True;
     148             : 
     149           0 :         while( nCompareValue !=0 && canGo )
     150             :         {
     151           0 :             canGo = xCursor->goRight( 1, sal_False );
     152           0 :             nCompareValue = xCompare->compareRegionStarts( xCursor->getStart(), rTextRange );
     153           0 :             nPosition++;
     154             :         }
     155             : 
     156             :         // check fails: no correct position found
     157           0 :         if( !canGo && nCompareValue != 0 )
     158             :         {
     159           0 :             nPosition = -1;
     160           0 :         }
     161             :     }
     162             : 
     163           0 :     return nPosition;
     164             : }
     165             : 
     166           0 : uno::Reference< text::XTextContent > SwVbaRangeHelper::findBookmarkByPosition( const uno::Reference< text::XTextDocument >& xTextDoc, const uno::Reference< text::XTextRange >& xTextRange ) throw ( css::uno::RuntimeException )
     167             : {
     168           0 :     uno::Reference< text::XBookmarksSupplier > xBookmarksSupplier( xTextDoc, uno::UNO_QUERY_THROW );
     169           0 :     uno::Reference< container::XIndexAccess > xIndexAccess( xBookmarksSupplier->getBookmarks(), uno::UNO_QUERY_THROW );
     170           0 :     for( sal_Int32 index = 0; index < xIndexAccess->getCount(); index++ )
     171             :     {
     172           0 :         uno::Reference< text::XTextContent > xBookmark( xIndexAccess->getByIndex( index ), uno::UNO_QUERY_THROW );
     173           0 :         uno::Reference< text::XTextRange > xBkAnchor = xBookmark->getAnchor();
     174           0 :         uno::Reference< text::XTextRangeCompare > xCompare( xBkAnchor->getText(), uno::UNO_QUERY_THROW );
     175           0 :         if( xCompare->compareRegionStarts( xBkAnchor->getStart(), xBkAnchor->getEnd() ) == 0 )
     176             :         {
     177             :             try
     178             :             {
     179           0 :                 if( xCompare->compareRegionStarts( xTextRange, xBkAnchor->getStart() ) == 0 )
     180           0 :                     return xBookmark;
     181             :             }
     182           0 :             catch (const uno::Exception&)
     183             :             {
     184           0 :                 continue;
     185             :             }
     186             :         }
     187           0 :     }
     188           0 :     return uno::Reference< text::XTextContent >();
     189             : }
     190             : 
     191             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10