android: Update app-specific/MIME type icons
[LibreOffice.git] / sw / source / ui / vba / vbarange.cxx
blobc48dd6193c2f648e1d357971109ae9e9268233d3
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #include "vbarange.hxx"
20 #include <utility>
21 #include <vbahelper/vbahelper.hxx>
22 #include <basic/sberrors.hxx>
23 #include "vbarangehelper.hxx"
24 #include <ooo/vba/word/WdBreakType.hpp>
25 #include <com/sun/star/style/BreakType.hpp>
26 #include <com/sun/star/text/ControlCharacter.hpp>
27 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
28 #include <com/sun/star/text/XTextRangeCompare.hpp>
29 #include <com/sun/star/text/XTextViewCursor.hpp>
30 #include "vbaparagraphformat.hxx"
31 #include "vbastyle.hxx"
32 #include "vbafont.hxx"
33 #include "vbafind.hxx"
34 #include "vbapalette.hxx"
35 #include "vbapagesetup.hxx"
36 #include "vbalistformat.hxx"
37 #include "vbarevisions.hxx"
38 #include "vbabookmarks.hxx"
39 #include "vbasections.hxx"
40 #include "vbafield.hxx"
41 #include "wordvbahelper.hxx"
43 using namespace ::ooo::vba;
44 using namespace ::com::sun::star;
46 SwVbaRange::SwVbaRange( const uno::Reference< ooo::vba::XHelperInterface >& rParent, const uno::Reference< uno::XComponentContext >& rContext, uno::Reference< text::XTextDocument > xTextDocument, const uno::Reference< text::XTextRange >& rStart ) : SwVbaRange_BASE( rParent, rContext ), mxTextDocument(std::move( xTextDocument ))
48 uno::Reference< text::XTextRange > xEnd;
49 initialize( rStart, xEnd );
52 SwVbaRange::SwVbaRange( const uno::Reference< ooo::vba::XHelperInterface >& rParent, const uno::Reference< uno::XComponentContext >& rContext, uno::Reference< text::XTextDocument > xTextDocument, const uno::Reference< text::XTextRange >& rStart, const uno::Reference< text::XTextRange >& rEnd ) : SwVbaRange_BASE( rParent, rContext ), mxTextDocument(std::move( xTextDocument ))
54 initialize( rStart, rEnd );
57 SwVbaRange::SwVbaRange( const uno::Reference< ooo::vba::XHelperInterface >& rParent, const uno::Reference< uno::XComponentContext >& rContext, uno::Reference< text::XTextDocument > xTextDocument, const uno::Reference< text::XTextRange >& rStart, const uno::Reference< text::XTextRange >& rEnd, uno::Reference< text::XText > xText ) : SwVbaRange_BASE( rParent, rContext ),mxTextDocument(std::move( xTextDocument )), mxText(std::move( xText ))
59 initialize( rStart, rEnd );
62 SwVbaRange::~SwVbaRange()
66 void SwVbaRange::initialize( const uno::Reference< text::XTextRange >& rStart, const uno::Reference< text::XTextRange >& rEnd )
68 if( !mxText.is() )
70 mxText = mxTextDocument->getText();
73 mxTextCursor = SwVbaRangeHelper::initCursor( rStart, mxText );
74 if( !mxTextCursor.is() )
75 throw uno::RuntimeException("Fails to create text cursor" );
76 mxTextCursor->collapseToStart();
78 if( rEnd.is() )
79 mxTextCursor->gotoRange( rEnd, true );
80 else
81 mxTextCursor->gotoEnd( true );
84 uno::Reference< text::XTextRange > SAL_CALL
85 SwVbaRange::getXTextRange()
87 uno::Reference< text::XTextRange > xTextRange( mxTextCursor, uno::UNO_QUERY_THROW );
88 return xTextRange;
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
96 OUString SAL_CALL
97 SwVbaRange::getText()
99 OUString aText = mxTextCursor->getString();
100 sal_Int32 nLen = aText.getLength();
102 // FIXME: should add a line separator if the range includes the last paragraph
103 if( nLen == 0 )
105 if( mxTextCursor->isCollapsed() )
107 mxTextCursor->goRight( 1, true );
108 aText = mxTextCursor->getString();
109 mxTextCursor->collapseToStart();
111 else
113 uno::Reference< text::XTextRange > xStart = mxTextCursor->getStart();
114 uno::Reference< text::XTextRange > xEnd = mxTextCursor->getEnd();
115 mxTextCursor->collapseToEnd();
116 mxTextCursor->goRight( 1, true );
117 mxTextCursor->gotoRange( xStart, false );
118 mxTextCursor->gotoRange( xEnd, true );
122 return aText;
125 void SAL_CALL
126 SwVbaRange::setText( const OUString& rText )
128 // Emulate the MSWord behavior, Don't delete the bookmark
129 // which contains no text string in current inserting position,
130 OUString sName;
131 uno::Reference< text::XTextRange > xRange( mxTextCursor, uno::UNO_QUERY_THROW );
134 uno::Reference< text::XTextContent > xBookmark = SwVbaRangeHelper::findBookmarkByPosition( mxTextDocument, xRange->getStart() );
135 if( xBookmark.is() )
137 uno::Reference< container::XNamed > xNamed( xBookmark, uno::UNO_QUERY_THROW );
138 sName = xNamed->getName();
141 catch (const uno::Exception&)
143 // do nothing
146 if( rText.indexOf( '\n' ) != -1 )
148 mxTextCursor->setString( OUString() );
149 // process CR in strings
150 SwVbaRangeHelper::insertString( xRange, mxText, rText, true );
152 else
154 mxTextCursor->setString( rText );
157 // insert the bookmark if the bookmark is deleted during setting text string
158 if( !sName.isEmpty() )
160 uno::Reference< text::XBookmarksSupplier > xBookmarksSupplier( mxTextDocument, uno::UNO_QUERY_THROW );
161 uno::Reference< container::XNameAccess > xNameAccess( xBookmarksSupplier->getBookmarks(), uno::UNO_SET_THROW );
162 if( !xNameAccess->hasByName( sName ) )
164 uno::Reference< frame::XModel > xModel( mxTextDocument, uno::UNO_QUERY_THROW );
165 SwVbaBookmarks::addBookmarkByName( xModel, sName, xRange->getStart() );
170 // FIXME: test is not pass
171 void SAL_CALL SwVbaRange::InsertBreak(const uno::Any& _breakType)
173 // default type is wdPageBreak;
174 sal_Int32 nBreakType = word::WdBreakType::wdPageBreak;
175 if( _breakType.hasValue() )
176 _breakType >>= nBreakType;
178 style::BreakType eBreakType = style::BreakType_NONE;
179 switch( nBreakType )
181 case word::WdBreakType::wdPageBreak:
182 eBreakType = style::BreakType_PAGE_BEFORE;
183 break;
184 case word::WdBreakType::wdColumnBreak:
185 eBreakType = style::BreakType_COLUMN_AFTER;
186 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 DebugHelper::basicexception( ERRCODE_BASIC_NOT_IMPLEMENTED, {} );
196 break;
197 default:
198 DebugHelper::basicexception( ERRCODE_BASIC_BAD_PARAMETER, {} );
201 if( eBreakType != style::BreakType_NONE )
203 if( !mxTextCursor->isCollapsed() )
205 mxTextCursor->setString( OUString() );
206 mxTextCursor->collapseToStart();
209 uno::Reference< beans::XPropertySet > xProp( mxTextCursor, uno::UNO_QUERY_THROW );
210 xProp->setPropertyValue("BreakType", uno::Any( eBreakType ) );
214 void SAL_CALL
215 SwVbaRange::Select()
217 uno::Reference< frame::XModel > xModel( mxTextDocument, uno::UNO_QUERY_THROW );
218 uno::Reference< text::XTextViewCursor > xTextViewCursor = word::getXTextViewCursor( xModel );
219 xTextViewCursor->gotoRange( mxTextCursor->getStart(), false );
220 xTextViewCursor->gotoRange( mxTextCursor->getEnd(), true );
223 void SAL_CALL
224 SwVbaRange::InsertParagraph()
226 mxTextCursor->setString( "" );
227 InsertParagraphBefore();
230 void SAL_CALL
231 SwVbaRange::InsertParagraphBefore()
233 uno::Reference< text::XTextRange > xTextRange = mxTextCursor->getStart();
234 mxText->insertControlCharacter( xTextRange, text::ControlCharacter::PARAGRAPH_BREAK, true );
235 mxTextCursor->gotoRange( xTextRange, true );
238 void SAL_CALL
239 SwVbaRange::InsertParagraphAfter()
241 uno::Reference< text::XTextRange > xTextRange = mxTextCursor->getEnd();
242 mxText->insertControlCharacter( xTextRange, text::ControlCharacter::PARAGRAPH_BREAK, true );
245 uno::Reference< word::XParagraphFormat > SAL_CALL
246 SwVbaRange::getParagraphFormat()
248 uno::Reference< beans::XPropertySet > xParaProps( mxTextCursor, uno::UNO_QUERY_THROW );
249 return uno::Reference< word::XParagraphFormat >( new SwVbaParagraphFormat( this, mxContext, xParaProps ) );
252 void SAL_CALL
253 SwVbaRange::setParagraphFormat( const uno::Reference< word::XParagraphFormat >& /*rParagraphFormat*/ )
255 throw uno::RuntimeException("Not implemented" );
258 void SwVbaRange::GetStyleInfo(OUString& aStyleName, OUString& aStyleType )
260 uno::Reference< beans::XPropertySet > xProp( mxTextCursor, uno::UNO_QUERY_THROW );
261 if( ( xProp->getPropertyValue("CharStyleName") >>= aStyleName ) && !aStyleName.isEmpty() )
263 aStyleType = "CharacterStyles";
265 else if( ( xProp->getPropertyValue("ParaStyleName") >>= aStyleName ) && !aStyleName.isEmpty() )
267 aStyleType = "ParagraphStyles";
269 if( aStyleType.isEmpty() )
271 DebugHelper::runtimeexception( ERRCODE_BASIC_INTERNAL_ERROR );
275 uno::Any SAL_CALL
276 SwVbaRange::getStyle()
278 OUString aStyleName;
279 OUString aStyleType;
280 GetStyleInfo( aStyleName, aStyleType );
281 uno::Reference< style::XStyleFamiliesSupplier > xStyleSupplier( mxTextDocument, uno::UNO_QUERY_THROW);
282 uno::Reference< container::XNameAccess > xStylesAccess( xStyleSupplier->getStyleFamilies()->getByName( aStyleType ), uno::UNO_QUERY_THROW );
283 uno::Reference< beans::XPropertySet > xStyleProps( xStylesAccess->getByName( aStyleName ), uno::UNO_QUERY_THROW );
284 uno::Reference< frame::XModel > xModel( mxTextDocument, uno::UNO_QUERY_THROW );
285 return uno::Any( uno::Reference< word::XStyle >( new SwVbaStyle( this, mxContext, xModel, xStyleProps ) ) );
288 void SAL_CALL
289 SwVbaRange::setStyle( const uno::Any& rStyle )
291 uno::Reference< beans::XPropertySet > xParaProps( mxTextCursor, uno::UNO_QUERY_THROW );
292 SwVbaStyle::setStyle( xParaProps, rStyle );
295 uno::Reference< word::XFont > SAL_CALL
296 SwVbaRange::getFont()
298 VbaPalette aColors;
299 return new SwVbaFont( mxParent, mxContext, aColors.getPalette(), uno::Reference< beans::XPropertySet >( getXTextRange(), uno::UNO_QUERY_THROW ) );
302 uno::Reference< word::XFind > SAL_CALL
303 SwVbaRange::getFind()
305 uno::Reference< text::XTextRange > xTextRange = getXTextRange();
306 uno::Reference< frame::XModel > xModel( mxTextDocument, uno::UNO_QUERY_THROW );
307 return SwVbaFind::GetOrCreateFind(this, mxContext, xModel, xTextRange);
310 uno::Reference< word::XListFormat > SAL_CALL
311 SwVbaRange::getListFormat()
313 return uno::Reference< word::XListFormat >( new SwVbaListFormat( this, mxContext, getXTextRange() ) );
316 ::sal_Int32 SAL_CALL SwVbaRange::getLanguageID()
318 uno::Reference< beans::XPropertySet > xParaProps( mxTextCursor, uno::UNO_QUERY_THROW );
319 return static_cast<sal_uInt16>(SwVbaStyle::getLanguageID( xParaProps ));
322 void SAL_CALL SwVbaRange::setLanguageID( ::sal_Int32 _languageid )
324 uno::Reference< beans::XPropertySet > xParaProps( mxTextCursor, uno::UNO_QUERY_THROW );
325 SwVbaStyle::setLanguageID( xParaProps, LanguageType(_languageid) );
328 uno::Any SAL_CALL
329 SwVbaRange::PageSetup( )
331 uno::Reference< beans::XPropertySet > xParaProps( mxTextCursor, uno::UNO_QUERY_THROW );
332 uno::Reference< frame::XModel > xModel( mxTextDocument, uno::UNO_QUERY_THROW );
333 OUString aPageStyleName;
334 xParaProps->getPropertyValue("PageStyleName") >>= aPageStyleName;
335 uno::Reference< style::XStyleFamiliesSupplier > xSytleFamSupp( xModel, uno::UNO_QUERY_THROW );
336 uno::Reference< container::XNameAccess > xSytleFamNames( xSytleFamSupp->getStyleFamilies(), uno::UNO_SET_THROW );
337 uno::Reference< container::XNameAccess > xPageStyles( xSytleFamNames->getByName("PageStyles"), uno::UNO_QUERY_THROW );
338 uno::Reference< beans::XPropertySet > xPageProps( xPageStyles->getByName( aPageStyleName ), uno::UNO_QUERY_THROW );
339 return uno::Any( uno::Reference< word::XPageSetup >( new SwVbaPageSetup( this, mxContext, xModel, xPageProps ) ) );
342 ::sal_Int32 SAL_CALL SwVbaRange::getStart()
344 uno::Reference< text::XText > xText = mxTextDocument->getText();
345 return SwVbaRangeHelper::getPosition( xText, mxTextCursor->getStart() );
348 void SAL_CALL SwVbaRange::setStart( ::sal_Int32 _start )
350 uno::Reference< text::XText > xText = mxTextDocument->getText();
351 uno::Reference< text::XTextRange > xStart = SwVbaRangeHelper::getRangeByPosition( xText, _start );
352 uno::Reference< text::XTextRange > xEnd = mxTextCursor->getEnd();
354 mxTextCursor->gotoRange( xStart, false );
355 mxTextCursor->gotoRange( xEnd, true );
358 ::sal_Int32 SAL_CALL SwVbaRange::getEnd()
360 uno::Reference< text::XText > xText = mxTextDocument->getText();
361 return SwVbaRangeHelper::getPosition( xText, mxTextCursor->getEnd() );
364 void SAL_CALL SwVbaRange::setEnd( ::sal_Int32 _end )
366 uno::Reference< text::XText > xText = mxTextDocument->getText();
367 uno::Reference< text::XTextRange > xEnd = SwVbaRangeHelper::getRangeByPosition( xText, _end );
369 mxTextCursor->collapseToStart();
370 mxTextCursor->gotoRange( xEnd, true );
373 sal_Bool SAL_CALL SwVbaRange::InRange( const uno::Reference< ::ooo::vba::word::XRange >& Range )
375 SwVbaRange* pRange = dynamic_cast< SwVbaRange* >( Range.get() );
376 if( !pRange )
377 throw uno::RuntimeException();
378 uno::Reference< text::XTextRange > xTextRange = pRange->getXTextRange();
379 uno::Reference< text::XTextRangeCompare > xTRC( mxTextCursor->getText(), uno::UNO_QUERY_THROW );
380 if( xTRC->compareRegionStarts( xTextRange, getXTextRange() ) >= 0 && xTRC->compareRegionEnds( xTextRange, getXTextRange() ) <= 0 )
381 return true;
382 return false;
385 uno::Any SAL_CALL
386 SwVbaRange::Revisions( const uno::Any& index )
388 uno::Reference< text::XTextRange > xTextRange = getXTextRange();
389 uno::Reference< frame::XModel > xModel( mxTextDocument, uno::UNO_QUERY_THROW );
390 uno::Reference< XCollection > xCol( new SwVbaRevisions( mxParent, mxContext, xModel, xTextRange ) );
391 if ( index.hasValue() )
392 return xCol->Item( index, uno::Any() );
393 return uno::Any( xCol );
396 uno::Any SAL_CALL
397 SwVbaRange::Sections( const uno::Any& index )
399 uno::Reference< text::XTextRange > xTextRange = getXTextRange();
400 uno::Reference< frame::XModel > xModel( mxTextDocument, uno::UNO_QUERY_THROW );
401 uno::Reference< XCollection > xCol( new SwVbaSections( mxParent, mxContext, xModel, xTextRange ) );
402 if ( index.hasValue() )
403 return xCol->Item( index, uno::Any() );
404 return uno::Any( xCol );
407 uno::Any SAL_CALL
408 SwVbaRange::Fields( const uno::Any& index )
410 //FIXME: should be get the field in current range
411 uno::Reference< frame::XModel > xModel( mxTextDocument, uno::UNO_QUERY_THROW );
412 uno::Reference< XCollection > xCol( new SwVbaFields( mxParent, mxContext, xModel ) );
413 if ( index.hasValue() )
414 return xCol->Item( index, uno::Any() );
415 return uno::Any( xCol );
418 OUString
419 SwVbaRange::getServiceImplName()
421 return "SwVbaRange";
424 uno::Sequence< OUString >
425 SwVbaRange::getServiceNames()
427 static uno::Sequence< OUString > const aServiceNames
429 "ooo.vba.word.Range"
431 return aServiceNames;
434 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */