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