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