1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
28 using namespace ::ooo::vba
;
29 using namespace ::com::sun::star
;
33 class TablesOfContentsEnumWrapper
: public EnumerationHelper_BASE
35 uno::Reference
< container::XIndexAccess
> mxIndexAccess
;
39 explicit TablesOfContentsEnumWrapper( uno::Reference
< container::XIndexAccess
> xIndexAccess
) : mxIndexAccess(std::move( xIndexAccess
)), m_nIndex( 0 )
42 virtual sal_Bool SAL_CALL
hasMoreElements( ) override
44 return ( m_nIndex
< mxIndexAccess
->getCount() );
47 virtual uno::Any SAL_CALL
nextElement( ) override
49 if( m_nIndex
< mxIndexAccess
->getCount() )
51 return mxIndexAccess
->getByIndex( m_nIndex
++ );
53 throw container::NoSuchElementException();
57 class TableOfContentsCollectionHelper
: public ::cppu::WeakImplHelper
< container::XIndexAccess
,
58 container::XEnumerationAccess
>
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
;
67 /// @throws uno::RuntimeException
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
))
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
++ )
75 uno::Reference
< text::XDocumentIndex
> xToc( xDocIndexes
->getByIndex(i
), uno::UNO_QUERY_THROW
);
76 if( xToc
->getServiceName() == "com.sun.star.text.ContentIndex" )
78 maToc
.push_back( xToc
);
83 virtual sal_Int32 SAL_CALL
getCount( ) override
87 virtual uno::Any SAL_CALL
getByIndex( sal_Int32 Index
) override
89 if ( Index
< 0 || Index
>= getCount() )
90 throw lang::IndexOutOfBoundsException();
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
) ) );
95 virtual uno::Type SAL_CALL
getElementType( ) override
97 return cppu::UnoType
<word::XTableOfContents
>::get();
99 virtual sal_Bool SAL_CALL
hasElements( ) override
103 // XEnumerationAccess
104 virtual uno::Reference
< container::XEnumeration
> SAL_CALL
createEnumeration( ) override
106 return new TablesOfContentsEnumWrapper( this );
112 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
)
116 uno::Reference
< word::XTableOfContents
> SAL_CALL
117 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*/ )
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
);
122 uno::Reference
< beans::XPropertySet
> xTocProps( xDocumentIndex
, uno::UNO_QUERY_THROW
);
123 xTocProps
->setPropertyValue("IsProtected", uno::Any( false ) );
125 uno::Reference
< word::XTableOfContents
> xToc( new SwVbaTableOfContents( this, mxContext
, mxTextDocument
, xDocumentIndex
) );
127 sal_Int32 nLowerHeadingLevel
= 9;
128 if( LowerHeadingLevel
.hasValue() )
129 LowerHeadingLevel
>>= nLowerHeadingLevel
;
130 xToc
->setLowerHeadingLevel( nLowerHeadingLevel
);
132 bool bUseFields
= false;
133 if( UseFields
.hasValue() )
134 UseFields
>>= bUseFields
;
135 xToc
->setUseFields( bUseFields
);
137 xToc
->setUseOutlineLevels( true );
139 SwVbaRange
* pVbaRange
= dynamic_cast<SwVbaRange
*>( Range
.get() );
141 throw uno::RuntimeException();
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 );
152 // XEnumerationAccess
154 SwVbaTablesOfContents::getElementType()
156 return cppu::UnoType
<word::XTableOfContents
>::get();
158 uno::Reference
< container::XEnumeration
>
159 SwVbaTablesOfContents::createEnumeration()
161 return new TablesOfContentsEnumWrapper( m_xIndexAccess
);
165 SwVbaTablesOfContents::createCollectionObject( const uno::Any
& aSource
)
171 SwVbaTablesOfContents::getServiceImplName()
173 return "SwVbaTablesOfContents";
176 uno::Sequence
<OUString
>
177 SwVbaTablesOfContents::getServiceNames()
179 static uno::Sequence
< OUString
> const sNames
181 "ooo.vba.word.TablesOfContents"
186 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */