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