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>
29 using namespace ::com::sun::star
;
30 using namespace ::ooo::vba
;
34 class RangePageBreaks
: public ::cppu::WeakImplHelper
<container::XIndexAccess
>
37 uno::Reference
< XHelperInterface
> mxParent
;
38 uno::Reference
< uno::XComponentContext
> mxContext
;
39 uno::Reference
< sheet::XSheetPageBreak
> mxSheetPageBreak
;
43 RangePageBreaks( const uno::Reference
< XHelperInterface
>& xParent
,
44 const uno::Reference
< uno::XComponentContext
>& xContext
,
45 const uno::Reference
< sheet::XSheetPageBreak
>& xSheetPageBreak
,
46 bool bColumn
) : mxParent( xParent
), mxContext( xContext
), mxSheetPageBreak( xSheetPageBreak
), m_bColumn( bColumn
)
50 /// @throws css::uno::RuntimeException
51 sal_Int32
getAPIStartofRange( const uno::Reference
< excel::XRange
>& xRange
)
54 return xRange
->getColumn() - 1;
55 return xRange
->getRow() - 1;
58 /// @throws uno::RuntimeException
59 sal_Int32
getAPIEndIndexofRange( const uno::Reference
< excel::XRange
>& xRange
, sal_Int32 nUsedStart
)
62 return nUsedStart
+ xRange
->Columns( uno::Any() )->getCount() - 1;
63 return nUsedStart
+ xRange
->Rows( uno::Any() )->getCount();
66 /// @throws uno::RuntimeException
67 uno::Sequence
<sheet::TablePageBreakData
> getAllPageBreaks()
70 return mxSheetPageBreak
->getColumnPageBreaks();
71 return mxSheetPageBreak
->getRowPageBreaks();
74 /// @throws uno::RuntimeException
75 uno::Reference
<container::XIndexAccess
> getRowColContainer() const
77 uno::Reference
< table::XColumnRowRange
> xColumnRowRange( mxSheetPageBreak
, uno::UNO_QUERY_THROW
);
78 uno::Reference
<container::XIndexAccess
> xIndexAccess
;
80 xIndexAccess
.set( xColumnRowRange
->getColumns(), uno::UNO_QUERY_THROW
);
82 xIndexAccess
.set( xColumnRowRange
->getRows(), uno::UNO_QUERY_THROW
);
86 /// @throws uno::RuntimeException
87 sheet::TablePageBreakData
getTablePageBreakData( sal_Int32 nAPIItemIndex
);
88 /// @throws css::script::BasicErrorException
89 /// @throws css::uno::RuntimeException
90 uno::Any
Add( const css::uno::Any
& Before
);
93 virtual sal_Int32 SAL_CALL
getCount( ) override
;
94 virtual uno::Any SAL_CALL
getByIndex( sal_Int32 Index
) override
;
95 virtual uno::Type SAL_CALL
getElementType( ) override
98 return cppu::UnoType
<excel::XVPageBreak
>::get();
99 return cppu::UnoType
<excel::XHPageBreak
>::get();
101 virtual sal_Bool SAL_CALL
hasElements( ) override
109 /** @TODO Unlike MS Excel this method only considers the pagebreaks that intersect the used range
110 * To become completely compatible the print area has to be considered. As far as I found out this printarea
111 * also considers the position and sizes of shapes and manually inserted page breaks
112 * Note: In MS there is a limit of 1026 horizontal page breaks per sheet.
114 sal_Int32 SAL_CALL
RangePageBreaks::getCount( )
116 uno::Reference
< excel::XWorksheet
> xWorksheet( mxParent
, uno::UNO_QUERY_THROW
);
117 uno::Reference
< excel::XRange
> xRange
= xWorksheet
->getUsedRange();
118 sal_Int32 nUsedStart
= getAPIStartofRange( xRange
);
119 sal_Int32 nUsedEnd
= getAPIEndIndexofRange( xRange
, nUsedStart
);
120 const uno::Sequence
<sheet::TablePageBreakData
> aTablePageBreakData
= getAllPageBreaks();
122 auto pPageBreak
= std::find_if(aTablePageBreakData
.begin(), aTablePageBreakData
.end(),
123 [nUsedEnd
](const sheet::TablePageBreakData
& rPageBreak
) { return rPageBreak
.Position
> nUsedEnd
+ 1; });
125 return static_cast<sal_Int32
>(std::distance(aTablePageBreakData
.begin(), pPageBreak
));
128 uno::Any SAL_CALL
RangePageBreaks::getByIndex( sal_Int32 Index
)
130 if( (Index
< getCount()) && ( Index
>= 0 ))
132 sheet::TablePageBreakData aTablePageBreakData
= getTablePageBreakData( Index
);
133 uno::Reference
< container::XIndexAccess
> xIndexAccess
= getRowColContainer();
134 sal_Int32 nPos
= aTablePageBreakData
.Position
;
135 if( (nPos
< xIndexAccess
->getCount()) && (nPos
> -1) )
137 uno::Reference
< beans::XPropertySet
> xRowColPropertySet( xIndexAccess
->getByIndex(nPos
), uno::UNO_QUERY_THROW
);
139 return uno::makeAny( uno::Reference
< excel::XVPageBreak
>( new ScVbaVPageBreak( mxParent
, mxContext
, xRowColPropertySet
, aTablePageBreakData
) ));
140 return uno::makeAny( uno::Reference
< excel::XHPageBreak
>( new ScVbaHPageBreak( mxParent
, mxContext
, xRowColPropertySet
, aTablePageBreakData
) ));
143 throw lang::IndexOutOfBoundsException();
146 sheet::TablePageBreakData
RangePageBreaks::getTablePageBreakData( sal_Int32 nAPIItemIndex
)
148 sal_Int32 index
= -1;
149 sheet::TablePageBreakData aTablePageBreakData
;
150 uno::Reference
< excel::XWorksheet
> xWorksheet( mxParent
, uno::UNO_QUERY_THROW
);
151 uno::Reference
< excel::XRange
> xRange
= xWorksheet
->getUsedRange();
152 sal_Int32 nUsedStart
= getAPIStartofRange( xRange
);
153 sal_Int32 nUsedEnd
= getAPIEndIndexofRange( xRange
, nUsedStart
);
154 const uno::Sequence
<sheet::TablePageBreakData
> aTablePageBreakDataList
= getAllPageBreaks();
156 for( const auto& rTablePageBreakData
: aTablePageBreakDataList
)
158 aTablePageBreakData
= rTablePageBreakData
;
159 sal_Int32 nPos
= aTablePageBreakData
.Position
;
160 if( nPos
> nUsedEnd
+ 1 )
161 DebugHelper::runtimeexception(ERRCODE_BASIC_METHOD_FAILED
);
163 if( index
== nAPIItemIndex
)
164 return aTablePageBreakData
;
167 return aTablePageBreakData
;
170 uno::Any
RangePageBreaks::Add( const css::uno::Any
& Before
)
172 uno::Reference
< excel::XRange
> xRange
;
176 DebugHelper::basicexception(ERRCODE_BASIC_BAD_ARGUMENT
, {});
179 sal_Int32 nAPIRowColIndex
= getAPIStartofRange( xRange
);
180 uno::Reference
< container::XIndexAccess
> xIndexAccess
= getRowColContainer();
181 uno::Reference
< beans::XPropertySet
> xRowColPropertySet( xIndexAccess
->getByIndex(nAPIRowColIndex
), uno::UNO_QUERY_THROW
);
182 xRowColPropertySet
->setPropertyValue("IsStartOfNewPage", uno::makeAny(true));
183 sheet::TablePageBreakData aTablePageBreakData
;
184 aTablePageBreakData
.ManualBreak
= true;
185 aTablePageBreakData
.Position
= nAPIRowColIndex
;
187 return uno::makeAny( uno::Reference
< excel::XVPageBreak
>( new ScVbaVPageBreak( mxParent
, mxContext
, xRowColPropertySet
, aTablePageBreakData
) ));
188 return uno::makeAny( uno::Reference
< excel::XHPageBreak
>( new ScVbaHPageBreak( mxParent
, mxContext
, xRowColPropertySet
, aTablePageBreakData
) ));
193 class RangePageBreaksEnumWrapper
: public EnumerationHelper_BASE
195 uno::Reference
<container::XIndexAccess
> m_xIndexAccess
;
198 explicit RangePageBreaksEnumWrapper( const uno::Reference
< container::XIndexAccess
>& xIndexAccess
) : m_xIndexAccess( xIndexAccess
), nIndex( 0 ) {}
199 virtual sal_Bool SAL_CALL
hasMoreElements( ) override
201 return ( nIndex
< m_xIndexAccess
->getCount() );
204 virtual uno::Any SAL_CALL
nextElement( ) override
206 if ( nIndex
< m_xIndexAccess
->getCount() )
207 return m_xIndexAccess
->getByIndex( nIndex
++ );
208 throw container::NoSuchElementException();
214 ScVbaHPageBreaks::ScVbaHPageBreaks( const uno::Reference
< XHelperInterface
>& xParent
,
215 const uno::Reference
< uno::XComponentContext
>& xContext
,
216 const uno::Reference
< sheet::XSheetPageBreak
>& xSheetPageBreak
):
217 ScVbaHPageBreaks_BASE( xParent
,xContext
, new RangePageBreaks( xParent
, xContext
, xSheetPageBreak
, false ))
221 uno::Any SAL_CALL
ScVbaHPageBreaks::Add( const uno::Any
& Before
)
223 RangePageBreaks
* pPageBreaks
= dynamic_cast< RangePageBreaks
* >( m_xIndexAccess
.get() );
226 return pPageBreaks
->Add( Before
);
231 uno::Reference
< container::XEnumeration
>
232 ScVbaHPageBreaks::createEnumeration()
234 return new RangePageBreaksEnumWrapper( m_xIndexAccess
);
238 ScVbaHPageBreaks::createCollectionObject( const css::uno::Any
& aSource
)
240 return aSource
; // it's already a pagebreak object
244 ScVbaHPageBreaks::getElementType()
246 return cppu::UnoType
<excel::XHPageBreak
>::get();
250 ScVbaHPageBreaks::getServiceImplName()
252 return "ScVbaHPageBreaks";
255 uno::Sequence
< OUString
>
256 ScVbaHPageBreaks::getServiceNames()
258 static uno::Sequence
< OUString
> const aServiceNames
260 "ooo.vba.excel.HPageBreaks"
262 return aServiceNames
;
266 ScVbaVPageBreaks::ScVbaVPageBreaks( const uno::Reference
< XHelperInterface
>& xParent
,
267 const uno::Reference
< uno::XComponentContext
>& xContext
,
268 const uno::Reference
< sheet::XSheetPageBreak
>& xSheetPageBreak
)
269 : ScVbaVPageBreaks_BASE( xParent
, xContext
, new RangePageBreaks( xParent
, xContext
, xSheetPageBreak
, true ) )
273 ScVbaVPageBreaks::~ScVbaVPageBreaks()
278 ScVbaVPageBreaks::Add( const uno::Any
& Before
)
280 RangePageBreaks
* pPageBreaks
= dynamic_cast< RangePageBreaks
* >( m_xIndexAccess
.get() );
283 return pPageBreaks
->Add( Before
);
288 uno::Reference
< container::XEnumeration
>
289 ScVbaVPageBreaks::createEnumeration()
291 return new RangePageBreaksEnumWrapper( m_xIndexAccess
);
295 ScVbaVPageBreaks::createCollectionObject( const css::uno::Any
& aSource
)
297 return aSource
; // it's already a pagebreak object
301 ScVbaVPageBreaks::getElementType()
303 return cppu::UnoType
<excel::XVPageBreak
>::get();
307 ScVbaVPageBreaks::getServiceImplName()
309 return "ScVbaVPageBreaks";
312 uno::Sequence
< OUString
>
313 ScVbaVPageBreaks::getServiceNames()
315 static uno::Sequence
< OUString
> const aServiceNames
317 "ooo.vba.excel.VPageBreaks"
319 return aServiceNames
;
322 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */