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 
20 #include "vbapane.hxx"
21 #include <com/sun/star/frame/XModel.hpp>
22 #include <com/sun/star/sheet/XSpreadsheet.hpp>
23 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
24 #include <com/sun/star/table/CellRangeAddress.hpp>
25 #include "vbarange.hxx"
26 
27 using namespace com::sun::star;
28 using namespace ooo::vba;
29 
ScVbaPane(const css::uno::Reference<ov::XHelperInterface> & xParent,const uno::Reference<uno::XComponentContext> & xContext,const uno::Reference<frame::XModel> & rModel,const uno::Reference<sheet::XViewPane> & rViewPane)30 ScVbaPane::ScVbaPane(
31         const css::uno::Reference< ov::XHelperInterface >& xParent,
32         const uno::Reference< uno::XComponentContext >& xContext,
33         const uno::Reference< frame::XModel >& rModel,
34         const uno::Reference< sheet::XViewPane >& rViewPane ) :
35     m_xModel(rModel, uno::UNO_SET_THROW),
36     m_xViewPane(rViewPane, uno::UNO_SET_THROW),
37     m_xParent(xParent),
38     m_xContext(xContext)
39 {
40 }
41 
42 sal_Int32 SAL_CALL
getScrollColumn()43 ScVbaPane::getScrollColumn()
44 {
45     return ( m_xViewPane->getFirstVisibleColumn() + 1 );
46 }
47 
48 void SAL_CALL
setScrollColumn(sal_Int32 _scrollcolumn)49 ScVbaPane::setScrollColumn( sal_Int32 _scrollcolumn )
50 {
51     if( _scrollcolumn < 1 )
52     {
53         throw uno::RuntimeException("Column number should not be less than 1" );
54     }
55     m_xViewPane->setFirstVisibleColumn( _scrollcolumn - 1 );
56 }
57 
58 sal_Int32 SAL_CALL
getScrollRow()59 ScVbaPane::getScrollRow()
60 {
61     return ( m_xViewPane->getFirstVisibleRow() + 1 );
62 }
63 
64 void SAL_CALL
setScrollRow(sal_Int32 _scrollrow)65 ScVbaPane::setScrollRow( sal_Int32 _scrollrow )
66 {
67     if( _scrollrow < 1 )
68     {
69         throw uno::RuntimeException("Row number should not be less than 1" );
70     }
71     m_xViewPane->setFirstVisibleRow( _scrollrow - 1 );
72 }
73 
74 uno::Reference< excel::XRange > SAL_CALL
getVisibleRange()75 ScVbaPane::getVisibleRange()
76 {
77     // TODO: Excel includes partly visible rows/columns, Calc does not
78     table::CellRangeAddress aRangeAddr = m_xViewPane->getVisibleRange();
79     uno::Reference< sheet::XSpreadsheetDocument > xDoc( m_xModel, uno::UNO_QUERY_THROW );
80     uno::Reference< container::XIndexAccess > xSheetsIA( xDoc->getSheets(), uno::UNO_QUERY_THROW );
81     uno::Reference< sheet::XSpreadsheet > xSheet( xSheetsIA->getByIndex( aRangeAddr.Sheet ), uno::UNO_QUERY_THROW );
82     uno::Reference< table::XCellRange > xRange( xSheet->getCellRangeByPosition( aRangeAddr.StartColumn, aRangeAddr.StartRow, aRangeAddr.EndColumn, aRangeAddr.EndRow ), uno::UNO_SET_THROW );
83     // TODO: m_xParent is the window, Range needs the worksheet
84     return new ScVbaRange( m_xParent, m_xContext, xRange );
85 }
86 
87 //Method
88 void SAL_CALL
SmallScroll(const uno::Any & Down,const uno::Any & Up,const uno::Any & ToRight,const uno::Any & ToLeft)89 ScVbaPane::SmallScroll( const uno::Any& Down, const uno::Any& Up, const uno::Any& ToRight, const uno::Any& ToLeft )
90 {
91     OUString messageBuffer;
92     sal_Int32 downRows = 0;
93     sal_Int32 rightCols = 0;
94     table::CellRangeAddress visibleRange = m_xViewPane->getVisibleRange();
95 
96     if( Down.hasValue() )
97     {
98         sal_Int32 down = 0;
99         if( Down >>= down )
100             downRows += down;
101         else
102             messageBuffer += "Error getting parameter: Down\n";
103     }
104     if( Up.hasValue() )
105     {
106         sal_Int32 up = 0;
107         if( Up >>= up )
108             downRows -= up;
109         else
110             messageBuffer += "Error getting parameter: Up\n";
111     }
112     if( ToRight.hasValue() )
113     {
114         sal_Int32 right = 0;
115         if( ToRight >>= right )
116             rightCols += right;
117         else
118             messageBuffer += "Error getting parameter: ToRight\n";
119     }
120     if( ToLeft.hasValue() )
121     {
122         sal_Int32 left = 0;
123         if( ToLeft >>= left )
124             rightCols -= left;
125         else
126             messageBuffer += "Error getting parameter: ToLeft\n";
127     }
128     if( !messageBuffer.isEmpty() )
129         throw uno::RuntimeException( messageBuffer );
130 
131     sal_Int32 newStartRow = visibleRange.StartRow + downRows;
132     if( newStartRow < 0 )
133         newStartRow = 0;
134     sal_Int32 newStartCol = visibleRange.StartColumn + rightCols;
135     if( newStartCol < 0 )
136         newStartCol = 0;
137     m_xViewPane->setFirstVisibleRow( newStartRow );
138     m_xViewPane->setFirstVisibleColumn( newStartCol );
139 }
140 
141 void SAL_CALL
LargeScroll(const uno::Any & Down,const uno::Any & Up,const uno::Any & ToRight,const uno::Any & ToLeft)142 ScVbaPane::LargeScroll( const uno::Any& Down, const uno::Any& Up, const uno::Any& ToRight, const uno::Any& ToLeft )
143 {
144     OUString messageBuffer;
145     table::CellRangeAddress visibleRange = m_xViewPane->getVisibleRange();
146 
147     sal_Int32 vertPageSize = 1 + visibleRange.EndRow - visibleRange.StartRow;
148     sal_Int32 horizPageSize = 1 + visibleRange.EndColumn - visibleRange.StartColumn;
149     sal_Int32 downPages = 0;
150     sal_Int32 acrossPages = 0;
151     if( Down.hasValue() )
152     {
153         sal_Int32 down = 0;
154         if( Down >>= down )
155             downPages += down;
156         else
157             messageBuffer += "Error getting parameter: Down\n";
158     }
159     if( Up.hasValue() )
160     {
161         sal_Int32 up = 0;
162         if( Up >>= up )
163             downPages -= up;
164         else
165             messageBuffer += "Error getting parameter: Up\n";
166     }
167     if( ToRight.hasValue() )
168     {
169         sal_Int32 right = 0;
170         if( ToRight >>= right )
171             acrossPages += right;
172         else
173             messageBuffer += "Error getting parameter: ToRight\n";
174     }
175     if( ToLeft.hasValue() )
176     {
177         sal_Int32 left = 0;
178         if( ToLeft >>= left )
179             acrossPages -= left;
180         else
181             messageBuffer += "Error getting parameter: ToLeft\n";
182     }
183     if( !messageBuffer.isEmpty() )
184         throw uno::RuntimeException( messageBuffer );
185 
186     sal_Int32 newStartRow = visibleRange.StartRow + (downPages * vertPageSize );
187     if( newStartRow < 0 )
188         newStartRow = 0;
189     sal_Int32 newStartCol = visibleRange.StartColumn + (acrossPages * horizPageSize );
190     if( newStartCol < 0 )
191         newStartCol = 0;
192     m_xViewPane->setFirstVisibleRow( newStartRow );
193     m_xViewPane->setFirstVisibleColumn( newStartCol );
194 }
195 
196 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
197