1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir #include "vbarangehelper.hxx" 28*cdf0e10cSrcweir #include <com/sun/star/text/ControlCharacter.hpp> 29*cdf0e10cSrcweir #include <com/sun/star/text/XTextRangeCompare.hpp> 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir using namespace ::ooo::vba; 32*cdf0e10cSrcweir using namespace ::com::sun::star; 33*cdf0e10cSrcweir 34*cdf0e10cSrcweir /** 35*cdf0e10cSrcweir * get a range in a xText by creating 36*cdf0e10cSrcweir * a cursor that iterates over the text. If the iterating cursor is 37*cdf0e10cSrcweir * equal to the desired position, the range equivalent is returned. 38*cdf0e10cSrcweir * Some special cases are tables that are inside of the text, because the 39*cdf0e10cSrcweir * position has to be adjusted. 40*cdf0e10cSrcweir * @param xText a text where a range position is searched 41*cdf0e10cSrcweir * @param position a position inside o the text 42*cdf0e10cSrcweir * @return a range for the postion; null is returned if no range can be 43*cdf0e10cSrcweir * constructed. 44*cdf0e10cSrcweir */ 45*cdf0e10cSrcweir uno::Reference< text::XTextRange > SwVbaRangeHelper::getRangeByPosition( const uno::Reference< text::XText >& rText, sal_Int32 _position ) throw ( uno::RuntimeException ) 46*cdf0e10cSrcweir { 47*cdf0e10cSrcweir uno::Reference< text::XTextRange > xRange; 48*cdf0e10cSrcweir if( rText.is() ) 49*cdf0e10cSrcweir { 50*cdf0e10cSrcweir sal_Int32 nPos = 0; 51*cdf0e10cSrcweir uno::Reference< text::XTextCursor > xCursor = rText->createTextCursor(); 52*cdf0e10cSrcweir xCursor->collapseToStart(); 53*cdf0e10cSrcweir sal_Bool bCanGo = sal_True; 54*cdf0e10cSrcweir while( !xRange.is() && bCanGo ) 55*cdf0e10cSrcweir { 56*cdf0e10cSrcweir if( _position == nPos ) 57*cdf0e10cSrcweir { 58*cdf0e10cSrcweir xRange = xCursor->getStart(); 59*cdf0e10cSrcweir } 60*cdf0e10cSrcweir else 61*cdf0e10cSrcweir { 62*cdf0e10cSrcweir bCanGo = xCursor->goRight( 1, sal_False ); 63*cdf0e10cSrcweir nPos++; 64*cdf0e10cSrcweir } 65*cdf0e10cSrcweir } 66*cdf0e10cSrcweir } 67*cdf0e10cSrcweir return xRange; 68*cdf0e10cSrcweir } 69*cdf0e10cSrcweir 70*cdf0e10cSrcweir 71*cdf0e10cSrcweir void SwVbaRangeHelper::insertString( uno::Reference< text::XTextRange >& rTextRange, uno::Reference< text::XText >& rText, const rtl::OUString& rStr, sal_Bool _bAbsorb ) throw ( uno::RuntimeException ) 72*cdf0e10cSrcweir { 73*cdf0e10cSrcweir sal_Int32 nlastIndex = 0; 74*cdf0e10cSrcweir sal_Int32 nIndex = 0; 75*cdf0e10cSrcweir uno::Reference< text::XTextRange > xRange = rTextRange; 76*cdf0e10cSrcweir 77*cdf0e10cSrcweir while(( nIndex = rStr.indexOf('\n', nlastIndex)) >= 0 ) 78*cdf0e10cSrcweir { 79*cdf0e10cSrcweir xRange = xRange->getEnd(); 80*cdf0e10cSrcweir if( nlastIndex < ( nIndex - 1 ) ) 81*cdf0e10cSrcweir { 82*cdf0e10cSrcweir rText->insertString( xRange, rStr.copy( nlastIndex, ( nIndex - 1 - nlastIndex ) ), _bAbsorb ); 83*cdf0e10cSrcweir xRange = xRange->getEnd(); 84*cdf0e10cSrcweir } 85*cdf0e10cSrcweir 86*cdf0e10cSrcweir rText->insertControlCharacter( xRange, text::ControlCharacter::PARAGRAPH_BREAK, _bAbsorb ); 87*cdf0e10cSrcweir nlastIndex = nIndex + 1; 88*cdf0e10cSrcweir } 89*cdf0e10cSrcweir 90*cdf0e10cSrcweir if( nlastIndex < rStr.getLength() ) 91*cdf0e10cSrcweir { 92*cdf0e10cSrcweir xRange = xRange->getEnd(); 93*cdf0e10cSrcweir 94*cdf0e10cSrcweir rtl::OUString aWatt = rStr.copy( nlastIndex ); 95*cdf0e10cSrcweir rText->insertString( xRange, aWatt, _bAbsorb ); 96*cdf0e10cSrcweir } 97*cdf0e10cSrcweir } 98*cdf0e10cSrcweir 99*cdf0e10cSrcweir uno::Reference< text::XTextCursor > SwVbaRangeHelper::initCursor( const uno::Reference< text::XTextRange >& rTextRange, const uno::Reference< text::XText >& rText ) throw ( uno::RuntimeException ) 100*cdf0e10cSrcweir { 101*cdf0e10cSrcweir uno::Reference< text::XTextCursor > xTextCursor; 102*cdf0e10cSrcweir sal_Bool bGotTextCursor = sal_False; 103*cdf0e10cSrcweir 104*cdf0e10cSrcweir try 105*cdf0e10cSrcweir { 106*cdf0e10cSrcweir xTextCursor = rText->createTextCursorByRange( rTextRange ); 107*cdf0e10cSrcweir bGotTextCursor = sal_True; 108*cdf0e10cSrcweir } 109*cdf0e10cSrcweir catch (uno::Exception& e) 110*cdf0e10cSrcweir { 111*cdf0e10cSrcweir DebugHelper::exception(e); 112*cdf0e10cSrcweir } 113*cdf0e10cSrcweir 114*cdf0e10cSrcweir if( !bGotTextCursor ) 115*cdf0e10cSrcweir { 116*cdf0e10cSrcweir try 117*cdf0e10cSrcweir { 118*cdf0e10cSrcweir uno::Reference< text::XText > xText = rTextRange->getText(); 119*cdf0e10cSrcweir xTextCursor = xText->createTextCursor(); 120*cdf0e10cSrcweir bGotTextCursor = sal_True; 121*cdf0e10cSrcweir } 122*cdf0e10cSrcweir catch( uno::Exception& e ) 123*cdf0e10cSrcweir { 124*cdf0e10cSrcweir DebugHelper::exception(e); 125*cdf0e10cSrcweir } 126*cdf0e10cSrcweir } 127*cdf0e10cSrcweir 128*cdf0e10cSrcweir if( !bGotTextCursor ) 129*cdf0e10cSrcweir { 130*cdf0e10cSrcweir try 131*cdf0e10cSrcweir { 132*cdf0e10cSrcweir xTextCursor = rText->createTextCursor(); 133*cdf0e10cSrcweir bGotTextCursor = sal_True; 134*cdf0e10cSrcweir } 135*cdf0e10cSrcweir catch( uno::Exception& e ) 136*cdf0e10cSrcweir { 137*cdf0e10cSrcweir DebugHelper::exception(e); 138*cdf0e10cSrcweir } 139*cdf0e10cSrcweir } 140*cdf0e10cSrcweir return xTextCursor; 141*cdf0e10cSrcweir } 142*cdf0e10cSrcweir 143*cdf0e10cSrcweir sal_Int32 SwVbaRangeHelper::getPosition( const uno::Reference< text::XText >& rText, const uno::Reference< text::XTextRange >& rTextRange ) throw ( uno::RuntimeException ) 144*cdf0e10cSrcweir { 145*cdf0e10cSrcweir sal_Int32 nPosition = -1; 146*cdf0e10cSrcweir if( rText.is() && rTextRange.is() ) 147*cdf0e10cSrcweir { 148*cdf0e10cSrcweir nPosition = 0; 149*cdf0e10cSrcweir uno::Reference< text::XTextCursor > xCursor = rText->createTextCursor(); 150*cdf0e10cSrcweir xCursor->collapseToStart(); 151*cdf0e10cSrcweir uno::Reference< text::XTextRangeCompare > xCompare( rText, uno::UNO_QUERY_THROW ); 152*cdf0e10cSrcweir // compareValue is 0 if the ranges are equal 153*cdf0e10cSrcweir sal_Int32 nCompareValue = xCompare->compareRegionStarts( xCursor->getStart(), rTextRange ); 154*cdf0e10cSrcweir sal_Bool canGo = sal_True; 155*cdf0e10cSrcweir 156*cdf0e10cSrcweir while( nCompareValue !=0 && canGo ) 157*cdf0e10cSrcweir { 158*cdf0e10cSrcweir canGo = xCursor->goRight( 1, sal_False ); 159*cdf0e10cSrcweir nCompareValue = xCompare->compareRegionStarts( xCursor->getStart(), rTextRange ); 160*cdf0e10cSrcweir nPosition++; 161*cdf0e10cSrcweir } 162*cdf0e10cSrcweir 163*cdf0e10cSrcweir // check fails: no correct position found 164*cdf0e10cSrcweir if( !canGo && nCompareValue != 0 ) 165*cdf0e10cSrcweir { 166*cdf0e10cSrcweir nPosition = -1; 167*cdf0e10cSrcweir } 168*cdf0e10cSrcweir } 169*cdf0e10cSrcweir 170*cdf0e10cSrcweir return nPosition; 171*cdf0e10cSrcweir } 172