Line data Source code
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 "vbarange.hxx"
20 : #include <vbahelper/vbahelper.hxx>
21 : #include <tools/diagnose_ex.h>
22 : #include "vbarangehelper.hxx"
23 : #include <ooo/vba/word/WdBreakType.hpp>
24 : #include <com/sun/star/style/BreakType.hpp>
25 : #include <com/sun/star/text/ControlCharacter.hpp>
26 : #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
27 : #include <com/sun/star/text/XTextRangeCompare.hpp>
28 : #include <com/sun/star/text/XWordCursor.hpp>
29 : #include <com/sun/star/text/XParagraphCursor.hpp>
30 : #include <ooo/vba/word/WdUnits.hpp>
31 : #include <ooo/vba/word/WdMovementType.hpp>
32 : #include "vbaparagraphformat.hxx"
33 : #include "vbastyle.hxx"
34 : #include "vbafont.hxx"
35 : #include "vbapalette.hxx"
36 : #include "vbapagesetup.hxx"
37 : #include "vbalistformat.hxx"
38 : #include "vbarevisions.hxx"
39 : #include "vbabookmarks.hxx"
40 : #include "vbasections.hxx"
41 : #include "vbafield.hxx"
42 :
43 : using namespace ::ooo::vba;
44 : using namespace ::com::sun::star;
45 :
46 0 : SwVbaRange::SwVbaRange( const uno::Reference< ooo::vba::XHelperInterface >& rParent, const uno::Reference< uno::XComponentContext >& rContext, const uno::Reference< text::XTextDocument >& rTextDocument, const uno::Reference< text::XTextRange >& rStart, bool _bMaySpanEndOfDocument ) throw (script::BasicErrorException, uno::RuntimeException) : SwVbaRange_BASE( rParent, rContext ), mxTextDocument( rTextDocument ), mbMaySpanEndOfDocument( _bMaySpanEndOfDocument )
47 : {
48 0 : uno::Reference< text::XTextRange > xEnd;
49 0 : initialize( rStart, xEnd );
50 0 : }
51 :
52 0 : SwVbaRange::SwVbaRange( const uno::Reference< ooo::vba::XHelperInterface >& rParent, const uno::Reference< uno::XComponentContext >& rContext, const uno::Reference< text::XTextDocument >& rTextDocument, const uno::Reference< text::XTextRange >& rStart, const uno::Reference< text::XTextRange >& rEnd, bool _bMaySpanEndOfDocument ) throw (script::BasicErrorException, uno::RuntimeException) : SwVbaRange_BASE( rParent, rContext ), mxTextDocument( rTextDocument ), mbMaySpanEndOfDocument( _bMaySpanEndOfDocument )
53 : {
54 0 : initialize( rStart, rEnd );
55 0 : }
56 :
57 0 : SwVbaRange::SwVbaRange( const uno::Reference< ooo::vba::XHelperInterface >& rParent, const uno::Reference< uno::XComponentContext >& rContext, const uno::Reference< text::XTextDocument >& rTextDocument, const uno::Reference< text::XTextRange >& rStart, const uno::Reference< text::XTextRange >& rEnd, const uno::Reference< text::XText >& rText, bool _bMaySpanEndOfDocument ) throw (script::BasicErrorException, uno::RuntimeException) : SwVbaRange_BASE( rParent, rContext ),mxTextDocument( rTextDocument ), mxText( rText ), mbMaySpanEndOfDocument( _bMaySpanEndOfDocument )
58 : {
59 0 : initialize( rStart, rEnd );
60 0 : }
61 :
62 0 : SwVbaRange::~SwVbaRange()
63 : {
64 0 : }
65 :
66 0 : void SwVbaRange::initialize( const uno::Reference< text::XTextRange >& rStart, const uno::Reference< text::XTextRange >& rEnd ) throw (css::script::BasicErrorException, uno::RuntimeException)
67 : {
68 0 : if( !mxText.is() )
69 : {
70 0 : mxText = mxTextDocument->getText();
71 : }
72 :
73 0 : mxTextCursor = SwVbaRangeHelper::initCursor( rStart, mxText );
74 0 : if( !mxTextCursor.is() )
75 0 : throw uno::RuntimeException("Fails to create text cursor" );
76 0 : mxTextCursor->collapseToStart();
77 :
78 0 : if( rEnd.is() )
79 0 : mxTextCursor->gotoRange( rEnd, sal_True );
80 : else
81 0 : mxTextCursor->gotoEnd( sal_True );
82 0 : }
83 :
84 : uno::Reference< text::XTextRange > SAL_CALL
85 0 : SwVbaRange::getXTextRange() throw (uno::RuntimeException, std::exception)
86 : {
87 0 : uno::Reference< text::XTextRange > xTextRange( mxTextCursor, uno::UNO_QUERY_THROW );
88 0 : return xTextRange;
89 : }
90 :
91 : /**
92 : * The complexity in this method is because we need to workaround
93 : * an issue that the last paragraph in a document does not have a trailing CRLF.
94 : * @return
95 : */
96 : OUString SAL_CALL
97 0 : SwVbaRange::getText() throw ( uno::RuntimeException, std::exception )
98 : {
99 0 : OUString aText = mxTextCursor->getString();
100 0 : sal_Int32 nLen = aText.getLength();
101 :
102 : // FIXME: should add a line separator if the range includes the last paragraph
103 0 : if( nLen == 0 )
104 : {
105 0 : if( mxTextCursor->isCollapsed() )
106 : {
107 0 : mxTextCursor->goRight( 1, sal_True );
108 0 : aText = mxTextCursor->getString();
109 0 : mxTextCursor->collapseToStart();
110 : }
111 : else
112 : {
113 0 : uno::Reference< text::XTextRange > xStart = mxTextCursor->getStart();
114 0 : uno::Reference< text::XTextRange > xEnd = mxTextCursor->getEnd();
115 0 : mxTextCursor->collapseToEnd();
116 0 : mxTextCursor->goRight( 1, sal_True );
117 0 : mxTextCursor->gotoRange( xStart, sal_False );
118 0 : mxTextCursor->gotoRange( xEnd, sal_True );
119 : }
120 : }
121 :
122 0 : return aText;
123 : }
124 :
125 : void SAL_CALL
126 0 : SwVbaRange::setText( const OUString& rText ) throw ( uno::RuntimeException, std::exception )
127 : {
128 : // Emulate the MSWord behavior, Don't delete the bookmark
129 : // which contains no text string in current inserting position,
130 0 : OUString sName;
131 0 : uno::Reference< text::XTextRange > xRange( mxTextCursor, uno::UNO_QUERY_THROW );
132 : try
133 : {
134 0 : uno::Reference< text::XTextContent > xBookmark = SwVbaRangeHelper::findBookmarkByPosition( mxTextDocument, xRange->getStart() );
135 0 : if( xBookmark.is() )
136 : {
137 0 : uno::Reference< container::XNamed > xNamed( xBookmark, uno::UNO_QUERY_THROW );
138 0 : sName = xNamed->getName();
139 0 : }
140 : }
141 0 : catch (const uno::Exception&)
142 : {
143 : // do nothing
144 : }
145 :
146 0 : if( rText.indexOf( '\n' ) != -1 )
147 : {
148 0 : mxTextCursor->setString( OUString() );
149 : // process CR in strings
150 0 : SwVbaRangeHelper::insertString( xRange, mxText, rText, true );
151 : }
152 : else
153 : {
154 0 : mxTextCursor->setString( rText );
155 : }
156 :
157 : // insert the bookmark if the bookmark is deleted during setting text string
158 0 : if( !sName.isEmpty() )
159 : {
160 0 : uno::Reference< text::XBookmarksSupplier > xBookmarksSupplier( mxTextDocument, uno::UNO_QUERY_THROW );
161 0 : uno::Reference< container::XNameAccess > xNameAccess( xBookmarksSupplier->getBookmarks(), uno::UNO_QUERY_THROW );
162 0 : if( !xNameAccess->hasByName( sName ) )
163 : {
164 0 : uno::Reference< frame::XModel > xModel( mxTextDocument, uno::UNO_QUERY_THROW );
165 0 : SwVbaBookmarks::addBookmarkByName( xModel, sName, xRange->getStart() );
166 0 : }
167 0 : }
168 0 : }
169 :
170 : // FIXME: test is not pass
171 0 : void SAL_CALL SwVbaRange::InsertBreak(const uno::Any& _breakType)
172 : throw (css::script::BasicErrorException, uno::RuntimeException, std::exception)
173 : {
174 : // default type is wdPageBreak;
175 0 : sal_Int32 nBreakType = word::WdBreakType::wdPageBreak;
176 0 : if( _breakType.hasValue() )
177 0 : _breakType >>= nBreakType;
178 :
179 0 : style::BreakType eBreakType = style::BreakType_NONE;
180 0 : switch( nBreakType )
181 : {
182 : case word::WdBreakType::wdPageBreak:
183 0 : eBreakType = style::BreakType_PAGE_BEFORE;
184 0 : break;
185 : case word::WdBreakType::wdColumnBreak:
186 0 : eBreakType = style::BreakType_COLUMN_AFTER;
187 0 : break;
188 : case word::WdBreakType::wdLineBreak:
189 : case word::WdBreakType::wdLineBreakClearLeft:
190 : case word::WdBreakType::wdLineBreakClearRight:
191 : case word::WdBreakType::wdSectionBreakContinuous:
192 : case word::WdBreakType::wdSectionBreakEvenPage:
193 : case word::WdBreakType::wdSectionBreakNextPage:
194 : case word::WdBreakType::wdSectionBreakOddPage:
195 : case word::WdBreakType::wdTextWrappingBreak:
196 0 : DebugHelper::basicexception( SbERR_NOT_IMPLEMENTED, OUString() );
197 0 : break;
198 : default:
199 0 : DebugHelper::basicexception( SbERR_BAD_PARAMETER, OUString() );
200 : }
201 :
202 0 : if( eBreakType != style::BreakType_NONE )
203 : {
204 0 : if( !mxTextCursor->isCollapsed() )
205 : {
206 0 : mxTextCursor->setString( OUString() );
207 0 : mxTextCursor->collapseToStart();
208 : }
209 :
210 0 : uno::Reference< beans::XPropertySet > xProp( mxTextCursor, uno::UNO_QUERY_THROW );
211 0 : xProp->setPropertyValue("BreakType", uno::makeAny( eBreakType ) );
212 : }
213 0 : }
214 :
215 : void SAL_CALL
216 0 : SwVbaRange::Select() throw ( uno::RuntimeException, std::exception )
217 : {
218 0 : uno::Reference< frame::XModel > xModel( mxTextDocument, uno::UNO_QUERY_THROW );
219 0 : uno::Reference< text::XTextViewCursor > xTextViewCursor = word::getXTextViewCursor( xModel );
220 0 : xTextViewCursor->gotoRange( mxTextCursor->getStart(), sal_False );
221 0 : xTextViewCursor->gotoRange( mxTextCursor->getEnd(), sal_True );
222 0 : }
223 :
224 : void SAL_CALL
225 0 : SwVbaRange::InsertParagraph() throw ( uno::RuntimeException, std::exception )
226 : {
227 0 : mxTextCursor->setString( OUString("") );
228 0 : InsertParagraphBefore();
229 0 : }
230 :
231 : void SAL_CALL
232 0 : SwVbaRange::InsertParagraphBefore() throw ( uno::RuntimeException, std::exception )
233 : {
234 0 : uno::Reference< text::XTextRange > xTextRange = mxTextCursor->getStart();
235 0 : mxText->insertControlCharacter( xTextRange, text::ControlCharacter::PARAGRAPH_BREAK, sal_True );
236 0 : mxTextCursor->gotoRange( xTextRange, sal_True );
237 0 : }
238 :
239 : void SAL_CALL
240 0 : SwVbaRange::InsertParagraphAfter() throw ( uno::RuntimeException, std::exception )
241 : {
242 0 : uno::Reference< text::XTextRange > xTextRange = mxTextCursor->getEnd();
243 0 : mxText->insertControlCharacter( xTextRange, text::ControlCharacter::PARAGRAPH_BREAK, sal_True );
244 0 : }
245 :
246 : uno::Reference< word::XParagraphFormat > SAL_CALL
247 0 : SwVbaRange::getParagraphFormat() throw ( uno::RuntimeException, std::exception )
248 : {
249 0 : uno::Reference< beans::XPropertySet > xParaProps( mxTextCursor, uno::UNO_QUERY_THROW );
250 0 : return uno::Reference< word::XParagraphFormat >( new SwVbaParagraphFormat( this, mxContext, mxTextDocument, xParaProps ) );
251 : }
252 :
253 : void SAL_CALL
254 0 : SwVbaRange::setParagraphFormat( const uno::Reference< word::XParagraphFormat >& /*rParagraphFormat*/ ) throw ( uno::RuntimeException, std::exception )
255 : {
256 0 : throw uno::RuntimeException("Not implemented" );
257 : }
258 :
259 0 : void SwVbaRange::GetStyleInfo(OUString& aStyleName, OUString& aStyleType ) throw ( uno::RuntimeException )
260 : {
261 0 : uno::Reference< beans::XPropertySet > xProp( mxTextCursor, uno::UNO_QUERY_THROW );
262 0 : if( ( xProp->getPropertyValue("CharStyleName") >>= aStyleName ) && !aStyleName.isEmpty() )
263 : {
264 0 : aStyleType = "CharacterStyles";
265 : }
266 0 : else if( ( xProp->getPropertyValue("ParaStyleName") >>= aStyleName ) && !aStyleName.isEmpty() )
267 : {
268 0 : aStyleType = "ParagraphStyles";
269 : }
270 0 : if( aStyleType.isEmpty() )
271 : {
272 0 : DebugHelper::runtimeexception( SbERR_INTERNAL_ERROR, OUString() );
273 0 : }
274 0 : }
275 :
276 : uno::Any SAL_CALL
277 0 : SwVbaRange::getStyle() throw ( uno::RuntimeException, std::exception )
278 : {
279 0 : OUString aStyleName;
280 0 : OUString aStyleType;
281 0 : GetStyleInfo( aStyleName, aStyleType );
282 0 : uno::Reference< style::XStyleFamiliesSupplier > xStyleSupplier( mxTextDocument, uno::UNO_QUERY_THROW);
283 0 : uno::Reference< container::XNameAccess > xStylesAccess( xStyleSupplier->getStyleFamilies()->getByName( aStyleType ), uno::UNO_QUERY_THROW );
284 0 : uno::Reference< beans::XPropertySet > xStyleProps( xStylesAccess->getByName( aStyleName ), uno::UNO_QUERY_THROW );
285 0 : uno::Reference< frame::XModel > xModel( mxTextDocument, uno::UNO_QUERY_THROW );
286 0 : return uno::makeAny( uno::Reference< word::XStyle >( new SwVbaStyle( this, mxContext, xModel, xStyleProps ) ) );
287 : }
288 :
289 : void SAL_CALL
290 0 : SwVbaRange::setStyle( const uno::Any& rStyle ) throw ( uno::RuntimeException, std::exception )
291 : {
292 0 : uno::Reference< beans::XPropertySet > xParaProps( mxTextCursor, uno::UNO_QUERY_THROW );
293 0 : SwVbaStyle::setStyle( xParaProps, rStyle );
294 0 : }
295 :
296 : uno::Reference< word::XFont > SAL_CALL
297 0 : SwVbaRange::getFont() throw ( uno::RuntimeException, std::exception )
298 : {
299 0 : VbaPalette aColors;
300 0 : return new SwVbaFont( mxParent, mxContext, aColors.getPalette(), uno::Reference< beans::XPropertySet >( getXTextRange(), uno::UNO_QUERY_THROW ) );
301 : }
302 :
303 : uno::Reference< word::XListFormat > SAL_CALL
304 0 : SwVbaRange::getListFormat() throw ( uno::RuntimeException, std::exception )
305 : {
306 0 : return uno::Reference< word::XListFormat >( new SwVbaListFormat( this, mxContext, getXTextRange() ) );
307 : }
308 :
309 0 : ::sal_Int32 SAL_CALL SwVbaRange::getLanguageID() throw (uno::RuntimeException, std::exception)
310 : {
311 0 : uno::Reference< beans::XPropertySet > xParaProps( mxTextCursor, uno::UNO_QUERY_THROW );
312 0 : return SwVbaStyle::getLanguageID( xParaProps );
313 : }
314 :
315 0 : void SAL_CALL SwVbaRange::setLanguageID( ::sal_Int32 _languageid ) throw (uno::RuntimeException, std::exception)
316 : {
317 0 : uno::Reference< beans::XPropertySet > xParaProps( mxTextCursor, uno::UNO_QUERY_THROW );
318 0 : SwVbaStyle::setLanguageID( xParaProps, _languageid );
319 0 : }
320 :
321 : uno::Any SAL_CALL
322 0 : SwVbaRange::PageSetup( ) throw (uno::RuntimeException, std::exception)
323 : {
324 0 : uno::Reference< beans::XPropertySet > xParaProps( mxTextCursor, uno::UNO_QUERY_THROW );
325 0 : uno::Reference< frame::XModel > xModel( mxTextDocument, uno::UNO_QUERY_THROW );
326 0 : OUString aPageStyleName;
327 0 : xParaProps->getPropertyValue("PageStyleName") >>= aPageStyleName;
328 0 : uno::Reference< style::XStyleFamiliesSupplier > xSytleFamSupp( xModel, uno::UNO_QUERY_THROW );
329 0 : uno::Reference< container::XNameAccess > xSytleFamNames( xSytleFamSupp->getStyleFamilies(), uno::UNO_QUERY_THROW );
330 0 : uno::Reference< container::XNameAccess > xPageStyles( xSytleFamNames->getByName("PageStyles"), uno::UNO_QUERY_THROW );
331 0 : uno::Reference< beans::XPropertySet > xPageProps( xPageStyles->getByName( aPageStyleName ), uno::UNO_QUERY_THROW );
332 0 : return uno::makeAny( uno::Reference< word::XPageSetup >( new SwVbaPageSetup( this, mxContext, xModel, xPageProps ) ) );
333 : }
334 :
335 0 : ::sal_Int32 SAL_CALL SwVbaRange::getStart() throw (uno::RuntimeException, std::exception)
336 : {
337 0 : uno::Reference< text::XText > xText = mxTextDocument->getText();
338 0 : return SwVbaRangeHelper::getPosition( xText, mxTextCursor->getStart() );
339 : }
340 :
341 0 : void SAL_CALL SwVbaRange::setStart( ::sal_Int32 _start ) throw (uno::RuntimeException, std::exception)
342 : {
343 0 : uno::Reference< text::XText > xText = mxTextDocument->getText();
344 0 : uno::Reference< text::XTextRange > xStart = SwVbaRangeHelper::getRangeByPosition( xText, _start );
345 0 : uno::Reference< text::XTextRange > xEnd = mxTextCursor->getEnd();
346 :
347 0 : mxTextCursor->gotoRange( xStart, sal_False );
348 0 : mxTextCursor->gotoRange( xEnd, sal_True );
349 0 : }
350 :
351 0 : ::sal_Int32 SAL_CALL SwVbaRange::getEnd() throw (uno::RuntimeException, std::exception)
352 : {
353 0 : uno::Reference< text::XText > xText = mxTextDocument->getText();
354 0 : return SwVbaRangeHelper::getPosition( xText, mxTextCursor->getEnd() );
355 : }
356 :
357 0 : void SAL_CALL SwVbaRange::setEnd( ::sal_Int32 _end ) throw (uno::RuntimeException, std::exception)
358 : {
359 0 : uno::Reference< text::XText > xText = mxTextDocument->getText();
360 0 : uno::Reference< text::XTextRange > xEnd = SwVbaRangeHelper::getRangeByPosition( xText, _end );
361 :
362 0 : mxTextCursor->collapseToStart();
363 0 : mxTextCursor->gotoRange( xEnd, sal_True );
364 0 : }
365 :
366 0 : sal_Bool SAL_CALL SwVbaRange::InRange( const uno::Reference< ::ooo::vba::word::XRange >& Range ) throw (uno::RuntimeException, std::exception)
367 : {
368 0 : SwVbaRange* pRange = dynamic_cast< SwVbaRange* >( Range.get() );
369 0 : if( !pRange )
370 0 : throw uno::RuntimeException();
371 0 : uno::Reference< text::XTextRange > xTextRange = pRange->getXTextRange();
372 0 : uno::Reference< text::XTextRangeCompare > xTRC( mxTextCursor->getText(), uno::UNO_QUERY_THROW );
373 0 : if( xTRC->compareRegionStarts( xTextRange, getXTextRange() ) >= 0 && xTRC->compareRegionEnds( xTextRange, getXTextRange() ) <= 0 )
374 0 : return sal_True;
375 0 : return sal_False;
376 : }
377 :
378 : uno::Any SAL_CALL
379 0 : SwVbaRange::Revisions( const uno::Any& index ) throw (uno::RuntimeException, std::exception)
380 : {
381 0 : uno::Reference< text::XTextRange > xTextRange = getXTextRange();
382 0 : uno::Reference< frame::XModel > xModel( mxTextDocument, uno::UNO_QUERY_THROW );
383 0 : uno::Reference< XCollection > xCol( new SwVbaRevisions( mxParent, mxContext, xModel, xTextRange ) );
384 0 : if ( index.hasValue() )
385 0 : return xCol->Item( index, uno::Any() );
386 0 : return uno::makeAny( xCol );
387 : }
388 :
389 : uno::Any SAL_CALL
390 0 : SwVbaRange::Sections( const uno::Any& index ) throw (uno::RuntimeException, std::exception)
391 : {
392 0 : uno::Reference< text::XTextRange > xTextRange = getXTextRange();
393 0 : uno::Reference< frame::XModel > xModel( mxTextDocument, uno::UNO_QUERY_THROW );
394 0 : uno::Reference< XCollection > xCol( new SwVbaSections( mxParent, mxContext, xModel, xTextRange ) );
395 0 : if ( index.hasValue() )
396 0 : return xCol->Item( index, uno::Any() );
397 0 : return uno::makeAny( xCol );
398 : }
399 :
400 : uno::Any SAL_CALL
401 0 : SwVbaRange::Fields( const uno::Any& index ) throw (uno::RuntimeException, std::exception)
402 : {
403 : //FIXME: should be get the field in current range
404 0 : uno::Reference< frame::XModel > xModel( mxTextDocument, uno::UNO_QUERY_THROW );
405 0 : uno::Reference< XCollection > xCol( new SwVbaFields( mxParent, mxContext, xModel ) );
406 0 : if ( index.hasValue() )
407 0 : return xCol->Item( index, uno::Any() );
408 0 : return uno::makeAny( xCol );
409 : }
410 :
411 : OUString
412 0 : SwVbaRange::getServiceImplName()
413 : {
414 0 : return OUString("SwVbaRange");
415 : }
416 :
417 : uno::Sequence< OUString >
418 0 : SwVbaRange::getServiceNames()
419 : {
420 0 : static uno::Sequence< OUString > aServiceNames;
421 0 : if ( aServiceNames.getLength() == 0 )
422 : {
423 0 : aServiceNames.realloc( 1 );
424 0 : aServiceNames[ 0 ] = "ooo.vba.word.Range";
425 : }
426 0 : return aServiceNames;
427 : }
428 :
429 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|