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"
25 #include <cppuhelper/implbase.hxx>
26 
27 using namespace ::ooo::vba;
28 using namespace ::com::sun::star;
29 
30 typedef std::vector< uno::Reference< beans::XPropertySet > > XSectionVec;
31 
32 namespace {
33 
34 class SectionEnumeration : public ::cppu::WeakImplHelper< container::XEnumeration >
35 {
36  XSectionVec mxSections;
37  XSectionVec::iterator mIt;
38 
39 public:
40  explicit SectionEnumeration( const XSectionVec& rVec ) : mxSections( 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::makeAny( *mIt++ );
50  throw container::NoSuchElementException();
51  }
52 };
53 
54 // here I regard pagestyle as section
55 class SectionCollectionHelper : public ::cppu::WeakImplHelper< container::XIndexAccess,
56  container::XEnumerationAccess >
57 {
58 private:
59  uno::Reference< XHelperInterface > mxParent;
60  uno::Reference< uno::XComponentContext > mxContext;
61  uno::Reference< frame::XModel > mxModel;
62  XSectionVec mxSections;
63 
64 public:
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::makeAny( 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( mxSections );
119  }
120 };
121 
122 class SectionsEnumWrapper : public EnumerationHelperImpl
123 {
124  uno::Reference< frame::XModel > mxModel;
125 public:
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::makeAny( uno::Reference< word::XSection > ( new SwVbaSection( m_xParent, m_xContext, mxModel, xPageProps ) ) );
133  }
134 };
135 
136 }
137 
138 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 )
139 {
140 }
141 
142 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 )
143 {
144 }
145 
146 uno::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
159 uno::Type SAL_CALL
161 {
163 }
164 
165 uno::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 
172 uno::Any
173 SwVbaSections::createCollectionObject( const css::uno::Any& aSource )
174 {
175  return aSource;
176 }
177 
178 OUString
180 {
181  return "SwVbaSections";
182 }
183 
184 css::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: */
std::vector< uno::Reference< beans::XPropertySet > > XSectionVec
Definition: vbasections.cxx:30
css::uno::Reference< css::container::XIndexAccess > m_xIndexAccess
uno::Reference< uno::XComponentContext > mxContext
EnumerationHelperImpl(const css::uno::Reference< ov::XHelperInterface > &xParent, const css::uno::Reference< css::uno::XComponentContext > &xContext, const css::uno::Reference< css::container::XEnumeration > &xEnumeration)
WeakReference< XInterface > mxParent
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
exports com.sun.star. container
virtual css::uno::Any SAL_CALL PageSetup() override
uno::Reference< style::XStyle > getCurrentPageStyle(const uno::Reference< frame::XModel > &xModel)
int nCount
virtual OUString getServiceImplName() override
unsigned char sal_Bool
css::uno::Type const & get()
tuple index
virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createEnumeration() override
virtual css::uno::Sequence< OUString > getServiceNames() override
virtual css::uno::Type SAL_CALL getElementType() override
css::uno::Reference< css::frame::XModel > mxModel
Definition: vbasections.hxx:31
css::uno::Reference< css::uno::XComponentContext > mxContext
Reference< XModel > xModel
Reference< XComponentContext > m_xContext
struct _ADOIndex Index