merge the formfield patch from ooo-build
[ooovba.git] / svx / source / table / tableundo.cxx
blobf4d6e89aec373e3d00786ea24819e227e3b19026
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: tableundo.cxx,v $
10 * $Revision: 1.3 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svx.hxx"
34 #include "svx/sdr/properties/textproperties.hxx"
35 #include "svx/outlobj.hxx"
37 #include "cell.hxx"
38 #include "tableundo.hxx"
39 #include "svx/svdotable.hxx"
40 #include "tablerow.hxx"
41 #include "tablecolumn.hxx"
44 // -----------------------------------------------------------------------------
46 using ::rtl::OUString;
47 using namespace ::com::sun::star::uno;
48 using namespace ::com::sun::star::table;
50 // -----------------------------------------------------------------------------
52 namespace sdr { namespace table {
54 CellUndo::CellUndo( const SdrObjectWeakRef& xObjRef, const CellRef& xCell )
55 : SdrUndoAction( *xCell->GetModel() )
56 , mxObjRef( xObjRef )
57 , mxCell( xCell )
58 , mbUndo( true )
60 if( mxCell.is() && mxObjRef.is() )
62 getDataFromCell( maUndoData );
63 mxObjRef->AddObjectUser( *this );
67 CellUndo::~CellUndo()
69 if( mxObjRef.is() )
70 mxObjRef->RemoveObjectUser( *this );
71 dispose();
74 void CellUndo::dispose()
76 mxCell.clear();
77 delete maUndoData.mpProperties;
78 maUndoData.mpProperties = 0;
79 delete maRedoData.mpProperties;
80 maRedoData.mpProperties = 0;
81 delete maUndoData.mpOutlinerParaObject;
82 maUndoData.mpOutlinerParaObject = 0;
83 delete maRedoData.mpOutlinerParaObject;
84 maRedoData.mpOutlinerParaObject = 0;
87 void CellUndo::ObjectInDestruction(const SdrObject& )
89 dispose();
92 void CellUndo::Undo()
94 if( mxCell.is() && mbUndo )
96 if( maRedoData.mpProperties == 0 )
97 getDataFromCell( maRedoData );
99 setDataToCell( maUndoData );
100 mbUndo = false;
104 void CellUndo::Redo()
106 if( mxCell.is() && !mbUndo )
108 setDataToCell( maRedoData );
109 mbUndo = true;
113 BOOL CellUndo::Merge( SfxUndoAction *pNextAction )
115 CellUndo* pNext = dynamic_cast< CellUndo* >( pNextAction );
116 if( pNext && pNext->mxCell.get() == mxCell.get() )
118 return TRUE;
120 else
122 return FALSE;
126 void CellUndo::setDataToCell( const Data& rData )
128 delete mxCell->mpProperties;
129 if( rData.mpProperties )
130 mxCell->mpProperties = Cell::CloneProperties( rData.mpProperties, *mxObjRef.get(), *mxCell.get() );
131 else
132 mxCell->mpProperties = 0;
134 if( rData.mpOutlinerParaObject )
135 mxCell->SetOutlinerParaObject( new OutlinerParaObject(*rData.mpOutlinerParaObject) );
136 else
137 mxCell->RemoveOutlinerParaObject();
139 mxCell->msFormula = rData.msFormula;
140 mxCell->mfValue = rData.mfValue;
141 mxCell->mnError = rData.mnError;
142 mxCell->mbMerged = rData.mbMerged;
143 mxCell->mnRowSpan = rData.mnRowSpan;
144 mxCell->mnColSpan = rData.mnColSpan;
146 if( mxObjRef.is() )
147 mxObjRef->ActionChanged();
150 void CellUndo::getDataFromCell( Data& rData )
152 if( mxObjRef.is() && mxCell.is() )
154 if( mxCell->mpProperties )
155 rData.mpProperties = mxCell->CloneProperties( *mxObjRef.get(), *mxCell.get());
157 if( mxCell->GetOutlinerParaObject() )
158 rData.mpOutlinerParaObject = new OutlinerParaObject(*mxCell->GetOutlinerParaObject());
159 else
160 rData.mpOutlinerParaObject = 0;
162 rData.mnCellContentType = mxCell->mnCellContentType;
164 rData.msFormula = mxCell->msFormula;
165 rData.mfValue = mxCell->mfValue;
166 rData.mnError = mxCell->mnError;
167 rData.mbMerged = mxCell->mbMerged;
168 rData.mnRowSpan = mxCell->mnRowSpan;
169 rData.mnColSpan = mxCell->mnColSpan;
173 // -----------------------------------------------------------------------------
174 // class InsertRowUndo : public SdrUndoAction
175 // -----------------------------------------------------------------------------
177 static void Dispose( RowVector& rRows )
179 RowVector::iterator aIter( rRows.begin() );
180 while( aIter != rRows.end() )
181 (*aIter++)->dispose();
184 // -----------------------------------------------------------------------------
186 InsertRowUndo::InsertRowUndo( const TableModelRef& xTable, sal_Int32 nIndex, RowVector& aNewRows )
187 : SdrUndoAction( *xTable->getSdrTableObj()->GetModel() )
188 , mxTable( xTable )
189 , mnIndex( nIndex )
190 , mbUndo( true )
192 maRows.swap( aNewRows );
195 // -----------------------------------------------------------------------------
197 InsertRowUndo::~InsertRowUndo()
199 if( !mbUndo )
200 Dispose( maRows );
203 // -----------------------------------------------------------------------------
205 void InsertRowUndo::Undo()
207 if( mxTable.is() )
209 mxTable->UndoInsertRows( mnIndex, sal::static_int_cast< sal_Int32 >( maRows.size() ) );
210 mbUndo = false;
214 // -----------------------------------------------------------------------------
216 void InsertRowUndo::Redo()
218 if( mxTable.is() )
220 mxTable->UndoRemoveRows( mnIndex, maRows );
221 mbUndo = true;
225 // -----------------------------------------------------------------------------
226 // class RemoveRowUndo : public SdrUndoAction
227 // -----------------------------------------------------------------------------
229 RemoveRowUndo::RemoveRowUndo( const TableModelRef& xTable, sal_Int32 nIndex, RowVector& aRemovedRows )
230 : SdrUndoAction( *xTable->getSdrTableObj()->GetModel() )
231 , mxTable( xTable )
232 , mnIndex( nIndex )
233 , mbUndo( true )
235 maRows.swap( aRemovedRows );
238 // -----------------------------------------------------------------------------
240 RemoveRowUndo::~RemoveRowUndo()
242 if( mbUndo )
243 Dispose( maRows );
246 // -----------------------------------------------------------------------------
248 void RemoveRowUndo::Undo()
250 if( mxTable.is() )
252 mxTable->UndoRemoveRows( mnIndex, maRows );
253 mbUndo = false;
257 // -----------------------------------------------------------------------------
259 void RemoveRowUndo::Redo()
261 if( mxTable.is() )
263 mxTable->UndoInsertRows( mnIndex, sal::static_int_cast< sal_Int32 >( maRows.size() ) );
264 mbUndo = true;
268 // -----------------------------------------------------------------------------
269 // class InsertColUndo : public SdrUndoAction
270 // -----------------------------------------------------------------------------
272 static void Dispose( ColumnVector& rCols )
274 ColumnVector::iterator aIter( rCols.begin() );
275 while( aIter != rCols.end() )
276 (*aIter++)->dispose();
279 // -----------------------------------------------------------------------------
281 static void Dispose( CellVector& rCells )
283 CellVector::iterator aIter( rCells.begin() );
284 while( aIter != rCells.end() )
285 (*aIter++)->dispose();
288 // -----------------------------------------------------------------------------
290 InsertColUndo::InsertColUndo( const TableModelRef& xTable, sal_Int32 nIndex, ColumnVector& aNewCols, CellVector& aCells )
291 : SdrUndoAction( *xTable->getSdrTableObj()->GetModel() )
292 , mxTable( xTable )
293 , mnIndex( nIndex )
294 , mbUndo( true )
296 maColumns.swap( aNewCols );
297 maCells.swap( aCells );
300 // -----------------------------------------------------------------------------
302 InsertColUndo::~InsertColUndo()
304 if( !mbUndo )
306 Dispose( maColumns );
307 Dispose( maCells );
311 // -----------------------------------------------------------------------------
313 void InsertColUndo::Undo()
315 if( mxTable.is() )
317 mxTable->UndoInsertColumns( mnIndex, sal::static_int_cast< sal_Int32 >( maColumns.size() ) );
318 mbUndo = false;
322 // -----------------------------------------------------------------------------
324 void InsertColUndo::Redo()
326 if( mxTable.is() )
328 mxTable->UndoRemoveColumns( mnIndex, maColumns, maCells );
329 mbUndo = true;
333 // -----------------------------------------------------------------------------
334 // class RemoveColUndo : public SdrUndoAction
335 // -----------------------------------------------------------------------------
337 RemoveColUndo::RemoveColUndo( const TableModelRef& xTable, sal_Int32 nIndex, ColumnVector& aNewCols, CellVector& aCells )
338 : SdrUndoAction( *xTable->getSdrTableObj()->GetModel() )
339 , mxTable( xTable )
340 , mnIndex( nIndex )
341 , mbUndo( true )
343 maColumns.swap( aNewCols );
344 maCells.swap( aCells );
347 // -----------------------------------------------------------------------------
349 RemoveColUndo::~RemoveColUndo()
351 if( mbUndo )
353 Dispose( maColumns );
354 Dispose( maCells );
358 // -----------------------------------------------------------------------------
360 void RemoveColUndo::Undo()
362 if( mxTable.is() )
364 mxTable->UndoRemoveColumns( mnIndex, maColumns, maCells );
365 mbUndo = false;
369 // -----------------------------------------------------------------------------
371 void RemoveColUndo::Redo()
373 if( mxTable.is() )
375 mxTable->UndoInsertColumns( mnIndex, sal::static_int_cast< sal_Int32 >( maColumns.size() ) );
376 mbUndo = true;
380 // -----------------------------------------------------------------------------
381 // class TableColumnUndo : public SdrUndoAction
382 // -----------------------------------------------------------------------------
384 TableColumnUndo::TableColumnUndo( const TableColumnRef& xCol )
385 : SdrUndoAction( *xCol->mxTableModel->getSdrTableObj()->GetModel() )
386 , mxCol( xCol )
387 , mbHasRedoData( false )
389 getData( maUndoData );
392 // -----------------------------------------------------------------------------
394 TableColumnUndo::~TableColumnUndo()
398 // -----------------------------------------------------------------------------
400 void TableColumnUndo::Undo()
402 if( !mbHasRedoData )
404 getData( maRedoData );
405 mbHasRedoData = true;
407 setData( maUndoData );
410 // -----------------------------------------------------------------------------
412 void TableColumnUndo::Redo()
414 setData( maRedoData );
417 // -----------------------------------------------------------------------------
419 BOOL TableColumnUndo::Merge( SfxUndoAction *pNextAction )
421 TableColumnUndo* pNext = dynamic_cast< TableColumnUndo* >( pNextAction );
422 return pNext && pNext->mxCol == mxCol;
425 // -----------------------------------------------------------------------------
427 void TableColumnUndo::setData( const Data& rData )
429 mxCol->mnColumn = rData.mnColumn;
430 mxCol->mnWidth = rData.mnWidth;
431 mxCol->mbOptimalWidth = rData.mbOptimalWidth;
432 mxCol->mbIsVisible = rData.mbIsVisible;
433 mxCol->mbIsStartOfNewPage = rData.mbIsStartOfNewPage;
434 mxCol->maName = rData.maName;
437 // -----------------------------------------------------------------------------
439 void TableColumnUndo::getData( Data& rData )
441 rData.mnColumn = mxCol->mnColumn;
442 rData.mnWidth = mxCol->mnWidth;
443 rData.mbOptimalWidth = mxCol->mbOptimalWidth;
444 rData.mbIsVisible = mxCol->mbIsVisible;
445 rData.mbIsStartOfNewPage = mxCol->mbIsStartOfNewPage;
446 rData.maName = mxCol->maName;
449 // -----------------------------------------------------------------------------
450 // class TableRowUndo : public SdrUndoAction
451 // -----------------------------------------------------------------------------
453 TableRowUndo::TableRowUndo( const TableRowRef& xRow )
454 : SdrUndoAction( *xRow->mxTableModel->getSdrTableObj()->GetModel() )
455 , mxRow( xRow )
456 , mbHasRedoData( false )
458 getData( maUndoData );
461 // -----------------------------------------------------------------------------
463 TableRowUndo::~TableRowUndo()
467 // -----------------------------------------------------------------------------
469 void TableRowUndo::Undo()
471 if( !mbHasRedoData )
473 getData( maRedoData );
474 mbHasRedoData = true;
476 setData( maUndoData );
479 // -----------------------------------------------------------------------------
481 void TableRowUndo::Redo()
483 setData( maRedoData );
486 // -----------------------------------------------------------------------------
488 BOOL TableRowUndo::Merge( SfxUndoAction *pNextAction )
490 TableRowUndo* pNext = dynamic_cast< TableRowUndo* >( pNextAction );
491 return pNext && pNext->mxRow == mxRow;
494 // -----------------------------------------------------------------------------
496 void TableRowUndo::setData( const Data& rData )
498 mxRow->mnRow = rData.mnRow;
499 mxRow->mnHeight = rData.mnHeight;
500 mxRow->mbOptimalHeight = rData.mbOptimalHeight;
501 mxRow->mbIsVisible = rData.mbIsVisible;
502 mxRow->mbIsStartOfNewPage = rData.mbIsStartOfNewPage;
503 mxRow->maName = rData.maName;
506 // -----------------------------------------------------------------------------
508 void TableRowUndo::getData( Data& rData )
510 rData.mnRow = mxRow->mnRow;
511 rData.mnHeight = mxRow->mnHeight;
512 rData.mbOptimalHeight = mxRow->mbOptimalHeight;
513 rData.mbIsVisible = mxRow->mbIsVisible;
514 rData.mbIsStartOfNewPage = mxRow->mbIsStartOfNewPage;
515 rData.maName = mxRow->maName;
518 // -----------------------------------------------------------------------------
520 TableStyleUndo::TableStyleUndo( const SdrTableObj& rTableObj )
521 : SdrUndoAction( *rTableObj.GetModel() )
522 , mxObjRef( const_cast< sdr::table::SdrTableObj*>( &rTableObj ) )
524 getData( maUndoData );
527 void TableStyleUndo::Undo()
529 if( !mbHasRedoData )
531 getData( maRedoData );
532 mbHasRedoData = true;
534 setData( maUndoData );
537 void TableStyleUndo::Redo()
539 setData( maRedoData );
542 void TableStyleUndo::setData( const Data& rData )
544 SdrTableObj* pTableObj = dynamic_cast< SdrTableObj* >( mxObjRef.get() );
545 if( pTableObj )
547 pTableObj->setTableStyle( rData.mxTableStyle );
548 pTableObj->setTableStyleSettings( rData.maSettings );
552 void TableStyleUndo::getData( Data& rData )
554 SdrTableObj* pTableObj = dynamic_cast< SdrTableObj* >( mxObjRef.get() );
555 if( pTableObj )
557 rData.maSettings = pTableObj->getTableStyleSettings();
558 rData.mxTableStyle = pTableObj->getTableStyle();