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