LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/sc/source/ui/vba - vbapagebreaks.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 102 146 69.9 %
Date: 2013-07-09 Functions: 21 36 58.3 %
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             : #include "vbapagebreaks.hxx"
      20             : #include "vbapagebreak.hxx"
      21             : #include <ooo/vba/excel/XWorksheet.hpp>
      22             : using namespace ::com::sun::star;
      23             : using namespace ::ooo::vba;
      24             : 
      25             : typedef ::cppu::WeakImplHelper1<container::XIndexAccess > RangePageBreaks_Base;
      26           6 : class RangePageBreaks : public RangePageBreaks_Base
      27             : {
      28             : private:
      29             :     uno::Reference< XHelperInterface > mxParent;
      30             :     uno::Reference< uno::XComponentContext > mxContext;
      31             :     uno::Reference< sheet::XSheetPageBreak > mxSheetPageBreak;
      32             :     sal_Bool m_bColumn;
      33             : 
      34             : public:
      35           3 :     RangePageBreaks( const uno::Reference< XHelperInterface >& xParent,
      36             :                      const uno::Reference< uno::XComponentContext >& xContext,
      37             :                      uno::Reference< sheet::XSheetPageBreak >& xSheetPageBreak,
      38           3 :                      sal_Bool bColumn ) : mxParent( xParent ), mxContext( xContext ), mxSheetPageBreak( xSheetPageBreak ), m_bColumn( bColumn )
      39             :     {
      40           3 :     }
      41             : 
      42          14 :     sal_Int32 getAPIStartofRange( const uno::Reference< excel::XRange >& xRange ) throw (css::uno::RuntimeException)
      43             :     {
      44          14 :         if( m_bColumn )
      45           7 :             return xRange->getColumn() - 1;
      46           7 :         return xRange->getRow() - 1;
      47             :     }
      48             : 
      49          12 :     sal_Int32 getAPIEndIndexofRange( const uno::Reference< excel::XRange >& xRange, sal_Int32 nUsedStart ) throw (uno::RuntimeException)
      50             :     {
      51          12 :         if( m_bColumn )
      52           6 :             return nUsedStart + xRange->Columns( uno::Any() )->getCount() - 1;
      53           6 :         return nUsedStart + xRange->Rows( uno::Any() )->getCount();
      54             :     }
      55             : 
      56          12 :     uno::Sequence<sheet::TablePageBreakData> getAllPageBreaks() throw (uno::RuntimeException)
      57             :     {
      58          12 :         if( m_bColumn )
      59           6 :             return mxSheetPageBreak->getColumnPageBreaks();
      60           6 :         return mxSheetPageBreak->getRowPageBreaks();
      61             :     }
      62             : 
      63           4 :     uno::Reference<container::XIndexAccess> getRowColContainer() throw (uno::RuntimeException)
      64             :     {
      65           4 :         uno::Reference< table::XColumnRowRange > xColumnRowRange( mxSheetPageBreak, uno::UNO_QUERY_THROW );
      66           4 :         uno::Reference<container::XIndexAccess> xIndexAccess;
      67           4 :         if( m_bColumn )
      68           2 :             xIndexAccess.set( xColumnRowRange->getColumns(), uno::UNO_QUERY_THROW );
      69             :         else
      70           2 :             xIndexAccess.set( xColumnRowRange->getRows(), uno::UNO_QUERY_THROW );
      71           4 :         return xIndexAccess;
      72             :     }
      73             : 
      74             :     sheet::TablePageBreakData getTablePageBreakData( sal_Int32 nAPIItemIndex ) throw ( script::BasicErrorException, uno::RuntimeException);
      75             :     uno::Any Add( const css::uno::Any& Before ) throw ( css::script::BasicErrorException, css::uno::RuntimeException);
      76             : 
      77             :     // XIndexAccess
      78             :     virtual sal_Int32 SAL_CALL getCount(  ) throw (uno::RuntimeException);
      79             :     virtual uno::Any SAL_CALL getByIndex( sal_Int32 Index ) throw (lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException);
      80           0 :     virtual uno::Type SAL_CALL getElementType(  ) throw (uno::RuntimeException)
      81             :     {
      82           0 :         if( m_bColumn )
      83           0 :              return excel::XVPageBreak::static_type(0);
      84           0 :         return  excel::XHPageBreak::static_type(0);
      85             :     }
      86           0 :     virtual sal_Bool SAL_CALL hasElements(  ) throw (uno::RuntimeException)
      87             :     {
      88           0 :         return sal_True;
      89             :     }
      90             : };
      91             : 
      92             : /** @TODO Unlike MS Excel this method only considers the pagebreaks that intersect the used range
      93             : *  To become completely compatible the print area has to be considered. As far as I found out this printarea
      94             : *  also considers the position and sizes of shapes and manually inserted page breaks
      95             : *  Note: In MS  there is a limit of 1026 horizontal page breaks per sheet.
      96             : */
      97          10 : sal_Int32 SAL_CALL RangePageBreaks::getCount(  ) throw (uno::RuntimeException)
      98             : {
      99          10 :     sal_Int32 nCount = 0;
     100          10 :     uno::Reference< excel::XWorksheet > xWorksheet( mxParent, uno::UNO_QUERY_THROW );
     101          20 :     uno::Reference< excel::XRange > xRange = xWorksheet->getUsedRange();
     102          10 :     sal_Int32 nUsedStart = getAPIStartofRange( xRange );
     103          10 :     sal_Int32 nUsedEnd = getAPIEndIndexofRange( xRange, nUsedStart );
     104          20 :     uno::Sequence<sheet::TablePageBreakData> aTablePageBreakData = getAllPageBreaks();
     105             : 
     106          10 :     sal_Int32 nLength = aTablePageBreakData.getLength();
     107          31 :     for( sal_Int32 i=0; i<nLength; i++ )
     108             :     {
     109          31 :         sal_Int32 nPos = aTablePageBreakData[i].Position;
     110          31 :         if( nPos > nUsedEnd + 1)
     111          10 :             return nCount;
     112          21 :         nCount++;
     113             :     }
     114             : 
     115          10 :     return nCount;
     116             : }
     117             : 
     118           2 : uno::Any SAL_CALL RangePageBreaks::getByIndex( sal_Int32 Index ) throw (lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException)
     119             : {
     120           2 :     if( (Index < getCount()) && ( Index >= 0 ))
     121             :     {
     122           2 :         sheet::TablePageBreakData aTablePageBreakData = getTablePageBreakData( Index );
     123           2 :         uno::Reference< container::XIndexAccess > xIndexAccess = getRowColContainer();
     124           2 :         sal_Int32 nPos = aTablePageBreakData.Position;
     125           2 :         if( (nPos < xIndexAccess->getCount()) && (nPos > -1) )
     126             :         {
     127           2 :             uno::Reference< beans::XPropertySet > xRowColPropertySet( xIndexAccess->getByIndex(nPos), uno::UNO_QUERY_THROW );
     128           2 :             if( m_bColumn )
     129           1 :                 return uno::makeAny( uno::Reference< excel::XVPageBreak >( new ScVbaVPageBreak( mxParent, mxContext, xRowColPropertySet, aTablePageBreakData) ));
     130           1 :             return uno::makeAny( uno::Reference< excel::XHPageBreak >( new ScVbaHPageBreak( mxParent, mxContext, xRowColPropertySet, aTablePageBreakData) ));
     131           0 :         }
     132             :     }
     133           0 :     throw lang::IndexOutOfBoundsException();
     134             : }
     135             : 
     136           2 : sheet::TablePageBreakData RangePageBreaks::getTablePageBreakData( sal_Int32 nAPIItemIndex ) throw ( script::BasicErrorException, uno::RuntimeException)
     137             : {
     138           2 :     sal_Int32 index = -1;
     139           2 :     sheet::TablePageBreakData aTablePageBreakData;
     140           2 :     uno::Reference< excel::XWorksheet > xWorksheet( mxParent, uno::UNO_QUERY_THROW );
     141           4 :     uno::Reference< excel::XRange > xRange = xWorksheet->getUsedRange();
     142           2 :     sal_Int32 nUsedStart = getAPIStartofRange( xRange );
     143           2 :     sal_Int32 nUsedEnd = getAPIEndIndexofRange( xRange, nUsedStart );
     144           4 :     uno::Sequence<sheet::TablePageBreakData> aTablePageBreakDataList = getAllPageBreaks();
     145             : 
     146           2 :     sal_Int32 nLength = aTablePageBreakDataList.getLength();
     147           2 :     for( sal_Int32 i=0; i<nLength; i++ )
     148             :     {
     149           2 :         aTablePageBreakData = aTablePageBreakDataList[i];
     150           2 :         sal_Int32 nPos = aTablePageBreakData.Position;
     151           2 :         if( nPos >= nUsedStart )
     152           2 :             index++;
     153           2 :         if( nPos > nUsedEnd )
     154           0 :             DebugHelper::exception(SbERR_METHOD_FAILED, OUString());
     155           2 :         if( index == nAPIItemIndex )
     156           2 :             return aTablePageBreakData;
     157             :     }
     158             : 
     159           2 :     return aTablePageBreakData;
     160             : }
     161             : 
     162           2 : uno::Any RangePageBreaks::Add( const css::uno::Any& Before ) throw ( css::script::BasicErrorException, css::uno::RuntimeException)
     163             : {
     164           2 :     uno::Reference< excel::XRange > xRange;
     165           2 :     Before >>= xRange;
     166           2 :     if( !xRange.is() )
     167             :     {
     168           0 :         DebugHelper::exception(SbERR_BAD_ARGUMENT, OUString());
     169             :     }
     170             : 
     171           2 :     sal_Int32 nAPIRowColIndex = getAPIStartofRange( xRange );
     172           4 :     uno::Reference< container::XIndexAccess > xIndexAccess = getRowColContainer();
     173           4 :     uno::Reference< beans::XPropertySet > xRowColPropertySet( xIndexAccess->getByIndex(nAPIRowColIndex), uno::UNO_QUERY_THROW );
     174           2 :     xRowColPropertySet->setPropertyValue("IsStartOfNewPage", uno::makeAny(sal_True));
     175           2 :     sheet::TablePageBreakData aTablePageBreakData;
     176           2 :     aTablePageBreakData.ManualBreak = sal_True;
     177           2 :     aTablePageBreakData.Position = nAPIRowColIndex;
     178           2 :     if( m_bColumn )
     179           1 :         return uno::makeAny( uno::Reference< excel::XVPageBreak >( new ScVbaVPageBreak( mxParent, mxContext, xRowColPropertySet, aTablePageBreakData) ));
     180           3 :     return uno::makeAny( uno::Reference< excel::XHPageBreak >( new ScVbaHPageBreak( mxParent, mxContext, xRowColPropertySet, aTablePageBreakData) ));
     181             : }
     182             : 
     183             : 
     184           0 : class RangePageBreaksEnumWrapper : public EnumerationHelper_BASE
     185             : {
     186             :     uno::Reference<container::XIndexAccess > m_xIndexAccess;
     187             :     sal_Int32 nIndex;
     188             : public:
     189           0 :     RangePageBreaksEnumWrapper( const uno::Reference< container::XIndexAccess >& xIndexAccess ) : m_xIndexAccess( xIndexAccess ), nIndex( 0 ) {}
     190           0 :     virtual ::sal_Bool SAL_CALL hasMoreElements(  ) throw (uno::RuntimeException)
     191             :     {
     192           0 :         return ( nIndex < m_xIndexAccess->getCount() );
     193             :     }
     194             : 
     195           0 :     virtual uno::Any SAL_CALL nextElement(  ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
     196             :     {
     197           0 :         if ( nIndex < m_xIndexAccess->getCount() )
     198           0 :             return m_xIndexAccess->getByIndex( nIndex++ );
     199           0 :         throw container::NoSuchElementException();
     200             :     }
     201             : };
     202             : 
     203           1 : ScVbaHPageBreaks::ScVbaHPageBreaks( const uno::Reference< XHelperInterface >& xParent,
     204             :                                     const uno::Reference< uno::XComponentContext >& xContext,
     205             :                                     uno::Reference< sheet::XSheetPageBreak >& xSheetPageBreak) throw (uno::RuntimeException):
     206           1 :                           ScVbaHPageBreaks_BASE( xParent,xContext, new RangePageBreaks( xParent, xContext, xSheetPageBreak, false )),
     207           2 :                           mxSheetPageBreak( xSheetPageBreak )
     208             : {
     209           1 : }
     210             : 
     211           1 : uno::Any SAL_CALL ScVbaHPageBreaks::Add( const uno::Any& Before) throw ( script::BasicErrorException, uno::RuntimeException)
     212             : {
     213           1 :     RangePageBreaks* pPageBreaks = dynamic_cast< RangePageBreaks* >( m_xIndexAccess.get() );
     214           1 :     if( pPageBreaks )
     215             :     {
     216           1 :         return pPageBreaks->Add( Before );
     217             :     }
     218           0 :     return uno::Any();
     219             : }
     220             : 
     221             : uno::Reference< container::XEnumeration >
     222           0 : ScVbaHPageBreaks::createEnumeration() throw (uno::RuntimeException)
     223             : {
     224           0 :     return new RangePageBreaksEnumWrapper( m_xIndexAccess );
     225             : }
     226             : 
     227             : uno::Any
     228           1 : ScVbaHPageBreaks::createCollectionObject( const css::uno::Any& aSource )
     229             : {
     230           1 :     return aSource; // its already a pagebreak object
     231             : }
     232             : 
     233             : uno::Type
     234           0 : ScVbaHPageBreaks::getElementType() throw (uno::RuntimeException)
     235             : {
     236           0 :     return excel::XHPageBreak::static_type(0);
     237             : }
     238             : 
     239             : OUString
     240           0 : ScVbaHPageBreaks::getServiceImplName()
     241             : {
     242           0 :     return OUString("ScVbaHPageBreaks");
     243             : }
     244             : 
     245             : uno::Sequence< OUString >
     246           0 : ScVbaHPageBreaks::getServiceNames()
     247             : {
     248           0 :     static uno::Sequence< OUString > aServiceNames;
     249           0 :     if ( aServiceNames.getLength() == 0 )
     250             :     {
     251           0 :         aServiceNames.realloc( 1 );
     252           0 :         aServiceNames[ 0 ] = OUString("ooo.vba.excel.HPageBreaks" );
     253             :     }
     254           0 :     return aServiceNames;
     255             : }
     256             : 
     257             : //VPageBreak
     258           2 : ScVbaVPageBreaks::ScVbaVPageBreaks( const uno::Reference< XHelperInterface >& xParent,
     259             :                                     const uno::Reference< uno::XComponentContext >& xContext,
     260             :                                     uno::Reference< sheet::XSheetPageBreak >& xSheetPageBreak ) throw ( uno::RuntimeException )
     261           2 : :   ScVbaVPageBreaks_BASE( xParent, xContext, new RangePageBreaks( xParent, xContext, xSheetPageBreak, sal_True ) ),
     262           4 :     mxSheetPageBreak( xSheetPageBreak )
     263             : {
     264           2 : }
     265             : 
     266           4 : ScVbaVPageBreaks::~ScVbaVPageBreaks()
     267             : {
     268           4 : }
     269             : 
     270             : uno::Any SAL_CALL
     271           1 : ScVbaVPageBreaks::Add( const uno::Any& Before ) throw ( script::BasicErrorException, uno::RuntimeException )
     272             : {
     273           1 :     RangePageBreaks* pPageBreaks = dynamic_cast< RangePageBreaks* >( m_xIndexAccess.get() );
     274           1 :     if( pPageBreaks )
     275             :     {
     276           1 :         return pPageBreaks->Add( Before );
     277             :     }
     278           0 :     return uno::Any();
     279             : }
     280             : 
     281             : uno::Reference< container::XEnumeration >
     282           0 : ScVbaVPageBreaks::createEnumeration() throw ( uno::RuntimeException )
     283             : {
     284           0 :     return new RangePageBreaksEnumWrapper( m_xIndexAccess );
     285             : }
     286             : 
     287             : uno::Any
     288           1 : ScVbaVPageBreaks::createCollectionObject( const css::uno::Any& aSource )
     289             : {
     290           1 :     return aSource; // its already a pagebreak object
     291             : }
     292             : 
     293             : uno::Type
     294           0 : ScVbaVPageBreaks::getElementType() throw ( uno::RuntimeException )
     295             : {
     296           0 :     return excel::XVPageBreak::static_type( 0 );
     297             : }
     298             : 
     299             : OUString
     300           0 : ScVbaVPageBreaks::getServiceImplName()
     301             : {
     302           0 :     return OUString("ScVbaVPageBreaks");
     303             : }
     304             : 
     305             : uno::Sequence< OUString >
     306           0 : ScVbaVPageBreaks::getServiceNames()
     307             : {
     308           0 :     static uno::Sequence< OUString > aServiceNames;
     309           0 :     if ( aServiceNames.getLength() == 0 )
     310             :     {
     311           0 :         aServiceNames.realloc( 1 );
     312           0 :         aServiceNames[ 0 ] = OUString( "ooo.vba.excel.VPageBreaks" );
     313             :     }
     314           0 :     return aServiceNames;
     315           6 : }
     316             : 
     317             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10