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