fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / sc / source / ui / vba / vbapagebreaks.cxx
blob47a8febc46d58c8f13119bd7cdac00fbb1506521
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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;
25 typedef ::cppu::WeakImplHelper1<container::XIndexAccess > RangePageBreaks_Base;
26 class RangePageBreaks : public RangePageBreaks_Base
28 private:
29 uno::Reference< XHelperInterface > mxParent;
30 uno::Reference< uno::XComponentContext > mxContext;
31 uno::Reference< sheet::XSheetPageBreak > mxSheetPageBreak;
32 bool m_bColumn;
34 public:
35 RangePageBreaks( const uno::Reference< XHelperInterface >& xParent,
36 const uno::Reference< uno::XComponentContext >& xContext,
37 uno::Reference< sheet::XSheetPageBreak >& xSheetPageBreak,
38 bool bColumn ) : mxParent( xParent ), mxContext( xContext ), mxSheetPageBreak( xSheetPageBreak ), m_bColumn( bColumn )
42 sal_Int32 getAPIStartofRange( const uno::Reference< excel::XRange >& xRange ) throw (css::uno::RuntimeException)
44 if( m_bColumn )
45 return xRange->getColumn() - 1;
46 return xRange->getRow() - 1;
49 sal_Int32 getAPIEndIndexofRange( const uno::Reference< excel::XRange >& xRange, sal_Int32 nUsedStart ) throw (uno::RuntimeException)
51 if( m_bColumn )
52 return nUsedStart + xRange->Columns( uno::Any() )->getCount() - 1;
53 return nUsedStart + xRange->Rows( uno::Any() )->getCount();
56 uno::Sequence<sheet::TablePageBreakData> getAllPageBreaks() throw (uno::RuntimeException)
58 if( m_bColumn )
59 return mxSheetPageBreak->getColumnPageBreaks();
60 return mxSheetPageBreak->getRowPageBreaks();
63 uno::Reference<container::XIndexAccess> getRowColContainer() throw (uno::RuntimeException)
65 uno::Reference< table::XColumnRowRange > xColumnRowRange( mxSheetPageBreak, uno::UNO_QUERY_THROW );
66 uno::Reference<container::XIndexAccess> xIndexAccess;
67 if( m_bColumn )
68 xIndexAccess.set( xColumnRowRange->getColumns(), uno::UNO_QUERY_THROW );
69 else
70 xIndexAccess.set( xColumnRowRange->getRows(), uno::UNO_QUERY_THROW );
71 return xIndexAccess;
74 sheet::TablePageBreakData getTablePageBreakData( sal_Int32 nAPIItemIndex ) throw (uno::RuntimeException);
75 uno::Any Add( const css::uno::Any& Before ) throw ( css::script::BasicErrorException, css::uno::RuntimeException);
77 // XIndexAccess
78 virtual sal_Int32 SAL_CALL getCount( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE;
79 virtual uno::Any SAL_CALL getByIndex( sal_Int32 Index ) throw (lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException, std::exception) SAL_OVERRIDE;
80 virtual uno::Type SAL_CALL getElementType( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
82 if( m_bColumn )
83 return cppu::UnoType<excel::XVPageBreak>::get();
84 return cppu::UnoType<excel::XHPageBreak>::get();
86 virtual sal_Bool SAL_CALL hasElements( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
88 return sal_True;
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.
97 sal_Int32 SAL_CALL RangePageBreaks::getCount( ) throw (uno::RuntimeException, std::exception)
99 sal_Int32 nCount = 0;
100 uno::Reference< excel::XWorksheet > xWorksheet( mxParent, uno::UNO_QUERY_THROW );
101 uno::Reference< excel::XRange > xRange = xWorksheet->getUsedRange();
102 sal_Int32 nUsedStart = getAPIStartofRange( xRange );
103 sal_Int32 nUsedEnd = getAPIEndIndexofRange( xRange, nUsedStart );
104 uno::Sequence<sheet::TablePageBreakData> aTablePageBreakData = getAllPageBreaks();
106 sal_Int32 nLength = aTablePageBreakData.getLength();
107 for( sal_Int32 i=0; i<nLength; i++ )
109 sal_Int32 nPos = aTablePageBreakData[i].Position;
110 if( nPos > nUsedEnd + 1)
111 return nCount;
112 nCount++;
115 return nCount;
118 uno::Any SAL_CALL RangePageBreaks::getByIndex( sal_Int32 Index ) throw (lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException, std::exception)
120 if( (Index < getCount()) && ( Index >= 0 ))
122 sheet::TablePageBreakData aTablePageBreakData = getTablePageBreakData( Index );
123 uno::Reference< container::XIndexAccess > xIndexAccess = getRowColContainer();
124 sal_Int32 nPos = aTablePageBreakData.Position;
125 if( (nPos < xIndexAccess->getCount()) && (nPos > -1) )
127 uno::Reference< beans::XPropertySet > xRowColPropertySet( xIndexAccess->getByIndex(nPos), uno::UNO_QUERY_THROW );
128 if( m_bColumn )
129 return uno::makeAny( uno::Reference< excel::XVPageBreak >( new ScVbaVPageBreak( mxParent, mxContext, xRowColPropertySet, aTablePageBreakData) ));
130 return uno::makeAny( uno::Reference< excel::XHPageBreak >( new ScVbaHPageBreak( mxParent, mxContext, xRowColPropertySet, aTablePageBreakData) ));
133 throw lang::IndexOutOfBoundsException();
136 sheet::TablePageBreakData RangePageBreaks::getTablePageBreakData( sal_Int32 nAPIItemIndex ) throw (uno::RuntimeException)
138 sal_Int32 index = -1;
139 sheet::TablePageBreakData aTablePageBreakData;
140 uno::Reference< excel::XWorksheet > xWorksheet( mxParent, uno::UNO_QUERY_THROW );
141 uno::Reference< excel::XRange > xRange = xWorksheet->getUsedRange();
142 sal_Int32 nUsedStart = getAPIStartofRange( xRange );
143 sal_Int32 nUsedEnd = getAPIEndIndexofRange( xRange, nUsedStart );
144 uno::Sequence<sheet::TablePageBreakData> aTablePageBreakDataList = getAllPageBreaks();
146 sal_Int32 nLength = aTablePageBreakDataList.getLength();
147 for( sal_Int32 i=0; i<nLength; i++ )
149 aTablePageBreakData = aTablePageBreakDataList[i];
150 sal_Int32 nPos = aTablePageBreakData.Position;
151 if( nPos > nUsedEnd + 1 )
152 DebugHelper::runtimeexception(SbERR_METHOD_FAILED, OUString());
153 index++;
154 if( index == nAPIItemIndex )
155 return aTablePageBreakData;
158 return aTablePageBreakData;
161 uno::Any RangePageBreaks::Add( const css::uno::Any& Before ) throw ( css::script::BasicErrorException, css::uno::RuntimeException)
163 uno::Reference< excel::XRange > xRange;
164 Before >>= xRange;
165 if( !xRange.is() )
167 DebugHelper::basicexception(SbERR_BAD_ARGUMENT, OUString());
170 sal_Int32 nAPIRowColIndex = getAPIStartofRange( xRange );
171 uno::Reference< container::XIndexAccess > xIndexAccess = getRowColContainer();
172 uno::Reference< beans::XPropertySet > xRowColPropertySet( xIndexAccess->getByIndex(nAPIRowColIndex), uno::UNO_QUERY_THROW );
173 xRowColPropertySet->setPropertyValue("IsStartOfNewPage", uno::makeAny(sal_True));
174 sheet::TablePageBreakData aTablePageBreakData;
175 aTablePageBreakData.ManualBreak = sal_True;
176 aTablePageBreakData.Position = nAPIRowColIndex;
177 if( m_bColumn )
178 return uno::makeAny( uno::Reference< excel::XVPageBreak >( new ScVbaVPageBreak( mxParent, mxContext, xRowColPropertySet, aTablePageBreakData) ));
179 return uno::makeAny( uno::Reference< excel::XHPageBreak >( new ScVbaHPageBreak( mxParent, mxContext, xRowColPropertySet, aTablePageBreakData) ));
182 class RangePageBreaksEnumWrapper : public EnumerationHelper_BASE
184 uno::Reference<container::XIndexAccess > m_xIndexAccess;
185 sal_Int32 nIndex;
186 public:
187 RangePageBreaksEnumWrapper( const uno::Reference< container::XIndexAccess >& xIndexAccess ) : m_xIndexAccess( xIndexAccess ), nIndex( 0 ) {}
188 virtual sal_Bool SAL_CALL hasMoreElements( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
190 return ( nIndex < m_xIndexAccess->getCount() );
193 virtual uno::Any SAL_CALL nextElement( ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException, std::exception) SAL_OVERRIDE
195 if ( nIndex < m_xIndexAccess->getCount() )
196 return m_xIndexAccess->getByIndex( nIndex++ );
197 throw container::NoSuchElementException();
201 ScVbaHPageBreaks::ScVbaHPageBreaks( const uno::Reference< XHelperInterface >& xParent,
202 const uno::Reference< uno::XComponentContext >& xContext,
203 uno::Reference< sheet::XSheetPageBreak >& xSheetPageBreak) throw (uno::RuntimeException):
204 ScVbaHPageBreaks_BASE( xParent,xContext, new RangePageBreaks( xParent, xContext, xSheetPageBreak, false )),
205 mxSheetPageBreak( xSheetPageBreak )
209 uno::Any SAL_CALL ScVbaHPageBreaks::Add( const uno::Any& Before) throw ( script::BasicErrorException, uno::RuntimeException, std::exception)
211 RangePageBreaks* pPageBreaks = dynamic_cast< RangePageBreaks* >( m_xIndexAccess.get() );
212 if( pPageBreaks )
214 return pPageBreaks->Add( Before );
216 return uno::Any();
219 uno::Reference< container::XEnumeration >
220 ScVbaHPageBreaks::createEnumeration() throw (uno::RuntimeException)
222 return new RangePageBreaksEnumWrapper( m_xIndexAccess );
225 uno::Any
226 ScVbaHPageBreaks::createCollectionObject( const css::uno::Any& aSource )
228 return aSource; // its already a pagebreak object
231 uno::Type
232 ScVbaHPageBreaks::getElementType() throw (uno::RuntimeException)
234 return cppu::UnoType<excel::XHPageBreak>::get();
237 OUString
238 ScVbaHPageBreaks::getServiceImplName()
240 return OUString("ScVbaHPageBreaks");
243 uno::Sequence< OUString >
244 ScVbaHPageBreaks::getServiceNames()
246 static uno::Sequence< OUString > aServiceNames;
247 if ( aServiceNames.getLength() == 0 )
249 aServiceNames.realloc( 1 );
250 aServiceNames[ 0 ] = "ooo.vba.excel.HPageBreaks";
252 return aServiceNames;
255 //VPageBreak
256 ScVbaVPageBreaks::ScVbaVPageBreaks( const uno::Reference< XHelperInterface >& xParent,
257 const uno::Reference< uno::XComponentContext >& xContext,
258 uno::Reference< sheet::XSheetPageBreak >& xSheetPageBreak ) throw ( uno::RuntimeException )
259 : ScVbaVPageBreaks_BASE( xParent, xContext, new RangePageBreaks( xParent, xContext, xSheetPageBreak, true ) ),
260 mxSheetPageBreak( xSheetPageBreak )
264 ScVbaVPageBreaks::~ScVbaVPageBreaks()
268 uno::Any SAL_CALL
269 ScVbaVPageBreaks::Add( const uno::Any& Before ) throw ( script::BasicErrorException, uno::RuntimeException, std::exception )
271 RangePageBreaks* pPageBreaks = dynamic_cast< RangePageBreaks* >( m_xIndexAccess.get() );
272 if( pPageBreaks )
274 return pPageBreaks->Add( Before );
276 return uno::Any();
279 uno::Reference< container::XEnumeration >
280 ScVbaVPageBreaks::createEnumeration() throw ( uno::RuntimeException )
282 return new RangePageBreaksEnumWrapper( m_xIndexAccess );
285 uno::Any
286 ScVbaVPageBreaks::createCollectionObject( const css::uno::Any& aSource )
288 return aSource; // its already a pagebreak object
291 uno::Type
292 ScVbaVPageBreaks::getElementType() throw ( uno::RuntimeException )
294 return cppu::UnoType<excel::XVPageBreak>::get();
297 OUString
298 ScVbaVPageBreaks::getServiceImplName()
300 return OUString("ScVbaVPageBreaks");
303 uno::Sequence< OUString >
304 ScVbaVPageBreaks::getServiceNames()
306 static uno::Sequence< OUString > aServiceNames;
307 if ( aServiceNames.getLength() == 0 )
309 aServiceNames.realloc( 1 );
310 aServiceNames[ 0 ] = "ooo.vba.excel.VPageBreaks";
312 return aServiceNames;
315 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */