1*efeef26fSAndrew Rist /************************************************************** 2*efeef26fSAndrew Rist * 3*efeef26fSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*efeef26fSAndrew Rist * or more contributor license agreements. See the NOTICE file 5*efeef26fSAndrew Rist * distributed with this work for additional information 6*efeef26fSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*efeef26fSAndrew Rist * to you under the Apache License, Version 2.0 (the 8*efeef26fSAndrew Rist * "License"); you may not use this file except in compliance 9*efeef26fSAndrew Rist * with the License. You may obtain a copy of the License at 10*efeef26fSAndrew Rist * 11*efeef26fSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*efeef26fSAndrew Rist * 13*efeef26fSAndrew Rist * Unless required by applicable law or agreed to in writing, 14*efeef26fSAndrew Rist * software distributed under the License is distributed on an 15*efeef26fSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*efeef26fSAndrew Rist * KIND, either express or implied. See the License for the 17*efeef26fSAndrew Rist * specific language governing permissions and limitations 18*efeef26fSAndrew Rist * under the License. 19*efeef26fSAndrew Rist * 20*efeef26fSAndrew Rist *************************************************************/ 21*efeef26fSAndrew Rist 22cdf0e10cSrcweir #include "vbatables.hxx" 23cdf0e10cSrcweir #include "vbatable.hxx" 24cdf0e10cSrcweir #include "vbarange.hxx" 25cdf0e10cSrcweir #include <com/sun/star/text/XTextTable.hpp> 26cdf0e10cSrcweir #include <com/sun/star/text/XTextTablesSupplier.hpp> 27cdf0e10cSrcweir #include <com/sun/star/text/XTextDocument.hpp> 28cdf0e10cSrcweir #include <comphelper/componentcontext.hxx> 29cdf0e10cSrcweir 30cdf0e10cSrcweir using namespace ::ooo::vba; 31cdf0e10cSrcweir using namespace css; 32cdf0e10cSrcweir 33cdf0e10cSrcweir uno::Reference< container::XIndexAccess > lcl_getTables( const uno::Reference< frame::XModel >& xDoc ) 34cdf0e10cSrcweir { 35cdf0e10cSrcweir uno::Reference< container::XIndexAccess > xTables; 36cdf0e10cSrcweir uno::Reference< text::XTextTablesSupplier > xSupp( xDoc, uno::UNO_QUERY ); 37cdf0e10cSrcweir if ( xSupp.is() ) 38cdf0e10cSrcweir xTables.set( xSupp->getTextTables(), uno::UNO_QUERY_THROW ); 39cdf0e10cSrcweir return xTables; 40cdf0e10cSrcweir } 41cdf0e10cSrcweir 42cdf0e10cSrcweir 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 ) 43cdf0e10cSrcweir { 44cdf0e10cSrcweir uno::Reference< text::XTextTable > xTextTable( aSource, uno::UNO_QUERY_THROW ); 45cdf0e10cSrcweir uno::Reference< text::XTextDocument > xTextDocument( xDocument, uno::UNO_QUERY_THROW ); 46cdf0e10cSrcweir uno::Reference< word::XTable > xTable( new SwVbaTable( xParent, xContext, xTextDocument, xTextTable ) ); 47cdf0e10cSrcweir return uno::makeAny( xTable ); 48cdf0e10cSrcweir } 49cdf0e10cSrcweir 50cdf0e10cSrcweir typedef ::cppu::WeakImplHelper1< css::container::XEnumeration > EnumBase; 51cdf0e10cSrcweir 52cdf0e10cSrcweir class TableEnumerationImpl : public EnumBase 53cdf0e10cSrcweir { 54cdf0e10cSrcweir uno::Reference< XHelperInterface > mxParent; 55cdf0e10cSrcweir uno::Reference< uno::XComponentContext > mxContext; 56cdf0e10cSrcweir uno::Reference< frame::XModel > mxDocument; 57cdf0e10cSrcweir uno::Reference< container::XIndexAccess > mxIndexAccess; 58cdf0e10cSrcweir sal_Int32 mnCurIndex; 59cdf0e10cSrcweir public: 60cdf0e10cSrcweir 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) 61cdf0e10cSrcweir { 62cdf0e10cSrcweir } 63cdf0e10cSrcweir virtual ::sal_Bool SAL_CALL hasMoreElements( ) throw (uno::RuntimeException) 64cdf0e10cSrcweir { 65cdf0e10cSrcweir return ( mnCurIndex < mxIndexAccess->getCount() ); 66cdf0e10cSrcweir } 67cdf0e10cSrcweir virtual uno::Any SAL_CALL nextElement( ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException) 68cdf0e10cSrcweir { 69cdf0e10cSrcweir if ( !hasMoreElements() ) 70cdf0e10cSrcweir throw container::NoSuchElementException(); 71cdf0e10cSrcweir return lcl_createTable( mxParent, mxContext, mxDocument, mxIndexAccess->getByIndex( mnCurIndex++ ) ); 72cdf0e10cSrcweir } 73cdf0e10cSrcweir 74cdf0e10cSrcweir }; 75cdf0e10cSrcweir 76cdf0e10cSrcweir SwVbaTables::SwVbaTables( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext > & xContext, const uno::Reference< frame::XModel >& xDocument ) : SwVbaTables_BASE( xParent, xContext , lcl_getTables( xDocument ) ), mxDocument( xDocument ) 77cdf0e10cSrcweir { 78cdf0e10cSrcweir } 79cdf0e10cSrcweir 80cdf0e10cSrcweir 81cdf0e10cSrcweir uno::Reference< word::XTable > SAL_CALL 82cdf0e10cSrcweir 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) 83cdf0e10cSrcweir { 84cdf0e10cSrcweir sal_Int32 nCols = 0; 85cdf0e10cSrcweir sal_Int32 nRows = 0; 86cdf0e10cSrcweir SwVbaRange* pVbaRange = dynamic_cast< SwVbaRange* >( Range.get() ); 87cdf0e10cSrcweir // Preconditions 88cdf0e10cSrcweir if ( !( pVbaRange && ( NumRows >>= nRows ) && ( NumColumns >>= nCols ) ) ) 89cdf0e10cSrcweir throw uno::RuntimeException(); // #FIXME better exception?? 90cdf0e10cSrcweir if ( nCols <= 0 || nRows <= 0 ) 91cdf0e10cSrcweir throw uno::RuntimeException(); // #FIXME better exception?? 92cdf0e10cSrcweir 93cdf0e10cSrcweir uno::Reference< frame::XModel > xModel( pVbaRange->getDocument(), uno::UNO_QUERY_THROW ); 94cdf0e10cSrcweir uno::Reference< lang::XMultiServiceFactory > xMsf( xModel, uno::UNO_QUERY_THROW ); 95cdf0e10cSrcweir uno::Reference< text::XTextRange > xTextRange = pVbaRange->getXTextRange(); 96cdf0e10cSrcweir 97cdf0e10cSrcweir uno::Reference< text::XTextTable > xTable; 98cdf0e10cSrcweir xTable.set( xMsf->createInstance( rtl::OUString::createFromAscii("com.sun.star.text.TextTable") ), uno::UNO_QUERY_THROW ); 99cdf0e10cSrcweir /* 100cdf0e10cSrcweir comphelper::ComponentContext aCtx( xMsf ); 101cdf0e10cSrcweir if ( !aCtx.createComponent( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.text.TextTable") ), xTable ) ); 102cdf0e10cSrcweir throw uno::RuntimeException(); // #FIXME better exception?? 103cdf0e10cSrcweir */ 104cdf0e10cSrcweir xTable->initialize( nRows, nCols ); 105cdf0e10cSrcweir uno::Reference< text::XText > xText = xTextRange->getText(); 106cdf0e10cSrcweir uno::Reference< text::XTextContent > xContext( xTable, uno::UNO_QUERY_THROW ); 107cdf0e10cSrcweir 108cdf0e10cSrcweir xText->insertTextContent( xTextRange, xContext, true ); 109cdf0e10cSrcweir uno::Reference< word::XTable > xVBATable( new SwVbaTable( mxParent, mxContext, pVbaRange->getDocument(), xTable ) ); 110cdf0e10cSrcweir return xVBATable; 111cdf0e10cSrcweir } 112cdf0e10cSrcweir 113cdf0e10cSrcweir uno::Reference< container::XEnumeration > SAL_CALL 114cdf0e10cSrcweir SwVbaTables::createEnumeration() throw (uno::RuntimeException) 115cdf0e10cSrcweir { 116cdf0e10cSrcweir return new TableEnumerationImpl( mxParent, mxContext, mxDocument, m_xIndexAccess ); 117cdf0e10cSrcweir } 118cdf0e10cSrcweir 119cdf0e10cSrcweir // ScVbaCollectionBaseImpl 120cdf0e10cSrcweir uno::Any 121cdf0e10cSrcweir SwVbaTables::createCollectionObject( const uno::Any& aSource ) 122cdf0e10cSrcweir { 123cdf0e10cSrcweir return lcl_createTable( mxParent, mxContext, mxDocument, aSource ); 124cdf0e10cSrcweir } 125cdf0e10cSrcweir 126cdf0e10cSrcweir // XHelperInterface 127cdf0e10cSrcweir rtl::OUString& 128cdf0e10cSrcweir SwVbaTables::getServiceImplName() 129cdf0e10cSrcweir { 130cdf0e10cSrcweir static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("SwVbaTables") ); 131cdf0e10cSrcweir return sImplName; 132cdf0e10cSrcweir } 133cdf0e10cSrcweir 134cdf0e10cSrcweir // XEnumerationAccess 135cdf0e10cSrcweir uno::Type SAL_CALL 136cdf0e10cSrcweir SwVbaTables::getElementType() throw (uno::RuntimeException) 137cdf0e10cSrcweir { 138cdf0e10cSrcweir return word::XTable::static_type(0); 139cdf0e10cSrcweir } 140cdf0e10cSrcweir 141cdf0e10cSrcweir uno::Sequence<rtl::OUString> 142cdf0e10cSrcweir SwVbaTables::getServiceNames() 143cdf0e10cSrcweir { 144cdf0e10cSrcweir static uno::Sequence< rtl::OUString > aServiceNames; 145cdf0e10cSrcweir if ( aServiceNames.getLength() == 0 ) 146cdf0e10cSrcweir { 147cdf0e10cSrcweir aServiceNames.realloc( 1 ); 148cdf0e10cSrcweir aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.word.Tables" ) ); 149cdf0e10cSrcweir } 150cdf0e10cSrcweir return aServiceNames; 151cdf0e10cSrcweir } 152cdf0e10cSrcweir 153