xref: /aoo4110/main/sw/source/ui/vba/vbaparagraph.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 "vbaparagraph.hxx"
24 #include <vbahelper/vbahelper.hxx>
25 #include <tools/diagnose_ex.h>
26 #include "vbarange.hxx"
27 #include <com/sun/star/lang/XServiceInfo.hpp>
28 
29 using namespace ::ooo::vba;
30 using namespace ::com::sun::star;
31 
SwVbaParagraph(const uno::Reference<ooo::vba::XHelperInterface> & rParent,const uno::Reference<uno::XComponentContext> & rContext,const uno::Reference<text::XTextDocument> & xDocument,const uno::Reference<text::XTextRange> & xTextRange)32 SwVbaParagraph::SwVbaParagraph( const uno::Reference< ooo::vba::XHelperInterface >& rParent, const uno::Reference< uno::XComponentContext >& rContext, const uno::Reference< text::XTextDocument >& xDocument, const uno::Reference< text::XTextRange >& xTextRange ) throw ( uno::RuntimeException ) :
33     SwVbaParagraph_BASE( rParent, rContext ), mxTextDocument( xDocument ), mxTextRange( xTextRange )
34 {
35 }
36 
~SwVbaParagraph()37 SwVbaParagraph::~SwVbaParagraph()
38 {
39 }
40 
41 uno::Reference< word::XRange > SAL_CALL
getRange()42 SwVbaParagraph::getRange( ) throw ( uno::RuntimeException )
43 {
44     return uno::Reference< word::XRange >( new SwVbaRange( this, mxContext, mxTextDocument, mxTextRange->getStart(), mxTextRange->getEnd(), mxTextRange->getText(), sal_True ) );
45 }
46 
47 rtl::OUString&
getServiceImplName()48 SwVbaParagraph::getServiceImplName()
49 {
50 	static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("SwVbaParagraph") );
51 	return sImplName;
52 }
53 
54 uno::Sequence< rtl::OUString >
getServiceNames()55 SwVbaParagraph::getServiceNames()
56 {
57 	static uno::Sequence< rtl::OUString > aServiceNames;
58 	if ( aServiceNames.getLength() == 0 )
59 	{
60 		aServiceNames.realloc( 1 );
61 		aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.word.Paragraph" ) );
62 	}
63 	return aServiceNames;
64 }
65 
66 
67 //typedef ::cppu::WeakImplHelper1< container::XEnumeration > ParagraphEnumeration_BASE;
68 typedef ::cppu::WeakImplHelper2< container::XIndexAccess, container::XEnumerationAccess > ParagraphCollectionHelper_BASE;
69 
70 class ParagraphCollectionHelper : public ParagraphCollectionHelper_BASE
71 {
72 private:
73     uno::Reference< text::XTextDocument > mxTextDocument;
74 
getEnumeration()75     uno::Reference< container::XEnumeration > getEnumeration() throw (uno::RuntimeException)
76     {
77         uno::Reference< container::XEnumerationAccess > xParEnumAccess( mxTextDocument->getText(), uno::UNO_QUERY_THROW );
78         return xParEnumAccess->createEnumeration();
79     }
80 
81 public:
ParagraphCollectionHelper(const uno::Reference<text::XTextDocument> & xDocument)82     ParagraphCollectionHelper( const uno::Reference< text::XTextDocument >& xDocument ) throw (uno::RuntimeException): mxTextDocument( xDocument )
83     {
84     }
85 	// XElementAccess
getElementType()86 	virtual uno::Type SAL_CALL getElementType(  ) throw (uno::RuntimeException) { return  text::XTextRange::static_type(0); }
hasElements()87 	virtual ::sal_Bool SAL_CALL hasElements(  ) throw (uno::RuntimeException) { return sal_True; }
88 	// XIndexAccess
getCount()89 	virtual ::sal_Int32 SAL_CALL getCount(  ) throw (uno::RuntimeException)
90     {
91         sal_Int32 nCount = 0;
92         uno::Reference< container::XEnumeration > xParEnum = getEnumeration();
93         while( xParEnum->hasMoreElements() )
94         {
95             uno::Reference< lang::XServiceInfo > xServiceInfo( xParEnum->nextElement(), uno::UNO_QUERY_THROW );
96             if( xServiceInfo->supportsService( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.text.Paragraph") ) ) )
97             {
98                 nCount++;
99             }
100         }
101         return nCount;
102     }
getByIndex(::sal_Int32 Index)103 	virtual uno::Any SAL_CALL getByIndex( ::sal_Int32 Index ) throw (lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException )
104 	{
105         if( Index < getCount() )
106         {
107             sal_Int32 nCount = 0;
108             uno::Reference< container::XEnumeration > xParEnum = getEnumeration();
109             while( xParEnum->hasMoreElements() )
110             {
111                 uno::Reference< lang::XServiceInfo > xServiceInfo( xParEnum->nextElement(), uno::UNO_QUERY_THROW );
112                 if( xServiceInfo->supportsService( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.text.Paragraph") ) ) )
113                 {
114                     if( Index == nCount )
115                         return uno::makeAny( xServiceInfo );
116                     nCount++;
117                 }
118             }
119         }
120         throw lang::IndexOutOfBoundsException();
121     }
122 	// XEnumerationAccess
createEnumeration()123 	virtual uno::Reference< container::XEnumeration > SAL_CALL createEnumeration(  ) throw (uno::RuntimeException)
124 	{
125 		return getEnumeration();
126     }
127 };
128 
SwVbaParagraphs(const uno::Reference<XHelperInterface> & xParent,const uno::Reference<::com::sun::star::uno::XComponentContext> & xContext,const uno::Reference<text::XTextDocument> & xDocument)129 SwVbaParagraphs::SwVbaParagraphs( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< ::com::sun::star::uno::XComponentContext > & xContext, const uno::Reference< text::XTextDocument >& xDocument ) throw (uno::RuntimeException) : SwVbaParagraphs_BASE( xParent, xContext, new ParagraphCollectionHelper( xDocument ) ), mxTextDocument( xDocument )
130 {
131 }
132 
133 // XEnumerationAccess
134 uno::Type
getElementType()135 SwVbaParagraphs::getElementType() throw (uno::RuntimeException)
136 {
137 	return word::XParagraph::static_type(0);
138 }
139 uno::Reference< container::XEnumeration >
createEnumeration()140 SwVbaParagraphs::createEnumeration() throw (uno::RuntimeException)
141 {
142     uno::Reference< container::XEnumerationAccess > xEnumerationAccess( m_xIndexAccess, uno::UNO_QUERY_THROW );
143     return xEnumerationAccess->createEnumeration();
144 }
145 
146 uno::Any
createCollectionObject(const css::uno::Any & aSource)147 SwVbaParagraphs::createCollectionObject( const css::uno::Any& aSource )
148 {
149     uno::Reference< text::XTextRange > xTextRange( aSource, uno::UNO_QUERY_THROW );
150     return uno::makeAny( uno::Reference< word::XParagraph >( new SwVbaParagraph( this, mxContext, mxTextDocument, xTextRange ) ) );
151 }
152 
153 rtl::OUString&
getServiceImplName()154 SwVbaParagraphs::getServiceImplName()
155 {
156 	static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("SwVbaParagraphs") );
157 	return sImplName;
158 }
159 
160 css::uno::Sequence<rtl::OUString>
getServiceNames()161 SwVbaParagraphs::getServiceNames()
162 {
163 	static uno::Sequence< rtl::OUString > sNames;
164 	if ( sNames.getLength() == 0 )
165 	{
166 		sNames.realloc( 1 );
167 		sNames[0] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.word.Paragraphs") );
168 	}
169 	return sNames;
170 }
171