bump product version to 4.1.6.2
[LibreOffice.git] / svx / source / table / tableundo.cxx
blobe889427ef354fe03059f522bc2d749b8af73d963
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"
31 // -----------------------------------------------------------------------------
33 using namespace ::com::sun::star::uno;
34 using namespace ::com::sun::star::table;
36 // -----------------------------------------------------------------------------
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 sal_Bool CellUndo::Merge( SfxUndoAction *pNextAction )
101 CellUndo* pNext = dynamic_cast< CellUndo* >( pNextAction );
102 if( pNext && pNext->mxCell.get() == mxCell.get() )
104 return sal_True;
106 else
108 return sal_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() )
133 mxObjRef->ActionChanged();
136 void CellUndo::getDataFromCell( Data& rData )
138 if( mxObjRef.is() && mxCell.is() )
140 if( mxCell->mpProperties )
141 rData.mpProperties = mxCell->CloneProperties( *mxObjRef.get(), *mxCell.get());
143 if( mxCell->GetOutlinerParaObject() )
144 rData.mpOutlinerParaObject = new OutlinerParaObject(*mxCell->GetOutlinerParaObject());
145 else
146 rData.mpOutlinerParaObject = 0;
148 rData.mnCellContentType = mxCell->mnCellContentType;
150 rData.msFormula = mxCell->msFormula;
151 rData.mfValue = mxCell->mfValue;
152 rData.mnError = mxCell->mnError;
153 rData.mbMerged = mxCell->mbMerged;
154 rData.mnRowSpan = mxCell->mnRowSpan;
155 rData.mnColSpan = mxCell->mnColSpan;
159 // -----------------------------------------------------------------------------
160 // class InsertRowUndo : public SdrUndoAction
161 // -----------------------------------------------------------------------------
163 static void Dispose( RowVector& rRows )
165 RowVector::iterator aIter( rRows.begin() );
166 while( aIter != rRows.end() )
167 (*aIter++)->dispose();
170 // -----------------------------------------------------------------------------
172 InsertRowUndo::InsertRowUndo( const TableModelRef& xTable, sal_Int32 nIndex, RowVector& aNewRows )
173 : SdrUndoAction( *xTable->getSdrTableObj()->GetModel() )
174 , mxTable( xTable )
175 , mnIndex( nIndex )
176 , mbUndo( true )
178 maRows.swap( aNewRows );
181 // -----------------------------------------------------------------------------
183 InsertRowUndo::~InsertRowUndo()
185 if( !mbUndo )
186 Dispose( maRows );
189 // -----------------------------------------------------------------------------
191 void InsertRowUndo::Undo()
193 if( mxTable.is() )
195 mxTable->UndoInsertRows( mnIndex, sal::static_int_cast< sal_Int32 >( maRows.size() ) );
196 mbUndo = false;
200 // -----------------------------------------------------------------------------
202 void InsertRowUndo::Redo()
204 if( mxTable.is() )
206 mxTable->UndoRemoveRows( mnIndex, maRows );
207 mbUndo = true;
211 // -----------------------------------------------------------------------------
212 // class RemoveRowUndo : public SdrUndoAction
213 // -----------------------------------------------------------------------------
215 RemoveRowUndo::RemoveRowUndo( const TableModelRef& xTable, sal_Int32 nIndex, RowVector& aRemovedRows )
216 : SdrUndoAction( *xTable->getSdrTableObj()->GetModel() )
217 , mxTable( xTable )
218 , mnIndex( nIndex )
219 , mbUndo( true )
221 maRows.swap( aRemovedRows );
224 // -----------------------------------------------------------------------------
226 RemoveRowUndo::~RemoveRowUndo()
228 if( mbUndo )
229 Dispose( maRows );
232 // -----------------------------------------------------------------------------
234 void RemoveRowUndo::Undo()
236 if( mxTable.is() )
238 mxTable->UndoRemoveRows( mnIndex, maRows );
239 mbUndo = false;
243 // -----------------------------------------------------------------------------
245 void RemoveRowUndo::Redo()
247 if( mxTable.is() )
249 mxTable->UndoInsertRows( mnIndex, sal::static_int_cast< sal_Int32 >( maRows.size() ) );
250 mbUndo = true;
254 // -----------------------------------------------------------------------------
255 // class InsertColUndo : public SdrUndoAction
256 // -----------------------------------------------------------------------------
258 static void Dispose( ColumnVector& rCols )
260 ColumnVector::iterator aIter( rCols.begin() );
261 while( aIter != rCols.end() )
262 (*aIter++)->dispose();
265 // -----------------------------------------------------------------------------
267 static void Dispose( CellVector& rCells )
269 CellVector::iterator aIter( rCells.begin() );
270 while( aIter != rCells.end() )
271 (*aIter++)->dispose();
274 // -----------------------------------------------------------------------------
276 InsertColUndo::InsertColUndo( const TableModelRef& xTable, sal_Int32 nIndex, ColumnVector& aNewCols, CellVector& aCells )
277 : SdrUndoAction( *xTable->getSdrTableObj()->GetModel() )
278 , mxTable( xTable )
279 , mnIndex( nIndex )
280 , mbUndo( true )
282 maColumns.swap( aNewCols );
283 maCells.swap( aCells );
286 // -----------------------------------------------------------------------------
288 InsertColUndo::~InsertColUndo()
290 if( !mbUndo )
292 Dispose( maColumns );
293 Dispose( maCells );
297 // -----------------------------------------------------------------------------
299 void InsertColUndo::Undo()
301 if( mxTable.is() )
303 mxTable->UndoInsertColumns( mnIndex, sal::static_int_cast< sal_Int32 >( maColumns.size() ) );
304 mbUndo = false;
308 // -----------------------------------------------------------------------------
310 void InsertColUndo::Redo()
312 if( mxTable.is() )
314 mxTable->UndoRemoveColumns( mnIndex, maColumns, maCells );
315 mbUndo = true;
319 // -----------------------------------------------------------------------------
320 // class RemoveColUndo : public SdrUndoAction
321 // -----------------------------------------------------------------------------
323 RemoveColUndo::RemoveColUndo( const TableModelRef& xTable, sal_Int32 nIndex, ColumnVector& aNewCols, CellVector& aCells )
324 : SdrUndoAction( *xTable->getSdrTableObj()->GetModel() )
325 , mxTable( xTable )
326 , mnIndex( nIndex )
327 , mbUndo( true )
329 maColumns.swap( aNewCols );
330 maCells.swap( aCells );
333 // -----------------------------------------------------------------------------
335 RemoveColUndo::~RemoveColUndo()
337 if( mbUndo )
339 Dispose( maColumns );
340 Dispose( maCells );
344 // -----------------------------------------------------------------------------
346 void RemoveColUndo::Undo()
348 if( mxTable.is() )
350 mxTable->UndoRemoveColumns( mnIndex, maColumns, maCells );
351 mbUndo = false;
355 // -----------------------------------------------------------------------------
357 void RemoveColUndo::Redo()
359 if( mxTable.is() )
361 mxTable->UndoInsertColumns( mnIndex, sal::static_int_cast< sal_Int32 >( maColumns.size() ) );
362 mbUndo = true;
366 // -----------------------------------------------------------------------------
367 // class TableColumnUndo : public SdrUndoAction
368 // -----------------------------------------------------------------------------
370 TableColumnUndo::TableColumnUndo( const TableColumnRef& xCol )
371 : SdrUndoAction( *xCol->mxTableModel->getSdrTableObj()->GetModel() )
372 , mxCol( xCol )
373 , mbHasRedoData( false )
375 getData( maUndoData );
378 // -----------------------------------------------------------------------------
380 TableColumnUndo::~TableColumnUndo()
384 // -----------------------------------------------------------------------------
386 void TableColumnUndo::Undo()
388 if( !mbHasRedoData )
390 getData( maRedoData );
391 mbHasRedoData = true;
393 setData( maUndoData );
396 // -----------------------------------------------------------------------------
398 void TableColumnUndo::Redo()
400 setData( maRedoData );
403 // -----------------------------------------------------------------------------
405 sal_Bool TableColumnUndo::Merge( SfxUndoAction *pNextAction )
407 TableColumnUndo* pNext = dynamic_cast< TableColumnUndo* >( pNextAction );
408 return pNext && pNext->mxCol == mxCol;
411 // -----------------------------------------------------------------------------
413 void TableColumnUndo::setData( const Data& rData )
415 mxCol->mnColumn = rData.mnColumn;
416 mxCol->mnWidth = rData.mnWidth;
417 mxCol->mbOptimalWidth = rData.mbOptimalWidth;
418 mxCol->mbIsVisible = rData.mbIsVisible;
419 mxCol->mbIsStartOfNewPage = rData.mbIsStartOfNewPage;
420 mxCol->maName = rData.maName;
423 // -----------------------------------------------------------------------------
425 void TableColumnUndo::getData( Data& rData )
427 rData.mnColumn = mxCol->mnColumn;
428 rData.mnWidth = mxCol->mnWidth;
429 rData.mbOptimalWidth = mxCol->mbOptimalWidth;
430 rData.mbIsVisible = mxCol->mbIsVisible;
431 rData.mbIsStartOfNewPage = mxCol->mbIsStartOfNewPage;
432 rData.maName = mxCol->maName;
435 // -----------------------------------------------------------------------------
436 // class TableRowUndo : public SdrUndoAction
437 // -----------------------------------------------------------------------------
439 TableRowUndo::TableRowUndo( const TableRowRef& xRow )
440 : SdrUndoAction( *xRow->mxTableModel->getSdrTableObj()->GetModel() )
441 , mxRow( xRow )
442 , mbHasRedoData( false )
444 getData( maUndoData );
447 // -----------------------------------------------------------------------------
449 TableRowUndo::~TableRowUndo()
453 // -----------------------------------------------------------------------------
455 void TableRowUndo::Undo()
457 if( !mbHasRedoData )
459 getData( maRedoData );
460 mbHasRedoData = true;
462 setData( maUndoData );
465 // -----------------------------------------------------------------------------
467 void TableRowUndo::Redo()
469 setData( maRedoData );
472 // -----------------------------------------------------------------------------
474 sal_Bool TableRowUndo::Merge( SfxUndoAction *pNextAction )
476 TableRowUndo* pNext = dynamic_cast< TableRowUndo* >( pNextAction );
477 return pNext && pNext->mxRow == mxRow;
480 // -----------------------------------------------------------------------------
482 void TableRowUndo::setData( const Data& rData )
484 mxRow->mnRow = rData.mnRow;
485 mxRow->mnHeight = rData.mnHeight;
486 mxRow->mbOptimalHeight = rData.mbOptimalHeight;
487 mxRow->mbIsVisible = rData.mbIsVisible;
488 mxRow->mbIsStartOfNewPage = rData.mbIsStartOfNewPage;
489 mxRow->maName = rData.maName;
492 // -----------------------------------------------------------------------------
494 void TableRowUndo::getData( Data& rData )
496 rData.mnRow = mxRow->mnRow;
497 rData.mnHeight = mxRow->mnHeight;
498 rData.mbOptimalHeight = mxRow->mbOptimalHeight;
499 rData.mbIsVisible = mxRow->mbIsVisible;
500 rData.mbIsStartOfNewPage = mxRow->mbIsStartOfNewPage;
501 rData.maName = mxRow->maName;
504 // -----------------------------------------------------------------------------
506 TableStyleUndo::TableStyleUndo( const SdrTableObj& rTableObj )
507 : SdrUndoAction( *rTableObj.GetModel() )
508 , mxObjRef( const_cast< sdr::table::SdrTableObj*>( &rTableObj ) )
510 getData( maUndoData );
513 void TableStyleUndo::Undo()
515 if( !mbHasRedoData )
517 getData( maRedoData );
518 mbHasRedoData = true;
520 setData( maUndoData );
523 void TableStyleUndo::Redo()
525 setData( maRedoData );
528 void TableStyleUndo::setData( const Data& rData )
530 SdrTableObj* pTableObj = dynamic_cast< SdrTableObj* >( mxObjRef.get() );
531 if( pTableObj )
533 pTableObj->setTableStyle( rData.mxTableStyle );
534 pTableObj->setTableStyleSettings( rData.maSettings );
538 void TableStyleUndo::getData( Data& rData )
540 SdrTableObj* pTableObj = dynamic_cast< SdrTableObj* >( mxObjRef.get() );
541 if( pTableObj )
543 rData.maSettings = pTableObj->getTableStyleSettings();
544 rData.mxTableStyle = pTableObj->getTableStyle();
550 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */