tdf#35361 Add a Quick Look plugins for .od* files on macOS
[LibreOffice.git] / sw / source / ui / vba / vbasections.cxx
blob75fd915aae08ca7dd549d50ede3b3bda60983711
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 "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>
26 #include <utility>
27 #include <unotxdoc.hxx>
28 #include <unostyle.hxx>
30 using namespace ::ooo::vba;
31 using namespace ::com::sun::star;
33 typedef std::vector< uno::Reference< beans::XPropertySet > > XSectionVec;
35 namespace {
37 class SectionEnumeration : public ::cppu::WeakImplHelper< container::XEnumeration >
39 XSectionVec mxSections;
40 XSectionVec::iterator mIt;
42 public:
43 explicit SectionEnumeration( XSectionVec&& rVec ) : mxSections( std::move(rVec) ), mIt( mxSections.begin() ) {}
44 virtual sal_Bool SAL_CALL hasMoreElements( ) override
46 return ( mIt != mxSections.end() );
49 virtual uno::Any SAL_CALL nextElement( ) override
51 if ( hasMoreElements() )
52 return uno::Any( *mIt++ );
53 throw container::NoSuchElementException();
57 // here I regard pagestyle as section
58 class SectionCollectionHelper : public ::cppu::WeakImplHelper< container::XIndexAccess,
59 container::XEnumerationAccess >
61 private:
62 uno::Reference< XHelperInterface > mxParent;
63 uno::Reference< uno::XComponentContext > mxContext;
64 rtl::Reference< SwXTextDocument > mxModel;
65 XSectionVec mxSections;
67 public:
68 /// @throws uno::RuntimeException
69 SectionCollectionHelper( uno::Reference< XHelperInterface > xParent,
70 uno::Reference< uno::XComponentContext > xContext,
71 rtl::Reference< SwXTextDocument > xModel )
72 : mxParent(std::move( xParent )),
73 mxContext(std::move( xContext )),
74 mxModel(std::move( xModel ))
76 rtl::Reference< SwXStyleFamilies > xSytleFamNames( mxModel->getSwStyleFamilies() );
77 uno::Reference< container::XIndexAccess > xPageStyles( xSytleFamNames->getByName(u"PageStyles"_ustr), uno::UNO_QUERY_THROW );
78 sal_Int32 nCount = xPageStyles->getCount();
79 for( sal_Int32 index = 0; index < nCount; ++index )
81 uno::Reference< style::XStyle > xStyle( xPageStyles->getByIndex( index ), uno::UNO_QUERY_THROW );
82 // only the pagestyles in using are considered
83 if( xStyle->isInUse( ) )
85 uno::Reference< beans::XPropertySet > xPageProps( xStyle, uno::UNO_QUERY_THROW );
86 mxSections.push_back( xPageProps );
91 /// @throws uno::RuntimeException
92 SectionCollectionHelper( uno::Reference< XHelperInterface > xParent,
93 uno::Reference< uno::XComponentContext > xContext,
94 rtl::Reference< SwXTextDocument > xModel,
95 const uno::Reference< text::XTextRange >& xTextRange )
96 : mxParent(std::move( xParent )),
97 mxContext(std::move( xContext )),
98 mxModel(std::move( xModel ))
100 // Hacky implementation of Range.Sections, only support 1 section
101 uno::Reference< beans::XPropertySet > xRangeProps( xTextRange, uno::UNO_QUERY_THROW );
102 uno::Reference< style::XStyle > xStyle = word::getCurrentPageStyle( mxModel, xRangeProps );
103 uno::Reference< beans::XPropertySet > xPageProps( xStyle, uno::UNO_QUERY_THROW );
104 mxSections.push_back( xPageProps );
107 // XIndexAccess
108 virtual sal_Int32 SAL_CALL getCount( ) override
110 return mxSections.size();
112 virtual uno::Any SAL_CALL getByIndex( sal_Int32 Index ) override
114 if ( Index < 0 || Index >= getCount() )
115 throw css::lang::IndexOutOfBoundsException();
117 uno::Reference< beans::XPropertySet > xPageProps( mxSections[ Index ], uno::UNO_SET_THROW );
118 return uno::Any( uno::Reference< word::XSection >( new SwVbaSection( mxParent, mxContext, mxModel, xPageProps ) ) );
120 virtual uno::Type SAL_CALL getElementType( ) override
122 return cppu::UnoType<word::XSection>::get();
124 virtual sal_Bool SAL_CALL hasElements( ) override
126 return true;
128 // XEnumerationAccess
129 virtual uno::Reference< container::XEnumeration > SAL_CALL createEnumeration( ) override
131 return new SectionEnumeration( std::vector(mxSections) );
135 class SectionsEnumWrapper : public EnumerationHelperImpl
137 rtl::Reference< SwXTextDocument > mxModel;
138 public:
139 /// @throws uno::RuntimeException
140 SectionsEnumWrapper( const uno::Reference< XHelperInterface >& xParent,
141 const uno::Reference< uno::XComponentContext >& xContext,
142 const uno::Reference< container::XEnumeration >& xEnumeration,
143 rtl::Reference< SwXTextDocument > xModel )
144 : EnumerationHelperImpl( xParent, xContext, xEnumeration ),
145 mxModel(std::move( xModel )){}
147 virtual uno::Any SAL_CALL nextElement( ) override
149 uno::Reference< beans::XPropertySet > xPageProps( m_xEnumeration->nextElement(), uno::UNO_QUERY_THROW );
150 return uno::Any( uno::Reference< word::XSection > ( new SwVbaSection( m_xParent, m_xContext, mxModel, xPageProps ) ) );
156 SwVbaSections::SwVbaSections( const uno::Reference< XHelperInterface >& xParent,
157 const uno::Reference< uno::XComponentContext > & xContext,
158 const rtl::Reference< SwXTextDocument >& xModel )
159 : SwVbaSections_BASE( xParent, xContext, uno::Reference< container::XIndexAccess >( new SectionCollectionHelper( xParent, xContext, xModel ) ) ),
160 mxModel( xModel )
164 SwVbaSections::SwVbaSections( const uno::Reference< XHelperInterface >& xParent,
165 const uno::Reference< uno::XComponentContext > & xContext,
166 const rtl::Reference< SwXTextDocument >& xModel,
167 const uno::Reference< text::XTextRange >& xTextRange )
168 : SwVbaSections_BASE( xParent, xContext, uno::Reference< container::XIndexAccess >( new SectionCollectionHelper( xParent, xContext, xModel, xTextRange ) ) ),
169 mxModel( xModel )
173 uno::Any SAL_CALL
174 SwVbaSections::PageSetup( )
176 if( m_xIndexAccess->getCount() )
178 // check if the first section is our want
179 uno::Reference< word::XSection > xSection( m_xIndexAccess->getByIndex( 0 ), uno::UNO_QUERY_THROW );
180 return xSection->PageSetup();
182 throw uno::RuntimeException(u"There is no section"_ustr );
185 // XEnumerationAccess
186 uno::Type SAL_CALL
187 SwVbaSections::getElementType()
189 return cppu::UnoType<word::XSection>::get();
192 uno::Reference< container::XEnumeration > SAL_CALL
193 SwVbaSections::createEnumeration()
195 uno::Reference< container::XEnumerationAccess > xEnumAccess( m_xIndexAccess, uno::UNO_QUERY_THROW );
196 return new SectionsEnumWrapper( this, mxContext, xEnumAccess->createEnumeration(), mxModel );
199 uno::Any
200 SwVbaSections::createCollectionObject( const css::uno::Any& aSource )
202 return aSource;
205 OUString
206 SwVbaSections::getServiceImplName()
208 return u"SwVbaSections"_ustr;
211 css::uno::Sequence<OUString>
212 SwVbaSections::getServiceNames()
214 static uno::Sequence< OUString > const sNames
216 u"ooo.vba.word.Sections"_ustr
218 return sNames;
221 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */