LCOV - code coverage report
Current view: top level - sw/source/ui/vba - vbatables.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 111 0.0 %
Date: 2014-04-14 Functions: 0 25 0.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10