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