lok: Don't attempt to select the exact text after a failed search.
[LibreOffice.git] / svx / source / table / cellcursor.cxx
blob830a88201c8045b11a1c40f9083eb7f0b787156f
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include "svx/svdotable.hxx"
22 #include "cellcursor.hxx"
23 #include "tablelayouter.hxx"
24 #include "cell.hxx"
25 #include "svx/svdmodel.hxx"
26 #include "svx/svdstr.hrc"
27 #include "svdglob.hxx"
31 using namespace ::com::sun::star::uno;
32 using namespace ::com::sun::star::lang;
33 using namespace ::com::sun::star::container;
34 using namespace ::com::sun::star::beans;
35 using namespace ::com::sun::star::table;
39 namespace sdr { namespace table {
41 CellCursor::CellCursor( const TableModelRef & xTable, sal_Int32 nLeft, sal_Int32 nTop, sal_Int32 nRight, sal_Int32 nBottom )
42 : CellCursorBase( xTable, nLeft, nTop, nRight, nBottom )
48 CellCursor::~CellCursor()
53 // XCellCursor
56 Reference< XCell > SAL_CALL CellCursor::getCellByPosition( sal_Int32 nColumn, sal_Int32 nRow ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
58 return CellRange::getCellByPosition( nColumn, nRow );
63 Reference< XCellRange > SAL_CALL CellCursor::getCellRangeByPosition( sal_Int32 nLeft, sal_Int32 nTop, sal_Int32 nRight, sal_Int32 nBottom ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
65 return CellRange::getCellRangeByPosition( nLeft, nTop, nRight, nBottom );
70 Reference< XCellRange > SAL_CALL CellCursor::getCellRangeByName( const OUString& aRange ) throw (RuntimeException, std::exception)
72 return CellRange::getCellRangeByName( aRange );
76 // XCellCursor
79 void SAL_CALL CellCursor::gotoStart( ) throw (RuntimeException, std::exception)
81 mnRight = mnLeft;
82 mnBottom = mnTop;
87 void SAL_CALL CellCursor::gotoEnd( ) throw (RuntimeException, std::exception)
89 mnLeft = mnRight;
90 mnTop = mnBottom;
95 void SAL_CALL CellCursor::gotoNext( ) throw (RuntimeException, std::exception)
97 if( mxTable.is() )
99 mnRight++;
100 if( mnRight >= mxTable->getColumnCount() )
102 // if we past the last column, try skip to the row line
103 mnTop++;
104 if( mnTop >= mxTable->getRowCount() )
106 // if we past the last row, do not move cursor at all
107 mnTop--;
108 mnRight--;
110 else
112 // restart at the first column on the next row
113 mnRight = 0;
118 mnLeft = mnRight;
119 mnTop = mnBottom;
124 void SAL_CALL CellCursor::gotoPrevious( ) throw (RuntimeException, std::exception)
126 if( mxTable.is() )
128 if( mnLeft > 0 )
130 --mnLeft;
132 else if( mnTop > 0 )
134 --mnTop;
135 mnLeft = mxTable->getColumnCount() - 1;
139 mnRight = mnLeft;
140 mnBottom = mnTop;
145 void SAL_CALL CellCursor::gotoOffset( ::sal_Int32 nColumnOffset, ::sal_Int32 nRowOffset ) throw (RuntimeException, std::exception)
147 if( mxTable.is() )
149 const sal_Int32 nLeft = mnLeft + nColumnOffset;
150 if( (nLeft >= 0) && (nLeft < mxTable->getColumnCount() ) )
151 mnRight = mnLeft = nLeft;
153 const sal_Int32 nTop = mnTop + nRowOffset;
154 if( (nTop >= 0) && (nTop < mxTable->getRowCount()) )
155 mnTop = mnBottom = nTop;
160 // XMergeableCellCursor
163 /** returns true and the merged cell positions if a merge is valid or false if a merge is
164 not valid for that range */
165 bool CellCursor::GetMergedSelection( CellPos& rStart, CellPos& rEnd )
167 rStart.mnCol = mnLeft; rStart.mnRow = mnTop;
168 rEnd.mnCol = mnRight; rEnd.mnRow = mnBottom;
170 // single cell merge is never valid
171 if( mxTable.is() && ((mnLeft != mnRight) || (mnTop != mnBottom)) ) try
173 CellRef xCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( mnLeft, mnTop ).get() ) );
175 // check if first cell is merged
176 if( xCell.is() && xCell->isMerged() )
177 findMergeOrigin( mxTable, mnLeft, mnTop, rStart.mnCol, rStart.mnRow );
179 // check if last cell is merged
180 xCell.set( dynamic_cast< Cell* >( mxTable->getCellByPosition( mnRight, mnBottom ).get() ) );
181 if( xCell.is() )
183 if( xCell->isMerged() )
185 findMergeOrigin( mxTable, mnRight, mnBottom, rEnd.mnCol, rEnd.mnRow );
186 // merge not possible if selection is only one cell and all its merges
187 if( rEnd == rStart )
188 return false;
189 xCell.set( dynamic_cast< Cell* >( mxTable->getCellByPosition( rEnd.mnCol, rEnd.mnRow ).get() ) );
192 if( xCell.is() )
194 rEnd.mnCol += xCell->getColumnSpan()-1;
195 rEnd.mnRow += xCell->getRowSpan()-1;
198 // now check if everything is inside the given bounds
199 sal_Int32 nRow, nCol;
200 for( nRow = rStart.mnRow; nRow <= rEnd.mnRow; nRow++ )
202 for( nCol = rStart.mnCol; nCol <= rEnd.mnCol; nCol++ )
204 xCell.set( dynamic_cast< Cell* >( mxTable->getCellByPosition( nCol, nRow ).get() ) );
205 if( !xCell.is() )
206 continue;
208 if( xCell->isMerged() )
210 sal_Int32 nOriginCol, nOriginRow;
211 if( findMergeOrigin( mxTable, nCol, nRow, nOriginCol, nOriginRow ) )
213 if( (nOriginCol < rStart.mnCol) || (nOriginRow < rStart.mnRow) )
214 return false;
216 xCell.set( dynamic_cast< Cell* >( mxTable->getCellByPosition( nOriginCol, nOriginRow ).get() ) );
217 if( xCell.is() )
219 nOriginCol += xCell->getColumnSpan()-1;
220 nOriginRow += xCell->getRowSpan()-1;
222 if( (nOriginCol > rEnd.mnCol) || (nOriginRow > rEnd.mnRow) )
223 return false;
227 else if( ((nCol + xCell->getColumnSpan() - 1) > rEnd.mnCol) || ((nRow + xCell->getRowSpan() - 1 ) > rEnd.mnRow) )
229 return false;
233 return true;
235 catch( Exception& )
237 OSL_FAIL("sdr::table::SvmxTableController::GetMergedSelection(), exception caught!");
239 return false;
244 void SAL_CALL CellCursor::merge( ) throw (NoSupportException, RuntimeException, std::exception)
246 CellPos aStart, aEnd;
247 if( !GetMergedSelection( aStart, aEnd ) )
248 throw NoSupportException();
250 if( !mxTable.is() || (mxTable->getSdrTableObj() == 0) )
251 throw DisposedException();
253 SdrModel* pModel = mxTable->getSdrTableObj()->GetModel();
254 const bool bUndo = pModel && mxTable->getSdrTableObj()->IsInserted() && pModel->IsUndoEnabled();
256 if( bUndo )
257 pModel->BegUndo( ImpGetResStr(STR_TABLE_MERGE) );
261 mxTable->merge( aStart.mnCol, aStart.mnRow, aEnd.mnCol - aStart.mnCol + 1, aEnd.mnRow - aStart.mnRow + 1 );
262 mxTable->optimize();
263 mxTable->setModified(sal_True);
265 catch( Exception& )
267 OSL_FAIL("sdr::table::CellCursor::merge(), exception caught!");
270 if( bUndo )
271 pModel->EndUndo();
273 if( pModel )
274 pModel->SetChanged();
279 void CellCursor::split_column( sal_Int32 nCol, sal_Int32 nColumns, std::vector< sal_Int32 >& rLeftOvers )
281 const sal_Int32 nRowCount = mxTable->getRowCount();
283 sal_Int32 nNewCols = 0, nRow;
285 // first check how many columns we need to add
286 for( nRow = mnTop; nRow <= mnBottom; ++nRow )
288 CellRef xCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( nCol, nRow ).get() ) );
289 if( xCell.is() && !xCell->isMerged() )
290 nNewCols = std::max( nNewCols, nColumns - xCell->getColumnSpan() + 1 - rLeftOvers[nRow] );
293 if( nNewCols > 0 )
295 const OUString sWidth("Width");
296 Reference< XTableColumns > xCols( mxTable->getColumns(), UNO_QUERY_THROW );
297 Reference< XPropertySet > xRefColumn( xCols->getByIndex( nCol ), UNO_QUERY_THROW );
298 sal_Int32 nWidth = 0;
299 xRefColumn->getPropertyValue( sWidth ) >>= nWidth;
300 const sal_Int32 nNewWidth = nWidth / (nNewCols + 1);
302 // reference column gets new width + rounding errors
303 xRefColumn->setPropertyValue( sWidth, Any( nWidth - (nNewWidth * nNewCols) ) );
305 xCols->insertByIndex( nCol + 1, nNewCols );
306 mnRight += nNewCols;
308 // distribute new width
309 for( sal_Int32 nNewCol = nCol + nNewCols; nNewCol > nCol; --nNewCol )
311 Reference< XPropertySet > xNewCol( xCols->getByIndex( nNewCol ), UNO_QUERY_THROW );
312 xNewCol->setPropertyValue( sWidth, Any( nNewWidth ) );
316 for( nRow = 0; nRow < nRowCount; ++nRow )
318 CellRef xCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( nCol, nRow ).get() ) );
319 if( !xCell.is() || xCell->isMerged() )
321 if( nNewCols > 0 )
323 // merged cells are ignored, but newly added columns will be added to leftovers
324 xCell.set( dynamic_cast< Cell* >(mxTable->getCellByPosition( nCol+1, nRow ).get() ) );
325 if( !xCell.is() || !xCell->isMerged() )
326 rLeftOvers[nRow] += nNewCols;
329 else
331 sal_Int32 nRowSpan = xCell->getRowSpan() - 1;
332 sal_Int32 nColSpan = xCell->getColumnSpan() - 1;
334 if( (nRow >= mnTop) && (nRow <= mnBottom) )
336 sal_Int32 nCellsAvailable = 1 + nColSpan + rLeftOvers[nRow];
337 if( nColSpan == 0 )
338 nCellsAvailable += nNewCols;
340 DBG_ASSERT( nCellsAvailable > nColumns, "sdr::table::CellCursor::split_column(), somethings wrong" );
342 sal_Int32 nSplitSpan = (nCellsAvailable / (nColumns + 1)) - 1;
344 sal_Int32 nSplitCol = nCol;
345 sal_Int32 nSplits = nColumns + 1;
346 while( nSplits-- )
348 // last split eats rounding cells
349 if( nSplits == 0 )
350 nSplitSpan = nCellsAvailable - ((nSplitSpan+1) * nColumns) - 1;
352 mxTable->merge( nSplitCol, nRow, nSplitSpan + 1, nRowSpan + 1);
353 if( nSplits > 0 )
354 nSplitCol += nSplitSpan + 1;
359 rLeftOvers[nRow++] = 0;
361 while( nRowSpan-- );
362 --nRow;
364 else
366 // cope with outside cells, merge if needed
367 if( nColSpan < (rLeftOvers[nRow] + nNewCols) )
368 mxTable->merge( nCol, nRow, (rLeftOvers[nRow] + nNewCols) + 1, nRowSpan + 1 );
372 rLeftOvers[nRow++] = 0; // consumed
374 while( nRowSpan-- );
375 --nRow;
383 void CellCursor::split_horizontal( sal_Int32 nColumns )
385 const sal_Int32 nRowCount = mxTable->getRowCount();
387 std::vector< sal_Int32 > aLeftOvers( nRowCount );
389 for( sal_Int32 nCol = mnRight; nCol >= mnLeft; --nCol )
390 split_column( nCol, nColumns, aLeftOvers );
395 void CellCursor::split_row( sal_Int32 nRow, sal_Int32 nRows, std::vector< sal_Int32 >& rLeftOvers )
397 const sal_Int32 nColCount = mxTable->getColumnCount();
399 sal_Int32 nNewRows = 0, nCol;
401 // first check how many columns we need to add
402 for( nCol = mnLeft; nCol <= mnRight; ++nCol )
404 CellRef xCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( nCol, nRow ).get() ) );
405 if( xCell.is() && !xCell->isMerged() )
406 nNewRows = std::max( nNewRows, nRows - xCell->getRowSpan() + 1 - rLeftOvers[nCol] );
409 if( nNewRows > 0 )
411 const OUString sHeight("Height");
412 Reference< XTableRows > xRows( mxTable->getRows(), UNO_QUERY_THROW );
413 Reference< XPropertySet > xRefRow( xRows->getByIndex( nRow ), UNO_QUERY_THROW );
414 sal_Int32 nHeight = 0;
415 xRefRow->getPropertyValue( sHeight ) >>= nHeight;
416 const sal_Int32 nNewHeight = nHeight / (nNewRows + 1);
418 // reference row gets new height + rounding errors
419 xRefRow->setPropertyValue( sHeight, Any( nHeight - (nNewHeight * nNewRows) ) );
421 xRows->insertByIndex( nRow + 1, nNewRows );
422 mnBottom += nNewRows;
424 // distribute new width
425 for( sal_Int32 nNewRow = nRow + nNewRows; nNewRow > nRow; --nNewRow )
427 Reference< XPropertySet > xNewRow( xRows->getByIndex( nNewRow ), UNO_QUERY_THROW );
428 xNewRow->setPropertyValue( sHeight, Any( nNewHeight ) );
432 for( nCol = 0; nCol < nColCount; ++nCol )
434 CellRef xCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( nCol, nRow ).get() ) );
435 if( !xCell.is() || xCell->isMerged() )
437 if( nNewRows )
439 // merged cells are ignored, but newly added columns will be added to leftovers
440 xCell.set( dynamic_cast< Cell* >(mxTable->getCellByPosition( nCol, nRow+1 ).get() ) );
441 if( !xCell.is() || !xCell->isMerged() )
442 rLeftOvers[nCol] += nNewRows;
445 else
447 sal_Int32 nRowSpan = xCell->getRowSpan() - 1;
448 sal_Int32 nColSpan = xCell->getColumnSpan() - 1;
450 if( (nCol >= mnLeft) && (nCol <= mnRight) )
452 sal_Int32 nCellsAvailable = 1 + nRowSpan + rLeftOvers[nCol];
453 if( nRowSpan == 0 )
454 nCellsAvailable += nNewRows;
456 DBG_ASSERT( nCellsAvailable > nRows, "sdr::table::CellCursor::split_row(), somethings wrong" );
458 sal_Int32 nSplitSpan = (nCellsAvailable / (nRows + 1)) - 1;
460 sal_Int32 nSplitRow = nRow;
461 sal_Int32 nSplits = nRows + 1;
462 while( nSplits-- )
464 // last split eats rounding cells
465 if( nSplits == 0 )
466 nSplitSpan = nCellsAvailable - ((nSplitSpan+1) * nRows) - 1;
468 mxTable->merge( nCol, nSplitRow, nColSpan + 1, nSplitSpan + 1 );
469 if( nSplits > 0 )
470 nSplitRow += nSplitSpan + 1;
475 rLeftOvers[nCol++] = 0;
477 while( nColSpan-- );
478 --nCol;
480 else
482 // cope with outside cells, merge if needed
483 if( nRowSpan < (rLeftOvers[nCol] + nNewRows) )
484 mxTable->merge( nCol, nRow, nColSpan + 1, (rLeftOvers[nCol] + nNewRows) + 1 );
488 rLeftOvers[nCol++] = 0; // consumed
490 while( nColSpan-- );
491 --nCol;
499 void CellCursor::split_vertical( sal_Int32 nRows )
501 const sal_Int32 nColCount = mxTable->getColumnCount();
503 std::vector< sal_Int32 > aLeftOvers( nColCount );
505 for( sal_Int32 nRow = mnBottom; nRow >= mnTop; --nRow )
506 split_row( nRow, nRows, aLeftOvers );
511 void SAL_CALL CellCursor::split( sal_Int32 nColumns, sal_Int32 nRows ) throw (NoSupportException, IllegalArgumentException, RuntimeException, std::exception)
513 if( (nColumns < 0) || (nRows < 0) )
514 throw IllegalArgumentException();
516 if( !mxTable.is() || (mxTable->getSdrTableObj() == 0) )
517 throw DisposedException();
519 SdrModel* pModel = mxTable->getSdrTableObj()->GetModel();
520 const bool bUndo = pModel && mxTable->getSdrTableObj()->IsInserted() && pModel->IsUndoEnabled();
521 if( bUndo )
522 pModel->BegUndo( ImpGetResStr(STR_TABLE_SPLIT) );
526 if( nColumns > 0 )
527 split_horizontal( nColumns );
529 if( nRows > 0 )
530 split_vertical( nRows );
532 if( nColumns > 0 ||nRows > 0 )
533 mxTable->setModified(sal_True);
535 catch( Exception& )
537 OSL_FAIL("sdr::table::CellCursor::split(), exception caught!");
538 throw NoSupportException();
541 if( bUndo )
542 pModel->EndUndo();
544 if( pModel )
545 pModel->SetChanged();
550 sal_Bool SAL_CALL CellCursor::isMergeable( ) throw (RuntimeException, std::exception)
552 CellPos aStart, aEnd;
553 return GetMergedSelection( aStart, aEnd ) ? sal_True : sal_False;
560 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */