LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/sw/source/ui/vba - vbatables.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 1 112 0.9 %
Date: 2013-07-09 Functions: 2 27 7.4 %
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)
      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)
      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) { return  text::XTextTable::static_type(0); }
      98           0 :     virtual ::sal_Bool SAL_CALL hasElements(  ) throw (uno::RuntimeException) { return getCount() > 0 ; }
      99             :     // XNameAcess
     100           0 :     virtual uno::Any SAL_CALL getByName( const OUString& aName ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
     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)
     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)
     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)
     146             :     {
     147           0 :         return ( mnCurIndex < mxIndexAccess->getCount() );
     148             :     }
     149           0 :     virtual uno::Any SAL_CALL nextElement(  ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
     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             : 
     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)
     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  word::XTable::static_type(0);
     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 ] = OUString("ooo.vba.word.Tables" );
     232             :     }
     233           0 :     return aServiceNames;
     234           3 : }
     235             : 
     236             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10