update dev300-m58
[ooovba.git] / sw / source / ui / vba / vbarangehelper.cxx
blob5df01b883ff59340e166ca24fae151ab41169a5f
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile:
10 * $Revision:
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #include "vbarangehelper.hxx"
31 #include <com/sun/star/text/ControlCharacter.hpp>
32 #include <com/sun/star/text/XTextRangeCompare.hpp>
34 using namespace ::ooo::vba;
35 using namespace ::com::sun::star;
37 /**
38 * get a range in a xText by creating
39 * a cursor that iterates over the text. If the iterating cursor is
40 * equal to the desired position, the range equivalent is returned.
41 * Some special cases are tables that are inside of the text, because the
42 * position has to be adjusted.
43 * @param xText a text where a range position is searched
44 * @param position a position inside o the text
45 * @return a range for the postion; null is returned if no range can be
46 * constructed.
48 uno::Reference< text::XTextRange > SwVbaRangeHelper::getRangeByPosition( const uno::Reference< text::XText >& rText, sal_Int32 _position ) throw ( uno::RuntimeException )
50 uno::Reference< text::XTextRange > xRange;
51 if( rText.is() )
53 sal_Int32 nPos = 0;
54 uno::Reference< text::XTextCursor > xCursor = rText->createTextCursor();
55 xCursor->collapseToStart();
56 sal_Bool bCanGo = sal_True;
57 while( !xRange.is() && bCanGo )
59 if( _position == nPos )
61 xRange = xCursor->getStart();
63 else
65 bCanGo = xCursor->goRight( 1, sal_False );
66 nPos++;
70 return xRange;
74 void SwVbaRangeHelper::insertString( uno::Reference< text::XTextRange >& rTextRange, uno::Reference< text::XText >& rText, const rtl::OUString& rStr, sal_Bool _bAbsorb ) throw ( uno::RuntimeException )
76 sal_Int32 nlastIndex = 0;
77 sal_Int32 nIndex = 0;
78 uno::Reference< text::XTextRange > xRange = rTextRange;
80 while(( nIndex = rStr.indexOf('\n', nlastIndex)) >= 0 )
82 xRange = xRange->getEnd();
83 if( nlastIndex < ( nIndex - 1 ) )
85 rText->insertString( xRange, rStr.copy( nlastIndex, ( nIndex - 1 - nlastIndex ) ), _bAbsorb );
86 xRange = xRange->getEnd();
89 rText->insertControlCharacter( xRange, text::ControlCharacter::PARAGRAPH_BREAK, _bAbsorb );
90 nlastIndex = nIndex + 1;
93 if( nlastIndex < rStr.getLength() )
95 xRange = xRange->getEnd();
97 rtl::OUString aWatt = rStr.copy( nlastIndex );
98 rText->insertString( xRange, aWatt, _bAbsorb );
102 uno::Reference< text::XTextCursor > SwVbaRangeHelper::initCursor( const uno::Reference< text::XTextRange >& rTextRange, const uno::Reference< text::XText >& rText ) throw ( uno::RuntimeException )
104 uno::Reference< text::XTextCursor > xTextCursor;
105 sal_Bool bGotTextCursor = sal_False;
109 xTextCursor = rText->createTextCursorByRange( rTextRange );
110 bGotTextCursor = sal_True;
112 catch (uno::Exception& e)
114 DebugHelper::exception(e);
117 if( !bGotTextCursor )
121 uno::Reference< text::XText > xText = rTextRange->getText();
122 xTextCursor = xText->createTextCursor();
123 bGotTextCursor = sal_True;
125 catch( uno::Exception& e )
127 DebugHelper::exception(e);
131 if( !bGotTextCursor )
135 xTextCursor = rText->createTextCursor();
136 bGotTextCursor = sal_True;
138 catch( uno::Exception& e )
140 DebugHelper::exception(e);
143 return xTextCursor;
146 sal_Int32 SwVbaRangeHelper::getPosition( const uno::Reference< text::XText >& rText, const uno::Reference< text::XTextRange >& rTextRange ) throw ( uno::RuntimeException )
148 sal_Int32 nPosition = -1;
149 if( rText.is() && rTextRange.is() )
151 nPosition = 0;
152 uno::Reference< text::XTextCursor > xCursor = rText->createTextCursor();
153 xCursor->collapseToStart();
154 uno::Reference< text::XTextRangeCompare > xCompare( rText, uno::UNO_QUERY_THROW );
155 // compareValue is 0 if the ranges are equal
156 sal_Int32 nCompareValue = xCompare->compareRegionStarts( xCursor->getStart(), rTextRange );
157 sal_Bool canGo = sal_True;
159 while( nCompareValue !=0 && canGo )
161 canGo = xCursor->goRight( 1, sal_False );
162 nCompareValue = xCompare->compareRegionStarts( xCursor->getStart(), rTextRange );
163 nPosition++;
166 // check fails: no correct position found
167 if( !canGo && nCompareValue != 0 )
169 nPosition = -1;
173 return nPosition;