1*efeef26fSAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*efeef26fSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*efeef26fSAndrew Rist * or more contributor license agreements. See the NOTICE file
5*efeef26fSAndrew Rist * distributed with this work for additional information
6*efeef26fSAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*efeef26fSAndrew Rist * to you under the Apache License, Version 2.0 (the
8*efeef26fSAndrew Rist * "License"); you may not use this file except in compliance
9*efeef26fSAndrew Rist * with the License. You may obtain a copy of the License at
10cdf0e10cSrcweir *
11*efeef26fSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir *
13*efeef26fSAndrew Rist * Unless required by applicable law or agreed to in writing,
14*efeef26fSAndrew Rist * software distributed under the License is distributed on an
15*efeef26fSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*efeef26fSAndrew Rist * KIND, either express or implied. See the License for the
17*efeef26fSAndrew Rist * specific language governing permissions and limitations
18*efeef26fSAndrew Rist * under the License.
19cdf0e10cSrcweir *
20*efeef26fSAndrew Rist *************************************************************/
21*efeef26fSAndrew Rist
22*efeef26fSAndrew Rist
23cdf0e10cSrcweir #include "vbarangehelper.hxx"
24cdf0e10cSrcweir #include <com/sun/star/text/ControlCharacter.hpp>
25cdf0e10cSrcweir #include <com/sun/star/text/XTextRangeCompare.hpp>
26cdf0e10cSrcweir
27cdf0e10cSrcweir using namespace ::ooo::vba;
28cdf0e10cSrcweir using namespace ::com::sun::star;
29cdf0e10cSrcweir
30cdf0e10cSrcweir /**
31cdf0e10cSrcweir * get a range in a xText by creating
32cdf0e10cSrcweir * a cursor that iterates over the text. If the iterating cursor is
33cdf0e10cSrcweir * equal to the desired position, the range equivalent is returned.
34cdf0e10cSrcweir * Some special cases are tables that are inside of the text, because the
35cdf0e10cSrcweir * position has to be adjusted.
36cdf0e10cSrcweir * @param xText a text where a range position is searched
37cdf0e10cSrcweir * @param position a position inside o the text
38cdf0e10cSrcweir * @return a range for the postion; null is returned if no range can be
39cdf0e10cSrcweir * constructed.
40cdf0e10cSrcweir */
getRangeByPosition(const uno::Reference<text::XText> & rText,sal_Int32 _position)41cdf0e10cSrcweir uno::Reference< text::XTextRange > SwVbaRangeHelper::getRangeByPosition( const uno::Reference< text::XText >& rText, sal_Int32 _position ) throw ( uno::RuntimeException )
42cdf0e10cSrcweir {
43cdf0e10cSrcweir uno::Reference< text::XTextRange > xRange;
44cdf0e10cSrcweir if( rText.is() )
45cdf0e10cSrcweir {
46cdf0e10cSrcweir sal_Int32 nPos = 0;
47cdf0e10cSrcweir uno::Reference< text::XTextCursor > xCursor = rText->createTextCursor();
48cdf0e10cSrcweir xCursor->collapseToStart();
49cdf0e10cSrcweir sal_Bool bCanGo = sal_True;
50cdf0e10cSrcweir while( !xRange.is() && bCanGo )
51cdf0e10cSrcweir {
52cdf0e10cSrcweir if( _position == nPos )
53cdf0e10cSrcweir {
54cdf0e10cSrcweir xRange = xCursor->getStart();
55cdf0e10cSrcweir }
56cdf0e10cSrcweir else
57cdf0e10cSrcweir {
58cdf0e10cSrcweir bCanGo = xCursor->goRight( 1, sal_False );
59cdf0e10cSrcweir nPos++;
60cdf0e10cSrcweir }
61cdf0e10cSrcweir }
62cdf0e10cSrcweir }
63cdf0e10cSrcweir return xRange;
64cdf0e10cSrcweir }
65cdf0e10cSrcweir
66cdf0e10cSrcweir
insertString(uno::Reference<text::XTextRange> & rTextRange,uno::Reference<text::XText> & rText,const rtl::OUString & rStr,sal_Bool _bAbsorb)67cdf0e10cSrcweir void SwVbaRangeHelper::insertString( uno::Reference< text::XTextRange >& rTextRange, uno::Reference< text::XText >& rText, const rtl::OUString& rStr, sal_Bool _bAbsorb ) throw ( uno::RuntimeException )
68cdf0e10cSrcweir {
69cdf0e10cSrcweir sal_Int32 nlastIndex = 0;
70cdf0e10cSrcweir sal_Int32 nIndex = 0;
71cdf0e10cSrcweir uno::Reference< text::XTextRange > xRange = rTextRange;
72cdf0e10cSrcweir
73cdf0e10cSrcweir while(( nIndex = rStr.indexOf('\n', nlastIndex)) >= 0 )
74cdf0e10cSrcweir {
75cdf0e10cSrcweir xRange = xRange->getEnd();
76cdf0e10cSrcweir if( nlastIndex < ( nIndex - 1 ) )
77cdf0e10cSrcweir {
78cdf0e10cSrcweir rText->insertString( xRange, rStr.copy( nlastIndex, ( nIndex - 1 - nlastIndex ) ), _bAbsorb );
79cdf0e10cSrcweir xRange = xRange->getEnd();
80cdf0e10cSrcweir }
81cdf0e10cSrcweir
82cdf0e10cSrcweir rText->insertControlCharacter( xRange, text::ControlCharacter::PARAGRAPH_BREAK, _bAbsorb );
83cdf0e10cSrcweir nlastIndex = nIndex + 1;
84cdf0e10cSrcweir }
85cdf0e10cSrcweir
86cdf0e10cSrcweir if( nlastIndex < rStr.getLength() )
87cdf0e10cSrcweir {
88cdf0e10cSrcweir xRange = xRange->getEnd();
89cdf0e10cSrcweir
90cdf0e10cSrcweir rtl::OUString aWatt = rStr.copy( nlastIndex );
91cdf0e10cSrcweir rText->insertString( xRange, aWatt, _bAbsorb );
92cdf0e10cSrcweir }
93cdf0e10cSrcweir }
94cdf0e10cSrcweir
initCursor(const uno::Reference<text::XTextRange> & rTextRange,const uno::Reference<text::XText> & rText)95cdf0e10cSrcweir uno::Reference< text::XTextCursor > SwVbaRangeHelper::initCursor( const uno::Reference< text::XTextRange >& rTextRange, const uno::Reference< text::XText >& rText ) throw ( uno::RuntimeException )
96cdf0e10cSrcweir {
97cdf0e10cSrcweir uno::Reference< text::XTextCursor > xTextCursor;
98cdf0e10cSrcweir sal_Bool bGotTextCursor = sal_False;
99cdf0e10cSrcweir
100cdf0e10cSrcweir try
101cdf0e10cSrcweir {
102cdf0e10cSrcweir xTextCursor = rText->createTextCursorByRange( rTextRange );
103cdf0e10cSrcweir bGotTextCursor = sal_True;
104cdf0e10cSrcweir }
105cdf0e10cSrcweir catch (uno::Exception& e)
106cdf0e10cSrcweir {
107cdf0e10cSrcweir DebugHelper::exception(e);
108cdf0e10cSrcweir }
109cdf0e10cSrcweir
110cdf0e10cSrcweir if( !bGotTextCursor )
111cdf0e10cSrcweir {
112cdf0e10cSrcweir try
113cdf0e10cSrcweir {
114cdf0e10cSrcweir uno::Reference< text::XText > xText = rTextRange->getText();
115cdf0e10cSrcweir xTextCursor = xText->createTextCursor();
116cdf0e10cSrcweir bGotTextCursor = sal_True;
117cdf0e10cSrcweir }
118cdf0e10cSrcweir catch( uno::Exception& e )
119cdf0e10cSrcweir {
120cdf0e10cSrcweir DebugHelper::exception(e);
121cdf0e10cSrcweir }
122cdf0e10cSrcweir }
123cdf0e10cSrcweir
124cdf0e10cSrcweir if( !bGotTextCursor )
125cdf0e10cSrcweir {
126cdf0e10cSrcweir try
127cdf0e10cSrcweir {
128cdf0e10cSrcweir xTextCursor = rText->createTextCursor();
129cdf0e10cSrcweir bGotTextCursor = sal_True;
130cdf0e10cSrcweir }
131cdf0e10cSrcweir catch( uno::Exception& e )
132cdf0e10cSrcweir {
133cdf0e10cSrcweir DebugHelper::exception(e);
134cdf0e10cSrcweir }
135cdf0e10cSrcweir }
136cdf0e10cSrcweir return xTextCursor;
137cdf0e10cSrcweir }
138cdf0e10cSrcweir
getPosition(const uno::Reference<text::XText> & rText,const uno::Reference<text::XTextRange> & rTextRange)139cdf0e10cSrcweir sal_Int32 SwVbaRangeHelper::getPosition( const uno::Reference< text::XText >& rText, const uno::Reference< text::XTextRange >& rTextRange ) throw ( uno::RuntimeException )
140cdf0e10cSrcweir {
141cdf0e10cSrcweir sal_Int32 nPosition = -1;
142cdf0e10cSrcweir if( rText.is() && rTextRange.is() )
143cdf0e10cSrcweir {
144cdf0e10cSrcweir nPosition = 0;
145cdf0e10cSrcweir uno::Reference< text::XTextCursor > xCursor = rText->createTextCursor();
146cdf0e10cSrcweir xCursor->collapseToStart();
147cdf0e10cSrcweir uno::Reference< text::XTextRangeCompare > xCompare( rText, uno::UNO_QUERY_THROW );
148cdf0e10cSrcweir // compareValue is 0 if the ranges are equal
149cdf0e10cSrcweir sal_Int32 nCompareValue = xCompare->compareRegionStarts( xCursor->getStart(), rTextRange );
150cdf0e10cSrcweir sal_Bool canGo = sal_True;
151cdf0e10cSrcweir
152cdf0e10cSrcweir while( nCompareValue !=0 && canGo )
153cdf0e10cSrcweir {
154cdf0e10cSrcweir canGo = xCursor->goRight( 1, sal_False );
155cdf0e10cSrcweir nCompareValue = xCompare->compareRegionStarts( xCursor->getStart(), rTextRange );
156cdf0e10cSrcweir nPosition++;
157cdf0e10cSrcweir }
158cdf0e10cSrcweir
159cdf0e10cSrcweir // check fails: no correct position found
160cdf0e10cSrcweir if( !canGo && nCompareValue != 0 )
161cdf0e10cSrcweir {
162cdf0e10cSrcweir nPosition = -1;
163cdf0e10cSrcweir }
164cdf0e10cSrcweir }
165cdf0e10cSrcweir
166cdf0e10cSrcweir return nPosition;
167cdf0e10cSrcweir }
168