Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / svx / source / table / tableundo.cxx
blob358691c77a6787160e923bff92a679075a6a0f4c
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/sdr/properties/textproperties.hxx"
22 #include "editeng/outlobj.hxx"
24 #include "cell.hxx"
25 #include "tableundo.hxx"
26 #include "svx/svdotable.hxx"
27 #include "tablerow.hxx"
28 #include "tablecolumn.hxx"
33 using namespace ::com::sun::star::uno;
34 using namespace ::com::sun::star::table;
38 namespace sdr { namespace table {
40 CellUndo::CellUndo( const SdrObjectWeakRef& xObjRef, const CellRef& xCell )
41 : SdrUndoAction( *xCell->GetModel() )
42 , mxObjRef( xObjRef )
43 , mxCell( xCell )
44 , mbUndo( true )
46 if( mxCell.is() && mxObjRef.is() )
48 getDataFromCell( maUndoData );
49 mxObjRef->AddObjectUser( *this );
53 CellUndo::~CellUndo()
55 if( mxObjRef.is() )
56 mxObjRef->RemoveObjectUser( *this );
57 dispose();
60 void CellUndo::dispose()
62 mxCell.clear();
63 delete maUndoData.mpProperties;
64 maUndoData.mpProperties = 0;
65 delete maRedoData.mpProperties;
66 maRedoData.mpProperties = 0;
67 delete maUndoData.mpOutlinerParaObject;
68 maUndoData.mpOutlinerParaObject = 0;
69 delete maRedoData.mpOutlinerParaObject;
70 maRedoData.mpOutlinerParaObject = 0;
73 void CellUndo::ObjectInDestruction(const SdrObject& )
75 dispose();
78 void CellUndo::Undo()
80 if( mxCell.is() && mbUndo )
82 if( maRedoData.mpProperties == 0 )
83 getDataFromCell( maRedoData );
85 setDataToCell( maUndoData );
86 mbUndo = false;
90 void CellUndo::Redo()
92 if( mxCell.is() && !mbUndo )
94 setDataToCell( maRedoData );
95 mbUndo = true;
99 bool CellUndo::Merge( SfxUndoAction *pNextAction )
101 CellUndo* pNext = dynamic_cast< CellUndo* >( pNextAction );
102 if( pNext && pNext->mxCell.get() == mxCell.get() )
104 return true;
106 else
108 return false;
112 void CellUndo::setDataToCell( const Data& rData )
114 delete mxCell->mpProperties;
115 if( rData.mpProperties )
116 mxCell->mpProperties = Cell::CloneProperties( rData.mpProperties, *mxObjRef.get(), *mxCell.get() );
117 else
118 mxCell->mpProperties = 0;
120 if( rData.mpOutlinerParaObject )
121 mxCell->SetOutlinerParaObject( new OutlinerParaObject(*rData.mpOutlinerParaObject) );
122 else
123 mxCell->RemoveOutlinerParaObject();
125 mxCell->msFormula = rData.msFormula;
126 mxCell->mfValue = rData.mfValue;
127 mxCell->mnError = rData.mnError;
128 mxCell->mbMerged = rData.mbMerged;
129 mxCell->mnRowSpan = rData.mnRowSpan;
130 mxCell->mnColSpan = rData.mnColSpan;
132 if(mxObjRef.is())
134 // #i120201# ActionChanged is not enough, we need to trigger TableLayouter::UpdateBorderLayout()
135 // and this is done best using ReformatText() for table objects
136 mxObjRef->ActionChanged();
137 mxObjRef->NbcReformatText();
141 void CellUndo::getDataFromCell( Data& rData )
143 if( mxObjRef.is() && mxCell.is() )
145 if( mxCell->mpProperties )
146 rData.mpProperties = mxCell->CloneProperties( *mxObjRef.get(), *mxCell.get());
148 if( mxCell->GetOutlinerParaObject() )
149 rData.mpOutlinerParaObject = new OutlinerParaObject(*mxCell->GetOutlinerParaObject());
150 else
151 rData.mpOutlinerParaObject = 0;
153 rData.mnCellContentType = mxCell->mnCellContentType;
155 rData.msFormula = mxCell->msFormula;
156 rData.mfValue = mxCell->mfValue;
157 rData.mnError = mxCell->mnError;
158 rData.mbMerged = mxCell->mbMerged;
159 rData.mnRowSpan = mxCell->mnRowSpan;
160 rData.mnColSpan = mxCell->mnColSpan;
165 // class InsertRowUndo : public SdrUndoAction
168 static void Dispose( RowVector& rRows )
170 RowVector::iterator aIter( rRows.begin() );
171 while( aIter != rRows.end() )
172 (*aIter++)->dispose();
177 InsertRowUndo::InsertRowUndo( const TableModelRef& xTable, sal_Int32 nIndex, RowVector& aNewRows )
178 : SdrUndoAction( *xTable->getSdrTableObj()->GetModel() )
179 , mxTable( xTable )
180 , mnIndex( nIndex )
181 , mbUndo( true )
183 maRows.swap( aNewRows );
188 InsertRowUndo::~InsertRowUndo()
190 if( !mbUndo )
191 Dispose( maRows );
196 void InsertRowUndo::Undo()
198 if( mxTable.is() )
200 mxTable->UndoInsertRows( mnIndex, sal::static_int_cast< sal_Int32 >( maRows.size() ) );
201 mbUndo = false;
207 void InsertRowUndo::Redo()
209 if( mxTable.is() )
211 mxTable->UndoRemoveRows( mnIndex, maRows );
212 mbUndo = true;
217 // class RemoveRowUndo : public SdrUndoAction
220 RemoveRowUndo::RemoveRowUndo( const TableModelRef& xTable, sal_Int32 nIndex, RowVector& aRemovedRows )
221 : SdrUndoAction( *xTable->getSdrTableObj()->GetModel() )
222 , mxTable( xTable )
223 , mnIndex( nIndex )
224 , mbUndo( true )
226 maRows.swap( aRemovedRows );
231 RemoveRowUndo::~RemoveRowUndo()
233 if( mbUndo )
234 Dispose( maRows );
239 void RemoveRowUndo::Undo()
241 if( mxTable.is() )
243 mxTable->UndoRemoveRows( mnIndex, maRows );
244 mbUndo = false;
250 void RemoveRowUndo::Redo()
252 if( mxTable.is() )
254 mxTable->UndoInsertRows( mnIndex, sal::static_int_cast< sal_Int32 >( maRows.size() ) );
255 mbUndo = true;
260 // class InsertColUndo : public SdrUndoAction
263 static void Dispose( ColumnVector& rCols )
265 ColumnVector::iterator aIter( rCols.begin() );
266 while( aIter != rCols.end() )
267 (*aIter++)->dispose();
272 static void Dispose( CellVector& rCells )
274 CellVector::iterator aIter( rCells.begin() );
275 while( aIter != rCells.end() )
276 (*aIter++)->dispose();
281 InsertColUndo::InsertColUndo( const TableModelRef& xTable, sal_Int32 nIndex, ColumnVector& aNewCols, CellVector& aCells )
282 : SdrUndoAction( *xTable->getSdrTableObj()->GetModel() )
283 , mxTable( xTable )
284 , mnIndex( nIndex )
285 , mbUndo( true )
287 maColumns.swap( aNewCols );
288 maCells.swap( aCells );
293 InsertColUndo::~InsertColUndo()
295 if( !mbUndo )
297 Dispose( maColumns );
298 Dispose( maCells );
304 void InsertColUndo::Undo()
306 if( mxTable.is() )
308 mxTable->UndoInsertColumns( mnIndex, sal::static_int_cast< sal_Int32 >( maColumns.size() ) );
309 mbUndo = false;
315 void InsertColUndo::Redo()
317 if( mxTable.is() )
319 mxTable->UndoRemoveColumns( mnIndex, maColumns, maCells );
320 mbUndo = true;
325 // class RemoveColUndo : public SdrUndoAction
328 RemoveColUndo::RemoveColUndo( const TableModelRef& xTable, sal_Int32 nIndex, ColumnVector& aNewCols, CellVector& aCells )
329 : SdrUndoAction( *xTable->getSdrTableObj()->GetModel() )
330 , mxTable( xTable )
331 , mnIndex( nIndex )
332 , mbUndo( true )
334 maColumns.swap( aNewCols );
335 maCells.swap( aCells );
340 RemoveColUndo::~RemoveColUndo()
342 if( mbUndo )
344 Dispose( maColumns );
345 Dispose( maCells );
351 void RemoveColUndo::Undo()
353 if( mxTable.is() )
355 mxTable->UndoRemoveColumns( mnIndex, maColumns, maCells );
356 mbUndo = false;
362 void RemoveColUndo::Redo()
364 if( mxTable.is() )
366 mxTable->UndoInsertColumns( mnIndex, sal::static_int_cast< sal_Int32 >( maColumns.size() ) );
367 mbUndo = true;
372 // class TableColumnUndo : public SdrUndoAction
375 TableColumnUndo::TableColumnUndo( const TableColumnRef& xCol )
376 : SdrUndoAction( *xCol->mxTableModel->getSdrTableObj()->GetModel() )
377 , mxCol( xCol )
378 , mbHasRedoData( false )
380 getData( maUndoData );
385 TableColumnUndo::~TableColumnUndo()
391 void TableColumnUndo::Undo()
393 if( !mbHasRedoData )
395 getData( maRedoData );
396 mbHasRedoData = true;
398 setData( maUndoData );
403 void TableColumnUndo::Redo()
405 setData( maRedoData );
410 bool TableColumnUndo::Merge( SfxUndoAction *pNextAction )
412 TableColumnUndo* pNext = dynamic_cast< TableColumnUndo* >( pNextAction );
413 return pNext && pNext->mxCol == mxCol;
418 void TableColumnUndo::setData( const Data& rData )
420 mxCol->mnColumn = rData.mnColumn;
421 mxCol->mnWidth = rData.mnWidth;
422 mxCol->mbOptimalWidth = rData.mbOptimalWidth;
423 mxCol->mbIsVisible = rData.mbIsVisible;
424 mxCol->mbIsStartOfNewPage = rData.mbIsStartOfNewPage;
425 mxCol->maName = rData.maName;
430 void TableColumnUndo::getData( Data& rData )
432 rData.mnColumn = mxCol->mnColumn;
433 rData.mnWidth = mxCol->mnWidth;
434 rData.mbOptimalWidth = mxCol->mbOptimalWidth;
435 rData.mbIsVisible = mxCol->mbIsVisible;
436 rData.mbIsStartOfNewPage = mxCol->mbIsStartOfNewPage;
437 rData.maName = mxCol->maName;
441 // class TableRowUndo : public SdrUndoAction
444 TableRowUndo::TableRowUndo( const TableRowRef& xRow )
445 : SdrUndoAction( *xRow->mxTableModel->getSdrTableObj()->GetModel() )
446 , mxRow( xRow )
447 , mbHasRedoData( false )
449 getData( maUndoData );
454 TableRowUndo::~TableRowUndo()
460 void TableRowUndo::Undo()
462 if( !mbHasRedoData )
464 getData( maRedoData );
465 mbHasRedoData = true;
467 setData( maUndoData );
472 void TableRowUndo::Redo()
474 setData( maRedoData );
479 bool TableRowUndo::Merge( SfxUndoAction *pNextAction )
481 TableRowUndo* pNext = dynamic_cast< TableRowUndo* >( pNextAction );
482 return pNext && pNext->mxRow == mxRow;
487 void TableRowUndo::setData( const Data& rData )
489 mxRow->mnRow = rData.mnRow;
490 mxRow->mnHeight = rData.mnHeight;
491 mxRow->mbOptimalHeight = rData.mbOptimalHeight;
492 mxRow->mbIsVisible = rData.mbIsVisible;
493 mxRow->mbIsStartOfNewPage = rData.mbIsStartOfNewPage;
494 mxRow->maName = rData.maName;
499 void TableRowUndo::getData( Data& rData )
501 rData.mnRow = mxRow->mnRow;
502 rData.mnHeight = mxRow->mnHeight;
503 rData.mbOptimalHeight = mxRow->mbOptimalHeight;
504 rData.mbIsVisible = mxRow->mbIsVisible;
505 rData.mbIsStartOfNewPage = mxRow->mbIsStartOfNewPage;
506 rData.maName = mxRow->maName;
511 TableStyleUndo::TableStyleUndo( const SdrTableObj& rTableObj )
512 : SdrUndoAction( *rTableObj.GetModel() )
513 , mxObjRef( const_cast< sdr::table::SdrTableObj*>( &rTableObj ) )
514 , mbHasRedoData(false)
516 getData( maUndoData );
519 void TableStyleUndo::Undo()
521 if( !mbHasRedoData )
523 getData( maRedoData );
524 mbHasRedoData = true;
526 setData( maUndoData );
529 void TableStyleUndo::Redo()
531 setData( maRedoData );
534 void TableStyleUndo::setData( const Data& rData )
536 SdrTableObj* pTableObj = dynamic_cast< SdrTableObj* >( mxObjRef.get() );
537 if( pTableObj )
539 pTableObj->setTableStyle( rData.mxTableStyle );
540 pTableObj->setTableStyleSettings( rData.maSettings );
544 void TableStyleUndo::getData( Data& rData )
546 SdrTableObj* pTableObj = dynamic_cast< SdrTableObj* >( mxObjRef.get() );
547 if( pTableObj )
549 rData.maSettings = pTableObj->getTableStyleSettings();
550 rData.mxTableStyle = pTableObj->getTableStyle();
556 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */