1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <basic/sberrors.hxx>
22 #include <cppuhelper/implbase.hxx>
23 #include <ooo/vba/excel/XWorksheet.hpp>
24 #include <com/sun/star/beans/XPropertySet.hpp>
25 #include <com/sun/star/sheet/XSheetPageBreak.hpp>
26 #include <com/sun/star/table/XColumnRowRange.hpp>
27 #include <com/sun/star/uno/XComponentContext.hpp>
30 using namespace ::com::sun::star
;
31 using namespace ::ooo::vba
;
35 class RangePageBreaks
: public ::cppu::WeakImplHelper
<container::XIndexAccess
>
38 uno::Reference
< XHelperInterface
> mxParent
;
39 uno::Reference
< uno::XComponentContext
> mxContext
;
40 uno::Reference
< sheet::XSheetPageBreak
> mxSheetPageBreak
;
44 RangePageBreaks( uno::Reference
< XHelperInterface
> xParent
,
45 uno::Reference
< uno::XComponentContext
> xContext
,
46 uno::Reference
< sheet::XSheetPageBreak
> xSheetPageBreak
,
47 bool bColumn
) : mxParent(std::move( xParent
)), mxContext(std::move( xContext
)), mxSheetPageBreak(std::move( xSheetPageBreak
)), m_bColumn( bColumn
)
51 /// @throws css::uno::RuntimeException
52 sal_Int32
getAPIStartofRange( const uno::Reference
< excel::XRange
>& xRange
)
55 return xRange
->getColumn() - 1;
56 return xRange
->getRow() - 1;
59 /// @throws uno::RuntimeException
60 sal_Int32
getAPIEndIndexofRange( const uno::Reference
< excel::XRange
>& xRange
, sal_Int32 nUsedStart
)
63 return nUsedStart
+ xRange
->Columns( uno::Any() )->getCount() - 1;
64 return nUsedStart
+ xRange
->Rows( uno::Any() )->getCount();
67 /// @throws uno::RuntimeException
68 uno::Sequence
<sheet::TablePageBreakData
> getAllPageBreaks()
71 return mxSheetPageBreak
->getColumnPageBreaks();
72 return mxSheetPageBreak
->getRowPageBreaks();
75 /// @throws uno::RuntimeException
76 uno::Reference
<container::XIndexAccess
> getRowColContainer() const
78 uno::Reference
< table::XColumnRowRange
> xColumnRowRange( mxSheetPageBreak
, uno::UNO_QUERY_THROW
);
79 uno::Reference
<container::XIndexAccess
> xIndexAccess
;
81 xIndexAccess
.set( xColumnRowRange
->getColumns(), uno::UNO_QUERY_THROW
);
83 xIndexAccess
.set( xColumnRowRange
->getRows(), uno::UNO_QUERY_THROW
);
87 /// @throws uno::RuntimeException
88 sheet::TablePageBreakData
getTablePageBreakData( sal_Int32 nAPIItemIndex
);
89 /// @throws css::script::BasicErrorException
90 /// @throws css::uno::RuntimeException
91 uno::Any
Add( const css::uno::Any
& Before
);
94 virtual sal_Int32 SAL_CALL
getCount( ) override
;
95 virtual uno::Any SAL_CALL
getByIndex( sal_Int32 Index
) override
;
96 virtual uno::Type SAL_CALL
getElementType( ) override
99 return cppu::UnoType
<excel::XVPageBreak
>::get();
100 return cppu::UnoType
<excel::XHPageBreak
>::get();
102 virtual sal_Bool SAL_CALL
hasElements( ) override
110 /** @TODO Unlike MS Excel this method only considers the pagebreaks that intersect the used range
111 * To become completely compatible the print area has to be considered. As far as I found out this printarea
112 * also considers the position and sizes of shapes and manually inserted page breaks
113 * Note: In MS there is a limit of 1026 horizontal page breaks per sheet.
115 sal_Int32 SAL_CALL
RangePageBreaks::getCount( )
117 uno::Reference
< excel::XWorksheet
> xWorksheet( mxParent
, uno::UNO_QUERY_THROW
);
118 uno::Reference
< excel::XRange
> xRange
= xWorksheet
->getUsedRange();
119 sal_Int32 nUsedStart
= getAPIStartofRange( xRange
);
120 sal_Int32 nUsedEnd
= getAPIEndIndexofRange( xRange
, nUsedStart
);
121 const uno::Sequence
<sheet::TablePageBreakData
> aTablePageBreakData
= getAllPageBreaks();
123 auto pPageBreak
= std::find_if(aTablePageBreakData
.begin(), aTablePageBreakData
.end(),
124 [nUsedEnd
](const sheet::TablePageBreakData
& rPageBreak
) { return rPageBreak
.Position
> nUsedEnd
+ 1; });
126 return static_cast<sal_Int32
>(std::distance(aTablePageBreakData
.begin(), pPageBreak
));
129 uno::Any SAL_CALL
RangePageBreaks::getByIndex( sal_Int32 Index
)
131 if( (Index
< getCount()) && ( Index
>= 0 ))
133 sheet::TablePageBreakData aTablePageBreakData
= getTablePageBreakData( Index
);
134 uno::Reference
< container::XIndexAccess
> xIndexAccess
= getRowColContainer();
135 sal_Int32 nPos
= aTablePageBreakData
.Position
;
136 if( (nPos
< xIndexAccess
->getCount()) && (nPos
> -1) )
138 uno::Reference
< beans::XPropertySet
> xRowColPropertySet( xIndexAccess
->getByIndex(nPos
), uno::UNO_QUERY_THROW
);
140 return uno::Any( uno::Reference
< excel::XVPageBreak
>( new ScVbaVPageBreak( mxParent
, mxContext
, xRowColPropertySet
, aTablePageBreakData
) ));
141 return uno::Any( uno::Reference
< excel::XHPageBreak
>( new ScVbaHPageBreak( mxParent
, mxContext
, xRowColPropertySet
, aTablePageBreakData
) ));
144 throw lang::IndexOutOfBoundsException();
147 sheet::TablePageBreakData
RangePageBreaks::getTablePageBreakData( sal_Int32 nAPIItemIndex
)
149 sal_Int32 index
= -1;
150 sheet::TablePageBreakData aTablePageBreakData
;
151 uno::Reference
< excel::XWorksheet
> xWorksheet( mxParent
, uno::UNO_QUERY_THROW
);
152 uno::Reference
< excel::XRange
> xRange
= xWorksheet
->getUsedRange();
153 sal_Int32 nUsedStart
= getAPIStartofRange( xRange
);
154 sal_Int32 nUsedEnd
= getAPIEndIndexofRange( xRange
, nUsedStart
);
155 const uno::Sequence
<sheet::TablePageBreakData
> aTablePageBreakDataList
= getAllPageBreaks();
157 for( const auto& rTablePageBreakData
: aTablePageBreakDataList
)
159 aTablePageBreakData
= rTablePageBreakData
;
160 sal_Int32 nPos
= aTablePageBreakData
.Position
;
161 if( nPos
> nUsedEnd
+ 1 )
162 DebugHelper::runtimeexception(ERRCODE_BASIC_METHOD_FAILED
);
164 if( index
== nAPIItemIndex
)
165 return aTablePageBreakData
;
168 return aTablePageBreakData
;
171 uno::Any
RangePageBreaks::Add( const css::uno::Any
& Before
)
173 uno::Reference
< excel::XRange
> xRange
;
177 DebugHelper::basicexception(ERRCODE_BASIC_BAD_ARGUMENT
, {});
180 sal_Int32 nAPIRowColIndex
= getAPIStartofRange( xRange
);
181 uno::Reference
< container::XIndexAccess
> xIndexAccess
= getRowColContainer();
182 uno::Reference
< beans::XPropertySet
> xRowColPropertySet( xIndexAccess
->getByIndex(nAPIRowColIndex
), uno::UNO_QUERY_THROW
);
183 xRowColPropertySet
->setPropertyValue(u
"IsStartOfNewPage"_ustr
, uno::Any(true));
184 sheet::TablePageBreakData aTablePageBreakData
;
185 aTablePageBreakData
.ManualBreak
= true;
186 aTablePageBreakData
.Position
= nAPIRowColIndex
;
188 return uno::Any( uno::Reference
< excel::XVPageBreak
>( new ScVbaVPageBreak( mxParent
, mxContext
, xRowColPropertySet
, aTablePageBreakData
) ));
189 return uno::Any( uno::Reference
< excel::XHPageBreak
>( new ScVbaHPageBreak( mxParent
, mxContext
, xRowColPropertySet
, aTablePageBreakData
) ));
194 class RangePageBreaksEnumWrapper
: public EnumerationHelper_BASE
196 uno::Reference
<container::XIndexAccess
> m_xIndexAccess
;
199 explicit RangePageBreaksEnumWrapper( uno::Reference
< container::XIndexAccess
> xIndexAccess
) : m_xIndexAccess(std::move( xIndexAccess
)), nIndex( 0 ) {}
200 virtual sal_Bool SAL_CALL
hasMoreElements( ) override
202 return ( nIndex
< m_xIndexAccess
->getCount() );
205 virtual uno::Any SAL_CALL
nextElement( ) override
207 if ( nIndex
< m_xIndexAccess
->getCount() )
208 return m_xIndexAccess
->getByIndex( nIndex
++ );
209 throw container::NoSuchElementException();
215 ScVbaHPageBreaks::ScVbaHPageBreaks( const uno::Reference
< XHelperInterface
>& xParent
,
216 const uno::Reference
< uno::XComponentContext
>& xContext
,
217 const uno::Reference
< sheet::XSheetPageBreak
>& xSheetPageBreak
):
218 ScVbaHPageBreaks_BASE( xParent
,xContext
, new RangePageBreaks( xParent
, xContext
, xSheetPageBreak
, false ))
222 uno::Any SAL_CALL
ScVbaHPageBreaks::Add( const uno::Any
& Before
)
224 RangePageBreaks
* pPageBreaks
= dynamic_cast< RangePageBreaks
* >( m_xIndexAccess
.get() );
227 return pPageBreaks
->Add( Before
);
232 uno::Reference
< container::XEnumeration
>
233 ScVbaHPageBreaks::createEnumeration()
235 return new RangePageBreaksEnumWrapper( m_xIndexAccess
);
239 ScVbaHPageBreaks::createCollectionObject( const css::uno::Any
& aSource
)
241 return aSource
; // it's already a pagebreak object
245 ScVbaHPageBreaks::getElementType()
247 return cppu::UnoType
<excel::XHPageBreak
>::get();
251 ScVbaHPageBreaks::getServiceImplName()
253 return u
"ScVbaHPageBreaks"_ustr
;
256 uno::Sequence
< OUString
>
257 ScVbaHPageBreaks::getServiceNames()
259 static uno::Sequence
< OUString
> const aServiceNames
261 u
"ooo.vba.excel.HPageBreaks"_ustr
263 return aServiceNames
;
267 ScVbaVPageBreaks::ScVbaVPageBreaks( const uno::Reference
< XHelperInterface
>& xParent
,
268 const uno::Reference
< uno::XComponentContext
>& xContext
,
269 const uno::Reference
< sheet::XSheetPageBreak
>& xSheetPageBreak
)
270 : ScVbaVPageBreaks_BASE( xParent
, xContext
, new RangePageBreaks( xParent
, xContext
, xSheetPageBreak
, true ) )
274 ScVbaVPageBreaks::~ScVbaVPageBreaks()
279 ScVbaVPageBreaks::Add( const uno::Any
& Before
)
281 RangePageBreaks
* pPageBreaks
= dynamic_cast< RangePageBreaks
* >( m_xIndexAccess
.get() );
284 return pPageBreaks
->Add( Before
);
289 uno::Reference
< container::XEnumeration
>
290 ScVbaVPageBreaks::createEnumeration()
292 return new RangePageBreaksEnumWrapper( m_xIndexAccess
);
296 ScVbaVPageBreaks::createCollectionObject( const css::uno::Any
& aSource
)
298 return aSource
; // it's already a pagebreak object
302 ScVbaVPageBreaks::getElementType()
304 return cppu::UnoType
<excel::XVPageBreak
>::get();
308 ScVbaVPageBreaks::getServiceImplName()
310 return u
"ScVbaVPageBreaks"_ustr
;
313 uno::Sequence
< OUString
>
314 ScVbaVPageBreaks::getServiceNames()
316 static uno::Sequence
< OUString
> const aServiceNames
318 u
"ooo.vba.excel.VPageBreaks"_ustr
320 return aServiceNames
;
323 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */