sc: factor out some more code
[LibreOffice.git] / sw / source / ui / vba / vbaparagraph.cxx
blobc5e6a0e5e00edba126553010ee9401e477a41790
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 "vbaparagraph.hxx"
20 #include "vbarange.hxx"
21 #include <com/sun/star/lang/XServiceInfo.hpp>
22 #include <cppuhelper/implbase.hxx>
23 #include <utility>
24 #include <unotxdoc.hxx>
26 using namespace ::ooo::vba;
27 using namespace ::com::sun::star;
29 SwVbaParagraph::SwVbaParagraph( const uno::Reference< ooo::vba::XHelperInterface >& rParent, const uno::Reference< uno::XComponentContext >& rContext, uno::Reference< text::XTextDocument > xDocument, uno::Reference< text::XTextRange > xTextRange ) :
30 SwVbaParagraph_BASE( rParent, rContext ), mxTextDocument(std::move( xDocument )), mxTextRange(std::move( xTextRange ))
34 SwVbaParagraph::~SwVbaParagraph()
38 uno::Reference< word::XRange > SAL_CALL
39 SwVbaParagraph::getRange( )
41 return uno::Reference< word::XRange >( new SwVbaRange( this, mxContext, dynamic_cast<SwXTextDocument*>(mxTextDocument.get()), mxTextRange->getStart(), mxTextRange->getEnd(), mxTextRange->getText() ) );
44 uno::Any SAL_CALL
45 SwVbaParagraph::getStyle( )
47 uno::Reference< word::XRange > xRange = getRange();
48 return xRange->getStyle();
51 void SAL_CALL
52 SwVbaParagraph::setStyle( const uno::Any& style )
54 uno::Reference< word::XRange > xRange = getRange();
55 xRange->setStyle( style );
58 OUString
59 SwVbaParagraph::getServiceImplName()
61 return u"SwVbaParagraph"_ustr;
64 uno::Sequence< OUString >
65 SwVbaParagraph::getServiceNames()
67 static uno::Sequence< OUString > const aServiceNames
69 u"ooo.vba.word.Paragraph"_ustr
71 return aServiceNames;
74 namespace {
76 class ParagraphCollectionHelper : public ::cppu::WeakImplHelper< container::XIndexAccess,
77 container::XEnumerationAccess >
79 private:
80 uno::Reference< text::XTextDocument > mxTextDocument;
82 /// @throws uno::RuntimeException
83 uno::Reference< container::XEnumeration > getEnumeration()
85 uno::Reference< container::XEnumerationAccess > xParEnumAccess( mxTextDocument->getText(), uno::UNO_QUERY_THROW );
86 return xParEnumAccess->createEnumeration();
89 public:
90 /// @throws uno::RuntimeException
91 explicit ParagraphCollectionHelper( uno::Reference< text::XTextDocument > xDocument ): mxTextDocument(std::move( xDocument ))
94 // XElementAccess
95 virtual uno::Type SAL_CALL getElementType( ) override { return cppu::UnoType<text::XTextRange>::get(); }
96 virtual sal_Bool SAL_CALL hasElements( ) override { return true; }
97 // XIndexAccess
98 virtual ::sal_Int32 SAL_CALL getCount( ) override
100 sal_Int32 nCount = 0;
101 uno::Reference< container::XEnumeration > xParEnum = getEnumeration();
102 while( xParEnum->hasMoreElements() )
104 uno::Reference< lang::XServiceInfo > xServiceInfo( xParEnum->nextElement(), uno::UNO_QUERY_THROW );
105 if( xServiceInfo->supportsService(u"com.sun.star.text.Paragraph"_ustr) )
107 nCount++;
110 return nCount;
112 virtual uno::Any SAL_CALL getByIndex( ::sal_Int32 Index ) override
114 if( Index < getCount() )
116 sal_Int32 nCount = 0;
117 uno::Reference< container::XEnumeration > xParEnum = getEnumeration();
118 while( xParEnum->hasMoreElements() )
120 uno::Reference< lang::XServiceInfo > xServiceInfo( xParEnum->nextElement(), uno::UNO_QUERY_THROW );
121 if( xServiceInfo->supportsService(u"com.sun.star.text.Paragraph"_ustr) )
123 if( Index == nCount )
124 return uno::Any( xServiceInfo );
125 nCount++;
129 throw lang::IndexOutOfBoundsException();
131 // XEnumerationAccess
132 virtual uno::Reference< container::XEnumeration > SAL_CALL createEnumeration( ) override
134 return getEnumeration();
140 SwVbaParagraphs::SwVbaParagraphs( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< css::uno::XComponentContext > & xContext, const uno::Reference< text::XTextDocument >& xDocument ) : SwVbaParagraphs_BASE( xParent, xContext, new ParagraphCollectionHelper( xDocument ) ), mxTextDocument( xDocument )
144 // XEnumerationAccess
145 uno::Type
146 SwVbaParagraphs::getElementType()
148 return cppu::UnoType<word::XParagraph>::get();
150 uno::Reference< container::XEnumeration >
151 SwVbaParagraphs::createEnumeration()
153 uno::Reference< container::XEnumerationAccess > xEnumerationAccess( m_xIndexAccess, uno::UNO_QUERY_THROW );
154 return xEnumerationAccess->createEnumeration();
157 uno::Any
158 SwVbaParagraphs::createCollectionObject( const css::uno::Any& aSource )
160 uno::Reference< text::XTextRange > xTextRange( aSource, uno::UNO_QUERY_THROW );
161 return uno::Any( uno::Reference< word::XParagraph >( new SwVbaParagraph( this, mxContext, mxTextDocument, xTextRange ) ) );
164 OUString
165 SwVbaParagraphs::getServiceImplName()
167 return u"SwVbaParagraphs"_ustr;
170 css::uno::Sequence<OUString>
171 SwVbaParagraphs::getServiceNames()
173 static uno::Sequence< OUString > const sNames
175 u"ooo.vba.word.Paragraphs"_ustr
177 return sNames;
180 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */