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