LibreOffice Module sw (master)  1
vbatablesofcontents.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 "vbatablesofcontents.hxx"
20 #include "vbatableofcontents.hxx"
21 #include "vbarange.hxx"
22 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
23 #include <com/sun/star/text/XDocumentIndexesSupplier.hpp>
24 #include <cppuhelper/implbase.hxx>
25 
26 using namespace ::ooo::vba;
27 using namespace ::com::sun::star;
28 
30 {
31  uno::Reference< container::XIndexAccess > mxIndexAccess;
32  sal_Int32 nIndex;
33 
34 public:
35  explicit TablesOfContentsEnumWrapper( const uno::Reference< container::XIndexAccess >& xIndexAccess ) : mxIndexAccess( xIndexAccess ), nIndex( 0 )
36  {
37  }
38  virtual sal_Bool SAL_CALL hasMoreElements( ) override
39  {
40  return ( nIndex < mxIndexAccess->getCount() );
41  }
42 
43  virtual uno::Any SAL_CALL nextElement( ) override
44  {
45  if( nIndex < mxIndexAccess->getCount() )
46  {
47  return mxIndexAccess->getByIndex( nIndex++ );
48  }
49  throw container::NoSuchElementException();
50  }
51 };
52 
53 class TableOfContentsCollectionHelper : public ::cppu::WeakImplHelper< container::XIndexAccess,
54  container::XEnumerationAccess >
55 {
56 private:
57  uno::Reference< XHelperInterface > mxParent;
58  uno::Reference< uno::XComponentContext > mxContext;
59  uno::Reference< text::XTextDocument > mxTextDocument;
60  std::vector< uno::Reference< text::XDocumentIndex > > maToc;
61 
62 public:
64  TableOfContentsCollectionHelper( const uno::Reference< ov::XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext > & xContext, const uno::Reference< text::XTextDocument >& xDoc ): mxParent( xParent ), mxContext( xContext ), mxTextDocument( xDoc )
65  {
66  uno::Reference< text::XDocumentIndexesSupplier > xDocIndexSupp( mxTextDocument, uno::UNO_QUERY_THROW );
67  uno::Reference< container::XIndexAccess > xDocIndexes = xDocIndexSupp->getDocumentIndexes();
68  sal_Int32 nCount = xDocIndexes->getCount();
69  for( sal_Int32 i = 0; i < nCount; i++ )
70  {
71  uno::Reference< text::XDocumentIndex > xToc( xDocIndexes->getByIndex(i), uno::UNO_QUERY_THROW );
72  if( xToc->getServiceName() == "com.sun.star.text.ContentIndex" )
73  {
74  maToc.push_back( xToc );
75  }
76  }
77  }
78 
79  virtual sal_Int32 SAL_CALL getCount( ) override
80  {
81  return maToc.size();
82  }
83  virtual uno::Any SAL_CALL getByIndex( sal_Int32 Index ) override
84  {
86  throw lang::IndexOutOfBoundsException();
87 
88  uno::Reference< text::XDocumentIndex > xToc( maToc[Index], uno::UNO_SET_THROW );
89  return uno::makeAny( uno::Reference< word::XTableOfContents >( new SwVbaTableOfContents( mxParent, mxContext, mxTextDocument, xToc ) ) );
90  }
91  virtual uno::Type SAL_CALL getElementType( ) override
92  {
94  }
95  virtual sal_Bool SAL_CALL hasElements( ) override
96  {
97  return true;
98  }
99  // XEnumerationAccess
100  virtual uno::Reference< container::XEnumeration > SAL_CALL createEnumeration( ) override
101  {
102  return new TablesOfContentsEnumWrapper( this );
103  }
104 };
105 
106 SwVbaTablesOfContents::SwVbaTablesOfContents( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext > & xContext, const uno::Reference< text::XTextDocument >& xDoc ) : SwVbaTablesOfContents_BASE( xParent, xContext, uno::Reference< container::XIndexAccess >( new TableOfContentsCollectionHelper( xParent, xContext, xDoc ) ) ), mxTextDocument( xDoc )
107 {
108 }
109 
110 uno::Reference< word::XTableOfContents > SAL_CALL
111 SwVbaTablesOfContents::Add( const uno::Reference< word::XRange >& Range, const uno::Any& /*UseHeadingStyles*/, const uno::Any& /*UpperHeadingLevel*/, const uno::Any& LowerHeadingLevel, const uno::Any& UseFields, const uno::Any& /*TableID*/, const uno::Any& /*RightAlignPageNumbers*/, const uno::Any& /*IncludePageNumbers*/, const uno::Any& /*AddedStyles*/, const uno::Any& /*UseHyperlinks*/, const uno::Any& /*HidePageNumbersInWeb*/, const uno::Any& /*UseOutlineLevels*/ )
112 {
113  uno::Reference< lang::XMultiServiceFactory > xDocMSF( mxTextDocument, uno::UNO_QUERY_THROW );
114  uno::Reference< text::XDocumentIndex > xDocumentIndex( xDocMSF->createInstance("com.sun.star.text.ContentIndex"), uno::UNO_QUERY_THROW );
115 
116  uno::Reference< beans::XPropertySet > xTocProps( xDocumentIndex, uno::UNO_QUERY_THROW );
117  xTocProps->setPropertyValue("IsProtected", uno::makeAny( false ) );
118 
119  uno::Reference< word::XTableOfContents > xToc( new SwVbaTableOfContents( this, mxContext, mxTextDocument, xDocumentIndex ) );
120 
121  sal_Int32 nLowerHeadingLevel = 9;
122  if( LowerHeadingLevel.hasValue() )
123  LowerHeadingLevel >>= nLowerHeadingLevel;
124  xToc->setLowerHeadingLevel( nLowerHeadingLevel );
125 
126  bool bUseFields = false;
127  if( UseFields.hasValue() )
128  UseFields >>= bUseFields;
129  xToc->setUseFields( bUseFields );
130 
131  xToc->setUseOutlineLevels( true );
132 
133  SwVbaRange* pVbaRange = dynamic_cast<SwVbaRange*>( Range.get() );
134  if( !pVbaRange )
135  throw uno::RuntimeException();
136 
137  uno::Reference< text::XTextRange > xTextRange = pVbaRange->getXTextRange();
138  uno::Reference< text::XText > xText = pVbaRange->getXText();
139  uno::Reference< text::XTextContent > xTextContent( xDocumentIndex, uno::UNO_QUERY_THROW );
140  xText->insertTextContent( xTextRange, xTextContent, false );
141  xToc->Update();
142 
143  return xToc;
144 }
145 
146 // XEnumerationAccess
147 uno::Type
149 {
151 }
152 uno::Reference< container::XEnumeration >
154 {
156 }
157 
158 uno::Any
160 {
161  return aSource;
162 }
163 
164 OUString
166 {
167  return OUString("SwVbaTablesOfContents");
168 }
169 
170 uno::Sequence<OUString>
172 {
173  static uno::Sequence< OUString > const sNames
174  {
175  "ooo.vba.word.TablesOfContents"
176  };
177  return sNames;
178 }
179 
180 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual uno::Reference< container::XEnumeration > SAL_CALL createEnumeration() override
bool hasValue()
css::uno::Reference< css::container::XIndexAccess > m_xIndexAccess
virtual OUString getServiceImplName() override
SwVbaTablesOfContents(const css::uno::Reference< ov::XHelperInterface > &xParent, const css::uno::Reference< css::uno::XComponentContext > &xContext, const css::uno::Reference< css::text::XTextDocument > &xDoc)
virtual css::uno::Any createCollectionObject(const css::uno::Any &aSource) override
Reference
virtual uno::Type SAL_CALL getElementType() override
::cppu::WeakImplHelper< css::container::XEnumeration > EnumerationHelper_BASE
const css::uno::Reference< css::text::XText > & getXText() const
Definition: vbarange.hxx:61
uno::Reference< XHelperInterface > mxParent
virtual uno::Any SAL_CALL nextElement() override
std::vector< uno::Reference< text::XDocumentIndex > > maToc
virtual css::uno::Sequence< OUString > getServiceNames() override
virtual css::uno::Reference< css::text::XTextRange > SAL_CALL getXTextRange() override
Definition: vbarange.cxx:87
virtual css::uno::Type SAL_CALL getElementType() override
virtual sal_Bool SAL_CALL hasElements() override
TablesOfContentsEnumWrapper(const uno::Reference< container::XIndexAccess > &xIndexAccess)
uno::Reference< container::XIndexAccess > mxIndexAccess
TableOfContentsCollectionHelper(const uno::Reference< ov::XHelperInterface > &xParent, const uno::Reference< uno::XComponentContext > &xContext, const uno::Reference< text::XTextDocument > &xDoc)
uno::Reference< text::XTextDocument > mxTextDocument
int i
unsigned char sal_Bool
virtual uno::Any SAL_CALL getByIndex(sal_Int32 Index) override
css::uno::Type const & get()
css::uno::Reference< css::text::XTextDocument > mxTextDocument
uno::Reference< uno::XComponentContext > mxContext
css::uno::Reference< css::uno::XComponentContext > mxContext
virtual sal_Int32 SAL_CALL getCount() override
virtual css::uno::Reference< ::ooo::vba::word::XTableOfContents > SAL_CALL Add(const css::uno::Reference< ::ooo::vba::word::XRange > &Range, const css::uno::Any &UseHeadingStyles, const css::uno::Any &UpperHeadingLevel, const css::uno::Any &LowerHeadingLevel, const css::uno::Any &UseFields, const css::uno::Any &TableID, const css::uno::Any &RightAlignPageNumbers, const css::uno::Any &IncludePageNumbers, const css::uno::Any &AddedStyles, const css::uno::Any &UseHyperlinks, const css::uno::Any &HidePageNumbersInWeb, const css::uno::Any &UseOutlineLevels) override
virtual sal_Bool SAL_CALL hasMoreElements() override
virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createEnumeration() override