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