1*b3f79822SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*b3f79822SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*b3f79822SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*b3f79822SAndrew Rist * distributed with this work for additional information 6*b3f79822SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*b3f79822SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*b3f79822SAndrew Rist * "License"); you may not use this file except in compliance 9*b3f79822SAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11*b3f79822SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13*b3f79822SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*b3f79822SAndrew Rist * software distributed under the License is distributed on an 15*b3f79822SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*b3f79822SAndrew Rist * KIND, either express or implied. See the License for the 17*b3f79822SAndrew Rist * specific language governing permissions and limitations 18*b3f79822SAndrew Rist * under the License. 19cdf0e10cSrcweir * 20*b3f79822SAndrew Rist *************************************************************/ 21*b3f79822SAndrew Rist 22*b3f79822SAndrew Rist 23cdf0e10cSrcweir #include "vbapagebreaks.hxx" 24cdf0e10cSrcweir #include "vbapagebreak.hxx" 25cdf0e10cSrcweir #include <ooo/vba/excel/XWorksheet.hpp> 26cdf0e10cSrcweir using namespace ::com::sun::star; 27cdf0e10cSrcweir using namespace ::ooo::vba; 28cdf0e10cSrcweir 29cdf0e10cSrcweir typedef ::cppu::WeakImplHelper1<container::XIndexAccess > RangePageBreaks_Base; 30cdf0e10cSrcweir class RangePageBreaks : public RangePageBreaks_Base 31cdf0e10cSrcweir { 32cdf0e10cSrcweir private: 33cdf0e10cSrcweir uno::Reference< XHelperInterface > mxParent; 34cdf0e10cSrcweir uno::Reference< uno::XComponentContext > mxContext; 35cdf0e10cSrcweir uno::Reference< sheet::XSheetPageBreak > mxSheetPageBreak; 36cdf0e10cSrcweir sal_Bool m_bColumn; 37cdf0e10cSrcweir 38cdf0e10cSrcweir public: 39cdf0e10cSrcweir RangePageBreaks( const uno::Reference< XHelperInterface >& xParent, 40cdf0e10cSrcweir const uno::Reference< uno::XComponentContext >& xContext, 41cdf0e10cSrcweir uno::Reference< sheet::XSheetPageBreak >& xSheetPageBreak, 42cdf0e10cSrcweir sal_Bool bColumn ) : mxParent( xParent ), mxContext( xContext ), mxSheetPageBreak( xSheetPageBreak ), m_bColumn( bColumn ) 43cdf0e10cSrcweir { 44cdf0e10cSrcweir } 45cdf0e10cSrcweir 46cdf0e10cSrcweir sal_Int32 getAPIStartofRange( const uno::Reference< excel::XRange >& xRange ) throw (css::uno::RuntimeException) 47cdf0e10cSrcweir { 48cdf0e10cSrcweir if( m_bColumn ) 49cdf0e10cSrcweir return xRange->getColumn() - 1; 50cdf0e10cSrcweir return xRange->getRow() - 1; 51cdf0e10cSrcweir } 52cdf0e10cSrcweir 53cdf0e10cSrcweir sal_Int32 getAPIEndIndexofRange( const uno::Reference< excel::XRange >& xRange, sal_Int32 nUsedStart ) throw (uno::RuntimeException) 54cdf0e10cSrcweir { 55cdf0e10cSrcweir if( m_bColumn ) 56cdf0e10cSrcweir return nUsedStart + xRange->Columns( uno::Any() )->getCount(); 57cdf0e10cSrcweir return nUsedStart + xRange->Rows( uno::Any() )->getCount(); 58cdf0e10cSrcweir } 59cdf0e10cSrcweir 60cdf0e10cSrcweir uno::Sequence<sheet::TablePageBreakData> getAllPageBreaks() throw (uno::RuntimeException) 61cdf0e10cSrcweir { 62cdf0e10cSrcweir if( m_bColumn ) 63cdf0e10cSrcweir return mxSheetPageBreak->getColumnPageBreaks(); 64cdf0e10cSrcweir return mxSheetPageBreak->getRowPageBreaks(); 65cdf0e10cSrcweir } 66cdf0e10cSrcweir 67cdf0e10cSrcweir uno::Reference<container::XIndexAccess> getRowColContainer() throw (uno::RuntimeException) 68cdf0e10cSrcweir { 69cdf0e10cSrcweir uno::Reference< table::XColumnRowRange > xColumnRowRange( mxSheetPageBreak, uno::UNO_QUERY_THROW ); 70cdf0e10cSrcweir uno::Reference<container::XIndexAccess> xIndexAccess; 71cdf0e10cSrcweir if( m_bColumn ) 72cdf0e10cSrcweir xIndexAccess.set( xColumnRowRange->getColumns(), uno::UNO_QUERY_THROW ); 73cdf0e10cSrcweir else 74cdf0e10cSrcweir xIndexAccess.set( xColumnRowRange->getRows(), uno::UNO_QUERY_THROW ); 75cdf0e10cSrcweir return xIndexAccess; 76cdf0e10cSrcweir } 77cdf0e10cSrcweir 78cdf0e10cSrcweir sheet::TablePageBreakData getTablePageBreakData( sal_Int32 nAPIItemIndex ) throw ( script::BasicErrorException, uno::RuntimeException); 79cdf0e10cSrcweir uno::Any Add( const css::uno::Any& Before ) throw ( css::script::BasicErrorException, css::uno::RuntimeException); 80cdf0e10cSrcweir 81cdf0e10cSrcweir // XIndexAccess 82cdf0e10cSrcweir virtual sal_Int32 SAL_CALL getCount( ) throw (uno::RuntimeException); 83cdf0e10cSrcweir virtual uno::Any SAL_CALL getByIndex( sal_Int32 Index ) throw (lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException); 84cdf0e10cSrcweir virtual uno::Type SAL_CALL getElementType( ) throw (uno::RuntimeException) 85cdf0e10cSrcweir { 86cdf0e10cSrcweir if( m_bColumn ) 87cdf0e10cSrcweir return excel::XVPageBreak::static_type(0); 88cdf0e10cSrcweir return excel::XHPageBreak::static_type(0); 89cdf0e10cSrcweir } 90cdf0e10cSrcweir virtual sal_Bool SAL_CALL hasElements( ) throw (uno::RuntimeException) 91cdf0e10cSrcweir { 92cdf0e10cSrcweir return sal_True; 93cdf0e10cSrcweir } 94cdf0e10cSrcweir }; 95cdf0e10cSrcweir 96cdf0e10cSrcweir /** @TODO Unlike MS Excel this method only considers the pagebreaks that intersect the used range 97cdf0e10cSrcweir * To become completely compatible the print area has to be considered. As far as I found out this printarea 98cdf0e10cSrcweir * also considers the position and sizes of shapes and manually inserted page breaks 99cdf0e10cSrcweir * Note: In MS there is a limit of 1026 horizontal page breaks per sheet. 100cdf0e10cSrcweir */ 101cdf0e10cSrcweir sal_Int32 SAL_CALL RangePageBreaks::getCount( ) throw (uno::RuntimeException) 102cdf0e10cSrcweir { 103cdf0e10cSrcweir sal_Int32 nCount = 0; 104cdf0e10cSrcweir uno::Reference< excel::XWorksheet > xWorksheet( mxParent, uno::UNO_QUERY_THROW ); 105cdf0e10cSrcweir uno::Reference< excel::XRange > xRange = xWorksheet->getUsedRange(); 106cdf0e10cSrcweir sal_Int32 nUsedStart = getAPIStartofRange( xRange ); 107cdf0e10cSrcweir sal_Int32 nUsedEnd = getAPIEndIndexofRange( xRange, nUsedStart ); 108cdf0e10cSrcweir uno::Sequence<sheet::TablePageBreakData> aTablePageBreakData = getAllPageBreaks(); 109cdf0e10cSrcweir 110cdf0e10cSrcweir sal_Int32 nLength = aTablePageBreakData.getLength(); 111cdf0e10cSrcweir for( sal_Int32 i=0; i<nLength; i++ ) 112cdf0e10cSrcweir { 113cdf0e10cSrcweir sal_Int32 nPos = aTablePageBreakData[i].Position; 114cdf0e10cSrcweir if( nPos > nUsedEnd ) 115cdf0e10cSrcweir return nCount; 116cdf0e10cSrcweir if( nPos >= nUsedStart ) 117cdf0e10cSrcweir nCount++; 118cdf0e10cSrcweir } 119cdf0e10cSrcweir 120cdf0e10cSrcweir return nCount; 121cdf0e10cSrcweir } 122cdf0e10cSrcweir 123cdf0e10cSrcweir uno::Any SAL_CALL RangePageBreaks::getByIndex( sal_Int32 Index ) throw (lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException) 124cdf0e10cSrcweir { 125cdf0e10cSrcweir if( (Index < getCount()) && ( Index >= 0 )) 126cdf0e10cSrcweir { 127cdf0e10cSrcweir sheet::TablePageBreakData aTablePageBreakData = getTablePageBreakData( Index ); 128cdf0e10cSrcweir uno::Reference< container::XIndexAccess > xIndexAccess = getRowColContainer(); 129cdf0e10cSrcweir sal_Int32 nPos = aTablePageBreakData.Position; 130cdf0e10cSrcweir if( (nPos < xIndexAccess->getCount()) && (nPos > -1) ) 131cdf0e10cSrcweir { 132cdf0e10cSrcweir uno::Reference< beans::XPropertySet > xRowColPropertySet( xIndexAccess->getByIndex(nPos), uno::UNO_QUERY_THROW ); 133cdf0e10cSrcweir if( m_bColumn ) 134cdf0e10cSrcweir return uno::makeAny( uno::Reference< excel::XVPageBreak >( new ScVbaVPageBreak( mxParent, mxContext, xRowColPropertySet, aTablePageBreakData) )); 135cdf0e10cSrcweir return uno::makeAny( uno::Reference< excel::XHPageBreak >( new ScVbaHPageBreak( mxParent, mxContext, xRowColPropertySet, aTablePageBreakData) )); 136cdf0e10cSrcweir } 137cdf0e10cSrcweir } 138cdf0e10cSrcweir throw lang::IndexOutOfBoundsException(); 139cdf0e10cSrcweir } 140cdf0e10cSrcweir 141cdf0e10cSrcweir sheet::TablePageBreakData RangePageBreaks::getTablePageBreakData( sal_Int32 nAPIItemIndex ) throw ( script::BasicErrorException, uno::RuntimeException) 142cdf0e10cSrcweir { 143cdf0e10cSrcweir sal_Int32 index = -1; 144cdf0e10cSrcweir sheet::TablePageBreakData aTablePageBreakData; 145cdf0e10cSrcweir uno::Reference< excel::XWorksheet > xWorksheet( mxParent, uno::UNO_QUERY_THROW ); 146cdf0e10cSrcweir uno::Reference< excel::XRange > xRange = xWorksheet->getUsedRange(); 147cdf0e10cSrcweir sal_Int32 nUsedStart = getAPIStartofRange( xRange ); 148cdf0e10cSrcweir sal_Int32 nUsedEnd = getAPIEndIndexofRange( xRange, nUsedStart ); 149cdf0e10cSrcweir uno::Sequence<sheet::TablePageBreakData> aTablePageBreakDataList = getAllPageBreaks(); 150cdf0e10cSrcweir 151cdf0e10cSrcweir sal_Int32 nLength = aTablePageBreakDataList.getLength(); 152cdf0e10cSrcweir for( sal_Int32 i=0; i<nLength; i++ ) 153cdf0e10cSrcweir { 154cdf0e10cSrcweir aTablePageBreakData = aTablePageBreakDataList[i]; 155cdf0e10cSrcweir sal_Int32 nPos = aTablePageBreakData.Position; 156cdf0e10cSrcweir if( nPos >= nUsedStart ) 157cdf0e10cSrcweir index++; 158cdf0e10cSrcweir if( nPos > nUsedEnd ) 159cdf0e10cSrcweir DebugHelper::exception(SbERR_METHOD_FAILED, rtl::OUString()); 160cdf0e10cSrcweir if( index == nAPIItemIndex ) 161cdf0e10cSrcweir return aTablePageBreakData; 162cdf0e10cSrcweir } 163cdf0e10cSrcweir 164cdf0e10cSrcweir return aTablePageBreakData; 165cdf0e10cSrcweir } 166cdf0e10cSrcweir 167cdf0e10cSrcweir uno::Any RangePageBreaks::Add( const css::uno::Any& Before ) throw ( css::script::BasicErrorException, css::uno::RuntimeException) 168cdf0e10cSrcweir { 169cdf0e10cSrcweir uno::Reference< excel::XRange > xRange; 170cdf0e10cSrcweir Before >>= xRange; 171cdf0e10cSrcweir if( !xRange.is() ) 172cdf0e10cSrcweir { 173cdf0e10cSrcweir DebugHelper::exception(SbERR_BAD_ARGUMENT, rtl::OUString()); 174cdf0e10cSrcweir } 175cdf0e10cSrcweir 176cdf0e10cSrcweir sal_Int32 nAPIRowColIndex = getAPIStartofRange( xRange ); 177cdf0e10cSrcweir uno::Reference< container::XIndexAccess > xIndexAccess = getRowColContainer(); 178cdf0e10cSrcweir uno::Reference< beans::XPropertySet > xRowColPropertySet( xIndexAccess->getByIndex(nAPIRowColIndex), uno::UNO_QUERY_THROW ); 179cdf0e10cSrcweir xRowColPropertySet->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "IsStartOfNewPage" )), uno::makeAny(sal_True)); 180cdf0e10cSrcweir sheet::TablePageBreakData aTablePageBreakData; 181cdf0e10cSrcweir aTablePageBreakData.ManualBreak = sal_True; 182cdf0e10cSrcweir aTablePageBreakData.Position = nAPIRowColIndex; 183cdf0e10cSrcweir if( m_bColumn ) 184cdf0e10cSrcweir return uno::makeAny( uno::Reference< excel::XVPageBreak >( new ScVbaVPageBreak( mxParent, mxContext, xRowColPropertySet, aTablePageBreakData) )); 185cdf0e10cSrcweir return uno::makeAny( uno::Reference< excel::XHPageBreak >( new ScVbaHPageBreak( mxParent, mxContext, xRowColPropertySet, aTablePageBreakData) )); 186cdf0e10cSrcweir } 187cdf0e10cSrcweir 188cdf0e10cSrcweir 189cdf0e10cSrcweir class RangePageBreaksEnumWrapper : public EnumerationHelper_BASE 190cdf0e10cSrcweir { 191cdf0e10cSrcweir uno::Reference<container::XIndexAccess > m_xIndexAccess; 192cdf0e10cSrcweir sal_Int32 nIndex; 193cdf0e10cSrcweir public: 194cdf0e10cSrcweir RangePageBreaksEnumWrapper( const uno::Reference< container::XIndexAccess >& xIndexAccess ) : m_xIndexAccess( xIndexAccess ), nIndex( 0 ) {} 195cdf0e10cSrcweir virtual ::sal_Bool SAL_CALL hasMoreElements( ) throw (uno::RuntimeException) 196cdf0e10cSrcweir { 197cdf0e10cSrcweir return ( nIndex < m_xIndexAccess->getCount() ); 198cdf0e10cSrcweir } 199cdf0e10cSrcweir 200cdf0e10cSrcweir virtual uno::Any SAL_CALL nextElement( ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException) 201cdf0e10cSrcweir { 202cdf0e10cSrcweir if ( nIndex < m_xIndexAccess->getCount() ) 203cdf0e10cSrcweir return m_xIndexAccess->getByIndex( nIndex++ ); 204cdf0e10cSrcweir throw container::NoSuchElementException(); 205cdf0e10cSrcweir } 206cdf0e10cSrcweir }; 207cdf0e10cSrcweir 208cdf0e10cSrcweir ScVbaHPageBreaks::ScVbaHPageBreaks( const uno::Reference< XHelperInterface >& xParent, 209cdf0e10cSrcweir const uno::Reference< uno::XComponentContext >& xContext, 210cdf0e10cSrcweir uno::Reference< sheet::XSheetPageBreak >& xSheetPageBreak) throw (uno::RuntimeException): 211cdf0e10cSrcweir ScVbaHPageBreaks_BASE( xParent,xContext, new RangePageBreaks( xParent, xContext, xSheetPageBreak, sal_False )), 212cdf0e10cSrcweir mxSheetPageBreak( xSheetPageBreak ) 213cdf0e10cSrcweir { 214cdf0e10cSrcweir } 215cdf0e10cSrcweir 216cdf0e10cSrcweir uno::Any SAL_CALL ScVbaHPageBreaks::Add( const uno::Any& Before) throw ( script::BasicErrorException, uno::RuntimeException) 217cdf0e10cSrcweir { 218cdf0e10cSrcweir RangePageBreaks* pPageBreaks = dynamic_cast< RangePageBreaks* >( m_xIndexAccess.get() ); 219cdf0e10cSrcweir if( pPageBreaks ) 220cdf0e10cSrcweir { 221cdf0e10cSrcweir return pPageBreaks->Add( Before ); 222cdf0e10cSrcweir } 223cdf0e10cSrcweir return uno::Any(); 224cdf0e10cSrcweir } 225cdf0e10cSrcweir 226cdf0e10cSrcweir uno::Reference< container::XEnumeration > 227cdf0e10cSrcweir ScVbaHPageBreaks::createEnumeration() throw (uno::RuntimeException) 228cdf0e10cSrcweir { 229cdf0e10cSrcweir return new RangePageBreaksEnumWrapper( m_xIndexAccess ); 230cdf0e10cSrcweir } 231cdf0e10cSrcweir 232cdf0e10cSrcweir uno::Any 233cdf0e10cSrcweir ScVbaHPageBreaks::createCollectionObject( const css::uno::Any& aSource ) 234cdf0e10cSrcweir { 235cdf0e10cSrcweir return aSource; // its already a pagebreak object 236cdf0e10cSrcweir } 237cdf0e10cSrcweir 238cdf0e10cSrcweir uno::Type 239cdf0e10cSrcweir ScVbaHPageBreaks::getElementType() throw (uno::RuntimeException) 240cdf0e10cSrcweir { 241cdf0e10cSrcweir return excel::XHPageBreak::static_type(0); 242cdf0e10cSrcweir } 243cdf0e10cSrcweir 244cdf0e10cSrcweir rtl::OUString& 245cdf0e10cSrcweir ScVbaHPageBreaks::getServiceImplName() 246cdf0e10cSrcweir { 247cdf0e10cSrcweir static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaHPageBreaks") ); 248cdf0e10cSrcweir return sImplName; 249cdf0e10cSrcweir } 250cdf0e10cSrcweir 251cdf0e10cSrcweir uno::Sequence< rtl::OUString > 252cdf0e10cSrcweir ScVbaHPageBreaks::getServiceNames() 253cdf0e10cSrcweir { 254cdf0e10cSrcweir static uno::Sequence< rtl::OUString > aServiceNames; 255cdf0e10cSrcweir if ( aServiceNames.getLength() == 0 ) 256cdf0e10cSrcweir { 257cdf0e10cSrcweir aServiceNames.realloc( 1 ); 258cdf0e10cSrcweir aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.HPageBreaks" ) ); 259cdf0e10cSrcweir } 260cdf0e10cSrcweir return aServiceNames; 261cdf0e10cSrcweir } 262cdf0e10cSrcweir 263cdf0e10cSrcweir //VPageBreak 264cdf0e10cSrcweir ScVbaVPageBreaks::ScVbaVPageBreaks( const uno::Reference< XHelperInterface >& xParent, 265cdf0e10cSrcweir const uno::Reference< uno::XComponentContext >& xContext, 266cdf0e10cSrcweir uno::Reference< sheet::XSheetPageBreak >& xSheetPageBreak ) throw ( uno::RuntimeException ) 267cdf0e10cSrcweir : ScVbaVPageBreaks_BASE( xParent, xContext, new RangePageBreaks( xParent, xContext, xSheetPageBreak, sal_True ) ), 268cdf0e10cSrcweir mxSheetPageBreak( xSheetPageBreak ) 269cdf0e10cSrcweir { 270cdf0e10cSrcweir } 271cdf0e10cSrcweir 272cdf0e10cSrcweir ScVbaVPageBreaks::~ScVbaVPageBreaks() 273cdf0e10cSrcweir { 274cdf0e10cSrcweir } 275cdf0e10cSrcweir 276cdf0e10cSrcweir uno::Any SAL_CALL 277cdf0e10cSrcweir ScVbaVPageBreaks::Add( const uno::Any& Before ) throw ( script::BasicErrorException, uno::RuntimeException ) 278cdf0e10cSrcweir { 279cdf0e10cSrcweir RangePageBreaks* pPageBreaks = dynamic_cast< RangePageBreaks* >( m_xIndexAccess.get() ); 280cdf0e10cSrcweir if( pPageBreaks ) 281cdf0e10cSrcweir { 282cdf0e10cSrcweir return pPageBreaks->Add( Before ); 283cdf0e10cSrcweir } 284cdf0e10cSrcweir return uno::Any(); 285cdf0e10cSrcweir } 286cdf0e10cSrcweir 287cdf0e10cSrcweir uno::Reference< container::XEnumeration > 288cdf0e10cSrcweir ScVbaVPageBreaks::createEnumeration() throw ( uno::RuntimeException ) 289cdf0e10cSrcweir { 290cdf0e10cSrcweir return new RangePageBreaksEnumWrapper( m_xIndexAccess ); 291cdf0e10cSrcweir } 292cdf0e10cSrcweir 293cdf0e10cSrcweir uno::Any 294cdf0e10cSrcweir ScVbaVPageBreaks::createCollectionObject( const css::uno::Any& aSource ) 295cdf0e10cSrcweir { 296cdf0e10cSrcweir return aSource; // its already a pagebreak object 297cdf0e10cSrcweir } 298cdf0e10cSrcweir 299cdf0e10cSrcweir uno::Type 300cdf0e10cSrcweir ScVbaVPageBreaks::getElementType() throw ( uno::RuntimeException ) 301cdf0e10cSrcweir { 302cdf0e10cSrcweir return excel::XVPageBreak::static_type( 0 ); 303cdf0e10cSrcweir } 304cdf0e10cSrcweir 305cdf0e10cSrcweir rtl::OUString& 306cdf0e10cSrcweir ScVbaVPageBreaks::getServiceImplName() 307cdf0e10cSrcweir { 308cdf0e10cSrcweir static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM( "ScVbaVPageBreaks" ) ); 309cdf0e10cSrcweir return sImplName; 310cdf0e10cSrcweir } 311cdf0e10cSrcweir 312cdf0e10cSrcweir uno::Sequence< rtl::OUString > 313cdf0e10cSrcweir ScVbaVPageBreaks::getServiceNames() 314cdf0e10cSrcweir { 315cdf0e10cSrcweir static uno::Sequence< rtl::OUString > aServiceNames; 316cdf0e10cSrcweir if ( aServiceNames.getLength() == 0 ) 317cdf0e10cSrcweir { 318cdf0e10cSrcweir aServiceNames.realloc( 1 ); 319cdf0e10cSrcweir aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ooo.vba.excel.VPageBreaks" ) ); 320cdf0e10cSrcweir } 321cdf0e10cSrcweir return aServiceNames; 322cdf0e10cSrcweir } 323cdf0e10cSrcweir 324