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
27using namespace ::ooo::vba;
28using namespace ::com::sun::star;
29
30typedef std::vector< uno::Reference< beans::XPropertySet > > XSectionVec;
31
32namespace {
33
34class SectionEnumeration : public ::cppu::WeakImplHelper< container::XEnumeration >
35{
36 XSectionVec mxSections;
37 XSectionVec::iterator mIt;
38
39public:
40 explicit SectionEnumeration( XSectionVec&& rVec ) : mxSections( std::move(rVec) ), mIt( mxSections.begin() ) {}
41 virtual sal_Bool SAL_CALL hasMoreElements( ) override
42 {
43 return ( mIt != mxSections.end() );
44 }
45
46 virtual uno::Any SAL_CALL nextElement( ) override
47 {
48 if ( hasMoreElements() )
49 return uno::Any( *mIt++ );
50 throw container::NoSuchElementException();
51 }
52};
53
54// here I regard pagestyle as section
55class SectionCollectionHelper : public ::cppu::WeakImplHelper< container::XIndexAccess,
56 container::XEnumerationAccess >
57{
58private:
59 uno::Reference< XHelperInterface > mxParent;
60 uno::Reference< uno::XComponentContext > mxContext;
61 uno::Reference< frame::XModel > mxModel;
62 XSectionVec mxSections;
63
64public:
66 SectionCollectionHelper( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< frame::XModel >& xModel ) : mxParent( xParent ), mxContext( xContext ), mxModel( xModel )
67 {
68 uno::Reference< style::XStyleFamiliesSupplier > xSytleFamSupp( mxModel, uno::UNO_QUERY_THROW );
69 uno::Reference< container::XNameAccess > xSytleFamNames( xSytleFamSupp->getStyleFamilies(), uno::UNO_SET_THROW );
70 uno::Reference< container::XIndexAccess > xPageStyles( xSytleFamNames->getByName("PageStyles"), uno::UNO_QUERY_THROW );
71 sal_Int32 nCount = xPageStyles->getCount();
72 for( sal_Int32 index = 0; index < nCount; ++index )
73 {
74 uno::Reference< style::XStyle > xStyle( xPageStyles->getByIndex( index ), uno::UNO_QUERY_THROW );
75 // only the pagestyles in using are considered
76 if( xStyle->isInUse( ) )
77 {
78 uno::Reference< beans::XPropertySet > xPageProps( xStyle, uno::UNO_QUERY_THROW );
79 mxSections.push_back( xPageProps );
80 }
81 }
82 }
83
85 SectionCollectionHelper( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< frame::XModel >& xModel, const uno::Reference< text::XTextRange >& xTextRange ) : mxParent( xParent ), mxContext( xContext ), mxModel( xModel )
86 {
87 // Hacky implementation of Range.Sections, only support 1 section
88 uno::Reference< beans::XPropertySet > xRangeProps( xTextRange, uno::UNO_QUERY_THROW );
89 uno::Reference< style::XStyle > xStyle = word::getCurrentPageStyle( mxModel, xRangeProps );
90 uno::Reference< beans::XPropertySet > xPageProps( xStyle, uno::UNO_QUERY_THROW );
91 mxSections.push_back( xPageProps );
92 }
93
94 // XIndexAccess
95 virtual sal_Int32 SAL_CALL getCount( ) override
96 {
97 return mxSections.size();
98 }
99 virtual uno::Any SAL_CALL getByIndex( sal_Int32 Index ) override
100 {
101 if ( Index < 0 || Index >= getCount() )
102 throw css::lang::IndexOutOfBoundsException();
103
104 uno::Reference< beans::XPropertySet > xPageProps( mxSections[ Index ], uno::UNO_SET_THROW );
105 return uno::Any( uno::Reference< word::XSection >( new SwVbaSection( mxParent, mxContext, mxModel, xPageProps ) ) );
106 }
107 virtual uno::Type SAL_CALL getElementType( ) override
108 {
110 }
111 virtual sal_Bool SAL_CALL hasElements( ) override
112 {
113 return true;
114 }
115 // XEnumerationAccess
116 virtual uno::Reference< container::XEnumeration > SAL_CALL createEnumeration( ) override
117 {
118 return new SectionEnumeration( std::vector(mxSections) );
119 }
120};
121
122class SectionsEnumWrapper : public EnumerationHelperImpl
123{
124 uno::Reference< frame::XModel > mxModel;
125public:
127 SectionsEnumWrapper( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< container::XEnumeration >& xEnumeration, const uno::Reference< frame::XModel >& xModel ) : EnumerationHelperImpl( xParent, xContext, xEnumeration ), mxModel( xModel ){}
128
129 virtual uno::Any SAL_CALL nextElement( ) override
130 {
131 uno::Reference< beans::XPropertySet > xPageProps( m_xEnumeration->nextElement(), uno::UNO_QUERY_THROW );
132 return uno::Any( uno::Reference< word::XSection > ( new SwVbaSection( m_xParent, m_xContext, mxModel, xPageProps ) ) );
133 }
134};
135
136}
137
138SwVbaSections::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 )
139{
140}
141
142SwVbaSections::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 )
143{
144}
145
146uno::Any SAL_CALL
148{
149 if( m_xIndexAccess->getCount() )
150 {
151 // check if the first section is our want
152 uno::Reference< word::XSection > xSection( m_xIndexAccess->getByIndex( 0 ), uno::UNO_QUERY_THROW );
153 return xSection->PageSetup();
154 }
155 throw uno::RuntimeException("There is no section" );
156}
157
158// XEnumerationAccess
159uno::Type SAL_CALL
161{
163}
164
165uno::Reference< container::XEnumeration > SAL_CALL
167{
168 uno::Reference< container::XEnumerationAccess > xEnumAccess( m_xIndexAccess, uno::UNO_QUERY_THROW );
169 return new SectionsEnumWrapper( this, mxContext, xEnumAccess->createEnumeration(), mxModel );
170}
171
173SwVbaSections::createCollectionObject( const css::uno::Any& aSource )
174{
175 return aSource;
176}
177
178OUString
180{
181 return "SwVbaSections";
182}
183
184css::uno::Sequence<OUString>
186{
187 static uno::Sequence< OUString > const sNames
188 {
189 "ooo.vba.word.Sections"
190 };
191 return sNames;
192}
193
194/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
struct _ADOIndex Index
Reference< XComponentContext > m_xContext
css::uno::Reference< css::frame::XModel2 > mxModel
WeakReference< XInterface > 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:30