LibreOffice Module sw (master)  1
vbaparagraph.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 "vbaparagraph.hxx"
20 #include <vbahelper/vbahelper.hxx>
21 #include <tools/diagnose_ex.h>
22 #include "vbarange.hxx"
23 #include <com/sun/star/lang/XServiceInfo.hpp>
24 #include <cppuhelper/implbase.hxx>
25 
26 using namespace ::ooo::vba;
27 using namespace ::com::sun::star;
28 
29 SwVbaParagraph::SwVbaParagraph( const uno::Reference< ooo::vba::XHelperInterface >& rParent, const uno::Reference< uno::XComponentContext >& rContext, const uno::Reference< text::XTextDocument >& xDocument, const uno::Reference< text::XTextRange >& xTextRange ) :
30  SwVbaParagraph_BASE( rParent, rContext ), mxTextDocument( xDocument ), mxTextRange( xTextRange )
31 {
32 }
33 
35 {
36 }
37 
38 uno::Reference< word::XRange > SAL_CALL
40 {
41  return uno::Reference< word::XRange >( new SwVbaRange( this, mxContext, mxTextDocument, mxTextRange->getStart(), mxTextRange->getEnd(), mxTextRange->getText() ) );
42 }
43 
44 uno::Any SAL_CALL
46 {
47  uno::Reference< word::XRange > xRange = getRange();
48  return xRange->getStyle();
49 }
50 
51 void SAL_CALL
53 {
54  uno::Reference< word::XRange > xRange = getRange();
55  xRange->setStyle( style );
56 }
57 
58 OUString
60 {
61  return "SwVbaParagraph";
62 }
63 
64 uno::Sequence< OUString >
66 {
67  static uno::Sequence< OUString > const aServiceNames
68  {
69  "ooo.vba.word.Paragraph"
70  };
71  return aServiceNames;
72 }
73 
74 class ParagraphCollectionHelper : public ::cppu::WeakImplHelper< container::XIndexAccess,
75  container::XEnumerationAccess >
76 {
77 private:
78  uno::Reference< text::XTextDocument > mxTextDocument;
79 
81  uno::Reference< container::XEnumeration > getEnumeration()
82  {
83  uno::Reference< container::XEnumerationAccess > xParEnumAccess( mxTextDocument->getText(), uno::UNO_QUERY_THROW );
84  return xParEnumAccess->createEnumeration();
85  }
86 
87 public:
89  explicit ParagraphCollectionHelper( const uno::Reference< text::XTextDocument >& xDocument ): mxTextDocument( xDocument )
90  {
91  }
92  // XElementAccess
93  virtual uno::Type SAL_CALL getElementType( ) override { return cppu::UnoType<text::XTextRange>::get(); }
94  virtual sal_Bool SAL_CALL hasElements( ) override { return true; }
95  // XIndexAccess
96  virtual ::sal_Int32 SAL_CALL getCount( ) override
97  {
98  sal_Int32 nCount = 0;
99  uno::Reference< container::XEnumeration > xParEnum = getEnumeration();
100  while( xParEnum->hasMoreElements() )
101  {
102  uno::Reference< lang::XServiceInfo > xServiceInfo( xParEnum->nextElement(), uno::UNO_QUERY_THROW );
103  if( xServiceInfo->supportsService("com.sun.star.text.Paragraph") )
104  {
105  nCount++;
106  }
107  }
108  return nCount;
109  }
110  virtual uno::Any SAL_CALL getByIndex( ::sal_Int32 Index ) override
111  {
112  if( Index < getCount() )
113  {
114  sal_Int32 nCount = 0;
115  uno::Reference< container::XEnumeration > xParEnum = getEnumeration();
116  while( xParEnum->hasMoreElements() )
117  {
118  uno::Reference< lang::XServiceInfo > xServiceInfo( xParEnum->nextElement(), uno::UNO_QUERY_THROW );
119  if( xServiceInfo->supportsService("com.sun.star.text.Paragraph") )
120  {
121  if( Index == nCount )
122  return uno::makeAny( xServiceInfo );
123  nCount++;
124  }
125  }
126  }
127  throw lang::IndexOutOfBoundsException();
128  }
129  // XEnumerationAccess
130  virtual uno::Reference< container::XEnumeration > SAL_CALL createEnumeration( ) override
131  {
132  return getEnumeration();
133  }
134 };
135 
136 SwVbaParagraphs::SwVbaParagraphs( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< css::uno::XComponentContext > & xContext, const uno::Reference< text::XTextDocument >& xDocument ) : SwVbaParagraphs_BASE( xParent, xContext, new ParagraphCollectionHelper( xDocument ) ), mxTextDocument( xDocument )
137 {
138 }
139 
140 // XEnumerationAccess
141 uno::Type
143 {
145 }
146 uno::Reference< container::XEnumeration >
148 {
149  uno::Reference< container::XEnumerationAccess > xEnumerationAccess( m_xIndexAccess, uno::UNO_QUERY_THROW );
150  return xEnumerationAccess->createEnumeration();
151 }
152 
153 uno::Any
154 SwVbaParagraphs::createCollectionObject( const css::uno::Any& aSource )
155 {
156  uno::Reference< text::XTextRange > xTextRange( aSource, uno::UNO_QUERY_THROW );
157  return uno::makeAny( uno::Reference< word::XParagraph >( new SwVbaParagraph( this, mxContext, mxTextDocument, xTextRange ) ) );
158 }
159 
160 OUString
162 {
163  return "SwVbaParagraphs";
164 }
165 
166 css::uno::Sequence<OUString>
168 {
169  static uno::Sequence< OUString > const sNames
170  {
171  "ooo.vba.word.Paragraphs"
172  };
173  return sNames;
174 }
175 
176 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual css::uno::Sequence< OUString > getServiceNames() override
virtual ::sal_Int32 SAL_CALL getCount() override
css::uno::Reference< css::text::XTextRange > mxTextRange
css::uno::Reference< css::container::XIndexAccess > m_xIndexAccess
virtual uno::Any SAL_CALL getByIndex(::sal_Int32 Index) override
virtual sal_Bool SAL_CALL hasElements() override
SwVbaParagraph(const css::uno::Reference< ooo::vba::XHelperInterface > &rParent, const css::uno::Reference< css::uno::XComponentContext > &rContext, const css::uno::Reference< css::text::XTextDocument > &xDocument, const css::uno::Reference< css::text::XTextRange > &xTextRange)
virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createEnumeration() override
virtual css::uno::Any SAL_CALL getStyle() override
uno::Reference< container::XEnumeration > getEnumeration()
css::uno::Reference< css::text::XTextDocument > mxTextDocument
virtual css::uno::Type SAL_CALL getElementType() override
virtual OUString getServiceImplName() override
virtual css::uno::Any createCollectionObject(const css::uno::Any &aSource) override
uno::Reference< text::XTextDocument > mxTextDocument
unsigned char sal_Bool
css::uno::Type const & get()
virtual css::uno::Sequence< OUString > getServiceNames() override
SwVbaParagraphs(const css::uno::Reference< ov::XHelperInterface > &xParent, const css::uno::Reference< css::uno::XComponentContext > &xContext, const css::uno::Reference< css::text::XTextDocument > &xDocument)
virtual uno::Reference< container::XEnumeration > SAL_CALL createEnumeration() override
css::uno::Reference< css::uno::XComponentContext > mxContext
virtual OUString getServiceImplName() override
virtual uno::Type SAL_CALL getElementType() override
virtual css::uno::Reference< ooo::vba::word::XRange > SAL_CALL getRange() override
ParagraphCollectionHelper(const uno::Reference< text::XTextDocument > &xDocument)
virtual ~SwVbaParagraph() override
css::uno::Reference< css::text::XTextDocument > mxTextDocument
virtual void SAL_CALL setStyle(const css::uno::Any &style) override