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