LibreOffice Module sw (master)  1
vbarangehelper.cxx
Go to the documentation of this file.
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 <vbahelper/vbahelper.hxx>
21 #include <com/sun/star/text/ControlCharacter.hpp>
22 #include <com/sun/star/text/XTextRangeCompare.hpp>
23 #include <com/sun/star/text/XBookmarksSupplier.hpp>
24 
25 using namespace ::ooo::vba;
26 using namespace ::com::sun::star;
27 
39 uno::Reference< text::XTextRange > SwVbaRangeHelper::getRangeByPosition( const uno::Reference< text::XText >& rText, sal_Int32 _position )
40 {
41  uno::Reference< text::XTextRange > xRange;
42  if( rText.is() )
43  {
44  sal_Int32 nPos = 0;
45  uno::Reference< text::XTextCursor > xCursor = rText->createTextCursor();
46  xCursor->collapseToStart();
47  bool bCanGo = true;
48  while( !xRange.is() && bCanGo )
49  {
50  if( _position == nPos )
51  {
52  xRange = xCursor->getStart();
53  }
54  else
55  {
56  bCanGo = xCursor->goRight( 1, false );
57  nPos++;
58  }
59  }
60  }
61  return xRange;
62 }
63 
64 void SwVbaRangeHelper::insertString( uno::Reference< text::XTextRange > const & rTextRange, uno::Reference< text::XText > const & rText, const OUString& rStr, bool _bAbsorb )
65 {
66  sal_Int32 nlastIndex = 0;
67  sal_Int32 nIndex = 0;
68  uno::Reference< text::XTextRange > xRange = rTextRange;
69 
70  while(( nIndex = rStr.indexOf('\n', nlastIndex)) >= 0 )
71  {
72  xRange = xRange->getEnd();
73  if( nlastIndex < ( nIndex - 1 ) )
74  {
75  rText->insertString( xRange, rStr.copy( nlastIndex, ( nIndex - 1 - nlastIndex ) ), _bAbsorb );
76  xRange = xRange->getEnd();
77  }
78 
79  rText->insertControlCharacter( xRange, text::ControlCharacter::PARAGRAPH_BREAK, _bAbsorb );
80  nlastIndex = nIndex + 1;
81  }
82 
83  if( nlastIndex < rStr.getLength() )
84  {
85  xRange = xRange->getEnd();
86 
87  OUString aWatt = rStr.copy( nlastIndex );
88  rText->insertString( xRange, aWatt, _bAbsorb );
89  }
90 }
91 
92 uno::Reference< text::XTextCursor > SwVbaRangeHelper::initCursor( const uno::Reference< text::XTextRange >& rTextRange,
93  const uno::Reference< text::XText >& rText )
94 {
95  uno::Reference< text::XTextCursor > xTextCursor;
96  bool bGotTextCursor = false;
97 
98  try
99  {
100  xTextCursor = rText->createTextCursorByRange( rTextRange );
101  bGotTextCursor = true;
102  }
103  catch (const uno::Exception& e)
104  {
105  DebugHelper::basicexception(e);
106  }
107 
108  if( !bGotTextCursor || !xTextCursor.is() )
109  {
110  try
111  {
112  uno::Reference< text::XText > xText = rTextRange->getText();
113  xTextCursor = xText->createTextCursor();
114  bGotTextCursor = true;
115  }
116  catch (const uno::Exception& e)
117  {
118  DebugHelper::basicexception(e);
119  }
120  }
121 
122  if( !bGotTextCursor || !xTextCursor.is() )
123  {
124  try
125  {
126  xTextCursor = rText->createTextCursor();
127  }
128  catch (const uno::Exception& e)
129  {
130  DebugHelper::basicexception(e);
131  }
132  }
133  return xTextCursor;
134 }
135 
136 sal_Int32 SwVbaRangeHelper::getPosition( const uno::Reference< text::XText >& rText, const uno::Reference< text::XTextRange >& rTextRange )
137 {
138  sal_Int32 nPosition = -1;
139  if( rText.is() && rTextRange.is() )
140  {
141  nPosition = 0;
142  uno::Reference< text::XTextCursor > xCursor = rText->createTextCursor();
143  xCursor->collapseToStart();
144  uno::Reference< text::XTextRangeCompare > xCompare( rText, uno::UNO_QUERY_THROW );
145  // compareValue is 0 if the ranges are equal
146  sal_Int32 nCompareValue = xCompare->compareRegionStarts( xCursor->getStart(), rTextRange );
147  bool canGo = true;
148 
149  while( nCompareValue !=0 && canGo )
150  {
151  canGo = xCursor->goRight( 1, false );
152  nCompareValue = xCompare->compareRegionStarts( xCursor->getStart(), rTextRange );
153  nPosition++;
154  }
155 
156  // check fails: no correct position found
157  if( !canGo && nCompareValue != 0 )
158  {
159  nPosition = -1;
160  }
161  }
162 
163  return nPosition;
164 }
165 
166 uno::Reference< text::XTextContent > SwVbaRangeHelper::findBookmarkByPosition( const uno::Reference< text::XTextDocument >& xTextDoc, const uno::Reference< text::XTextRange >& xTextRange )
167 {
168  uno::Reference< text::XBookmarksSupplier > xBookmarksSupplier( xTextDoc, uno::UNO_QUERY_THROW );
169  uno::Reference< container::XIndexAccess > xIndexAccess( xBookmarksSupplier->getBookmarks(), uno::UNO_QUERY_THROW );
170  for( sal_Int32 index = 0; index < xIndexAccess->getCount(); index++ )
171  {
172  uno::Reference< text::XTextContent > xBookmark( xIndexAccess->getByIndex( index ), uno::UNO_QUERY_THROW );
173  uno::Reference< text::XTextRange > xBkAnchor = xBookmark->getAnchor();
174  uno::Reference< text::XTextRangeCompare > xCompare( xBkAnchor->getText(), uno::UNO_QUERY_THROW );
175  if( xCompare->compareRegionStarts( xBkAnchor->getStart(), xBkAnchor->getEnd() ) == 0 )
176  {
177  try
178  {
179  if( xCompare->compareRegionStarts( xTextRange, xBkAnchor->getStart() ) == 0 )
180  return xBookmark;
181  }
182  catch (const uno::Exception&)
183  {
184  continue;
185  }
186  }
187  }
188  return uno::Reference< text::XTextContent >();
189 }
190 
191 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static css::uno::Reference< css::text::XTextContent > findBookmarkByPosition(const css::uno::Reference< css::text::XTextDocument > &xTextDoc, const css::uno::Reference< css::text::XTextRange > &xTextRange)
static css::uno::Reference< css::text::XTextCursor > initCursor(const css::uno::Reference< css::text::XTextRange > &rTextRange, const css::uno::Reference< css::text::XText > &rText)
static sal_Int32 getPosition(const css::uno::Reference< css::text::XText > &rText, const css::uno::Reference< css::text::XTextRange > &rTextRange)
static css::uno::Reference< css::text::XTextRange > getRangeByPosition(const css::uno::Reference< css::text::XText > &rText, sal_Int32 _position)
get a range in a xText by creating a cursor that iterates over the text.
static void insertString(css::uno::Reference< css::text::XTextRange > const &rTextRange, css::uno::Reference< css::text::XText > const &rText, const OUString &rStr, bool _bAbsorb)
tuple index
sal_Int32 nPos