LCOV - code coverage report
Current view: top level - sw/source/ui/vba - vbatables.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 0 111 0.0 %
Date: 2015-06-13 12:38:46 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             : #include <cppuhelper/implbase.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 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 :     OUString aImplName = xServiceInfo->getImplementationName();
      57           0 :     if ( aImplName == "SwXHeadFootText" )
      58           0 :         return true;
      59           0 :     return false;
      60             : }
      61             : 
      62             : typedef ::cppu::WeakImplHelper< css::container::XEnumeration > EnumBase;
      63             : typedef ::cppu::WeakImplHelper< 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 :     explicit 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, std::exception) SAL_OVERRIDE
      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, std::exception) SAL_OVERRIDE
      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, std::exception) SAL_OVERRIDE { return  cppu::UnoType<text::XTextTable>::get(); }
      99           0 :     virtual sal_Bool SAL_CALL hasElements(  ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE { return getCount() > 0 ; }
     100             :     // XNameAcess
     101           0 :     virtual uno::Any SAL_CALL getByName( const OUString& aName ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException, std::exception) SAL_OVERRIDE
     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< OUString > SAL_CALL getElementNames(  ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
     109             :     {
     110           0 :         uno::Sequence< OUString > sNames( mxTables.size() );
     111           0 :         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 OUString& aName ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
     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           0 :                 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, std::exception) SAL_OVERRIDE
     147             :     {
     148           0 :         return ( mnCurIndex < mxIndexAccess->getCount() );
     149             :     }
     150           0 :     virtual uno::Any SAL_CALL nextElement(  ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException, std::exception) SAL_OVERRIDE
     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             : uno::Reference< word::XTable > SAL_CALL
     164           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)
     165             : {
     166           0 :     sal_Int32 nCols = 0;
     167           0 :     sal_Int32 nRows = 0;
     168           0 :     SwVbaRange* pVbaRange = dynamic_cast< SwVbaRange* >( Range.get() );
     169             :     // Preconditions
     170           0 :     if ( !( pVbaRange && ( NumRows >>= nRows ) && ( NumColumns >>= nCols ) ) )
     171           0 :         throw uno::RuntimeException(); // #FIXME better exception??
     172           0 :     if ( nCols <= 0 || nRows <= 0 )
     173           0 :         throw uno::RuntimeException(); // #FIXME better exception??
     174             : 
     175           0 :     uno::Reference< frame::XModel > xModel( pVbaRange->getDocument(), uno::UNO_QUERY_THROW );
     176           0 :     uno::Reference< lang::XMultiServiceFactory > xMsf( xModel, uno::UNO_QUERY_THROW );
     177           0 :     uno::Reference< text::XTextRange > xTextRange = pVbaRange->getXTextRange();
     178             : 
     179           0 :     uno::Reference< text::XTextTable > xTable;
     180           0 :     xTable.set( xMsf->createInstance("com.sun.star.text.TextTable"), uno::UNO_QUERY_THROW );
     181             : 
     182           0 :     xTable->initialize( nRows, nCols );
     183           0 :     uno::Reference< text::XText > xText = xTextRange->getText();
     184           0 :     uno::Reference< text::XTextContent > xContext( xTable, uno::UNO_QUERY_THROW );
     185             : 
     186           0 :     xText->insertTextContent( xTextRange, xContext, true );
     187             : 
     188             :     // move the current cursor to the first table cell
     189           0 :     uno::Reference< table::XCellRange > xCellRange( xTable, uno::UNO_QUERY_THROW );
     190           0 :     uno::Reference< text::XText> xFirstCellText( xCellRange->getCellByPosition(0, 0), uno::UNO_QUERY_THROW );
     191           0 :     word::getXTextViewCursor( mxDocument )->gotoRange( xFirstCellText->getStart(), sal_False );
     192             : 
     193           0 :     uno::Reference< word::XTable > xVBATable( new SwVbaTable( mxParent, mxContext,  pVbaRange->getDocument(), xTable ) );
     194           0 :     return xVBATable;
     195             : }
     196             : 
     197             : uno::Reference< container::XEnumeration > SAL_CALL
     198           0 : SwVbaTables::createEnumeration() throw (uno::RuntimeException)
     199             : {
     200           0 :     return new TableEnumerationImpl( mxParent, mxContext, mxDocument, m_xIndexAccess );
     201             : }
     202             : 
     203             : // ScVbaCollectionBaseImpl
     204             : uno::Any
     205           0 : SwVbaTables::createCollectionObject( const uno::Any& aSource )
     206             : {
     207           0 :     return lcl_createTable( mxParent, mxContext, mxDocument, aSource );
     208             : }
     209             : 
     210             : // XHelperInterface
     211             : OUString
     212           0 : SwVbaTables::getServiceImplName()
     213             : {
     214           0 :     return OUString("SwVbaTables");
     215             : }
     216             : 
     217             : // XEnumerationAccess
     218             : uno::Type SAL_CALL
     219           0 : SwVbaTables::getElementType() throw (uno::RuntimeException)
     220             : {
     221           0 :     return  cppu::UnoType<word::XTable>::get();
     222             : }
     223             : 
     224             : uno::Sequence<OUString>
     225           0 : SwVbaTables::getServiceNames()
     226             : {
     227           0 :     static uno::Sequence< OUString > aServiceNames;
     228           0 :     if ( aServiceNames.getLength() == 0 )
     229             :     {
     230           0 :         aServiceNames.realloc( 1 );
     231           0 :         aServiceNames[ 0 ] = "ooo.vba.word.Tables";
     232             :     }
     233           0 :     return aServiceNames;
     234             : }
     235             : 
     236             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11