LibreOffice Module sc (master) 1
vbapagebreaks.cxx
Go to the documentation of this file.
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 <basic/sberrors.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>
28#include <utility>
29
30using namespace ::com::sun::star;
31using namespace ::ooo::vba;
32
33namespace {
34
35class RangePageBreaks : public ::cppu::WeakImplHelper<container::XIndexAccess >
36{
37private:
38 uno::Reference< XHelperInterface > mxParent;
39 uno::Reference< uno::XComponentContext > mxContext;
40 uno::Reference< sheet::XSheetPageBreak > mxSheetPageBreak;
41 bool m_bColumn;
42
43public:
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 )
48 {
49 }
50
52 sal_Int32 getAPIStartofRange( const uno::Reference< excel::XRange >& xRange )
53 {
54 if( m_bColumn )
55 return xRange->getColumn() - 1;
56 return xRange->getRow() - 1;
57 }
58
60 sal_Int32 getAPIEndIndexofRange( const uno::Reference< excel::XRange >& xRange, sal_Int32 nUsedStart )
61 {
62 if( m_bColumn )
63 return nUsedStart + xRange->Columns( uno::Any() )->getCount() - 1;
64 return nUsedStart + xRange->Rows( uno::Any() )->getCount();
65 }
66
68 uno::Sequence<sheet::TablePageBreakData> getAllPageBreaks()
69 {
70 if( m_bColumn )
71 return mxSheetPageBreak->getColumnPageBreaks();
72 return mxSheetPageBreak->getRowPageBreaks();
73 }
74
76 uno::Reference<container::XIndexAccess> getRowColContainer() const
77 {
78 uno::Reference< table::XColumnRowRange > xColumnRowRange( mxSheetPageBreak, uno::UNO_QUERY_THROW );
79 uno::Reference<container::XIndexAccess> xIndexAccess;
80 if( m_bColumn )
81 xIndexAccess.set( xColumnRowRange->getColumns(), uno::UNO_QUERY_THROW );
82 else
83 xIndexAccess.set( xColumnRowRange->getRows(), uno::UNO_QUERY_THROW );
84 return xIndexAccess;
85 }
86
88 sheet::TablePageBreakData getTablePageBreakData( sal_Int32 nAPIItemIndex );
91 uno::Any Add( const css::uno::Any& Before );
92
93 // XIndexAccess
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
97 {
98 if( m_bColumn )
101 }
102 virtual sal_Bool SAL_CALL hasElements( ) override
103 {
104 return true;
105 }
106};
107
108}
109
115sal_Int32 SAL_CALL RangePageBreaks::getCount( )
116{
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();
122
123 auto pPageBreak = std::find_if(aTablePageBreakData.begin(), aTablePageBreakData.end(),
124 [nUsedEnd](const sheet::TablePageBreakData& rPageBreak) { return rPageBreak.Position > nUsedEnd + 1; });
125
126 return static_cast<sal_Int32>(std::distance(aTablePageBreakData.begin(), pPageBreak));
127}
128
129uno::Any SAL_CALL RangePageBreaks::getByIndex( sal_Int32 Index )
130{
131 if( (Index < getCount()) && ( Index >= 0 ))
132 {
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) )
137 {
138 uno::Reference< beans::XPropertySet > xRowColPropertySet( xIndexAccess->getByIndex(nPos), uno::UNO_QUERY_THROW );
139 if( m_bColumn )
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) ));
142 }
143 }
144 throw lang::IndexOutOfBoundsException();
145}
146
147sheet::TablePageBreakData RangePageBreaks::getTablePageBreakData( sal_Int32 nAPIItemIndex )
148{
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();
156
157 for( const auto& rTablePageBreakData : aTablePageBreakDataList )
158 {
159 aTablePageBreakData = rTablePageBreakData;
160 sal_Int32 nPos = aTablePageBreakData.Position;
161 if( nPos > nUsedEnd + 1 )
162 DebugHelper::runtimeexception(ERRCODE_BASIC_METHOD_FAILED);
163 index++;
164 if( index == nAPIItemIndex )
165 return aTablePageBreakData;
166 }
167
168 return aTablePageBreakData;
169}
170
171uno::Any RangePageBreaks::Add( const css::uno::Any& Before )
172{
173 uno::Reference< excel::XRange > xRange;
174 Before >>= xRange;
175 if( !xRange.is() )
176 {
177 DebugHelper::basicexception(ERRCODE_BASIC_BAD_ARGUMENT, {});
178 }
179
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("IsStartOfNewPage", uno::Any(true));
184 sheet::TablePageBreakData aTablePageBreakData;
185 aTablePageBreakData.ManualBreak = true;
186 aTablePageBreakData.Position = nAPIRowColIndex;
187 if( m_bColumn )
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) ));
190}
191
192namespace {
193
194class RangePageBreaksEnumWrapper : public EnumerationHelper_BASE
195{
196 uno::Reference<container::XIndexAccess > m_xIndexAccess;
197 sal_Int32 nIndex;
198public:
199 explicit RangePageBreaksEnumWrapper( uno::Reference< container::XIndexAccess > xIndexAccess ) : m_xIndexAccess(std::move( xIndexAccess )), nIndex( 0 ) {}
200 virtual sal_Bool SAL_CALL hasMoreElements( ) override
201 {
202 return ( nIndex < m_xIndexAccess->getCount() );
203 }
204
205 virtual uno::Any SAL_CALL nextElement( ) override
206 {
207 if ( nIndex < m_xIndexAccess->getCount() )
208 return m_xIndexAccess->getByIndex( nIndex++ );
209 throw container::NoSuchElementException();
210 }
211};
212
213}
214
215ScVbaHPageBreaks::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 ))
219{
220}
221
223{
224 RangePageBreaks* pPageBreaks = dynamic_cast< RangePageBreaks* >( m_xIndexAccess.get() );
225 if( pPageBreaks )
226 {
227 return pPageBreaks->Add( Before );
228 }
229 return uno::Any();
230}
231
232uno::Reference< container::XEnumeration >
234{
235 return new RangePageBreaksEnumWrapper( m_xIndexAccess );
236}
237
239ScVbaHPageBreaks::createCollectionObject( const css::uno::Any& aSource )
240{
241 return aSource; // it's already a pagebreak object
242}
243
246{
248}
249
250OUString
252{
253 return "ScVbaHPageBreaks";
254}
255
256uno::Sequence< OUString >
258{
259 static uno::Sequence< OUString > const aServiceNames
260 {
261 "ooo.vba.excel.HPageBreaks"
262 };
263 return aServiceNames;
264}
265
266//VPageBreak
267ScVbaVPageBreaks::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 ) )
271{
272}
273
275{
276}
277
278uno::Any SAL_CALL
280{
281 RangePageBreaks* pPageBreaks = dynamic_cast< RangePageBreaks* >( m_xIndexAccess.get() );
282 if( pPageBreaks )
283 {
284 return pPageBreaks->Add( Before );
285 }
286 return uno::Any();
287}
288
289uno::Reference< container::XEnumeration >
291{
292 return new RangePageBreaksEnumWrapper( m_xIndexAccess );
293}
294
296ScVbaVPageBreaks::createCollectionObject( const css::uno::Any& aSource )
297{
298 return aSource; // it's already a pagebreak object
299}
300
303{
305}
306
307OUString
309{
310 return "ScVbaVPageBreaks";
311}
312
313uno::Sequence< OUString >
315{
316 static uno::Sequence< OUString > const aServiceNames
317 {
318 "ooo.vba.excel.VPageBreaks"
319 };
320 return aServiceNames;
321}
322
323/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
struct _ADOIndex Index
unotools::WeakReference< AnimationNode > mxParent
css::uno::Reference< css::container::XIndexAccess > m_xIndexAccess
virtual css::uno::Any createCollectionObject(const css::uno::Any &) override
virtual css::uno::Sequence< OUString > getServiceNames() override
virtual css::uno::Any SAL_CALL Add(const css::uno::Any &Before) override
virtual OUString getServiceImplName() override
virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createEnumeration() override
virtual css::uno::Type SAL_CALL getElementType() override
ScVbaHPageBreaks(const css::uno::Reference< ov::XHelperInterface > &xParent, const css::uno::Reference< css::uno::XComponentContext > &xContext, const css::uno::Reference< css::sheet::XSheetPageBreak > &xSheetPageBreak)
virtual css::uno::Any SAL_CALL Add(const css::uno::Any &Before) override
virtual ~ScVbaVPageBreaks() override
virtual css::uno::Type SAL_CALL getElementType() override
virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createEnumeration() override
virtual css::uno::Any createCollectionObject(const css::uno::Any &) override
ScVbaVPageBreaks(const css::uno::Reference< ov::XHelperInterface > &xParent, const css::uno::Reference< css::uno::XComponentContext > &xContext, const css::uno::Reference< css::sheet::XSheetPageBreak > &xSheetPageBreak)
virtual OUString getServiceImplName() override
virtual css::uno::Sequence< OUString > getServiceNames() override
css::uno::Type const & get()
uno::Reference< uno::XComponentContext > mxContext
Sequence< OUString > aServiceNames
sal_Int32 nIndex
sal_uInt16 nPos
index
unsigned char sal_Bool
::cppu::WeakImplHelper< css::container::XEnumeration > EnumerationHelper_BASE