LibreOffice Module sw (master) 1
vbasections.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
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"
26#include <utility>
27
28using namespace ::ooo::vba;
29using namespace ::com::sun::star;
30
31typedef std::vector< uno::Reference< beans::XPropertySet > > XSectionVec;
32
33namespace {
34
35class SectionEnumeration : public ::cppu::WeakImplHelper< container::XEnumeration >
36{
37 XSectionVec mxSections;
38 XSectionVec::iterator mIt;
39
40public:
41 explicit SectionEnumeration( XSectionVec&& rVec ) : mxSections( std::move(rVec) ), mIt( mxSections.begin() ) {}
42 virtual sal_Bool SAL_CALL hasMoreElements( ) override
43 {
44 return ( mIt != mxSections.end() );
45 }
46
47 virtual uno::Any SAL_CALL nextElement( ) override
48 {
49 if ( hasMoreElements() )
50 return uno::Any( *mIt++ );
51 throw container::NoSuchElementException();
52 }
53};
54
55// here I regard pagestyle as section
56class SectionCollectionHelper : public ::cppu::WeakImplHelper< container::XIndexAccess,
57 container::XEnumerationAccess >
58{
59private:
60 uno::Reference< XHelperInterface > mxParent;
61 uno::Reference< uno::XComponentContext > mxContext;
62 uno::Reference< frame::XModel > mxModel;
63 XSectionVec mxSections;
64
65public:
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 ))
68 {
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 )
74 {
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( ) )
78 {
79 uno::Reference< beans::XPropertySet > xPageProps( xStyle, uno::UNO_QUERY_THROW );
80 mxSections.push_back( xPageProps );
81 }
82 }
83 }
84
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 ))
87 {
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 );
93 }
94
95 // XIndexAccess
96 virtual sal_Int32 SAL_CALL getCount( ) override
97 {
98 return mxSections.size();
99 }
100 virtual uno::Any SAL_CALL getByIndex( sal_Int32 Index ) override
101 {
102 if ( Index < 0 || Index >= getCount() )
103 throw css::lang::IndexOutOfBoundsException();
104
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 ) ) );
107 }
108 virtual uno::Type SAL_CALL getElementType( ) override
109 {
111 }
112 virtual sal_Bool SAL_CALL hasElements( ) override
113 {
114 return true;
115 }
116 // XEnumerationAccess
117 virtual uno::Reference< container::XEnumeration > SAL_CALL createEnumeration( ) override
118 {
119 return new SectionEnumeration( std::vector(mxSections) );
120 }
121};
122
123class SectionsEnumWrapper : public EnumerationHelperImpl
124{
125 uno::Reference< frame::XModel > mxModel;
126public:
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 )){}
129
130 virtual uno::Any SAL_CALL nextElement( ) override
131 {
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 ) ) );
134 }
135};
136
137}
138
139SwVbaSections::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 )
140{
141}
142
143SwVbaSections::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 )
144{
145}
146
147uno::Any SAL_CALL
149{
150 if( m_xIndexAccess->getCount() )
151 {
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();
155 }
156 throw uno::RuntimeException("There is no section" );
157}
158
159// XEnumerationAccess
160uno::Type SAL_CALL
162{
164}
165
166uno::Reference< container::XEnumeration > SAL_CALL
168{
169 uno::Reference< container::XEnumerationAccess > xEnumAccess( m_xIndexAccess, uno::UNO_QUERY_THROW );
170 return new SectionsEnumWrapper( this, mxContext, xEnumAccess->createEnumeration(), mxModel );
171}
172
174SwVbaSections::createCollectionObject( const css::uno::Any& aSource )
175{
176 return aSource;
177}
178
179OUString
181{
182 return "SwVbaSections";
183}
184
185css::uno::Sequence<OUString>
187{
188 static uno::Sequence< OUString > const sNames
189 {
190 "ooo.vba.word.Sections"
191 };
192 return sNames;
193}
194
195/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
struct _ADOIndex Index
Reference< XComponentContext > m_xContext
css::uno::Reference< css::frame::XModel2 > mxModel
unotools::WeakReference< AnimationNode > mxParent
EnumerationHelperImpl(const css::uno::Reference< ov::XHelperInterface > &xParent, css::uno::Reference< css::uno::XComponentContext > xContext, css::uno::Reference< css::container::XEnumeration > xEnumeration)
css::uno::Reference< css::uno::XComponentContext > mxContext
css::uno::Reference< css::container::XIndexAccess > m_xIndexAccess
virtual css::uno::Any createCollectionObject(const css::uno::Any &aSource) override
virtual css::uno::Type SAL_CALL getElementType() override
virtual css::uno::Any SAL_CALL PageSetup() override
css::uno::Reference< css::frame::XModel > mxModel
Definition: vbasections.hxx:31
SwVbaSections(const css::uno::Reference< ov::XHelperInterface > &xParent, const css::uno::Reference< css::uno::XComponentContext > &xContext, const css::uno::Reference< css::frame::XModel > &xModel)
virtual css::uno::Sequence< OUString > getServiceNames() override
virtual OUString getServiceImplName() override
virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createEnumeration() override
css::uno::Type const & get()
int nCount
uno::Reference< uno::XComponentContext > mxContext
Reference
index
uno::Reference< style::XStyle > getCurrentPageStyle(const uno::Reference< frame::XModel > &xModel)
Reference< XModel > xModel
unsigned char sal_Bool
std::vector< uno::Reference< beans::XPropertySet > > XSectionVec
Definition: vbasections.cxx:31