1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 "vbasections.hxx"
20 #include "vbasection.hxx"
21 #include <com/sun/star/frame/XModel.hpp>
22 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
23 #include <com/sun/star/style/XStyle.hpp>
24 #include "wordvbahelper.hxx"
25 #include <cppuhelper/implbase.hxx>
28 using namespace ::ooo::vba
;
29 using namespace ::com::sun::star
;
31 typedef std::vector
< uno::Reference
< beans::XPropertySet
> > XSectionVec
;
35 class SectionEnumeration
: public ::cppu::WeakImplHelper
< container::XEnumeration
>
37 XSectionVec mxSections
;
38 XSectionVec::iterator mIt
;
41 explicit SectionEnumeration( XSectionVec
&& rVec
) : mxSections( std::move(rVec
) ), mIt( mxSections
.begin() ) {}
42 virtual sal_Bool SAL_CALL
hasMoreElements( ) override
44 return ( mIt
!= mxSections
.end() );
47 virtual uno::Any SAL_CALL
nextElement( ) override
49 if ( hasMoreElements() )
50 return uno::Any( *mIt
++ );
51 throw container::NoSuchElementException();
55 // here I regard pagestyle as section
56 class SectionCollectionHelper
: public ::cppu::WeakImplHelper
< container::XIndexAccess
,
57 container::XEnumerationAccess
>
60 uno::Reference
< XHelperInterface
> mxParent
;
61 uno::Reference
< uno::XComponentContext
> mxContext
;
62 uno::Reference
< frame::XModel
> mxModel
;
63 XSectionVec mxSections
;
66 /// @throws uno::RuntimeException
67 SectionCollectionHelper( uno::Reference
< XHelperInterface
> xParent
, uno::Reference
< uno::XComponentContext
> xContext
, uno::Reference
< frame::XModel
> xModel
) : mxParent(std::move( xParent
)), mxContext(std::move( xContext
)), mxModel(std::move( xModel
))
69 uno::Reference
< style::XStyleFamiliesSupplier
> xSytleFamSupp( mxModel
, uno::UNO_QUERY_THROW
);
70 uno::Reference
< container::XNameAccess
> xSytleFamNames( xSytleFamSupp
->getStyleFamilies(), uno::UNO_SET_THROW
);
71 uno::Reference
< container::XIndexAccess
> xPageStyles( xSytleFamNames
->getByName("PageStyles"), uno::UNO_QUERY_THROW
);
72 sal_Int32 nCount
= xPageStyles
->getCount();
73 for( sal_Int32 index
= 0; index
< nCount
; ++index
)
75 uno::Reference
< style::XStyle
> xStyle( xPageStyles
->getByIndex( index
), uno::UNO_QUERY_THROW
);
76 // only the pagestyles in using are considered
77 if( xStyle
->isInUse( ) )
79 uno::Reference
< beans::XPropertySet
> xPageProps( xStyle
, uno::UNO_QUERY_THROW
);
80 mxSections
.push_back( xPageProps
);
85 /// @throws uno::RuntimeException
86 SectionCollectionHelper( uno::Reference
< XHelperInterface
> xParent
, uno::Reference
< uno::XComponentContext
> xContext
, uno::Reference
< frame::XModel
> xModel
, const uno::Reference
< text::XTextRange
>& xTextRange
) : mxParent(std::move( xParent
)), mxContext(std::move( xContext
)), mxModel(std::move( xModel
))
88 // Hacky implementation of Range.Sections, only support 1 section
89 uno::Reference
< beans::XPropertySet
> xRangeProps( xTextRange
, uno::UNO_QUERY_THROW
);
90 uno::Reference
< style::XStyle
> xStyle
= word::getCurrentPageStyle( mxModel
, xRangeProps
);
91 uno::Reference
< beans::XPropertySet
> xPageProps( xStyle
, uno::UNO_QUERY_THROW
);
92 mxSections
.push_back( xPageProps
);
96 virtual sal_Int32 SAL_CALL
getCount( ) override
98 return mxSections
.size();
100 virtual uno::Any SAL_CALL
getByIndex( sal_Int32 Index
) override
102 if ( Index
< 0 || Index
>= getCount() )
103 throw css::lang::IndexOutOfBoundsException();
105 uno::Reference
< beans::XPropertySet
> xPageProps( mxSections
[ Index
], uno::UNO_SET_THROW
);
106 return uno::Any( uno::Reference
< word::XSection
>( new SwVbaSection( mxParent
, mxContext
, mxModel
, xPageProps
) ) );
108 virtual uno::Type SAL_CALL
getElementType( ) override
110 return cppu::UnoType
<word::XSection
>::get();
112 virtual sal_Bool SAL_CALL
hasElements( ) override
116 // XEnumerationAccess
117 virtual uno::Reference
< container::XEnumeration
> SAL_CALL
createEnumeration( ) override
119 return new SectionEnumeration( std::vector(mxSections
) );
123 class SectionsEnumWrapper
: public EnumerationHelperImpl
125 uno::Reference
< frame::XModel
> mxModel
;
127 /// @throws uno::RuntimeException
128 SectionsEnumWrapper( const uno::Reference
< XHelperInterface
>& xParent
, const uno::Reference
< uno::XComponentContext
>& xContext
, const uno::Reference
< container::XEnumeration
>& xEnumeration
, uno::Reference
< frame::XModel
> xModel
) : EnumerationHelperImpl( xParent
, xContext
, xEnumeration
), mxModel(std::move( xModel
)){}
130 virtual uno::Any SAL_CALL
nextElement( ) override
132 uno::Reference
< beans::XPropertySet
> xPageProps( m_xEnumeration
->nextElement(), uno::UNO_QUERY_THROW
);
133 return uno::Any( uno::Reference
< word::XSection
> ( new SwVbaSection( m_xParent
, m_xContext
, mxModel
, xPageProps
) ) );
139 SwVbaSections::SwVbaSections( const uno::Reference
< XHelperInterface
>& xParent
, const uno::Reference
< uno::XComponentContext
> & xContext
, const uno::Reference
< frame::XModel
>& xModel
): SwVbaSections_BASE( xParent
, xContext
, uno::Reference
< container::XIndexAccess
>( new SectionCollectionHelper( xParent
, xContext
, xModel
) ) ), mxModel( xModel
)
143 SwVbaSections::SwVbaSections( const uno::Reference
< XHelperInterface
>& xParent
, const uno::Reference
< uno::XComponentContext
> & xContext
, const uno::Reference
< frame::XModel
>& xModel
, const uno::Reference
< text::XTextRange
>& xTextRange
): SwVbaSections_BASE( xParent
, xContext
, uno::Reference
< container::XIndexAccess
>( new SectionCollectionHelper( xParent
, xContext
, xModel
, xTextRange
) ) ), mxModel( xModel
)
148 SwVbaSections::PageSetup( )
150 if( m_xIndexAccess
->getCount() )
152 // check if the first section is our want
153 uno::Reference
< word::XSection
> xSection( m_xIndexAccess
->getByIndex( 0 ), uno::UNO_QUERY_THROW
);
154 return xSection
->PageSetup();
156 throw uno::RuntimeException("There is no section" );
159 // XEnumerationAccess
161 SwVbaSections::getElementType()
163 return cppu::UnoType
<word::XSection
>::get();
166 uno::Reference
< container::XEnumeration
> SAL_CALL
167 SwVbaSections::createEnumeration()
169 uno::Reference
< container::XEnumerationAccess
> xEnumAccess( m_xIndexAccess
, uno::UNO_QUERY_THROW
);
170 return new SectionsEnumWrapper( this, mxContext
, xEnumAccess
->createEnumeration(), mxModel
);
174 SwVbaSections::createCollectionObject( const css::uno::Any
& aSource
)
180 SwVbaSections::getServiceImplName()
182 return "SwVbaSections";
185 css::uno::Sequence
<OUString
>
186 SwVbaSections::getServiceNames()
188 static uno::Sequence
< OUString
> const sNames
190 "ooo.vba.word.Sections"
195 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */