LibreOffice Module sw (master) 1
vbatables.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
20#include "vbatables.hxx"
21#include "vbatable.hxx"
22#include "vbarange.hxx"
23#include "wordvbahelper.hxx"
24#include <com/sun/star/text/XTextTable.hpp>
25#include <com/sun/star/text/XTextTablesSupplier.hpp>
26#include <com/sun/star/text/XTextDocument.hpp>
27#include <com/sun/star/lang/XServiceInfo.hpp>
28#include <com/sun/star/lang/XMultiServiceFactory.hpp>
29#include <com/sun/star/text/XText.hpp>
30#include <com/sun/star/table/XCellRange.hpp>
32#include <utility>
33
34using namespace ::ooo::vba;
35using namespace css;
36
37static uno::Reference< container::XIndexAccess > lcl_getTables( const uno::Reference< frame::XModel >& xDoc )
38{
39 uno::Reference< container::XIndexAccess > xTables;
40 uno::Reference< text::XTextTablesSupplier > xSupp( xDoc, uno::UNO_QUERY );
41 if ( xSupp.is() )
42 xTables.set( xSupp->getTextTables(), uno::UNO_QUERY_THROW );
43 return xTables;
44}
45
46static uno::Any lcl_createTable( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< frame::XModel >& xDocument, const uno::Any& aSource )
47{
48 uno::Reference< text::XTextTable > xTextTable( aSource, uno::UNO_QUERY_THROW );
49 uno::Reference< text::XTextDocument > xTextDocument( xDocument, uno::UNO_QUERY_THROW );
50 uno::Reference< word::XTable > xTable( new SwVbaTable( xParent, xContext, xTextDocument, xTextTable ) );
51 return uno::Any( xTable );
52}
53
54static bool lcl_isInHeaderFooter( const uno::Reference< text::XTextTable >& xTable )
55{
56 uno::Reference< text::XTextContent > xTextContent( xTable, uno::UNO_QUERY_THROW );
57 uno::Reference< text::XText > xText = xTextContent->getAnchor()->getText();
58 uno::Reference< lang::XServiceInfo > xServiceInfo( xText, uno::UNO_QUERY );
59 if ( !xServiceInfo )
60 return false;
61 OUString aImplName = xServiceInfo->getImplementationName();
62 return aImplName == "SwXHeadFootText";
63}
64
65typedef std::vector< uno::Reference< text::XTextTable > > XTextTableVec;
66
67namespace {
68
69class TableCollectionHelper : public ::cppu::WeakImplHelper< container::XIndexAccess,
70 container::XNameAccess >
71{
72 XTextTableVec mxTables;
73 XTextTableVec::iterator m_cachePos;
74
75public:
76 explicit TableCollectionHelper( const uno::Reference< frame::XModel >& xDocument )
77 {
78 // only count the tables in the body text, not in the header/footer
79 uno::Reference< container::XIndexAccess > xTables = lcl_getTables( xDocument );
80 sal_Int32 nCount = xTables->getCount();
81 for( sal_Int32 i = 0; i < nCount; i++ )
82 {
83 uno::Reference< text::XTextTable > xTable( xTables->getByIndex( i ) , uno::UNO_QUERY_THROW );
84 if( !lcl_isInHeaderFooter( xTable ) )
85 mxTables.push_back( xTable );
86 }
87 m_cachePos = mxTables.begin();
88 }
89 // XIndexAccess
90 virtual sal_Int32 SAL_CALL getCount( ) override
91 {
92 return mxTables.size();
93 }
94 virtual uno::Any SAL_CALL getByIndex( sal_Int32 Index ) override
95 {
96 if ( Index < 0 || Index >= getCount() )
97 throw lang::IndexOutOfBoundsException();
98 uno::Reference< text::XTextTable > xTable( mxTables[ Index ], uno::UNO_SET_THROW );
99 return uno::Any( xTable );
100 }
101 // XElementAccess
102 virtual uno::Type SAL_CALL getElementType( ) override { return cppu::UnoType<text::XTextTable>::get(); }
103 virtual sal_Bool SAL_CALL hasElements( ) override { return getCount() > 0 ; }
104 // XNameAccess
105 virtual uno::Any SAL_CALL getByName( const OUString& aName ) override
106 {
107 if ( !hasByName(aName) )
108 throw container::NoSuchElementException();
109 uno::Reference< text::XTextTable > xTable( *m_cachePos, uno::UNO_SET_THROW );
110 return uno::Any( xTable );
111 }
112 virtual uno::Sequence< OUString > SAL_CALL getElementNames( ) override
113 {
114 uno::Sequence< OUString > sNames( mxTables.size() );
115 OUString* pString = sNames.getArray();
116 for ( const auto& rxTable : mxTables )
117 {
118 uno::Reference< container::XNamed > xName( rxTable, uno::UNO_QUERY_THROW );
119 *pString = xName->getName();
120 ++pString;
121 }
122 return sNames;
123 }
124 virtual sal_Bool SAL_CALL hasByName( const OUString& aName ) override
125 {
126 m_cachePos = mxTables.begin();
127 XTextTableVec::iterator it_end = mxTables.end();
128 for ( ; m_cachePos != it_end; ++m_cachePos )
129 {
130 uno::Reference< container::XNamed > xName( *m_cachePos, uno::UNO_QUERY_THROW );
131 if ( aName.equalsIgnoreAsciiCase( xName->getName() ) )
132 break;
133 }
134 return ( m_cachePos != it_end );
135 }
136};
137
138class TableEnumerationImpl : public ::cppu::WeakImplHelper< css::container::XEnumeration >
139{
140 uno::Reference< XHelperInterface > mxParent;
141 uno::Reference< uno::XComponentContext > mxContext;
142 uno::Reference< frame::XModel > mxDocument;
143 uno::Reference< container::XIndexAccess > mxIndexAccess;
144 sal_Int32 mnCurIndex;
145public:
146 TableEnumerationImpl( uno::Reference< XHelperInterface > xParent, uno::Reference< uno::XComponentContext > xContext, uno::Reference< frame::XModel > xDocument, uno::Reference< container::XIndexAccess > xIndexAccess ) : mxParent(std::move( xParent )), mxContext(std::move( xContext )), mxDocument(std::move( xDocument )), mxIndexAccess(std::move( xIndexAccess )), mnCurIndex(0)
147 {
148 }
149 virtual sal_Bool SAL_CALL hasMoreElements( ) override
150 {
151 return ( mnCurIndex < mxIndexAccess->getCount() );
152 }
153 virtual uno::Any SAL_CALL nextElement( ) override
154 {
155 if ( !hasMoreElements() )
156 throw container::NoSuchElementException();
157 return lcl_createTable( mxParent, mxContext, mxDocument, mxIndexAccess->getByIndex( mnCurIndex++ ) );
158 }
159
160};
161
162}
163
164SwVbaTables::SwVbaTables( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext > & xContext, const uno::Reference< frame::XModel >& xDocument ) : SwVbaTables_BASE( xParent, xContext , uno::Reference< container::XIndexAccess >( new TableCollectionHelper( xDocument ) ) ), mxDocument( xDocument )
165{
166}
167
168uno::Reference< word::XTable > SAL_CALL
169SwVbaTables::Add( const uno::Reference< word::XRange >& Range, const uno::Any& NumRows, const uno::Any& NumColumns, const uno::Any& /*DefaultTableBehavior*/, const uno::Any& /*AutoFitBehavior*/ )
170{
171 sal_Int32 nCols = 0;
172 sal_Int32 nRows = 0;
173 SwVbaRange* pVbaRange = dynamic_cast< SwVbaRange* >( Range.get() );
174 // Preconditions
175 if ( !( pVbaRange && ( NumRows >>= nRows ) && ( NumColumns >>= nCols ) ) )
176 throw uno::RuntimeException(); // #FIXME better exception??
177 if ( nCols <= 0 || nRows <= 0 )
178 throw uno::RuntimeException(); // #FIXME better exception??
179
180 uno::Reference< frame::XModel > xModel( pVbaRange->getDocument(), uno::UNO_QUERY_THROW );
181 uno::Reference< lang::XMultiServiceFactory > xMsf( xModel, uno::UNO_QUERY_THROW );
182 uno::Reference< text::XTextRange > xTextRange = pVbaRange->getXTextRange();
183
184 uno::Reference< text::XTextTable > xTable;
185 xTable.set( xMsf->createInstance("com.sun.star.text.TextTable"), uno::UNO_QUERY_THROW );
186
187 xTable->initialize( nRows, nCols );
188 uno::Reference< text::XText > xText = xTextRange->getText();
189 uno::Reference< text::XTextContent > xContext( xTable, uno::UNO_QUERY_THROW );
190
191 xText->insertTextContent( xTextRange, xContext, true );
192
193 // move the current cursor to the first table cell
194 uno::Reference< table::XCellRange > xCellRange( xTable, uno::UNO_QUERY_THROW );
195 uno::Reference< text::XText> xFirstCellText( xCellRange->getCellByPosition(0, 0), uno::UNO_QUERY_THROW );
196 word::getXTextViewCursor( mxDocument )->gotoRange( xFirstCellText->getStart(), false );
197
198 uno::Reference< word::XTable > xVBATable( new SwVbaTable( mxParent, mxContext, pVbaRange->getDocument(), xTable ) );
199 return xVBATable;
200}
201
202uno::Reference< container::XEnumeration > SAL_CALL
204{
205 return new TableEnumerationImpl( mxParent, mxContext, mxDocument, m_xIndexAccess );
206}
207
208// ScVbaCollectionBaseImpl
211{
212 return lcl_createTable( mxParent, mxContext, mxDocument, aSource );
213}
214
215// XHelperInterface
216OUString
218{
219 return "SwVbaTables";
220}
221
222// XEnumerationAccess
223uno::Type SAL_CALL
225{
227}
228
229uno::Sequence<OUString>
231{
232 static uno::Sequence< OUString > const aServiceNames
233 {
234 "ooo.vba.word.Tables"
235 };
236 return aServiceNames;
237}
238
239/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
struct _ADOIndex Index
unotools::WeakReference< AnimationNode > mxParent
css::uno::Reference< css::uno::XComponentContext > mxContext
css::uno::WeakReference< ov::XHelperInterface > mxParent
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::XTextDocument > & getDocument() const
Definition: vbarange.hxx:57
virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createEnumeration() override
Definition: vbatables.cxx:203
css::uno::Reference< css::frame::XModel > mxDocument
Definition: vbatables.hxx:30
virtual css::uno::Type SAL_CALL getElementType() override
Definition: vbatables.cxx:224
virtual OUString getServiceImplName() override
Definition: vbatables.cxx:217
virtual css::uno::Reference< ov::word::XTable > SAL_CALL Add(const css::uno::Reference< ::ooo::vba::word::XRange > &Range, const css::uno::Any &NumRows, const css::uno::Any &NumColumns, const css::uno::Any &DefaultTableBehavior, const css::uno::Any &AutoFitBehavior) override
Definition: vbatables.cxx:169
virtual css::uno::Sequence< OUString > getServiceNames() override
Definition: vbatables.cxx:230
SwVbaTables(const css::uno::Reference< ov::XHelperInterface > &xParent, const css::uno::Reference< css::uno::XComponentContext > &xContext, const css::uno::Reference< css::frame::XModel > &xDocument)
Definition: vbatables.cxx:164
virtual css::uno::Any createCollectionObject(const css::uno::Any &aSource) override
Definition: vbatables.cxx:210
css::uno::Type const & get()
int nCount
uno::Reference< uno::XComponentContext > mxContext
Sequence< OUString > aServiceNames
OUString aName
Reference
int i
uno::Reference< text::XTextViewCursor > getXTextViewCursor(const uno::Reference< frame::XModel > &xModel)
Reference< XModel > xModel
unsigned char sal_Bool
static uno::Reference< container::XIndexAccess > lcl_getTables(const uno::Reference< frame::XModel > &xDoc)
Definition: vbatables.cxx:37
std::vector< uno::Reference< text::XTextTable > > XTextTableVec
Definition: vbatables.cxx:65
static bool lcl_isInHeaderFooter(const uno::Reference< text::XTextTable > &xTable)
Definition: vbatables.cxx:54
static uno::Any lcl_createTable(const uno::Reference< XHelperInterface > &xParent, const uno::Reference< uno::XComponentContext > &xContext, const uno::Reference< frame::XModel > &xDocument, const uno::Any &aSource)
Definition: vbatables.cxx:46