1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <sal/config.h>
#include <com/sun/star/lang/NoSupportException.hpp>
#include <svx/svdotable.hxx>
#include "cellcursor.hxx"
#include "tablelayouter.hxx"
#include <cell.hxx>
#include <svx/svdmodel.hxx>
#include <svx/strings.hrc>
#include <svx/dialmgr.hxx>
#include <tools/debug.hxx>
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::container;
using namespace ::com::sun::star::beans;
using namespace ::com::sun::star::table;
namespace sdr::table {
CellCursor::CellCursor( const TableModelRef & xTable, sal_Int32 nLeft, sal_Int32 nTop, sal_Int32 nRight, sal_Int32 nBottom )
: CellCursorBase( xTable, nLeft, nTop, nRight, nBottom )
{
}
CellCursor::~CellCursor()
{
}
// XCellCursor
Reference< XCell > SAL_CALL CellCursor::getCellByPosition( sal_Int32 nColumn, sal_Int32 nRow )
{
return CellRange::getCellByPosition( nColumn, nRow );
}
Reference< XCellRange > SAL_CALL CellCursor::getCellRangeByPosition( sal_Int32 nLeft, sal_Int32 nTop, sal_Int32 nRight, sal_Int32 nBottom )
{
return CellRange::getCellRangeByPosition( nLeft, nTop, nRight, nBottom );
}
Reference< XCellRange > SAL_CALL CellCursor::getCellRangeByName( const OUString& aRange )
{
return CellRange::getCellRangeByName( aRange );
}
// XCellCursor
void SAL_CALL CellCursor::gotoStart( )
{
mnRight = mnLeft;
mnBottom = mnTop;
}
void SAL_CALL CellCursor::gotoEnd( )
{
mnLeft = mnRight;
mnTop = mnBottom;
}
void SAL_CALL CellCursor::gotoNext( )
{
if( mxTable.is() )
{
mnRight++;
if( mnRight >= mxTable->getColumnCount() )
{
// if we past the last column, try skip to the row line
mnTop++;
if( mnTop >= mxTable->getRowCount() )
{
// if we past the last row, do not move cursor at all
mnTop--;
mnRight--;
}
else
{
// restart at the first column on the next row
mnRight = 0;
}
}
}
mnLeft = mnRight;
mnTop = mnBottom;
}
void SAL_CALL CellCursor::gotoPrevious( )
{
if( mxTable.is() )
{
if( mnLeft > 0 )
{
--mnLeft;
}
else if( mnTop > 0 )
{
--mnTop;
mnLeft = mxTable->getColumnCount() - 1;
}
}
mnRight = mnLeft;
mnBottom = mnTop;
}
void SAL_CALL CellCursor::gotoOffset( ::sal_Int32 nColumnOffset, ::sal_Int32 nRowOffset )
{
if( mxTable.is() )
{
const sal_Int32 nLeft = mnLeft + nColumnOffset;
if( (nLeft >= 0) && (nLeft < mxTable->getColumnCount() ) )
mnRight = mnLeft = nLeft;
const sal_Int32 nTop = mnTop + nRowOffset;
if( (nTop >= 0) && (nTop < mxTable->getRowCount()) )
mnTop = mnBottom = nTop;
}
}
// XMergeableCellCursor
/** returns true and the merged cell positions if a merge is valid or false if a merge is
not valid for that range */
bool CellCursor::GetMergedSelection( CellPos& rStart, CellPos& rEnd )
{
rStart.mnCol = mnLeft; rStart.mnRow = mnTop;
rEnd.mnCol = mnRight; rEnd.mnRow = mnBottom;
// single cell merge is never valid
if( mxTable.is() && ((mnLeft != mnRight) || (mnTop != mnBottom)) ) try
{
CellRef xCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( mnLeft, mnTop ).get() ) );
// check if first cell is merged
if( xCell.is() && xCell->isMerged() )
findMergeOrigin( mxTable, mnLeft, mnTop, rStart.mnCol, rStart.mnRow );
// check if last cell is merged
xCell.set( dynamic_cast< Cell* >( mxTable->getCellByPosition( mnRight, mnBottom ).get() ) );
if( xCell.is() )
{
if( xCell->isMerged() )
{
findMergeOrigin( mxTable, mnRight, mnBottom, rEnd.mnCol, rEnd.mnRow );
// merge not possible if selection is only one cell and all its merges
if( rEnd == rStart )
return false;
xCell.set( dynamic_cast< Cell* >( mxTable->getCellByPosition( rEnd.mnCol, rEnd.mnRow ).get() ) );
}
}
if( xCell.is() )
{
rEnd.mnCol += xCell->getColumnSpan()-1;
rEnd.mnRow += xCell->getRowSpan()-1;
}
// now check if everything is inside the given bounds
sal_Int32 nRow, nCol;
for( nRow = rStart.mnRow; nRow <= rEnd.mnRow; nRow++ )
{
for( nCol = rStart.mnCol; nCol <= rEnd.mnCol; nCol++ )
{
xCell.set( dynamic_cast< Cell* >( mxTable->getCellByPosition( nCol, nRow ).get() ) );
if( !xCell.is() )
continue;
if( xCell->isMerged() )
{
sal_Int32 nOriginCol, nOriginRow;
if( findMergeOrigin( mxTable, nCol, nRow, nOriginCol, nOriginRow ) )
{
if( (nOriginCol < rStart.mnCol) || (nOriginRow < rStart.mnRow) )
return false;
xCell.set( dynamic_cast< Cell* >( mxTable->getCellByPosition( nOriginCol, nOriginRow ).get() ) );
if( xCell.is() )
{
nOriginCol += xCell->getColumnSpan()-1;
nOriginRow += xCell->getRowSpan()-1;
if( (nOriginCol > rEnd.mnCol) || (nOriginRow > rEnd.mnRow) )
return false;
}
}
}
else if( ((nCol + xCell->getColumnSpan() - 1) > rEnd.mnCol) || ((nRow + xCell->getRowSpan() - 1 ) > rEnd.mnRow) )
{
return false;
}
}
}
return true;
}
catch( Exception& )
{
OSL_FAIL("sdr::table::SvmxTableController::GetMergedSelection(), exception caught!");
}
return false;
}
void SAL_CALL CellCursor::merge( )
{
CellPos aStart, aEnd;
if( !GetMergedSelection( aStart, aEnd ) )
throw NoSupportException();
if( !mxTable.is() || (mxTable->getSdrTableObj() == nullptr) )
throw DisposedException();
SdrModel& rModel(mxTable->getSdrTableObj()->getSdrModelFromSdrObject());
const bool bUndo(mxTable->getSdrTableObj()->IsInserted() && rModel.IsUndoEnabled());
if( bUndo )
rModel.BegUndo( SvxResId(STR_TABLE_MERGE) );
try
{
mxTable->merge( aStart.mnCol, aStart.mnRow, aEnd.mnCol - aStart.mnCol + 1, aEnd.mnRow - aStart.mnRow + 1 );
mxTable->optimize();
mxTable->setModified(true);
}
catch( Exception& )
{
OSL_FAIL("sdr::table::CellCursor::merge(), exception caught!");
}
if( bUndo )
rModel.EndUndo();
rModel.SetChanged();
}
void CellCursor::split_column( sal_Int32 nCol, sal_Int32 nColumns, std::vector< sal_Int32 >& rLeftOvers )
{
const sal_Int32 nRowCount = mxTable->getRowCount();
sal_Int32 nNewCols = 0, nRow;
// first check how many columns we need to add
for( nRow = mnTop; nRow <= mnBottom; ++nRow )
{
CellRef xCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( nCol, nRow ).get() ) );
if( xCell.is() && !xCell->isMerged() )
nNewCols = std::max( nNewCols, nColumns - xCell->getColumnSpan() + 1 - rLeftOvers[nRow] );
}
if( nNewCols > 0 )
{
const OUString sWidth("Width");
Reference< XTableColumns > xCols( mxTable->getColumns(), UNO_SET_THROW );
Reference< XPropertySet > xRefColumn( xCols->getByIndex( nCol ), UNO_QUERY_THROW );
sal_Int32 nWidth = 0;
xRefColumn->getPropertyValue( sWidth ) >>= nWidth;
const sal_Int32 nNewWidth = nWidth / (nNewCols + 1);
// reference column gets new width + rounding errors
xRefColumn->setPropertyValue( sWidth, Any( nWidth - (nNewWidth * nNewCols) ) );
xCols->insertByIndex( nCol + 1, nNewCols );
mnRight += nNewCols;
// distribute new width
for( sal_Int32 nNewCol = nCol + nNewCols; nNewCol > nCol; --nNewCol )
{
Reference< XPropertySet > xNewCol( xCols->getByIndex( nNewCol ), UNO_QUERY_THROW );
xNewCol->setPropertyValue( sWidth, Any( nNewWidth ) );
}
}
for( nRow = 0; nRow < nRowCount; ++nRow )
{
CellRef xCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( nCol, nRow ).get() ) );
if( !xCell.is() || xCell->isMerged() )
{
if( nNewCols > 0 )
{
// merged cells are ignored, but newly added columns will be added to leftovers
xCell.set( dynamic_cast< Cell* >(mxTable->getCellByPosition( nCol+1, nRow ).get() ) );
if( !xCell.is() || !xCell->isMerged() )
rLeftOvers[nRow] += nNewCols;
}
}
else
{
sal_Int32 nRowSpan = xCell->getRowSpan() - 1;
sal_Int32 nColSpan = xCell->getColumnSpan() - 1;
if( (nRow >= mnTop) && (nRow <= mnBottom) )
{
sal_Int32 nCellsAvailable = 1 + nColSpan + rLeftOvers[nRow];
if( nColSpan == 0 )
nCellsAvailable += nNewCols;
DBG_ASSERT( nCellsAvailable > nColumns, "sdr::table::CellCursor::split_column(), somethings wrong" );
sal_Int32 nSplitSpan = (nCellsAvailable / (nColumns + 1)) - 1;
sal_Int32 nSplitCol = nCol;
sal_Int32 nSplits = nColumns + 1;
while( nSplits-- )
{
// last split eats rounding cells
if( nSplits == 0 )
nSplitSpan = nCellsAvailable - ((nSplitSpan+1) * nColumns) - 1;
mxTable->merge( nSplitCol, nRow, nSplitSpan + 1, nRowSpan + 1);
if( nSplits > 0 )
nSplitCol += nSplitSpan + 1;
}
do
{
rLeftOvers[nRow++] = 0;
}
while( nRowSpan-- );
--nRow;
}
else
{
// cope with outside cells, merge if needed
if( nColSpan < (rLeftOvers[nRow] + nNewCols) )
mxTable->merge( nCol, nRow, (rLeftOvers[nRow] + nNewCols) + 1, nRowSpan + 1 );
do
{
rLeftOvers[nRow++] = 0; // consumed
}
while( nRowSpan-- );
--nRow;
}
}
}
}
void CellCursor::split_horizontal( sal_Int32 nColumns )
{
const sal_Int32 nRowCount = mxTable->getRowCount();
std::vector< sal_Int32 > aLeftOvers( nRowCount );
for( sal_Int32 nCol = mnRight; nCol >= mnLeft; --nCol )
split_column( nCol, nColumns, aLeftOvers );
}
void CellCursor::split_row( sal_Int32 nRow, sal_Int32 nRows, std::vector< sal_Int32 >& rLeftOvers )
{
const sal_Int32 nColCount = mxTable->getColumnCount();
sal_Int32 nNewRows = 0, nCol;
// first check how many columns we need to add
for( nCol = mnLeft; nCol <= mnRight; ++nCol )
{
CellRef xCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( nCol, nRow ).get() ) );
if( xCell.is() && !xCell->isMerged() )
nNewRows = std::max( nNewRows, nRows - xCell->getRowSpan() + 1 - rLeftOvers[nCol] );
}
if( nNewRows > 0 )
{
const OUString sHeight("Height");
Reference< XTableRows > xRows( mxTable->getRows(), UNO_SET_THROW );
Reference< XPropertySet > xRefRow( xRows->getByIndex( nRow ), UNO_QUERY_THROW );
sal_Int32 nHeight = 0;
xRefRow->getPropertyValue( sHeight ) >>= nHeight;
const sal_Int32 nNewHeight = nHeight / (nNewRows + 1);
// reference row gets new height + rounding errors
xRefRow->setPropertyValue( sHeight, Any( nHeight - (nNewHeight * nNewRows) ) );
xRows->insertByIndex( nRow + 1, nNewRows );
mnBottom += nNewRows;
// distribute new width
for( sal_Int32 nNewRow = nRow + nNewRows; nNewRow > nRow; --nNewRow )
{
Reference< XPropertySet > xNewRow( xRows->getByIndex( nNewRow ), UNO_QUERY_THROW );
xNewRow->setPropertyValue( sHeight, Any( nNewHeight ) );
}
}
for( nCol = 0; nCol < nColCount; ++nCol )
{
CellRef xCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( nCol, nRow ).get() ) );
if( !xCell.is() || xCell->isMerged() )
{
if( nNewRows )
{
// merged cells are ignored, but newly added columns will be added to leftovers
xCell.set( dynamic_cast< Cell* >(mxTable->getCellByPosition( nCol, nRow+1 ).get() ) );
if( !xCell.is() || !xCell->isMerged() )
rLeftOvers[nCol] += nNewRows;
}
}
else
{
sal_Int32 nRowSpan = xCell->getRowSpan() - 1;
sal_Int32 nColSpan = xCell->getColumnSpan() - 1;
if( (nCol >= mnLeft) && (nCol <= mnRight) )
{
sal_Int32 nCellsAvailable = 1 + nRowSpan + rLeftOvers[nCol];
if( nRowSpan == 0 )
nCellsAvailable += nNewRows;
DBG_ASSERT( nCellsAvailable > nRows, "sdr::table::CellCursor::split_row(), somethings wrong" );
sal_Int32 nSplitSpan = (nCellsAvailable / (nRows + 1)) - 1;
sal_Int32 nSplitRow = nRow;
sal_Int32 nSplits = nRows + 1;
while( nSplits-- )
{
// last split eats rounding cells
if( nSplits == 0 )
nSplitSpan = nCellsAvailable - ((nSplitSpan+1) * nRows) - 1;
mxTable->merge( nCol, nSplitRow, nColSpan + 1, nSplitSpan + 1 );
if( nSplits > 0 )
nSplitRow += nSplitSpan + 1;
}
do
{
rLeftOvers[nCol++] = 0;
}
while( nColSpan-- );
--nCol;
}
else
{
// cope with outside cells, merge if needed
if( nRowSpan < (rLeftOvers[nCol] + nNewRows) )
mxTable->merge( nCol, nRow, nColSpan + 1, (rLeftOvers[nCol] + nNewRows) + 1 );
do
{
rLeftOvers[nCol++] = 0; // consumed
}
while( nColSpan-- );
--nCol;
}
}
}
}
void CellCursor::split_vertical( sal_Int32 nRows )
{
const sal_Int32 nColCount = mxTable->getColumnCount();
std::vector< sal_Int32 > aLeftOvers( nColCount );
for( sal_Int32 nRow = mnBottom; nRow >= mnTop; --nRow )
split_row( nRow, nRows, aLeftOvers );
}
void SAL_CALL CellCursor::split( sal_Int32 nColumns, sal_Int32 nRows )
{
if( (nColumns < 0) || (nRows < 0) )
throw IllegalArgumentException();
if( !mxTable.is() || (mxTable->getSdrTableObj() == nullptr) )
throw DisposedException();
SdrModel& rModel(mxTable->getSdrTableObj()->getSdrModelFromSdrObject());
const bool bUndo(mxTable->getSdrTableObj()->IsInserted() && rModel.IsUndoEnabled());
if( bUndo )
rModel.BegUndo( SvxResId(STR_TABLE_SPLIT) );
try
{
if( nColumns > 0 )
split_horizontal( nColumns );
if( nRows > 0 )
split_vertical( nRows );
if( nColumns > 0 ||nRows > 0 )
mxTable->setModified(true);
}
catch( Exception& )
{
OSL_FAIL("sdr::table::CellCursor::split(), exception caught!");
throw NoSupportException();
}
if( bUndo )
rModel.EndUndo();
rModel.SetChanged();
}
sal_Bool SAL_CALL CellCursor::isMergeable( )
{
CellPos aStart, aEnd;
return GetMergedSelection( aStart, aEnd );
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|