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, sal_Bool _bMaySpanEndOfDocument ) throw (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, sal_Bool _bMaySpanEndOfDocument ) throw (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, sal_Bool _bMaySpanEndOfDocument ) throw (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 (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", uno::Reference< uno::XInterface >() );
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, sal_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 ) throw (uno::RuntimeException, std::exception)
172 : {
173 : // default type is wdPageBreak;
174 0 : sal_Int32 nBreakType = word::WdBreakType::wdPageBreak;
175 0 : if( _breakType.hasValue() )
176 0 : _breakType >>= nBreakType;
177 :
178 0 : style::BreakType eBreakType = style::BreakType_NONE;
179 0 : switch( nBreakType )
180 : {
181 : case word::WdBreakType::wdPageBreak:
182 0 : eBreakType = style::BreakType_PAGE_BEFORE;
183 0 : break;
184 : case word::WdBreakType::wdColumnBreak:
185 0 : eBreakType = style::BreakType_COLUMN_AFTER;
186 0 : break;
187 : case word::WdBreakType::wdLineBreak:
188 : case word::WdBreakType::wdLineBreakClearLeft:
189 : case word::WdBreakType::wdLineBreakClearRight:
190 : case word::WdBreakType::wdSectionBreakContinuous:
191 : case word::WdBreakType::wdSectionBreakEvenPage:
192 : case word::WdBreakType::wdSectionBreakNextPage:
193 : case word::WdBreakType::wdSectionBreakOddPage:
194 : case word::WdBreakType::wdTextWrappingBreak:
195 0 : DebugHelper::exception( SbERR_NOT_IMPLEMENTED, OUString() );
196 0 : break;
197 : default:
198 0 : DebugHelper::exception( SbERR_BAD_PARAMETER, OUString() );
199 : }
200 :
201 0 : if( eBreakType != style::BreakType_NONE )
202 : {
203 0 : if( !mxTextCursor->isCollapsed() )
204 : {
205 0 : mxTextCursor->setString( OUString() );
206 0 : mxTextCursor->collapseToStart();
207 : }
208 :
209 0 : uno::Reference< beans::XPropertySet > xProp( mxTextCursor, uno::UNO_QUERY_THROW );
210 0 : xProp->setPropertyValue("BreakType", uno::makeAny( eBreakType ) );
211 : }
212 0 : }
213 :
214 : void SAL_CALL
215 0 : SwVbaRange::Select() throw ( uno::RuntimeException, std::exception )
216 : {
217 0 : uno::Reference< frame::XModel > xModel( mxTextDocument, uno::UNO_QUERY_THROW );
218 0 : uno::Reference< text::XTextViewCursor > xTextViewCursor = word::getXTextViewCursor( xModel );
219 0 : xTextViewCursor->gotoRange( mxTextCursor->getStart(), sal_False );
220 0 : xTextViewCursor->gotoRange( mxTextCursor->getEnd(), sal_True );
221 0 : }
222 :
223 : void SAL_CALL
224 0 : SwVbaRange::InsertParagraph() throw ( uno::RuntimeException, std::exception )
225 : {
226 0 : mxTextCursor->setString( OUString("") );
227 0 : InsertParagraphBefore();
228 0 : }
229 :
230 : void SAL_CALL
231 0 : SwVbaRange::InsertParagraphBefore() throw ( uno::RuntimeException, std::exception )
232 : {
233 0 : uno::Reference< text::XTextRange > xTextRange = mxTextCursor->getStart();
234 0 : mxText->insertControlCharacter( xTextRange, text::ControlCharacter::PARAGRAPH_BREAK, sal_True );
235 0 : mxTextCursor->gotoRange( xTextRange, sal_True );
236 0 : }
237 :
238 : void SAL_CALL
239 0 : SwVbaRange::InsertParagraphAfter() throw ( uno::RuntimeException, std::exception )
240 : {
241 0 : uno::Reference< text::XTextRange > xTextRange = mxTextCursor->getEnd();
242 0 : mxText->insertControlCharacter( xTextRange, text::ControlCharacter::PARAGRAPH_BREAK, sal_True );
243 0 : }
244 :
245 : uno::Reference< word::XParagraphFormat > SAL_CALL
246 0 : SwVbaRange::getParagraphFormat() throw ( uno::RuntimeException, std::exception )
247 : {
248 0 : uno::Reference< beans::XPropertySet > xParaProps( mxTextCursor, uno::UNO_QUERY_THROW );
249 0 : return uno::Reference< word::XParagraphFormat >( new SwVbaParagraphFormat( this, mxContext, mxTextDocument, xParaProps ) );
250 : }
251 :
252 : void SAL_CALL
253 0 : SwVbaRange::setParagraphFormat( const uno::Reference< word::XParagraphFormat >& /*rParagraphFormat*/ ) throw ( uno::RuntimeException, std::exception )
254 : {
255 0 : throw uno::RuntimeException("Not implemented", uno::Reference< uno::XInterface >() );
256 : }
257 :
258 0 : void SwVbaRange::GetStyleInfo(OUString& aStyleName, OUString& aStyleType ) throw ( uno::RuntimeException )
259 : {
260 0 : uno::Reference< beans::XPropertySet > xProp( mxTextCursor, uno::UNO_QUERY_THROW );
261 0 : if( ( xProp->getPropertyValue("CharStyleName") >>= aStyleName ) && !aStyleName.isEmpty() )
262 : {
263 0 : aStyleType = "CharacterStyles";
264 : }
265 0 : else if( ( xProp->getPropertyValue("ParaStyleName") >>= aStyleName ) && !aStyleName.isEmpty() )
266 : {
267 0 : aStyleType = "ParagraphStyles";
268 : }
269 0 : if( aStyleType.isEmpty() )
270 : {
271 0 : DebugHelper::exception( SbERR_INTERNAL_ERROR, OUString() );
272 0 : }
273 0 : }
274 :
275 : uno::Any SAL_CALL
276 0 : SwVbaRange::getStyle() throw ( uno::RuntimeException, std::exception )
277 : {
278 0 : OUString aStyleName;
279 0 : OUString aStyleType;
280 0 : GetStyleInfo( aStyleName, aStyleType );
281 0 : uno::Reference< style::XStyleFamiliesSupplier > xStyleSupplier( mxTextDocument, uno::UNO_QUERY_THROW);
282 0 : uno::Reference< container::XNameAccess > xStylesAccess( xStyleSupplier->getStyleFamilies()->getByName( aStyleType ), uno::UNO_QUERY_THROW );
283 0 : uno::Reference< beans::XPropertySet > xStyleProps( xStylesAccess->getByName( aStyleName ), uno::UNO_QUERY_THROW );
284 0 : uno::Reference< frame::XModel > xModel( mxTextDocument, uno::UNO_QUERY_THROW );
285 0 : return uno::makeAny( uno::Reference< word::XStyle >( new SwVbaStyle( this, mxContext, xModel, xStyleProps ) ) );
286 : }
287 :
288 : void SAL_CALL
289 0 : SwVbaRange::setStyle( const uno::Any& rStyle ) throw ( uno::RuntimeException, std::exception )
290 : {
291 0 : uno::Reference< beans::XPropertySet > xParaProps( mxTextCursor, uno::UNO_QUERY_THROW );
292 0 : SwVbaStyle::setStyle( xParaProps, rStyle );
293 0 : }
294 :
295 : uno::Reference< word::XFont > SAL_CALL
296 0 : SwVbaRange::getFont() throw ( uno::RuntimeException, std::exception )
297 : {
298 0 : VbaPalette aColors;
299 0 : return new SwVbaFont( mxParent, mxContext, aColors.getPalette(), uno::Reference< beans::XPropertySet >( getXTextRange(), uno::UNO_QUERY_THROW ) );
300 : }
301 :
302 : uno::Reference< word::XListFormat > SAL_CALL
303 0 : SwVbaRange::getListFormat() throw ( uno::RuntimeException, std::exception )
304 : {
305 0 : return uno::Reference< word::XListFormat >( new SwVbaListFormat( this, mxContext, getXTextRange() ) );
306 : }
307 :
308 0 : ::sal_Int32 SAL_CALL SwVbaRange::getLanguageID() throw (uno::RuntimeException, std::exception)
309 : {
310 0 : uno::Reference< beans::XPropertySet > xParaProps( mxTextCursor, uno::UNO_QUERY_THROW );
311 0 : return SwVbaStyle::getLanguageID( xParaProps );
312 : }
313 :
314 0 : void SAL_CALL SwVbaRange::setLanguageID( ::sal_Int32 _languageid ) throw (uno::RuntimeException, std::exception)
315 : {
316 0 : uno::Reference< beans::XPropertySet > xParaProps( mxTextCursor, uno::UNO_QUERY_THROW );
317 0 : SwVbaStyle::setLanguageID( xParaProps, _languageid );
318 0 : }
319 :
320 : uno::Any SAL_CALL
321 0 : SwVbaRange::PageSetup( ) throw (uno::RuntimeException, std::exception)
322 : {
323 0 : uno::Reference< beans::XPropertySet > xParaProps( mxTextCursor, uno::UNO_QUERY_THROW );
324 0 : uno::Reference< frame::XModel > xModel( mxTextDocument, uno::UNO_QUERY_THROW );
325 0 : OUString aPageStyleName;
326 0 : xParaProps->getPropertyValue("PageStyleName") >>= aPageStyleName;
327 0 : uno::Reference< style::XStyleFamiliesSupplier > xSytleFamSupp( xModel, uno::UNO_QUERY_THROW );
328 0 : uno::Reference< container::XNameAccess > xSytleFamNames( xSytleFamSupp->getStyleFamilies(), uno::UNO_QUERY_THROW );
329 0 : uno::Reference< container::XNameAccess > xPageStyles( xSytleFamNames->getByName("PageStyles"), uno::UNO_QUERY_THROW );
330 0 : uno::Reference< beans::XPropertySet > xPageProps( xPageStyles->getByName( aPageStyleName ), uno::UNO_QUERY_THROW );
331 0 : return uno::makeAny( uno::Reference< word::XPageSetup >( new SwVbaPageSetup( this, mxContext, xModel, xPageProps ) ) );
332 : }
333 :
334 0 : ::sal_Int32 SAL_CALL SwVbaRange::getStart() throw (uno::RuntimeException, std::exception)
335 : {
336 0 : uno::Reference< text::XText > xText = mxTextDocument->getText();
337 0 : return SwVbaRangeHelper::getPosition( xText, mxTextCursor->getStart() );
338 : }
339 :
340 0 : void SAL_CALL SwVbaRange::setStart( ::sal_Int32 _start ) throw (uno::RuntimeException, std::exception)
341 : {
342 0 : uno::Reference< text::XText > xText = mxTextDocument->getText();
343 0 : uno::Reference< text::XTextRange > xStart = SwVbaRangeHelper::getRangeByPosition( xText, _start );
344 0 : uno::Reference< text::XTextRange > xEnd = mxTextCursor->getEnd();
345 :
346 0 : mxTextCursor->gotoRange( xStart, sal_False );
347 0 : mxTextCursor->gotoRange( xEnd, sal_True );
348 0 : }
349 :
350 0 : ::sal_Int32 SAL_CALL SwVbaRange::getEnd() throw (uno::RuntimeException, std::exception)
351 : {
352 0 : uno::Reference< text::XText > xText = mxTextDocument->getText();
353 0 : return SwVbaRangeHelper::getPosition( xText, mxTextCursor->getEnd() );
354 : }
355 :
356 0 : void SAL_CALL SwVbaRange::setEnd( ::sal_Int32 _end ) throw (uno::RuntimeException, std::exception)
357 : {
358 0 : uno::Reference< text::XText > xText = mxTextDocument->getText();
359 0 : uno::Reference< text::XTextRange > xEnd = SwVbaRangeHelper::getRangeByPosition( xText, _end );
360 :
361 0 : mxTextCursor->collapseToStart();
362 0 : mxTextCursor->gotoRange( xEnd, sal_True );
363 0 : }
364 :
365 0 : sal_Bool SAL_CALL SwVbaRange::InRange( const uno::Reference< ::ooo::vba::word::XRange >& Range ) throw (uno::RuntimeException, std::exception)
366 : {
367 0 : SwVbaRange* pRange = dynamic_cast< SwVbaRange* >( Range.get() );
368 0 : if( !pRange )
369 0 : throw uno::RuntimeException();
370 0 : uno::Reference< text::XTextRange > xTextRange = pRange->getXTextRange();
371 0 : uno::Reference< text::XTextRangeCompare > xTRC( mxTextCursor->getText(), uno::UNO_QUERY_THROW );
372 0 : if( xTRC->compareRegionStarts( xTextRange, getXTextRange() ) >= 0 && xTRC->compareRegionEnds( xTextRange, getXTextRange() ) <= 0 )
373 0 : return sal_True;
374 0 : return sal_False;
375 : }
376 :
377 : uno::Any SAL_CALL
378 0 : SwVbaRange::Revisions( const uno::Any& index ) throw (uno::RuntimeException, std::exception)
379 : {
380 0 : uno::Reference< text::XTextRange > xTextRange = getXTextRange();
381 0 : uno::Reference< frame::XModel > xModel( mxTextDocument, uno::UNO_QUERY_THROW );
382 0 : uno::Reference< XCollection > xCol( new SwVbaRevisions( mxParent, mxContext, xModel, xTextRange ) );
383 0 : if ( index.hasValue() )
384 0 : return xCol->Item( index, uno::Any() );
385 0 : return uno::makeAny( xCol );
386 : }
387 :
388 : uno::Any SAL_CALL
389 0 : SwVbaRange::Sections( const uno::Any& index ) throw (uno::RuntimeException, std::exception)
390 : {
391 0 : uno::Reference< text::XTextRange > xTextRange = getXTextRange();
392 0 : uno::Reference< frame::XModel > xModel( mxTextDocument, uno::UNO_QUERY_THROW );
393 0 : uno::Reference< XCollection > xCol( new SwVbaSections( mxParent, mxContext, xModel, xTextRange ) );
394 0 : if ( index.hasValue() )
395 0 : return xCol->Item( index, uno::Any() );
396 0 : return uno::makeAny( xCol );
397 : }
398 :
399 : uno::Any SAL_CALL
400 0 : SwVbaRange::Fields( const uno::Any& index ) throw (uno::RuntimeException, std::exception)
401 : {
402 : //FIXME: should be get the field in current range
403 0 : uno::Reference< frame::XModel > xModel( mxTextDocument, uno::UNO_QUERY_THROW );
404 0 : uno::Reference< XCollection > xCol( new SwVbaFields( mxParent, mxContext, xModel ) );
405 0 : if ( index.hasValue() )
406 0 : return xCol->Item( index, uno::Any() );
407 0 : return uno::makeAny( xCol );
408 : }
409 :
410 : OUString
411 0 : SwVbaRange::getServiceImplName()
412 : {
413 0 : return OUString("SwVbaRange");
414 : }
415 :
416 : uno::Sequence< OUString >
417 0 : SwVbaRange::getServiceNames()
418 : {
419 0 : static uno::Sequence< OUString > aServiceNames;
420 0 : if ( aServiceNames.getLength() == 0 )
421 : {
422 0 : aServiceNames.realloc( 1 );
423 0 : aServiceNames[ 0 ] = "ooo.vba.word.Range";
424 : }
425 0 : return aServiceNames;
426 : }
427 :
428 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|