xref: /aoo4110/main/sw/source/ui/vba/vbarange.cxx (revision b1cdbd2c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 #include "vbarange.hxx"
24 #include <vbahelper/vbahelper.hxx>
25 #include <tools/diagnose_ex.h>
26 #include "vbarangehelper.hxx"
27 #include <ooo/vba/word/WdBreakType.hpp>
28 #include <com/sun/star/style/BreakType.hpp>
29 #include <com/sun/star/text/ControlCharacter.hpp>
30 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
31 #include "wordvbahelper.hxx"
32 #include "vbaparagraphformat.hxx"
33 #include "vbastyle.hxx"
34 #include "vbafont.hxx"
35 #include "vbapalette.hxx"
36 #include "vbapagesetup.hxx"
37 
38 using namespace ::ooo::vba;
39 using namespace ::com::sun::star;
40 
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)41 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 )
42 {
43     uno::Reference< text::XTextRange > xEnd;
44     initialize( rStart, xEnd );
45 }
46 
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)47 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 )
48 {
49     initialize( rStart, rEnd );
50 }
51 
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)52 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 )
53 {
54     initialize( rStart, rEnd );
55 }
56 
~SwVbaRange()57 SwVbaRange::~SwVbaRange()
58 {
59 }
60 
initialize(const uno::Reference<text::XTextRange> & rStart,const uno::Reference<text::XTextRange> & rEnd)61 void SwVbaRange::initialize( const uno::Reference< text::XTextRange >& rStart, const uno::Reference< text::XTextRange >& rEnd ) throw (uno::RuntimeException)
62 {
63     if( !mxText.is() )
64     {
65         mxText = mxTextDocument->getText();
66     }
67 
68     mxTextCursor = SwVbaRangeHelper::initCursor( rStart, mxText );
69     mxTextCursor->collapseToStart();
70 
71     if( rEnd.is() )
72         mxTextCursor->gotoRange( rEnd, sal_True );
73     else
74         mxTextCursor->gotoEnd( sal_True );
75 }
76 
77 uno::Reference< text::XTextRange > SAL_CALL
getXTextRange()78 SwVbaRange::getXTextRange() throw (uno::RuntimeException)
79 {
80     uno::Reference< text::XTextRange > xTextRange( mxTextCursor, uno::UNO_QUERY_THROW );
81     return xTextRange;
82 }
83 #ifdef TOMORROW
setXTextRange(const uno::Reference<text::XTextRange> & xRange)84 void SwVbaRange::setXTextRange( const uno::Reference< text::XTextRange >& xRange ) throw (uno::RuntimeException)
85 {
86     mxTextCursor->gotoRange( xRange->getStart(), sal_False );
87     mxTextCursor->gotoRange( xRange->getEnd(), sal_True );
88 }
89 #endif
90 /**
91 * The complexity in this method is because we need to workaround
92 * an issue that the last paragraph in a document does not have a trailing CRLF.
93 * @return
94 */
95 rtl::OUString SAL_CALL
getText()96 SwVbaRange::getText() throw ( uno::RuntimeException )
97 {
98     rtl::OUString aText = mxTextCursor->getString();
99     sal_Int32 nLen = aText.getLength();
100 
101     // FIXME: should add a line separator if the range includes the last paragraph
102     if( nLen == 0 )
103     {
104         if( mxTextCursor->isCollapsed() )
105         {
106             mxTextCursor->goRight( 1, sal_True );
107             aText = mxTextCursor->getString();
108             mxTextCursor->collapseToStart();
109         }
110         else
111         {
112             uno::Reference< text::XTextRange > xStart = mxTextCursor->getStart();
113             uno::Reference< text::XTextRange > xEnd = mxTextCursor->getEnd();
114             mxTextCursor->collapseToEnd();
115             mxTextCursor->goRight( 1, sal_True );
116             mxTextCursor->gotoRange( xStart, sal_False );
117             mxTextCursor->gotoRange( xEnd, sal_True );
118         }
119     }
120 
121     return aText;
122 }
123 
124 void SAL_CALL
setText(const rtl::OUString & rText)125 SwVbaRange::setText( const rtl::OUString& rText ) throw ( uno::RuntimeException )
126 {
127     if( rText.indexOf( '\n' )  != -1 )
128     {
129         mxTextCursor->setString( rtl::OUString() );
130         // process CR in strings
131         uno::Reference< text::XTextRange > xRange( mxTextCursor, uno::UNO_QUERY_THROW );
132         SwVbaRangeHelper::insertString( xRange, mxText, rText, sal_True );
133     }
134     else
135     {
136         mxTextCursor->setString( rText );
137     }
138 }
139 
140 // FIXME: test is not pass
InsertBreak(const uno::Any & _breakType)141 void SAL_CALL SwVbaRange::InsertBreak( const uno::Any& _breakType ) throw (uno::RuntimeException)
142 {
143     // default type is wdPageBreak;
144     sal_Int32 nBreakType = word::WdBreakType::wdPageBreak;
145     if( _breakType.hasValue() )
146         _breakType >>= nBreakType;
147 
148     style::BreakType eBreakType = style::BreakType_NONE;
149     switch( nBreakType )
150     {
151         case word::WdBreakType::wdPageBreak:
152             eBreakType = style::BreakType_PAGE_BEFORE;
153             break;
154         case word::WdBreakType::wdColumnBreak:
155             eBreakType = style::BreakType_COLUMN_AFTER;
156             break;
157         case word::WdBreakType::wdLineBreak:
158         case word::WdBreakType::wdLineBreakClearLeft:
159         case word::WdBreakType::wdLineBreakClearRight:
160         case word::WdBreakType::wdSectionBreakContinuous:
161         case word::WdBreakType::wdSectionBreakEvenPage:
162         case word::WdBreakType::wdSectionBreakNextPage:
163         case word::WdBreakType::wdSectionBreakOddPage:
164         case word::WdBreakType::wdTextWrappingBreak:
165             DebugHelper::exception( SbERR_NOT_IMPLEMENTED, rtl::OUString() );
166             break;
167         default:
168             DebugHelper::exception( SbERR_BAD_PARAMETER, rtl::OUString() );
169     }
170 
171     if( eBreakType != style::BreakType_NONE )
172     {
173         if( !mxTextCursor->isCollapsed() )
174         {
175             mxTextCursor->setString( rtl::OUString() );
176             mxTextCursor->collapseToStart();
177         }
178 
179         uno::Reference< beans::XPropertySet > xProp( mxTextCursor, uno::UNO_QUERY_THROW );
180         xProp->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("BreakType") ), uno::makeAny( eBreakType ) );
181     }
182 }
183 
184 void SAL_CALL
Select()185 SwVbaRange::Select() throw ( uno::RuntimeException )
186 {
187     uno::Reference< frame::XModel > xModel( mxTextDocument, uno::UNO_QUERY_THROW );
188     uno::Reference< text::XTextViewCursor > xTextViewCursor = word::getXTextViewCursor( xModel );
189     xTextViewCursor->gotoRange( mxTextCursor->getStart(), sal_False );
190     xTextViewCursor->gotoRange( mxTextCursor->getEnd(), sal_True );
191 }
192 
193 void SAL_CALL
InsertParagraph()194 SwVbaRange::InsertParagraph() throw ( uno::RuntimeException )
195 {
196     mxTextCursor->setString( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("") ) );
197     InsertParagraphBefore();
198 }
199 
200 void SAL_CALL
InsertParagraphBefore()201 SwVbaRange::InsertParagraphBefore() throw ( uno::RuntimeException )
202 {
203     uno::Reference< text::XTextRange > xTextRange = mxTextCursor->getStart();
204     mxText->insertControlCharacter( xTextRange, text::ControlCharacter::PARAGRAPH_BREAK, sal_True );
205     mxTextCursor->gotoRange( xTextRange, sal_True );
206 }
207 
208 void SAL_CALL
InsertParagraphAfter()209 SwVbaRange::InsertParagraphAfter() throw ( uno::RuntimeException )
210 {
211     uno::Reference< text::XTextRange > xTextRange = mxTextCursor->getEnd();
212     mxText->insertControlCharacter( xTextRange, text::ControlCharacter::PARAGRAPH_BREAK, sal_True );
213 }
214 
215 uno::Reference< word::XParagraphFormat > SAL_CALL
getParagraphFormat()216 SwVbaRange::getParagraphFormat() throw ( uno::RuntimeException )
217 {
218     uno::Reference< beans::XPropertySet > xParaProps( mxTextCursor, uno::UNO_QUERY_THROW );
219     return uno::Reference< word::XParagraphFormat >( new SwVbaParagraphFormat( this, mxContext, mxTextDocument, xParaProps ) );
220 }
221 
222 void SAL_CALL
setParagraphFormat(const uno::Reference<word::XParagraphFormat> &)223 SwVbaRange::setParagraphFormat( const uno::Reference< word::XParagraphFormat >& /*rParagraphFormat*/ ) throw ( uno::RuntimeException )
224 {
225     throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Not implemented") ), uno::Reference< uno::XInterface >() );
226 }
227 
228 uno::Reference< word::XStyle > SAL_CALL
getStyle()229 SwVbaRange::getStyle() throw ( uno::RuntimeException )
230 {
231     rtl::OUString aStyleName;
232     rtl::OUString aStyleType;
233     uno::Reference< beans::XPropertySet > xProp( mxTextCursor, uno::UNO_QUERY_THROW );
234     if( ( xProp->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("CharStyleName") ) ) >>= aStyleName ) && aStyleName.getLength() )
235     {
236         aStyleType = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("CharacterStyles") );
237     }
238     else if( ( xProp->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ParaStyleName") ) ) >>= aStyleName ) && aStyleName.getLength() )
239     {
240         aStyleType = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ParagraphStyles") );
241     }
242     if( aStyleType.getLength() == 0 )
243     {
244         DebugHelper::exception( SbERR_INTERNAL_ERROR, rtl::OUString() );
245     }
246     uno::Reference< style::XStyleFamiliesSupplier > xStyleSupplier( mxTextDocument, uno::UNO_QUERY_THROW);
247     uno::Reference< container::XNameAccess > xStylesAccess( xStyleSupplier->getStyleFamilies()->getByName( aStyleType ), uno::UNO_QUERY_THROW );
248     uno::Reference< beans::XPropertySet > xStyleProps( xStylesAccess->getByName( aStyleName ), uno::UNO_QUERY_THROW );
249     return uno::Reference< word::XStyle >( new SwVbaStyle( this, mxContext, xStyleProps ) );
250 }
251 
252 void SAL_CALL
setStyle(const uno::Reference<word::XStyle> & rStyle)253 SwVbaRange::setStyle( const uno::Reference< word::XStyle >& rStyle ) throw ( uno::RuntimeException )
254 {
255     uno::Reference< beans::XPropertySet > xParaProps( mxTextCursor, uno::UNO_QUERY_THROW );
256     SwVbaStyle::setStyle( xParaProps, rStyle );
257 }
258 
259 uno::Reference< word::XFont > SAL_CALL
getFont()260 SwVbaRange::getFont() throw ( uno::RuntimeException )
261 {
262     VbaPalette aColors;
263     return new SwVbaFont( mxParent, mxContext, aColors.getPalette(), uno::Reference< beans::XPropertySet >( getXTextRange(), uno::UNO_QUERY_THROW ) );
264 }
265 
getLanguageID()266 ::sal_Int32 SAL_CALL SwVbaRange::getLanguageID() throw (uno::RuntimeException)
267 {
268     uno::Reference< beans::XPropertySet > xParaProps( mxTextCursor, uno::UNO_QUERY_THROW );
269     return SwVbaStyle::getLanguageID( xParaProps );
270 }
271 
setLanguageID(::sal_Int32 _languageid)272 void SAL_CALL SwVbaRange::setLanguageID( ::sal_Int32 _languageid ) throw (uno::RuntimeException)
273 {
274     uno::Reference< beans::XPropertySet > xParaProps( mxTextCursor, uno::UNO_QUERY_THROW );
275     SwVbaStyle::setLanguageID( xParaProps, _languageid );
276 }
277 
278 uno::Any SAL_CALL
PageSetup()279 SwVbaRange::PageSetup( ) throw (uno::RuntimeException)
280 {
281     uno::Reference< beans::XPropertySet > xParaProps( mxTextCursor, uno::UNO_QUERY_THROW );
282     uno::Reference< frame::XModel > xModel( mxTextDocument, uno::UNO_QUERY_THROW );
283     rtl::OUString aPageStyleName;
284     xParaProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("PageStyleName"))) >>= aPageStyleName;
285     uno::Reference< style::XStyleFamiliesSupplier > xSytleFamSupp( xModel, uno::UNO_QUERY_THROW );
286     uno::Reference< container::XNameAccess > xSytleFamNames( xSytleFamSupp->getStyleFamilies(), uno::UNO_QUERY_THROW );
287     uno::Reference< container::XNameAccess > xPageStyles( xSytleFamNames->getByName( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("PageStyles") ) ), uno::UNO_QUERY_THROW );
288     uno::Reference< beans::XPropertySet > xPageProps( xPageStyles->getByName( aPageStyleName ), uno::UNO_QUERY_THROW );
289     return uno::makeAny( uno::Reference< word::XPageSetup >( new SwVbaPageSetup( this, mxContext, xModel, xPageProps ) ) );
290 }
291 
getStart()292 ::sal_Int32 SAL_CALL SwVbaRange::getStart() throw (uno::RuntimeException)
293 {
294     uno::Reference< text::XText > xText = mxTextDocument->getText();
295     return SwVbaRangeHelper::getPosition( xText, mxTextCursor->getStart() );
296 }
297 
setStart(::sal_Int32 _start)298 void SAL_CALL SwVbaRange::setStart( ::sal_Int32 _start ) throw (uno::RuntimeException)
299 {
300     uno::Reference< text::XText > xText = mxTextDocument->getText();
301     uno::Reference< text::XTextRange > xStart = SwVbaRangeHelper::getRangeByPosition( xText, _start );
302     uno::Reference< text::XTextRange > xEnd = mxTextCursor->getEnd();
303 
304     mxTextCursor->gotoRange( xStart, sal_False );
305     mxTextCursor->gotoRange( xEnd, sal_True );
306 }
307 
getEnd()308 ::sal_Int32 SAL_CALL SwVbaRange::getEnd() throw (uno::RuntimeException)
309 {
310     uno::Reference< text::XText > xText = mxTextDocument->getText();
311     return SwVbaRangeHelper::getPosition( xText, mxTextCursor->getEnd() );
312 }
313 
setEnd(::sal_Int32 _end)314 void SAL_CALL SwVbaRange::setEnd( ::sal_Int32 _end ) throw (uno::RuntimeException)
315 {
316     uno::Reference< text::XText > xText = mxTextDocument->getText();
317     uno::Reference< text::XTextRange > xEnd = SwVbaRangeHelper::getRangeByPosition( xText, _end );
318 
319     mxTextCursor->collapseToStart();
320     mxTextCursor->gotoRange( xEnd, sal_True );
321 }
322 
323 rtl::OUString&
getServiceImplName()324 SwVbaRange::getServiceImplName()
325 {
326 	static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("SwVbaRange") );
327 	return sImplName;
328 }
329 
330 uno::Sequence< rtl::OUString >
getServiceNames()331 SwVbaRange::getServiceNames()
332 {
333 	static uno::Sequence< rtl::OUString > aServiceNames;
334 	if ( aServiceNames.getLength() == 0 )
335 	{
336 		aServiceNames.realloc( 1 );
337 		aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.word.Range" ) );
338 	}
339 	return aServiceNames;
340 }
341 
342