Line data Source code
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 <ooo/vba/excel/XWorksheet.hpp>
22 : using namespace ::com::sun::star;
23 : using namespace ::ooo::vba;
24 :
25 : typedef ::cppu::WeakImplHelper1<container::XIndexAccess > RangePageBreaks_Base;
26 12 : class RangePageBreaks : public RangePageBreaks_Base
27 : {
28 : private:
29 : uno::Reference< XHelperInterface > mxParent;
30 : uno::Reference< uno::XComponentContext > mxContext;
31 : uno::Reference< sheet::XSheetPageBreak > mxSheetPageBreak;
32 : bool m_bColumn;
33 :
34 : public:
35 6 : RangePageBreaks( const uno::Reference< XHelperInterface >& xParent,
36 : const uno::Reference< uno::XComponentContext >& xContext,
37 : uno::Reference< sheet::XSheetPageBreak >& xSheetPageBreak,
38 6 : bool bColumn ) : mxParent( xParent ), mxContext( xContext ), mxSheetPageBreak( xSheetPageBreak ), m_bColumn( bColumn )
39 : {
40 6 : }
41 :
42 28 : sal_Int32 getAPIStartofRange( const uno::Reference< excel::XRange >& xRange ) throw (css::uno::RuntimeException)
43 : {
44 28 : if( m_bColumn )
45 14 : return xRange->getColumn() - 1;
46 14 : return xRange->getRow() - 1;
47 : }
48 :
49 24 : sal_Int32 getAPIEndIndexofRange( const uno::Reference< excel::XRange >& xRange, sal_Int32 nUsedStart ) throw (uno::RuntimeException)
50 : {
51 24 : if( m_bColumn )
52 12 : return nUsedStart + xRange->Columns( uno::Any() )->getCount() - 1;
53 12 : return nUsedStart + xRange->Rows( uno::Any() )->getCount();
54 : }
55 :
56 24 : uno::Sequence<sheet::TablePageBreakData> getAllPageBreaks() throw (uno::RuntimeException)
57 : {
58 24 : if( m_bColumn )
59 12 : return mxSheetPageBreak->getColumnPageBreaks();
60 12 : return mxSheetPageBreak->getRowPageBreaks();
61 : }
62 :
63 8 : uno::Reference<container::XIndexAccess> getRowColContainer() throw (uno::RuntimeException)
64 : {
65 8 : uno::Reference< table::XColumnRowRange > xColumnRowRange( mxSheetPageBreak, uno::UNO_QUERY_THROW );
66 8 : uno::Reference<container::XIndexAccess> xIndexAccess;
67 8 : if( m_bColumn )
68 4 : xIndexAccess.set( xColumnRowRange->getColumns(), uno::UNO_QUERY_THROW );
69 : else
70 4 : xIndexAccess.set( xColumnRowRange->getRows(), uno::UNO_QUERY_THROW );
71 8 : return xIndexAccess;
72 : }
73 :
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);
76 :
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 0 : virtual uno::Type SAL_CALL getElementType( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
81 : {
82 0 : if( m_bColumn )
83 0 : return cppu::UnoType<excel::XVPageBreak>::get();
84 0 : return cppu::UnoType<excel::XHPageBreak>::get();
85 : }
86 0 : virtual sal_Bool SAL_CALL hasElements( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
87 : {
88 0 : return sal_True;
89 : }
90 : };
91 :
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.
96 : */
97 20 : sal_Int32 SAL_CALL RangePageBreaks::getCount( ) throw (uno::RuntimeException, std::exception)
98 : {
99 20 : sal_Int32 nCount = 0;
100 20 : uno::Reference< excel::XWorksheet > xWorksheet( mxParent, uno::UNO_QUERY_THROW );
101 40 : uno::Reference< excel::XRange > xRange = xWorksheet->getUsedRange();
102 20 : sal_Int32 nUsedStart = getAPIStartofRange( xRange );
103 20 : sal_Int32 nUsedEnd = getAPIEndIndexofRange( xRange, nUsedStart );
104 40 : uno::Sequence<sheet::TablePageBreakData> aTablePageBreakData = getAllPageBreaks();
105 :
106 20 : sal_Int32 nLength = aTablePageBreakData.getLength();
107 62 : for( sal_Int32 i=0; i<nLength; i++ )
108 : {
109 62 : sal_Int32 nPos = aTablePageBreakData[i].Position;
110 62 : if( nPos > nUsedEnd + 1)
111 20 : return nCount;
112 42 : nCount++;
113 : }
114 :
115 20 : return nCount;
116 : }
117 :
118 4 : uno::Any SAL_CALL RangePageBreaks::getByIndex( sal_Int32 Index ) throw (lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException, std::exception)
119 : {
120 4 : if( (Index < getCount()) && ( Index >= 0 ))
121 : {
122 4 : sheet::TablePageBreakData aTablePageBreakData = getTablePageBreakData( Index );
123 4 : uno::Reference< container::XIndexAccess > xIndexAccess = getRowColContainer();
124 4 : sal_Int32 nPos = aTablePageBreakData.Position;
125 4 : if( (nPos < xIndexAccess->getCount()) && (nPos > -1) )
126 : {
127 4 : uno::Reference< beans::XPropertySet > xRowColPropertySet( xIndexAccess->getByIndex(nPos), uno::UNO_QUERY_THROW );
128 4 : if( m_bColumn )
129 2 : return uno::makeAny( uno::Reference< excel::XVPageBreak >( new ScVbaVPageBreak( mxParent, mxContext, xRowColPropertySet, aTablePageBreakData) ));
130 2 : return uno::makeAny( uno::Reference< excel::XHPageBreak >( new ScVbaHPageBreak( mxParent, mxContext, xRowColPropertySet, aTablePageBreakData) ));
131 0 : }
132 : }
133 0 : throw lang::IndexOutOfBoundsException();
134 : }
135 :
136 4 : sheet::TablePageBreakData RangePageBreaks::getTablePageBreakData( sal_Int32 nAPIItemIndex ) throw (uno::RuntimeException)
137 : {
138 4 : sal_Int32 index = -1;
139 4 : sheet::TablePageBreakData aTablePageBreakData;
140 4 : uno::Reference< excel::XWorksheet > xWorksheet( mxParent, uno::UNO_QUERY_THROW );
141 8 : uno::Reference< excel::XRange > xRange = xWorksheet->getUsedRange();
142 4 : sal_Int32 nUsedStart = getAPIStartofRange( xRange );
143 4 : sal_Int32 nUsedEnd = getAPIEndIndexofRange( xRange, nUsedStart );
144 8 : uno::Sequence<sheet::TablePageBreakData> aTablePageBreakDataList = getAllPageBreaks();
145 :
146 4 : sal_Int32 nLength = aTablePageBreakDataList.getLength();
147 4 : for( sal_Int32 i=0; i<nLength; i++ )
148 : {
149 4 : aTablePageBreakData = aTablePageBreakDataList[i];
150 4 : sal_Int32 nPos = aTablePageBreakData.Position;
151 4 : if( nPos > nUsedEnd + 1 )
152 0 : DebugHelper::runtimeexception(SbERR_METHOD_FAILED, OUString());
153 4 : index++;
154 4 : if( index == nAPIItemIndex )
155 4 : return aTablePageBreakData;
156 : }
157 :
158 4 : return aTablePageBreakData;
159 : }
160 :
161 4 : uno::Any RangePageBreaks::Add( const css::uno::Any& Before ) throw ( css::script::BasicErrorException, css::uno::RuntimeException)
162 : {
163 4 : uno::Reference< excel::XRange > xRange;
164 4 : Before >>= xRange;
165 4 : if( !xRange.is() )
166 : {
167 0 : DebugHelper::basicexception(SbERR_BAD_ARGUMENT, OUString());
168 : }
169 :
170 4 : sal_Int32 nAPIRowColIndex = getAPIStartofRange( xRange );
171 8 : uno::Reference< container::XIndexAccess > xIndexAccess = getRowColContainer();
172 8 : uno::Reference< beans::XPropertySet > xRowColPropertySet( xIndexAccess->getByIndex(nAPIRowColIndex), uno::UNO_QUERY_THROW );
173 4 : xRowColPropertySet->setPropertyValue("IsStartOfNewPage", uno::makeAny(sal_True));
174 4 : sheet::TablePageBreakData aTablePageBreakData;
175 4 : aTablePageBreakData.ManualBreak = sal_True;
176 4 : aTablePageBreakData.Position = nAPIRowColIndex;
177 4 : if( m_bColumn )
178 2 : return uno::makeAny( uno::Reference< excel::XVPageBreak >( new ScVbaVPageBreak( mxParent, mxContext, xRowColPropertySet, aTablePageBreakData) ));
179 6 : return uno::makeAny( uno::Reference< excel::XHPageBreak >( new ScVbaHPageBreak( mxParent, mxContext, xRowColPropertySet, aTablePageBreakData) ));
180 : }
181 :
182 0 : class RangePageBreaksEnumWrapper : public EnumerationHelper_BASE
183 : {
184 : uno::Reference<container::XIndexAccess > m_xIndexAccess;
185 : sal_Int32 nIndex;
186 : public:
187 0 : RangePageBreaksEnumWrapper( const uno::Reference< container::XIndexAccess >& xIndexAccess ) : m_xIndexAccess( xIndexAccess ), nIndex( 0 ) {}
188 0 : virtual sal_Bool SAL_CALL hasMoreElements( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
189 : {
190 0 : return ( nIndex < m_xIndexAccess->getCount() );
191 : }
192 :
193 0 : virtual uno::Any SAL_CALL nextElement( ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException, std::exception) SAL_OVERRIDE
194 : {
195 0 : if ( nIndex < m_xIndexAccess->getCount() )
196 0 : return m_xIndexAccess->getByIndex( nIndex++ );
197 0 : throw container::NoSuchElementException();
198 : }
199 : };
200 :
201 2 : ScVbaHPageBreaks::ScVbaHPageBreaks( const uno::Reference< XHelperInterface >& xParent,
202 : const uno::Reference< uno::XComponentContext >& xContext,
203 : uno::Reference< sheet::XSheetPageBreak >& xSheetPageBreak) throw (uno::RuntimeException):
204 2 : ScVbaHPageBreaks_BASE( xParent,xContext, new RangePageBreaks( xParent, xContext, xSheetPageBreak, false )),
205 4 : mxSheetPageBreak( xSheetPageBreak )
206 : {
207 2 : }
208 :
209 2 : uno::Any SAL_CALL ScVbaHPageBreaks::Add( const uno::Any& Before) throw ( script::BasicErrorException, uno::RuntimeException, std::exception)
210 : {
211 2 : RangePageBreaks* pPageBreaks = dynamic_cast< RangePageBreaks* >( m_xIndexAccess.get() );
212 2 : if( pPageBreaks )
213 : {
214 2 : return pPageBreaks->Add( Before );
215 : }
216 0 : return uno::Any();
217 : }
218 :
219 : uno::Reference< container::XEnumeration >
220 0 : ScVbaHPageBreaks::createEnumeration() throw (uno::RuntimeException)
221 : {
222 0 : return new RangePageBreaksEnumWrapper( m_xIndexAccess );
223 : }
224 :
225 : uno::Any
226 2 : ScVbaHPageBreaks::createCollectionObject( const css::uno::Any& aSource )
227 : {
228 2 : return aSource; // its already a pagebreak object
229 : }
230 :
231 : uno::Type
232 0 : ScVbaHPageBreaks::getElementType() throw (uno::RuntimeException)
233 : {
234 0 : return cppu::UnoType<excel::XHPageBreak>::get();
235 : }
236 :
237 : OUString
238 0 : ScVbaHPageBreaks::getServiceImplName()
239 : {
240 0 : return OUString("ScVbaHPageBreaks");
241 : }
242 :
243 : uno::Sequence< OUString >
244 0 : ScVbaHPageBreaks::getServiceNames()
245 : {
246 0 : static uno::Sequence< OUString > aServiceNames;
247 0 : if ( aServiceNames.getLength() == 0 )
248 : {
249 0 : aServiceNames.realloc( 1 );
250 0 : aServiceNames[ 0 ] = "ooo.vba.excel.HPageBreaks";
251 : }
252 0 : return aServiceNames;
253 : }
254 :
255 : //VPageBreak
256 4 : ScVbaVPageBreaks::ScVbaVPageBreaks( const uno::Reference< XHelperInterface >& xParent,
257 : const uno::Reference< uno::XComponentContext >& xContext,
258 : uno::Reference< sheet::XSheetPageBreak >& xSheetPageBreak ) throw ( uno::RuntimeException )
259 4 : : ScVbaVPageBreaks_BASE( xParent, xContext, new RangePageBreaks( xParent, xContext, xSheetPageBreak, true ) ),
260 8 : mxSheetPageBreak( xSheetPageBreak )
261 : {
262 4 : }
263 :
264 8 : ScVbaVPageBreaks::~ScVbaVPageBreaks()
265 : {
266 8 : }
267 :
268 : uno::Any SAL_CALL
269 2 : ScVbaVPageBreaks::Add( const uno::Any& Before ) throw ( script::BasicErrorException, uno::RuntimeException, std::exception )
270 : {
271 2 : RangePageBreaks* pPageBreaks = dynamic_cast< RangePageBreaks* >( m_xIndexAccess.get() );
272 2 : if( pPageBreaks )
273 : {
274 2 : return pPageBreaks->Add( Before );
275 : }
276 0 : return uno::Any();
277 : }
278 :
279 : uno::Reference< container::XEnumeration >
280 0 : ScVbaVPageBreaks::createEnumeration() throw ( uno::RuntimeException )
281 : {
282 0 : return new RangePageBreaksEnumWrapper( m_xIndexAccess );
283 : }
284 :
285 : uno::Any
286 2 : ScVbaVPageBreaks::createCollectionObject( const css::uno::Any& aSource )
287 : {
288 2 : return aSource; // its already a pagebreak object
289 : }
290 :
291 : uno::Type
292 0 : ScVbaVPageBreaks::getElementType() throw ( uno::RuntimeException )
293 : {
294 0 : return cppu::UnoType<excel::XVPageBreak>::get();
295 : }
296 :
297 : OUString
298 0 : ScVbaVPageBreaks::getServiceImplName()
299 : {
300 0 : return OUString("ScVbaVPageBreaks");
301 : }
302 :
303 : uno::Sequence< OUString >
304 0 : ScVbaVPageBreaks::getServiceNames()
305 : {
306 0 : static uno::Sequence< OUString > aServiceNames;
307 0 : if ( aServiceNames.getLength() == 0 )
308 : {
309 0 : aServiceNames.realloc( 1 );
310 0 : aServiceNames[ 0 ] = "ooo.vba.excel.VPageBreaks";
311 : }
312 0 : return aServiceNames;
313 12 : }
314 :
315 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|