LibreOffice Module sc (master)  1
cursuno.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 
20 #include <vcl/svapp.hxx>
21 #include <comphelper/sequence.hxx>
23 
24 #include <cursuno.hxx>
25 #include <cellsuno.hxx>
26 #include <docsh.hxx>
27 #include <markdata.hxx>
28 #include <miscuno.hxx>
29 
30 using namespace com::sun::star;
31 
32 #define SCSHEETCELLCURSOR_SERVICE "com.sun.star.sheet.SheetCellCursor"
33 #define SCCELLCURSOR_SERVICE "com.sun.star.table.CellCursor"
34 
36  ScCellRangeObj( pDocSh, rR )
37 {
38 }
39 
41 {
42 }
43 
45 {
46  SC_QUERYINTERFACE( sheet::XSheetCellCursor )
47  SC_QUERYINTERFACE( sheet::XUsedAreaCursor )
48  SC_QUERYINTERFACE( table::XCellCursor )
49 
50  return ScCellRangeObj::queryInterface( rType );
51 }
52 
53 void SAL_CALL ScCellCursorObj::acquire() throw()
54 {
56 }
57 
58 void SAL_CALL ScCellCursorObj::release() throw()
59 {
61 }
62 
63 uno::Sequence<uno::Type> SAL_CALL ScCellCursorObj::getTypes()
64 {
67  uno::Sequence<uno::Type>
68  {
72  } );
73 }
74 
75 uno::Sequence<sal_Int8> SAL_CALL ScCellCursorObj::getImplementationId()
76 {
77  return css::uno::Sequence<sal_Int8>();
78 }
79 
80 // XSheetCellCursor
81 
83 {
84  SolarMutexGuard aGuard;
85  const ScRangeList& rRanges = GetRangeList();
86  OSL_ENSURE( rRanges.size() == 1, "Range? Ranges?" );
87  ScRange aOneRange( rRanges[ 0 ] );
88 
89  aOneRange.PutInOrder();
90  ScDocShell* pDocSh = GetDocShell();
91  if ( pDocSh )
92  {
93  SCCOL nStartCol = aOneRange.aStart.Col();
94  SCROW nStartRow = aOneRange.aStart.Row();
95  SCCOL nEndCol = aOneRange.aEnd.Col();
96  SCROW nEndRow = aOneRange.aEnd.Row();
97  SCTAB nTab = aOneRange.aStart.Tab();
98 
99  pDocSh->GetDocument().GetDataArea(
100  nTab, nStartCol, nStartRow, nEndCol, nEndRow, true, false );
101 
102  ScRange aNew( nStartCol, nStartRow, nTab, nEndCol, nEndRow, nTab );
103  SetNewRange( aNew );
104  }
105 }
106 
108 {
109  SolarMutexGuard aGuard;
110  const ScRangeList& rRanges = GetRangeList();
111  OSL_ENSURE( rRanges.size() == 1, "Range? Ranges?" );
112  ScRange aOneRange( rRanges[ 0 ] );
113 
114  aOneRange.PutInOrder();
115  ScAddress aCursor(aOneRange.aStart); // use the start address of the range
116 
117  ScDocShell* pDocSh = GetDocShell();
118  if ( pDocSh )
119  {
120  ScDocument& rDoc = pDocSh->GetDocument();
121  ScRange aMatrix;
122 
123  // finding the matrix range is now in GetMatrixFormulaRange in the document
124  if ( rDoc.GetMatrixFormulaRange( aCursor, aMatrix ) )
125  {
126  SetNewRange( aMatrix );
127  }
128  }
129  // that's a Bug, that this assertion comes; the API Reference says, that
130  // if there is no Matrix, the Range is left unchanged; they say nothing
131  // about an exception
132  /*if (!bFound)
133  {
134  OSL_FAIL("no matrix");
136  }*/
137 }
138 
140 {
141  SolarMutexGuard aGuard;
142  ScDocShell* pDocSh = GetDocShell();
143  if ( pDocSh )
144  {
145  const ScRangeList& rRanges = GetRangeList();
146  OSL_ENSURE( rRanges.size() == 1, "Range? Ranges?" );
147  ScRange aNewRange( rRanges[ 0 ] );
148 
149  ScDocument& rDoc = pDocSh->GetDocument();
150  rDoc.ExtendOverlapped( aNewRange );
151  rDoc.ExtendMerge( aNewRange ); // after ExtendOverlapped!
152 
153  SetNewRange( aNewRange );
154  }
155 }
156 
158 {
159  SolarMutexGuard aGuard;
160  const ScRangeList& rRanges = GetRangeList();
161  OSL_ENSURE( rRanges.size() == 1, "Range? Ranges?" );
162  ScRange aNewRange( rRanges[ 0 ] );
163 
164  aNewRange.aStart.SetRow( 0 );
165  aNewRange.aEnd.SetRow( GetDocShell()->GetDocument().MaxRow() );
166 
167  SetNewRange( aNewRange );
168 }
169 
171 {
172  SolarMutexGuard aGuard;
173  const ScRangeList& rRanges = GetRangeList();
174  OSL_ENSURE( rRanges.size() == 1, "Range? Ranges?" );
175  ScRange aNewRange( rRanges[ 0 ] );
176 
177  aNewRange.aStart.SetCol( 0 );
178  aNewRange.aEnd.SetCol( GetDocShell()->GetDocument().MaxCol() );
179 
180  SetNewRange( aNewRange );
181 }
182 
183 void SAL_CALL ScCellCursorObj::collapseToSize( sal_Int32 nColumns, sal_Int32 nRows )
184 {
185  SolarMutexGuard aGuard;
186  if ( nColumns <= 0 || nRows <= 0 )
187  {
188  OSL_FAIL("Empty range not allowed");
190  }
191  else
192  {
193  const ScRangeList& rRanges = GetRangeList();
194  OSL_ENSURE( rRanges.size() == 1, "Range? Ranges?" );
195  ScRange aNewRange( rRanges[ 0 ] );
196 
197  aNewRange.PutInOrder();
198 
199  const auto & rDoc = GetDocShell()->GetDocument();
200  long nEndX = aNewRange.aStart.Col() + nColumns - 1;
201  long nEndY = aNewRange.aStart.Row() + nRows - 1;
202  if ( nEndX < 0 ) nEndX = 0;
203  if ( nEndX > rDoc.MaxCol() ) nEndX = rDoc.MaxCol();
204  if ( nEndY < 0 ) nEndY = 0;
205  if ( nEndY > rDoc.MaxRow() ) nEndY = rDoc.MaxRow();
207 
208  aNewRange.aEnd.SetCol(static_cast<SCCOL>(nEndX));
209  aNewRange.aEnd.SetRow(static_cast<SCROW>(nEndY));
210 
211  aNewRange.PutInOrder();
212 
213  SetNewRange( aNewRange );
214  }
215 }
216 
217 // XUsedAreaCursor
218 
220 {
221  SolarMutexGuard aGuard;
222  ScDocShell* pDocSh = GetDocShell();
223  if ( pDocSh )
224  {
225  const ScRangeList& rRanges = GetRangeList();
226  OSL_ENSURE( rRanges.size() == 1, "Range? Ranges?" );
227  ScRange aNewRange( rRanges[0] );
228  SCTAB nTab = aNewRange.aStart.Tab();
229 
230  SCCOL nUsedX = 0; // fetch the beginning
231  SCROW nUsedY = 0;
232  if (!pDocSh->GetDocument().GetDataStart( nTab, nUsedX, nUsedY ))
233  {
234  nUsedX = 0;
235  nUsedY = 0;
236  }
237 
238  aNewRange.aStart.SetCol( nUsedX );
239  aNewRange.aStart.SetRow( nUsedY );
240  if (!bExpand)
241  aNewRange.aEnd = aNewRange.aStart;
242  SetNewRange( aNewRange );
243  }
244 }
245 
247 {
248  SolarMutexGuard aGuard;
249  ScDocShell* pDocSh = GetDocShell();
250  if ( pDocSh )
251  {
252  const ScRangeList& rRanges = GetRangeList();
253  OSL_ENSURE( rRanges.size() == 1, "Range? Ranges?" );
254  ScRange aNewRange( rRanges[ 0 ]);
255  SCTAB nTab = aNewRange.aStart.Tab();
256 
257  SCCOL nUsedX = 0; // fetch the end
258  SCROW nUsedY = 0;
259  if (!pDocSh->GetDocument().GetTableArea( nTab, nUsedX, nUsedY ))
260  {
261  nUsedX = 0;
262  nUsedY = 0;
263  }
264 
265  aNewRange.aEnd.SetCol( nUsedX );
266  aNewRange.aEnd.SetRow( nUsedY );
267  if (!bExpand)
268  aNewRange.aStart = aNewRange.aEnd;
269  SetNewRange( aNewRange );
270  }
271 }
272 
273 // XCellCursor
274 
276 {
277  // this is similar to collapseToCurrentRegion
279 
280  SolarMutexGuard aGuard;
281  const ScRangeList& rRanges = GetRangeList();
282  OSL_ENSURE( rRanges.size() == 1, "Range? Ranges?" );
283  ScRange aOneRange( rRanges[ 0 ]);
284 
285  aOneRange.PutInOrder();
286  ScDocShell* pDocSh = GetDocShell();
287  if ( pDocSh )
288  {
289  SCCOL nStartCol = aOneRange.aStart.Col();
290  SCROW nStartRow = aOneRange.aStart.Row();
291  SCCOL nEndCol = aOneRange.aEnd.Col();
292  SCROW nEndRow = aOneRange.aEnd.Row();
293  SCTAB nTab = aOneRange.aStart.Tab();
294 
295  pDocSh->GetDocument().GetDataArea(
296  nTab, nStartCol, nStartRow, nEndCol, nEndRow, false, false );
297 
298  ScRange aNew( nStartCol, nStartRow, nTab );
299  SetNewRange( aNew );
300  }
301 }
302 
304 {
305  // this is similar to collapseToCurrentRegion
307 
308  SolarMutexGuard aGuard;
309  const ScRangeList& rRanges = GetRangeList();
310  OSL_ENSURE( rRanges.size() == 1, "Range? Ranges?" );
311  ScRange aOneRange( rRanges[ 0 ] );
312 
313  aOneRange.PutInOrder();
314  ScDocShell* pDocSh = GetDocShell();
315  if ( pDocSh )
316  {
317  SCCOL nStartCol = aOneRange.aStart.Col();
318  SCROW nStartRow = aOneRange.aStart.Row();
319  SCCOL nEndCol = aOneRange.aEnd.Col();
320  SCROW nEndRow = aOneRange.aEnd.Row();
321  SCTAB nTab = aOneRange.aStart.Tab();
322 
323  pDocSh->GetDocument().GetDataArea(
324  nTab, nStartCol, nStartRow, nEndCol, nEndRow, false, false );
325 
326  ScRange aNew( nEndCol, nEndRow, nTab );
327  SetNewRange( aNew );
328  }
329 }
330 
332 {
333  SolarMutexGuard aGuard;
334  const ScRangeList& rRanges = GetRangeList();
335  OSL_ENSURE( rRanges.size() == 1, "Range? Ranges?" );
336  ScRange aOneRange( rRanges[ 0 ] );
337 
338  aOneRange.PutInOrder();
339  ScAddress aCursor(aOneRange.aStart); // always use start of block
340 
341  ScMarkData aMark(GetDocument()->GetSheetLimits()); // not used with bMarked=FALSE
342  SCCOL nNewX = aCursor.Col();
343  SCROW nNewY = aCursor.Row();
344  SCTAB nTab = aCursor.Tab();
345  ScDocShell* pDocSh = GetDocShell();
346  if ( pDocSh )
347  pDocSh->GetDocument().GetNextPos( nNewX,nNewY, nTab, 1,0, false,true, aMark );
349 
350  SetNewRange( ScRange( nNewX, nNewY, nTab ) );
351 }
352 
354 {
355  SolarMutexGuard aGuard;
356  const ScRangeList& rRanges = GetRangeList();
357  OSL_ENSURE( rRanges.size() == 1, "Range? Ranges?" );
358  ScRange aOneRange( rRanges[ 0 ] );
359 
360  aOneRange.PutInOrder();
361  ScAddress aCursor(aOneRange.aStart); // always use start of block
362 
363  ScMarkData aMark(GetDocument()->GetSheetLimits()); // not used with bMarked=FALSE
364  SCCOL nNewX = aCursor.Col();
365  SCROW nNewY = aCursor.Row();
366  SCTAB nTab = aCursor.Tab();
367  ScDocShell* pDocSh = GetDocShell();
368  if ( pDocSh )
369  pDocSh->GetDocument().GetNextPos( nNewX,nNewY, nTab, -1,0, false,true, aMark );
371 
372  SetNewRange( ScRange( nNewX, nNewY, nTab ) );
373 }
374 
375 void SAL_CALL ScCellCursorObj::gotoOffset( sal_Int32 nColumnOffset, sal_Int32 nRowOffset )
376 {
377  SolarMutexGuard aGuard;
378  const ScRangeList& rRanges = GetRangeList();
379  OSL_ENSURE( rRanges.size() == 1, "Range? Ranges?" );
380  ScRange aOneRange( rRanges[ 0 ] );
381  aOneRange.PutInOrder();
382 
383  const auto & rDoc = GetDocShell()->GetDocument();
384  if ( aOneRange.aStart.Col() + nColumnOffset >= 0 &&
385  aOneRange.aEnd.Col() + nColumnOffset <= rDoc.MaxCol() &&
386  aOneRange.aStart.Row() + nRowOffset >= 0 &&
387  aOneRange.aEnd.Row() + nRowOffset <= rDoc.MaxRow() )
388  {
389  ScRange aNew( static_cast<SCCOL>(aOneRange.aStart.Col() + nColumnOffset),
390  static_cast<SCROW>(aOneRange.aStart.Row() + nRowOffset),
391  aOneRange.aStart.Tab(),
392  static_cast<SCCOL>(aOneRange.aEnd.Col() + nColumnOffset),
393  static_cast<SCROW>(aOneRange.aEnd.Row() + nRowOffset),
394  aOneRange.aEnd.Tab() );
395  SetNewRange( aNew );
396  }
397 }
398 
399 // XSheetCellRange
400 
401 uno::Reference<sheet::XSpreadsheet> SAL_CALL ScCellCursorObj::getSpreadsheet()
402 {
403  SolarMutexGuard aGuard;
405 }
406 
407 // XCellRange
408 
409 uno::Reference<table::XCell> SAL_CALL ScCellCursorObj::getCellByPosition(
410  sal_Int32 nColumn, sal_Int32 nRow )
411 {
412  SolarMutexGuard aGuard;
413  return ScCellRangeObj::getCellByPosition(nColumn,nRow);
414 }
415 
416 uno::Reference<table::XCellRange> SAL_CALL ScCellCursorObj::getCellRangeByPosition(
417  sal_Int32 nLeft, sal_Int32 nTop, sal_Int32 nRight, sal_Int32 nBottom )
418 {
419  SolarMutexGuard aGuard;
420  return ScCellRangeObj::getCellRangeByPosition(nLeft,nTop,nRight,nBottom);
421 }
422 
423 uno::Reference<table::XCellRange> SAL_CALL ScCellCursorObj::getCellRangeByName(
424  const OUString& rRange )
425 {
426  SolarMutexGuard aGuard;
427  return ScCellRangeObj::getCellRangeByName(rRange);
428 }
429 
430 // XServiceInfo
431 
433 {
434  return "ScCellCursorObj";
435 }
436 
437 sal_Bool SAL_CALL ScCellCursorObj::supportsService( const OUString& rServiceName )
438 {
439  return cppu::supportsService(this, rServiceName);
440 }
441 
442 uno::Sequence<OUString> SAL_CALL ScCellCursorObj::getSupportedServiceNames()
443 {
444  // SheetCellCursor should be first (?)
445  return comphelper::concatSequences<OUString>(
448 }
449 
450 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual ~ScCellCursorObj() override
Definition: cursuno.cxx:40
virtual css::uno::Reference< css::table::XCell > SAL_CALL getCellByPosition(sal_Int32 nColumn, sal_Int32 nRow) override
Definition: cursuno.cxx:409
SC_DLLPUBLIC bool GetDataStart(SCTAB nTab, SCCOL &rStartCol, SCROW &rStartRow) const
Definition: documen2.cxx:641
todo: It should be possible to have MarkArrays for each table, in order to enable "search all" across...
Definition: markdata.hxx:43
virtual void SAL_CALL acquire() override
Definition: cursuno.cxx:53
SCROW Row() const
Definition: address.hxx:262
ScDocShell * GetDocShell() const
Definition: cellsuno.hxx:242
virtual void SAL_CALL expandToEntireRows() override
Definition: cursuno.cxx:170
virtual void SAL_CALL collapseToCurrentRegion() override
Definition: cursuno.cxx:82
virtual void SAL_CALL gotoEndOfUsedArea(sal_Bool bExpand) override
Definition: cursuno.cxx:246
virtual css::uno::Sequence< sal_Int8 > SAL_CALL getImplementationId() override
Definition: cursuno.cxx:75
virtual css::uno::Sequence< css::uno::Type > SAL_CALL getTypes() override
Definition: cursuno.cxx:63
virtual css::uno::Sequence< css::uno::Type > SAL_CALL getTypes() override
Definition: cellsuno.cxx:4637
const ScRangeList & GetRangeList() const
Definition: cellsuno.hxx:244
virtual css::uno::Reference< css::table::XCellRange > SAL_CALL getCellRangeByPosition(sal_Int32 nLeft, sal_Int32 nTop, sal_Int32 nRight, sal_Int32 nBottom) override
Definition: cellsuno.cxx:4704
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
Definition: cursuno.cxx:437
virtual css::uno::Reference< css::table::XCell > SAL_CALL getCellByPosition(sal_Int32 nColumn, sal_Int32 nRow) override
Definition: cellsuno.cxx:4696
virtual css::uno::Any SAL_CALL queryInterface(const css::uno::Type &rType) override
Definition: cellsuno.cxx:4602
ScCellCursorObj(ScDocShell *pDocSh, const ScRange &rR)
Definition: cursuno.cxx:35
virtual void SAL_CALL release() override
Definition: cellsuno.cxx:4632
virtual css::uno::Reference< css::table::XCellRange > SAL_CALL getCellRangeByName(const OUString &aRange) override
Definition: cellsuno.cxx:4732
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
SCTAB Tab() const
Definition: address.hxx:271
SC_DLLPUBLIC void GetNextPos(SCCOL &rCol, SCROW &rRow, SCTAB nTab, SCCOL nMovX, SCROW nMovY, bool bMarked, bool bUnprotected, const ScMarkData &rMark, SCCOL nTabStartCol=SC_TABSTART_NONE) const
Definition: document.cxx:6097
virtual css::uno::Reference< css::sheet::XSpreadsheet > SAL_CALL getSpreadsheet() override
Definition: cursuno.cxx:401
#define SC_QUERYINTERFACE(x)
Definition: miscuno.hxx:87
virtual void SAL_CALL gotoStart() override
Definition: cursuno.cxx:275
SC_DLLPUBLIC void GetDataArea(SCTAB nTab, SCCOL &rStartCol, SCROW &rStartRow, SCCOL &rEndCol, SCROW &rEndRow, bool bIncludeOld, bool bOnlyDown) const
Return the smallest area containing at least all contiguous cells having data.
Definition: document.cxx:1096
#define SCSHEETCELLCURSOR_SERVICE
Definition: cursuno.cxx:32
virtual void SAL_CALL collapseToCurrentArray() override
Definition: cursuno.cxx:107
virtual void SAL_CALL gotoStartOfUsedArea(sal_Bool bExpand) override
Definition: cursuno.cxx:219
SC_DLLPUBLIC SCCOL MaxCol() const
Definition: document.hxx:872
virtual void SAL_CALL collapseToSize(sal_Int32 nColumns, sal_Int32 nRows) override
Definition: cursuno.cxx:183
#define SCCELLCURSOR_SERVICE
Definition: cursuno.cxx:33
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Definition: cursuno.cxx:442
virtual OUString SAL_CALL getImplementationName() override
Definition: cursuno.cxx:432
sal_Int16 SCCOL
Definition: types.hxx:22
virtual css::uno::Reference< css::table::XCellRange > SAL_CALL getCellRangeByName(const OUString &aRange) override
Definition: cursuno.cxx:423
ScDocument * GetDocument() const
Definition: cellsuno.cxx:1673
virtual void SAL_CALL gotoOffset(sal_Int32 nColumnOffset, sal_Int32 nRowOffset) override
Definition: cursuno.cxx:375
size_t size() const
Definition: rangelst.hxx:90
unsigned char sal_Bool
virtual void SAL_CALL acquire() override
Definition: cellsuno.cxx:4627
css::uno::Type const & get()
css::uno::Sequence< T > concatSequences(const css::uno::Sequence< T > &rS1, const Ss &...rSn)
virtual void SAL_CALL gotoEnd() override
Definition: cursuno.cxx:303
SC_DLLPUBLIC bool ExtendMerge(SCCOL nStartCol, SCROW nStartRow, SCCOL &rEndCol, SCROW &rEndRow, SCTAB nTab, bool bRefresh=false)
Definition: document.cxx:5571
SCCOL Col() const
Definition: address.hxx:267
sal_Int32 SCROW
Definition: types.hxx:18
virtual css::uno::Reference< css::sheet::XSpreadsheet > SAL_CALL getSpreadsheet() override
Definition: cellsuno.cxx:4826
virtual void SAL_CALL gotoPrevious() override
Definition: cursuno.cxx:353
virtual css::uno::Reference< css::table::XCellRange > SAL_CALL getCellRangeByPosition(sal_Int32 nLeft, sal_Int32 nTop, sal_Int32 nRight, sal_Int32 nBottom) override
Definition: cursuno.cxx:416
virtual void SAL_CALL expandToEntireColumns() override
Definition: cursuno.cxx:157
SC_DLLPUBLIC bool GetTableArea(SCTAB nTab, SCCOL &rEndCol, SCROW &rEndRow) const
Definition: document.cxx:1029
virtual void SAL_CALL collapseToMergedArea() override
Definition: cursuno.cxx:139
const ScDocument & GetDocument() const
Definition: docsh.hxx:216
virtual void SAL_CALL release() override
Definition: cursuno.cxx:58
virtual css::uno::Any SAL_CALL queryInterface(const css::uno::Type &rType) override
Definition: cursuno.cxx:44
virtual void SAL_CALL gotoNext() override
Definition: cursuno.cxx:331
SC_DLLPUBLIC bool GetMatrixFormulaRange(const ScAddress &rCellPos, ScRange &rMatrix)
Definition: document.cxx:5431
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Definition: cellsuno.cxx:5780
SC_DLLPUBLIC void ExtendOverlapped(SCCOL &rStartCol, SCROW &rStartRow, SCCOL nEndCol, SCROW nEndRow, SCTAB nTab) const
Definition: document.cxx:5481
void SetNewRange(const ScRange &rNew)
Definition: cellsuno.cxx:1707
sal_Int16 SCTAB
Definition: types.hxx:23