1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #include "vbarange.hxx"
31 #include <vbahelper/vbahelper.hxx>
32 #include <tools/diagnose_ex.h>
33 #include "vbarangehelper.hxx"
34 #include <ooo/vba/word/WdBreakType.hpp>
35 #include <com/sun/star/style/BreakType.hpp>
36 #include <com/sun/star/text/ControlCharacter.hpp>
37 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
38 #include "wordvbahelper.hxx"
39 #include "vbaparagraphformat.hxx"
40 #include "vbastyle.hxx"
41 #include "vbafont.hxx"
42 #include "vbapalette.hxx"
43 #include "vbapagesetup.hxx"
45 using namespace ::ooo::vba
;
46 using namespace ::com::sun::star
;
48 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
)
50 uno::Reference
< text::XTextRange
> xEnd
;
51 initialize( rStart
, xEnd
);
54 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
)
56 initialize( rStart
, rEnd
);
59 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
)
61 initialize( rStart
, rEnd
);
64 SwVbaRange::~SwVbaRange()
68 void SwVbaRange::initialize( const uno::Reference
< text::XTextRange
>& rStart
, const uno::Reference
< text::XTextRange
>& rEnd
) throw (uno::RuntimeException
)
72 mxText
= mxTextDocument
->getText();
75 mxTextCursor
= SwVbaRangeHelper::initCursor( rStart
, mxText
);
76 mxTextCursor
->collapseToStart();
79 mxTextCursor
->gotoRange( rEnd
, sal_True
);
81 mxTextCursor
->gotoEnd( sal_True
);
84 uno::Reference
< text::XTextRange
> SAL_CALL
85 SwVbaRange::getXTextRange() throw (uno::RuntimeException
)
87 uno::Reference
< text::XTextRange
> xTextRange( mxTextCursor
, uno::UNO_QUERY_THROW
);
91 void SwVbaRange::setXTextRange( const uno::Reference
< text::XTextRange
>& xRange
) throw (uno::RuntimeException
)
93 mxTextCursor
->gotoRange( xRange
->getStart(), sal_False
);
94 mxTextCursor
->gotoRange( xRange
->getEnd(), sal_True
);
98 * The complexity in this method is because we need to workaround
99 * an issue that the last paragraph in a document does not have a trailing CRLF.
102 rtl::OUString SAL_CALL
103 SwVbaRange::getText() throw ( uno::RuntimeException
)
105 rtl::OUString aText
= mxTextCursor
->getString();
106 sal_Int32 nLen
= aText
.getLength();
108 // FIXME: should add a line separator if the range includes the last paragraph
111 if( mxTextCursor
->isCollapsed() )
113 mxTextCursor
->goRight( 1, sal_True
);
114 aText
= mxTextCursor
->getString();
115 mxTextCursor
->collapseToStart();
119 uno::Reference
< text::XTextRange
> xStart
= mxTextCursor
->getStart();
120 uno::Reference
< text::XTextRange
> xEnd
= mxTextCursor
->getEnd();
121 mxTextCursor
->collapseToEnd();
122 mxTextCursor
->goRight( 1, sal_True
);
123 mxTextCursor
->gotoRange( xStart
, sal_False
);
124 mxTextCursor
->gotoRange( xEnd
, sal_True
);
132 SwVbaRange::setText( const rtl::OUString
& rText
) throw ( uno::RuntimeException
)
134 if( rText
.indexOf( '\n' ) != -1 )
136 mxTextCursor
->setString( rtl::OUString() );
137 // process CR in strings
138 uno::Reference
< text::XTextRange
> xRange( mxTextCursor
, uno::UNO_QUERY_THROW
);
139 SwVbaRangeHelper::insertString( xRange
, mxText
, rText
, sal_True
);
143 mxTextCursor
->setString( rText
);
147 // FIXME: test is not pass
148 void SAL_CALL
SwVbaRange::InsertBreak( const uno::Any
& _breakType
) throw (uno::RuntimeException
)
150 // default type is wdPageBreak;
151 sal_Int32 nBreakType
= word::WdBreakType::wdPageBreak
;
152 if( _breakType
.hasValue() )
153 _breakType
>>= nBreakType
;
155 style::BreakType eBreakType
= style::BreakType_NONE
;
158 case word::WdBreakType::wdPageBreak
:
159 eBreakType
= style::BreakType_PAGE_BEFORE
;
161 case word::WdBreakType::wdColumnBreak
:
162 eBreakType
= style::BreakType_COLUMN_AFTER
;
164 case word::WdBreakType::wdLineBreak
:
165 case word::WdBreakType::wdLineBreakClearLeft
:
166 case word::WdBreakType::wdLineBreakClearRight
:
167 case word::WdBreakType::wdSectionBreakContinuous
:
168 case word::WdBreakType::wdSectionBreakEvenPage
:
169 case word::WdBreakType::wdSectionBreakNextPage
:
170 case word::WdBreakType::wdSectionBreakOddPage
:
171 case word::WdBreakType::wdTextWrappingBreak
:
172 DebugHelper::exception( SbERR_NOT_IMPLEMENTED
, rtl::OUString() );
175 DebugHelper::exception( SbERR_BAD_PARAMETER
, rtl::OUString() );
178 if( eBreakType
!= style::BreakType_NONE
)
180 if( !mxTextCursor
->isCollapsed() )
182 mxTextCursor
->setString( rtl::OUString() );
183 mxTextCursor
->collapseToStart();
186 uno::Reference
< beans::XPropertySet
> xProp( mxTextCursor
, uno::UNO_QUERY_THROW
);
187 xProp
->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("BreakType") ), uno::makeAny( eBreakType
) );
192 SwVbaRange::Select() throw ( uno::RuntimeException
)
194 uno::Reference
< frame::XModel
> xModel( mxTextDocument
, uno::UNO_QUERY_THROW
);
195 uno::Reference
< text::XTextViewCursor
> xTextViewCursor
= word::getXTextViewCursor( xModel
);
196 xTextViewCursor
->gotoRange( mxTextCursor
->getStart(), sal_False
);
197 xTextViewCursor
->gotoRange( mxTextCursor
->getEnd(), sal_True
);
201 SwVbaRange::InsertParagraph() throw ( uno::RuntimeException
)
203 mxTextCursor
->setString( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("") ) );
204 InsertParagraphBefore();
208 SwVbaRange::InsertParagraphBefore() throw ( uno::RuntimeException
)
210 uno::Reference
< text::XTextRange
> xTextRange
= mxTextCursor
->getStart();
211 mxText
->insertControlCharacter( xTextRange
, text::ControlCharacter::PARAGRAPH_BREAK
, sal_True
);
212 mxTextCursor
->gotoRange( xTextRange
, sal_True
);
216 SwVbaRange::InsertParagraphAfter() throw ( uno::RuntimeException
)
218 uno::Reference
< text::XTextRange
> xTextRange
= mxTextCursor
->getEnd();
219 mxText
->insertControlCharacter( xTextRange
, text::ControlCharacter::PARAGRAPH_BREAK
, sal_True
);
222 uno::Reference
< word::XParagraphFormat
> SAL_CALL
223 SwVbaRange::getParagraphFormat() throw ( uno::RuntimeException
)
225 uno::Reference
< beans::XPropertySet
> xParaProps( mxTextCursor
, uno::UNO_QUERY_THROW
);
226 return uno::Reference
< word::XParagraphFormat
>( new SwVbaParagraphFormat( this, mxContext
, mxTextDocument
, xParaProps
) );
230 SwVbaRange::setParagraphFormat( const uno::Reference
< word::XParagraphFormat
>& /*rParagraphFormat*/ ) throw ( uno::RuntimeException
)
232 throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Not implemented") ), uno::Reference
< uno::XInterface
>() );
235 uno::Reference
< word::XStyle
> SAL_CALL
236 SwVbaRange::getStyle() throw ( uno::RuntimeException
)
238 rtl::OUString aStyleName
;
239 rtl::OUString aStyleType
;
240 uno::Reference
< beans::XPropertySet
> xProp( mxTextCursor
, uno::UNO_QUERY_THROW
);
241 if( ( xProp
->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("CharStyleName") ) ) >>= aStyleName
) && aStyleName
.getLength() )
243 aStyleType
= rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("CharacterStyles") );
245 else if( ( xProp
->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ParaStyleName") ) ) >>= aStyleName
) && aStyleName
.getLength() )
247 aStyleType
= rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ParagraphStyles") );
249 if( aStyleType
.getLength() == 0 )
251 DebugHelper::exception( SbERR_INTERNAL_ERROR
, rtl::OUString() );
253 uno::Reference
< style::XStyleFamiliesSupplier
> xStyleSupplier( mxTextDocument
, uno::UNO_QUERY_THROW
);
254 uno::Reference
< container::XNameAccess
> xStylesAccess( xStyleSupplier
->getStyleFamilies()->getByName( aStyleType
), uno::UNO_QUERY_THROW
);
255 uno::Reference
< beans::XPropertySet
> xStyleProps( xStylesAccess
->getByName( aStyleName
), uno::UNO_QUERY_THROW
);
256 return uno::Reference
< word::XStyle
>( new SwVbaStyle( this, mxContext
, xStyleProps
) );
260 SwVbaRange::setStyle( const uno::Reference
< word::XStyle
>& rStyle
) throw ( uno::RuntimeException
)
262 uno::Reference
< beans::XPropertySet
> xParaProps( mxTextCursor
, uno::UNO_QUERY_THROW
);
263 SwVbaStyle::setStyle( xParaProps
, rStyle
);
266 uno::Reference
< word::XFont
> SAL_CALL
267 SwVbaRange::getFont() throw ( uno::RuntimeException
)
270 return new SwVbaFont( mxParent
, mxContext
, aColors
.getPalette(), uno::Reference
< beans::XPropertySet
>( getXTextRange(), uno::UNO_QUERY_THROW
) );
273 ::sal_Int32 SAL_CALL
SwVbaRange::getLanguageID() throw (uno::RuntimeException
)
275 uno::Reference
< beans::XPropertySet
> xParaProps( mxTextCursor
, uno::UNO_QUERY_THROW
);
276 return SwVbaStyle::getLanguageID( xParaProps
);
279 void SAL_CALL
SwVbaRange::setLanguageID( ::sal_Int32 _languageid
) throw (uno::RuntimeException
)
281 uno::Reference
< beans::XPropertySet
> xParaProps( mxTextCursor
, uno::UNO_QUERY_THROW
);
282 SwVbaStyle::setLanguageID( xParaProps
, _languageid
);
286 SwVbaRange::PageSetup( ) throw (uno::RuntimeException
)
288 uno::Reference
< beans::XPropertySet
> xParaProps( mxTextCursor
, uno::UNO_QUERY_THROW
);
289 uno::Reference
< frame::XModel
> xModel( mxTextDocument
, uno::UNO_QUERY_THROW
);
290 rtl::OUString aPageStyleName
;
291 xParaProps
->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("PageStyleName"))) >>= aPageStyleName
;
292 uno::Reference
< style::XStyleFamiliesSupplier
> xSytleFamSupp( xModel
, uno::UNO_QUERY_THROW
);
293 uno::Reference
< container::XNameAccess
> xSytleFamNames( xSytleFamSupp
->getStyleFamilies(), uno::UNO_QUERY_THROW
);
294 uno::Reference
< container::XNameAccess
> xPageStyles( xSytleFamNames
->getByName( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("PageStyles") ) ), uno::UNO_QUERY_THROW
);
295 uno::Reference
< beans::XPropertySet
> xPageProps( xPageStyles
->getByName( aPageStyleName
), uno::UNO_QUERY_THROW
);
296 return uno::makeAny( uno::Reference
< word::XPageSetup
>( new SwVbaPageSetup( this, mxContext
, xModel
, xPageProps
) ) );
299 ::sal_Int32 SAL_CALL
SwVbaRange::getStart() throw (uno::RuntimeException
)
301 uno::Reference
< text::XText
> xText
= mxTextDocument
->getText();
302 return SwVbaRangeHelper::getPosition( xText
, mxTextCursor
->getStart() );
305 void SAL_CALL
SwVbaRange::setStart( ::sal_Int32 _start
) throw (uno::RuntimeException
)
307 uno::Reference
< text::XText
> xText
= mxTextDocument
->getText();
308 uno::Reference
< text::XTextRange
> xStart
= SwVbaRangeHelper::getRangeByPosition( xText
, _start
);
309 uno::Reference
< text::XTextRange
> xEnd
= mxTextCursor
->getEnd();
311 mxTextCursor
->gotoRange( xStart
, sal_False
);
312 mxTextCursor
->gotoRange( xEnd
, sal_True
);
315 ::sal_Int32 SAL_CALL
SwVbaRange::getEnd() throw (uno::RuntimeException
)
317 uno::Reference
< text::XText
> xText
= mxTextDocument
->getText();
318 return SwVbaRangeHelper::getPosition( xText
, mxTextCursor
->getEnd() );
321 void SAL_CALL
SwVbaRange::setEnd( ::sal_Int32 _end
) throw (uno::RuntimeException
)
323 uno::Reference
< text::XText
> xText
= mxTextDocument
->getText();
324 uno::Reference
< text::XTextRange
> xEnd
= SwVbaRangeHelper::getRangeByPosition( xText
, _end
);
326 mxTextCursor
->collapseToStart();
327 mxTextCursor
->gotoRange( xEnd
, sal_True
);
331 SwVbaRange::getServiceImplName()
333 static rtl::OUString
sImplName( RTL_CONSTASCII_USTRINGPARAM("SwVbaRange") );
337 uno::Sequence
< rtl::OUString
>
338 SwVbaRange::getServiceNames()
340 static uno::Sequence
< rtl::OUString
> aServiceNames
;
341 if ( aServiceNames
.getLength() == 0 )
343 aServiceNames
.realloc( 1 );
344 aServiceNames
[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.word.Range" ) );
346 return aServiceNames
;