xref: /AOO41X/main/svx/source/table/tablemodel.cxx (revision f6e50924346d0b8c0b07c91832a97665dd718b0c)
1*f6e50924SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*f6e50924SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*f6e50924SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*f6e50924SAndrew Rist  * distributed with this work for additional information
6*f6e50924SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*f6e50924SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*f6e50924SAndrew Rist  * "License"); you may not use this file except in compliance
9*f6e50924SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*f6e50924SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*f6e50924SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*f6e50924SAndrew Rist  * software distributed under the License is distributed on an
15*f6e50924SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*f6e50924SAndrew Rist  * KIND, either express or implied.  See the License for the
17*f6e50924SAndrew Rist  * specific language governing permissions and limitations
18*f6e50924SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*f6e50924SAndrew Rist  *************************************************************/
21*f6e50924SAndrew Rist 
22*f6e50924SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_svx.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <com/sun/star/table/XMergeableCell.hpp>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include <algorithm>
30cdf0e10cSrcweir #include <boost/bind.hpp>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include <vcl/svapp.hxx>
33cdf0e10cSrcweir #include <vos/mutex.hxx>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir #include "cell.hxx"
36cdf0e10cSrcweir #include "cellcursor.hxx"
37cdf0e10cSrcweir #include "tablemodel.hxx"
38cdf0e10cSrcweir #include "tablerow.hxx"
39cdf0e10cSrcweir #include "tablerows.hxx"
40cdf0e10cSrcweir #include "tablecolumn.hxx"
41cdf0e10cSrcweir #include "tablecolumns.hxx"
42cdf0e10cSrcweir #include "tableundo.hxx"
43cdf0e10cSrcweir #include "svx/svdotable.hxx"
44cdf0e10cSrcweir #include "svx/svdmodel.hxx"
45cdf0e10cSrcweir #include "svx/svdstr.hrc"
46cdf0e10cSrcweir #include "svx/svdglob.hxx"
47cdf0e10cSrcweir 
48cdf0e10cSrcweir //#define PLEASE_DEBUG_THE_TABLES 1
49cdf0e10cSrcweir 
50cdf0e10cSrcweir using ::rtl::OUString;
51cdf0e10cSrcweir using namespace ::osl;
52cdf0e10cSrcweir using namespace ::vos;
53cdf0e10cSrcweir using namespace ::com::sun::star::uno;
54cdf0e10cSrcweir using namespace ::com::sun::star::table;
55cdf0e10cSrcweir using namespace ::com::sun::star::lang;
56cdf0e10cSrcweir using namespace ::com::sun::star::container;
57cdf0e10cSrcweir using namespace ::com::sun::star::beans;
58cdf0e10cSrcweir using namespace ::com::sun::star::util;
59cdf0e10cSrcweir 
60cdf0e10cSrcweir // -----------------------------------------------------------------------------
61cdf0e10cSrcweir 
62cdf0e10cSrcweir namespace sdr { namespace table {
63cdf0e10cSrcweir 
64cdf0e10cSrcweir // -----------------------------------------------------------------------------
65cdf0e10cSrcweir 
66cdf0e10cSrcweir // removes the given range from a vector
67cdf0e10cSrcweir template< class Vec, class Iter > void remove_range( Vec& rVector, sal_Int32 nIndex, sal_Int32 nCount )
68cdf0e10cSrcweir {
69cdf0e10cSrcweir 	const sal_Int32 nSize = static_cast<sal_Int32>(rVector.size());
70cdf0e10cSrcweir 	if( nCount && (nIndex >= 0) && (nIndex < nSize) )
71cdf0e10cSrcweir 	{
72cdf0e10cSrcweir 		if( (nIndex + nCount) >= nSize )
73cdf0e10cSrcweir 		{
74cdf0e10cSrcweir 			// remove at end
75cdf0e10cSrcweir 			rVector.resize( nIndex );
76cdf0e10cSrcweir 		}
77cdf0e10cSrcweir 		else
78cdf0e10cSrcweir 		{
79cdf0e10cSrcweir 			Iter aBegin( rVector.begin() );
80cdf0e10cSrcweir 			while( nIndex-- )
81cdf0e10cSrcweir 				aBegin++;
82cdf0e10cSrcweir 			if( nCount == 1 )
83cdf0e10cSrcweir 			{
84cdf0e10cSrcweir 				rVector.erase( aBegin );
85cdf0e10cSrcweir 			}
86cdf0e10cSrcweir 			else
87cdf0e10cSrcweir 			{
88cdf0e10cSrcweir 				Iter aEnd( aBegin );
89cdf0e10cSrcweir 
90cdf0e10cSrcweir 				while( nCount-- )
91cdf0e10cSrcweir 					aEnd++;
92cdf0e10cSrcweir 				rVector.erase( aBegin, aEnd );
93cdf0e10cSrcweir 			}
94cdf0e10cSrcweir 		}
95cdf0e10cSrcweir 	}
96cdf0e10cSrcweir }
97cdf0e10cSrcweir 
98cdf0e10cSrcweir // -----------------------------------------------------------------------------
99cdf0e10cSrcweir 
100cdf0e10cSrcweir /** inserts a range into a vector */
101cdf0e10cSrcweir template< class Vec, class Iter, class Entry > sal_Int32 insert_range( Vec& rVector, sal_Int32 nIndex, sal_Int32 nCount )
102cdf0e10cSrcweir {
103cdf0e10cSrcweir 	if( nCount )
104cdf0e10cSrcweir 	{
105cdf0e10cSrcweir 		if( nIndex >= static_cast< sal_Int32 >( rVector.size() ) )
106cdf0e10cSrcweir 		{
107cdf0e10cSrcweir 			// append at end
108cdf0e10cSrcweir 			nIndex = static_cast< sal_Int32 >( rVector.size() ); // cap to end
109cdf0e10cSrcweir 			rVector.resize( nIndex + nCount );
110cdf0e10cSrcweir 		}
111cdf0e10cSrcweir 		else
112cdf0e10cSrcweir 		{
113cdf0e10cSrcweir 			// insert
114cdf0e10cSrcweir 			sal_Int32 nFind = nIndex;
115cdf0e10cSrcweir 			Iter aIter( rVector.begin() );
116cdf0e10cSrcweir 			while( nFind-- )
117cdf0e10cSrcweir 				aIter++;
118cdf0e10cSrcweir 
119cdf0e10cSrcweir 			Entry aEmpty;
120cdf0e10cSrcweir 			rVector.insert( aIter, nCount, aEmpty );
121cdf0e10cSrcweir 		}
122cdf0e10cSrcweir 	}
123cdf0e10cSrcweir 	return nIndex;
124cdf0e10cSrcweir }
125cdf0e10cSrcweir 
126cdf0e10cSrcweir // -----------------------------------------------------------------------------
127cdf0e10cSrcweir 
128cdf0e10cSrcweir TableModel::TableModel( SdrTableObj* pTableObj )
129cdf0e10cSrcweir : TableModelBase( m_aMutex  )
130cdf0e10cSrcweir , mpTableObj( pTableObj )
131cdf0e10cSrcweir , mbModified( sal_False )
132cdf0e10cSrcweir , mbNotifyPending( false )
133cdf0e10cSrcweir , mnNotifyLock( 0 )
134cdf0e10cSrcweir {
135cdf0e10cSrcweir }
136cdf0e10cSrcweir 
137cdf0e10cSrcweir TableModel::TableModel( SdrTableObj* pTableObj, const TableModelRef& xSourceTable )
138cdf0e10cSrcweir : TableModelBase( m_aMutex  )
139cdf0e10cSrcweir , mpTableObj( pTableObj )
140cdf0e10cSrcweir , mbModified( sal_False )
141cdf0e10cSrcweir , mbNotifyPending( false )
142cdf0e10cSrcweir , mnNotifyLock( 0 )
143cdf0e10cSrcweir {
144cdf0e10cSrcweir 	if( xSourceTable.is() )
145cdf0e10cSrcweir 	{
146cdf0e10cSrcweir 		const sal_Int32 nColCount = xSourceTable->getColumnCountImpl();
147cdf0e10cSrcweir 		const sal_Int32 nRowCount = xSourceTable->getRowCountImpl();
148cdf0e10cSrcweir 
149cdf0e10cSrcweir 		init( nColCount, nRowCount );
150cdf0e10cSrcweir 
151cdf0e10cSrcweir 		sal_Int32 nRows = nRowCount;
152cdf0e10cSrcweir 		while( nRows-- )
153cdf0e10cSrcweir 			(*maRows[nRows]) = (*xSourceTable->maRows[nRows]);
154cdf0e10cSrcweir 
155cdf0e10cSrcweir 		sal_Int32 nColumns = nColCount;
156cdf0e10cSrcweir 		while( nColumns-- )
157cdf0e10cSrcweir 			(*maColumns[nColumns]) = (*xSourceTable->maColumns[nColumns]);
158cdf0e10cSrcweir 
159cdf0e10cSrcweir 		// copy cells
160cdf0e10cSrcweir 		for( sal_Int32 nCol = 0; nCol < nColCount; ++nCol )
161cdf0e10cSrcweir 		{
162cdf0e10cSrcweir 			for( sal_Int32 nRow = 0; nRow < nRowCount; ++nRow )
163cdf0e10cSrcweir 			{
164cdf0e10cSrcweir 				CellRef xTargetCell( getCell( nCol, nRow ) );
165cdf0e10cSrcweir 				if( xTargetCell.is() )
166cdf0e10cSrcweir 					xTargetCell->cloneFrom( xSourceTable->getCell( nCol, nRow ) );
167cdf0e10cSrcweir 			}
168cdf0e10cSrcweir 		}
169cdf0e10cSrcweir 	}
170cdf0e10cSrcweir }
171cdf0e10cSrcweir 
172cdf0e10cSrcweir // -----------------------------------------------------------------------------
173cdf0e10cSrcweir 
174cdf0e10cSrcweir TableModel::~TableModel()
175cdf0e10cSrcweir {
176cdf0e10cSrcweir }
177cdf0e10cSrcweir 
178cdf0e10cSrcweir // -----------------------------------------------------------------------------
179cdf0e10cSrcweir 
180cdf0e10cSrcweir void TableModel::init( sal_Int32 nColumns, sal_Int32 nRows )
181cdf0e10cSrcweir {
182cdf0e10cSrcweir 	if( nRows < 20 )
183cdf0e10cSrcweir 		maRows.reserve( 20 );
184cdf0e10cSrcweir 
185cdf0e10cSrcweir 	if( nColumns < 20 )
186cdf0e10cSrcweir 		maColumns.reserve( 20 );
187cdf0e10cSrcweir 
188cdf0e10cSrcweir 	if( nRows && nColumns )
189cdf0e10cSrcweir 	{
190cdf0e10cSrcweir 		maColumns.resize( nColumns );
191cdf0e10cSrcweir 		maRows.resize( nRows );
192cdf0e10cSrcweir 
193cdf0e10cSrcweir 		while( nRows-- )
194cdf0e10cSrcweir 			maRows[nRows].set( new TableRow( this, nRows, nColumns ) );
195cdf0e10cSrcweir 
196cdf0e10cSrcweir 		while( nColumns-- )
197cdf0e10cSrcweir 			maColumns[nColumns].set( new TableColumn( this, nColumns ) );
198cdf0e10cSrcweir 	}
199cdf0e10cSrcweir }
200cdf0e10cSrcweir 
201cdf0e10cSrcweir // -----------------------------------------------------------------------------
202cdf0e10cSrcweir // ICellRange
203cdf0e10cSrcweir // -----------------------------------------------------------------------------
204cdf0e10cSrcweir 
205cdf0e10cSrcweir sal_Int32 TableModel::getLeft()
206cdf0e10cSrcweir {
207cdf0e10cSrcweir 	return 0;
208cdf0e10cSrcweir }
209cdf0e10cSrcweir 
210cdf0e10cSrcweir // -----------------------------------------------------------------------------
211cdf0e10cSrcweir 
212cdf0e10cSrcweir sal_Int32 TableModel::getTop()
213cdf0e10cSrcweir {
214cdf0e10cSrcweir 	return 0;
215cdf0e10cSrcweir }
216cdf0e10cSrcweir 
217cdf0e10cSrcweir // -----------------------------------------------------------------------------
218cdf0e10cSrcweir 
219cdf0e10cSrcweir sal_Int32 TableModel::getRight()
220cdf0e10cSrcweir {
221cdf0e10cSrcweir 	return getColumnCount();
222cdf0e10cSrcweir }
223cdf0e10cSrcweir 
224cdf0e10cSrcweir // -----------------------------------------------------------------------------
225cdf0e10cSrcweir 
226cdf0e10cSrcweir sal_Int32 TableModel::getBottom()
227cdf0e10cSrcweir {
228cdf0e10cSrcweir 	return getRowCount();
229cdf0e10cSrcweir }
230cdf0e10cSrcweir 
231cdf0e10cSrcweir // -----------------------------------------------------------------------------
232cdf0e10cSrcweir 
233cdf0e10cSrcweir Reference< XTable > TableModel::getTable()
234cdf0e10cSrcweir {
235cdf0e10cSrcweir 	return this;
236cdf0e10cSrcweir }
237cdf0e10cSrcweir 
238cdf0e10cSrcweir // -----------------------------------------------------------------------------
239cdf0e10cSrcweir 
240cdf0e10cSrcweir void TableModel::UndoInsertRows( sal_Int32 nIndex, sal_Int32 nCount )
241cdf0e10cSrcweir {
242cdf0e10cSrcweir 	TableModelNotifyGuard aGuard( this );
243cdf0e10cSrcweir 
244cdf0e10cSrcweir 	// remove the rows
245cdf0e10cSrcweir 	remove_range<RowVector,RowVector::iterator>( maRows, nIndex, nCount );
246cdf0e10cSrcweir     updateRows();
247cdf0e10cSrcweir 	setModified(sal_True);
248cdf0e10cSrcweir }
249cdf0e10cSrcweir 
250cdf0e10cSrcweir // -----------------------------------------------------------------------------
251cdf0e10cSrcweir 
252cdf0e10cSrcweir void TableModel::UndoRemoveRows( sal_Int32 nIndex, RowVector& aRows )
253cdf0e10cSrcweir {
254cdf0e10cSrcweir 	TableModelNotifyGuard aGuard( this );
255cdf0e10cSrcweir 
256cdf0e10cSrcweir 	const sal_Int32 nCount = sal::static_int_cast< sal_Int32 >( aRows.size() );
257cdf0e10cSrcweir 
258cdf0e10cSrcweir 	nIndex = insert_range<RowVector,RowVector::iterator,TableRowRef>( maRows, nIndex, nCount );
259cdf0e10cSrcweir 
260cdf0e10cSrcweir 	for( sal_Int32 nOffset = 0; nOffset < nCount; ++nOffset )
261cdf0e10cSrcweir 		maRows[nIndex+nOffset] = aRows[nOffset];
262cdf0e10cSrcweir 
263cdf0e10cSrcweir     updateRows();
264cdf0e10cSrcweir 	setModified(sal_True);
265cdf0e10cSrcweir }
266cdf0e10cSrcweir 
267cdf0e10cSrcweir // -----------------------------------------------------------------------------
268cdf0e10cSrcweir 
269cdf0e10cSrcweir void TableModel::UndoInsertColumns( sal_Int32 nIndex, sal_Int32 nCount )
270cdf0e10cSrcweir {
271cdf0e10cSrcweir 	TableModelNotifyGuard aGuard( this );
272cdf0e10cSrcweir 
273cdf0e10cSrcweir 	// now remove the columns
274cdf0e10cSrcweir 	remove_range<ColumnVector,ColumnVector::iterator>( maColumns, nIndex, nCount );
275cdf0e10cSrcweir 	sal_Int32 nRows = getRowCountImpl();
276cdf0e10cSrcweir 	while( nRows-- )
277cdf0e10cSrcweir 		maRows[nRows]->removeColumns( nIndex, nCount );
278cdf0e10cSrcweir 
279cdf0e10cSrcweir     updateColumns();
280cdf0e10cSrcweir 	setModified(sal_True);
281cdf0e10cSrcweir }
282cdf0e10cSrcweir 
283cdf0e10cSrcweir // -----------------------------------------------------------------------------
284cdf0e10cSrcweir 
285cdf0e10cSrcweir void TableModel::UndoRemoveColumns( sal_Int32 nIndex, ColumnVector& aCols, CellVector& aCells )
286cdf0e10cSrcweir {
287cdf0e10cSrcweir 	TableModelNotifyGuard aGuard( this );
288cdf0e10cSrcweir 
289cdf0e10cSrcweir 	const sal_Int32 nCount = sal::static_int_cast< sal_Int32 >( aCols.size() );
290cdf0e10cSrcweir 
291cdf0e10cSrcweir 	// assert if there are not enough cells saved
292cdf0e10cSrcweir 	DBG_ASSERT( (aCols.size() * maRows.size()) == aCells.size(), "sdr::table::TableModel::UndoRemoveColumns(), invalid undo data!" );
293cdf0e10cSrcweir 
294cdf0e10cSrcweir 	nIndex = insert_range<ColumnVector,ColumnVector::iterator,TableColumnRef>( maColumns, nIndex, nCount );
295cdf0e10cSrcweir 	for( sal_Int32 nOffset = 0; nOffset < nCount; ++nOffset )
296cdf0e10cSrcweir 		maColumns[nIndex+nOffset] = aCols[nOffset];
297cdf0e10cSrcweir 
298cdf0e10cSrcweir 	CellVector::iterator aIter( aCells.begin() );
299cdf0e10cSrcweir 
300cdf0e10cSrcweir 	sal_Int32 nRows = getRowCountImpl();
301cdf0e10cSrcweir 	for( sal_Int32 nRow = 0; nRow < nRows; ++nRow )
302cdf0e10cSrcweir 		maRows[nRow]->insertColumns( nIndex, nCount, &aIter );
303cdf0e10cSrcweir 
304cdf0e10cSrcweir     updateColumns();
305cdf0e10cSrcweir 	setModified(sal_True);
306cdf0e10cSrcweir }
307cdf0e10cSrcweir 
308cdf0e10cSrcweir // -----------------------------------------------------------------------------
309cdf0e10cSrcweir // XTable
310cdf0e10cSrcweir // -----------------------------------------------------------------------------
311cdf0e10cSrcweir 
312cdf0e10cSrcweir Reference< XCellCursor > SAL_CALL TableModel::createCursor() throw (RuntimeException)
313cdf0e10cSrcweir {
314cdf0e10cSrcweir 	OGuard aGuard( Application::GetSolarMutex() );
315cdf0e10cSrcweir 	return createCursorByRange( Reference< XCellRange >( this ) );
316cdf0e10cSrcweir }
317cdf0e10cSrcweir 
318cdf0e10cSrcweir // -----------------------------------------------------------------------------
319cdf0e10cSrcweir 
320cdf0e10cSrcweir Reference< XCellCursor > SAL_CALL TableModel::createCursorByRange( const Reference< XCellRange >& Range ) throw (IllegalArgumentException, RuntimeException)
321cdf0e10cSrcweir {
322cdf0e10cSrcweir 	OGuard aGuard( Application::GetSolarMutex() );
323cdf0e10cSrcweir 
324cdf0e10cSrcweir 	ICellRange* pRange = dynamic_cast< ICellRange* >( Range.get() );
325cdf0e10cSrcweir 	if( (pRange == 0) || (pRange->getTable().get() != this) )
326cdf0e10cSrcweir 		throw IllegalArgumentException();
327cdf0e10cSrcweir 
328cdf0e10cSrcweir 	TableModelRef xModel( this );
329cdf0e10cSrcweir 	return new CellCursor( xModel, pRange->getLeft(), pRange->getTop(), pRange->getRight(), pRange->getBottom() );
330cdf0e10cSrcweir }
331cdf0e10cSrcweir 
332cdf0e10cSrcweir // -----------------------------------------------------------------------------
333cdf0e10cSrcweir 
334cdf0e10cSrcweir sal_Int32 SAL_CALL TableModel::getRowCount() throw (RuntimeException)
335cdf0e10cSrcweir {
336cdf0e10cSrcweir 	OGuard aGuard( Application::GetSolarMutex() );
337cdf0e10cSrcweir 	return getRowCountImpl();
338cdf0e10cSrcweir }
339cdf0e10cSrcweir 
340cdf0e10cSrcweir // -----------------------------------------------------------------------------
341cdf0e10cSrcweir 
342cdf0e10cSrcweir sal_Int32 SAL_CALL TableModel::getColumnCount() throw (RuntimeException)
343cdf0e10cSrcweir {
344cdf0e10cSrcweir 	OGuard aGuard( Application::GetSolarMutex() );
345cdf0e10cSrcweir 	return getColumnCountImpl();
346cdf0e10cSrcweir }
347cdf0e10cSrcweir 
348cdf0e10cSrcweir // -----------------------------------------------------------------------------
349cdf0e10cSrcweir // XComponent
350cdf0e10cSrcweir // -----------------------------------------------------------------------------
351cdf0e10cSrcweir 
352cdf0e10cSrcweir void TableModel::dispose() throw (RuntimeException)
353cdf0e10cSrcweir {
354cdf0e10cSrcweir 	OGuard aGuard( Application::GetSolarMutex() );
355cdf0e10cSrcweir 	TableModelBase::dispose();
356cdf0e10cSrcweir }
357cdf0e10cSrcweir 
358cdf0e10cSrcweir // -----------------------------------------------------------------------------
359cdf0e10cSrcweir 
360cdf0e10cSrcweir void SAL_CALL TableModel::addEventListener( const Reference< XEventListener >& xListener ) throw (RuntimeException)
361cdf0e10cSrcweir {
362cdf0e10cSrcweir 	TableModelBase::addEventListener( xListener );
363cdf0e10cSrcweir }
364cdf0e10cSrcweir 
365cdf0e10cSrcweir // -----------------------------------------------------------------------------
366cdf0e10cSrcweir 
367cdf0e10cSrcweir void SAL_CALL TableModel::removeEventListener( const Reference< XEventListener >& xListener ) throw (RuntimeException)
368cdf0e10cSrcweir {
369cdf0e10cSrcweir 	TableModelBase::removeEventListener( xListener );
370cdf0e10cSrcweir }
371cdf0e10cSrcweir 
372cdf0e10cSrcweir // -----------------------------------------------------------------------------
373cdf0e10cSrcweir // XModifiable
374cdf0e10cSrcweir // -----------------------------------------------------------------------------
375cdf0e10cSrcweir 
376cdf0e10cSrcweir sal_Bool SAL_CALL TableModel::isModified(  ) throw (RuntimeException)
377cdf0e10cSrcweir {
378cdf0e10cSrcweir 	OGuard aGuard( Application::GetSolarMutex() );
379cdf0e10cSrcweir 	return mbModified;
380cdf0e10cSrcweir }
381cdf0e10cSrcweir 
382cdf0e10cSrcweir // -----------------------------------------------------------------------------
383cdf0e10cSrcweir 
384cdf0e10cSrcweir void SAL_CALL TableModel::setModified( sal_Bool bModified ) throw (PropertyVetoException, RuntimeException)
385cdf0e10cSrcweir {
386cdf0e10cSrcweir 	{
387cdf0e10cSrcweir 		OGuard aGuard( Application::GetSolarMutex() );
388cdf0e10cSrcweir 		mbModified = bModified;
389cdf0e10cSrcweir 	}
390cdf0e10cSrcweir 	if( bModified )
391cdf0e10cSrcweir 		notifyModification();
392cdf0e10cSrcweir }
393cdf0e10cSrcweir 
394cdf0e10cSrcweir // -----------------------------------------------------------------------------
395cdf0e10cSrcweir // XModifyBroadcaster
396cdf0e10cSrcweir // -----------------------------------------------------------------------------
397cdf0e10cSrcweir 
398cdf0e10cSrcweir void SAL_CALL TableModel::addModifyListener( const Reference< XModifyListener >& xListener ) throw (RuntimeException)
399cdf0e10cSrcweir {
400cdf0e10cSrcweir 	rBHelper.addListener( XModifyListener::static_type() , xListener );
401cdf0e10cSrcweir }
402cdf0e10cSrcweir 
403cdf0e10cSrcweir // -----------------------------------------------------------------------------
404cdf0e10cSrcweir 
405cdf0e10cSrcweir void SAL_CALL TableModel::removeModifyListener( const Reference< XModifyListener >& xListener ) throw (RuntimeException)
406cdf0e10cSrcweir {
407cdf0e10cSrcweir 	rBHelper.removeListener( XModifyListener::static_type() , xListener );
408cdf0e10cSrcweir }
409cdf0e10cSrcweir 
410cdf0e10cSrcweir // -----------------------------------------------------------------------------
411cdf0e10cSrcweir // XColumnRowRange
412cdf0e10cSrcweir // -----------------------------------------------------------------------------
413cdf0e10cSrcweir 
414cdf0e10cSrcweir Reference< XTableColumns > SAL_CALL TableModel::getColumns() throw (RuntimeException)
415cdf0e10cSrcweir {
416cdf0e10cSrcweir 	OGuard aGuard( Application::GetSolarMutex() );
417cdf0e10cSrcweir 
418cdf0e10cSrcweir 	if( !mxTableColumns.is() )
419cdf0e10cSrcweir 		mxTableColumns.set( new TableColumns( this ) );
420cdf0e10cSrcweir 	return mxTableColumns.get();
421cdf0e10cSrcweir }
422cdf0e10cSrcweir 
423cdf0e10cSrcweir // -----------------------------------------------------------------------------
424cdf0e10cSrcweir 
425cdf0e10cSrcweir Reference< XTableRows > SAL_CALL TableModel::getRows() throw (RuntimeException)
426cdf0e10cSrcweir {
427cdf0e10cSrcweir 	OGuard aGuard( Application::GetSolarMutex() );
428cdf0e10cSrcweir 
429cdf0e10cSrcweir 	if( !mxTableRows.is() )
430cdf0e10cSrcweir 		mxTableRows.set( new TableRows( this ) );
431cdf0e10cSrcweir 	return mxTableRows.get();
432cdf0e10cSrcweir }
433cdf0e10cSrcweir 
434cdf0e10cSrcweir // -----------------------------------------------------------------------------
435cdf0e10cSrcweir // XCellRange
436cdf0e10cSrcweir // -----------------------------------------------------------------------------
437cdf0e10cSrcweir 
438cdf0e10cSrcweir Reference< XCell > SAL_CALL TableModel::getCellByPosition( sal_Int32 nColumn, sal_Int32 nRow ) throw ( IndexOutOfBoundsException, RuntimeException)
439cdf0e10cSrcweir {
440cdf0e10cSrcweir 	OGuard aGuard( Application::GetSolarMutex() );
441cdf0e10cSrcweir 
442cdf0e10cSrcweir 	CellRef xCell( getCell( nColumn, nRow ) );
443cdf0e10cSrcweir 	if( xCell.is() )
444cdf0e10cSrcweir 		return xCell.get();
445cdf0e10cSrcweir 
446cdf0e10cSrcweir 	throw IndexOutOfBoundsException();
447cdf0e10cSrcweir }
448cdf0e10cSrcweir 
449cdf0e10cSrcweir // -----------------------------------------------------------------------------
450cdf0e10cSrcweir 
451cdf0e10cSrcweir Reference< XCellRange > SAL_CALL TableModel::getCellRangeByPosition( sal_Int32 nLeft, sal_Int32 nTop, sal_Int32 nRight, sal_Int32 nBottom ) throw (IndexOutOfBoundsException, RuntimeException)
452cdf0e10cSrcweir {
453cdf0e10cSrcweir 	OGuard aGuard( Application::GetSolarMutex() );
454cdf0e10cSrcweir 
455cdf0e10cSrcweir 	if( (nLeft >= 0) && (nTop >= 0) && (nRight >= nLeft) && (nBottom >= nTop) && (nRight < getColumnCountImpl()) && (nBottom < getRowCountImpl() ) )
456cdf0e10cSrcweir 	{
457cdf0e10cSrcweir 		TableModelRef xModel( this );
458cdf0e10cSrcweir 		return new CellRange( xModel, nLeft, nTop, nRight, nBottom );
459cdf0e10cSrcweir 	}
460cdf0e10cSrcweir 
461cdf0e10cSrcweir 	throw IndexOutOfBoundsException();
462cdf0e10cSrcweir }
463cdf0e10cSrcweir 
464cdf0e10cSrcweir // -----------------------------------------------------------------------------
465cdf0e10cSrcweir 
466cdf0e10cSrcweir Reference< XCellRange > SAL_CALL TableModel::getCellRangeByName( const OUString& /*aRange*/ ) throw (RuntimeException)
467cdf0e10cSrcweir {
468cdf0e10cSrcweir 	return Reference< XCellRange >();
469cdf0e10cSrcweir }
470cdf0e10cSrcweir 
471cdf0e10cSrcweir // -----------------------------------------------------------------------------
472cdf0e10cSrcweir // XPropertySet
473cdf0e10cSrcweir // -----------------------------------------------------------------------------
474cdf0e10cSrcweir 
475cdf0e10cSrcweir Reference< XPropertySetInfo > SAL_CALL TableModel::getPropertySetInfo(  ) throw (RuntimeException)
476cdf0e10cSrcweir {
477cdf0e10cSrcweir 	Reference< XPropertySetInfo > xInfo;
478cdf0e10cSrcweir 	return xInfo;
479cdf0e10cSrcweir }
480cdf0e10cSrcweir 
481cdf0e10cSrcweir // -----------------------------------------------------------------------------
482cdf0e10cSrcweir 
483cdf0e10cSrcweir void SAL_CALL TableModel::setPropertyValue( const ::rtl::OUString& /*aPropertyName*/, const Any& /*aValue*/ ) throw (UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException, RuntimeException)
484cdf0e10cSrcweir {
485cdf0e10cSrcweir }
486cdf0e10cSrcweir 
487cdf0e10cSrcweir // -----------------------------------------------------------------------------
488cdf0e10cSrcweir 
489cdf0e10cSrcweir Any SAL_CALL TableModel::getPropertyValue( const OUString& /*PropertyName*/ ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException)
490cdf0e10cSrcweir {
491cdf0e10cSrcweir 	return Any();
492cdf0e10cSrcweir }
493cdf0e10cSrcweir 
494cdf0e10cSrcweir // -----------------------------------------------------------------------------
495cdf0e10cSrcweir 
496cdf0e10cSrcweir void SAL_CALL TableModel::addPropertyChangeListener( const OUString& /*aPropertyName*/, const Reference< XPropertyChangeListener >& /*xListener*/ ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException)
497cdf0e10cSrcweir {
498cdf0e10cSrcweir }
499cdf0e10cSrcweir 
500cdf0e10cSrcweir // -----------------------------------------------------------------------------
501cdf0e10cSrcweir 
502cdf0e10cSrcweir void SAL_CALL TableModel::removePropertyChangeListener( const OUString& /*aPropertyName*/, const Reference< XPropertyChangeListener >& /*xListener*/ ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException)
503cdf0e10cSrcweir {
504cdf0e10cSrcweir }
505cdf0e10cSrcweir 
506cdf0e10cSrcweir // -----------------------------------------------------------------------------
507cdf0e10cSrcweir 
508cdf0e10cSrcweir void SAL_CALL TableModel::addVetoableChangeListener( const OUString& /*aPropertyName*/, const Reference< XVetoableChangeListener >& /*xListener*/ ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException)
509cdf0e10cSrcweir {
510cdf0e10cSrcweir }
511cdf0e10cSrcweir 
512cdf0e10cSrcweir // -----------------------------------------------------------------------------
513cdf0e10cSrcweir 
514cdf0e10cSrcweir void SAL_CALL TableModel::removeVetoableChangeListener( const OUString& /*aPropertyName*/, const Reference< XVetoableChangeListener >& /*xListener*/ ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException)
515cdf0e10cSrcweir {
516cdf0e10cSrcweir }
517cdf0e10cSrcweir 
518cdf0e10cSrcweir // -----------------------------------------------------------------------------
519cdf0e10cSrcweir // XFastPropertySet
520cdf0e10cSrcweir // -----------------------------------------------------------------------------
521cdf0e10cSrcweir 
522cdf0e10cSrcweir void SAL_CALL TableModel::setFastPropertyValue( ::sal_Int32 /*nHandle*/, const Any& /*aValue*/ ) throw (UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException, RuntimeException)
523cdf0e10cSrcweir {
524cdf0e10cSrcweir }
525cdf0e10cSrcweir 
526cdf0e10cSrcweir // -----------------------------------------------------------------------------
527cdf0e10cSrcweir 
528cdf0e10cSrcweir Any SAL_CALL TableModel::getFastPropertyValue( ::sal_Int32 /*nHandle*/ ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException)
529cdf0e10cSrcweir {
530cdf0e10cSrcweir 	Any aAny;
531cdf0e10cSrcweir 	return aAny;
532cdf0e10cSrcweir }
533cdf0e10cSrcweir 
534cdf0e10cSrcweir // -----------------------------------------------------------------------------
535cdf0e10cSrcweir // internals
536cdf0e10cSrcweir // -----------------------------------------------------------------------------
537cdf0e10cSrcweir 
538cdf0e10cSrcweir sal_Int32 TableModel::getRowCountImpl() const
539cdf0e10cSrcweir {
540cdf0e10cSrcweir 	return static_cast< sal_Int32 >( maRows.size() );
541cdf0e10cSrcweir }
542cdf0e10cSrcweir 
543cdf0e10cSrcweir // -----------------------------------------------------------------------------
544cdf0e10cSrcweir 
545cdf0e10cSrcweir sal_Int32 TableModel::getColumnCountImpl() const
546cdf0e10cSrcweir {
547cdf0e10cSrcweir 	return static_cast< sal_Int32 >( maColumns.size() );
548cdf0e10cSrcweir }
549cdf0e10cSrcweir 
550cdf0e10cSrcweir // -----------------------------------------------------------------------------
551cdf0e10cSrcweir 
552cdf0e10cSrcweir void TableModel::disposing()
553cdf0e10cSrcweir {
554cdf0e10cSrcweir 	if( !maRows.empty() )
555cdf0e10cSrcweir 	{
556cdf0e10cSrcweir 		RowVector::iterator aIter( maRows.begin() );
557cdf0e10cSrcweir 		while( aIter != maRows.end() )
558cdf0e10cSrcweir 			(*aIter++)->dispose();
559cdf0e10cSrcweir 		RowVector().swap(maRows);
560cdf0e10cSrcweir 	}
561cdf0e10cSrcweir 
562cdf0e10cSrcweir 	if( !maColumns.empty() )
563cdf0e10cSrcweir 	{
564cdf0e10cSrcweir 		ColumnVector::iterator aIter( maColumns.begin() );
565cdf0e10cSrcweir 		while( aIter != maColumns.end() )
566cdf0e10cSrcweir 			(*aIter++)->dispose();
567cdf0e10cSrcweir 		ColumnVector().swap(maColumns);
568cdf0e10cSrcweir 	}
569cdf0e10cSrcweir 
570cdf0e10cSrcweir 	if( mxTableColumns.is() )
571cdf0e10cSrcweir 	{
572cdf0e10cSrcweir 		mxTableColumns->dispose();
573cdf0e10cSrcweir 		mxTableColumns.clear();
574cdf0e10cSrcweir 	}
575cdf0e10cSrcweir 
576cdf0e10cSrcweir 	if( mxTableRows.is() )
577cdf0e10cSrcweir 	{
578cdf0e10cSrcweir 		mxTableRows->dispose();
579cdf0e10cSrcweir 		mxTableRows.clear();
580cdf0e10cSrcweir 	}
581cdf0e10cSrcweir 
582cdf0e10cSrcweir 	mpTableObj = 0;
583cdf0e10cSrcweir }
584cdf0e10cSrcweir 
585cdf0e10cSrcweir // -----------------------------------------------------------------------------
586cdf0e10cSrcweir // XBroadcaster
587cdf0e10cSrcweir // -----------------------------------------------------------------------------
588cdf0e10cSrcweir 
589cdf0e10cSrcweir void TableModel::lockBroadcasts() throw (RuntimeException)
590cdf0e10cSrcweir {
591cdf0e10cSrcweir 	OGuard aGuard( Application::GetSolarMutex() );
592cdf0e10cSrcweir 	++mnNotifyLock;
593cdf0e10cSrcweir }
594cdf0e10cSrcweir // -----------------------------------------------------------------------------
595cdf0e10cSrcweir 
596cdf0e10cSrcweir void TableModel::unlockBroadcasts() throw (RuntimeException)
597cdf0e10cSrcweir {
598cdf0e10cSrcweir 	OGuard aGuard( Application::GetSolarMutex() );
599cdf0e10cSrcweir 	--mnNotifyLock;
600cdf0e10cSrcweir 	if( mnNotifyLock <= 0 )
601cdf0e10cSrcweir 	{
602cdf0e10cSrcweir 		mnNotifyLock = 0;
603cdf0e10cSrcweir 		if( mbNotifyPending )
604cdf0e10cSrcweir 			notifyModification();
605cdf0e10cSrcweir 	}
606cdf0e10cSrcweir }
607cdf0e10cSrcweir 
608cdf0e10cSrcweir // -----------------------------------------------------------------------------
609cdf0e10cSrcweir #ifdef PLEASE_DEBUG_THE_TABLES
610cdf0e10cSrcweir #include <stdio.h>
611cdf0e10cSrcweir #endif
612cdf0e10cSrcweir 
613cdf0e10cSrcweir void TableModel::notifyModification()
614cdf0e10cSrcweir {
615cdf0e10cSrcweir 	::osl::MutexGuard guard( m_aMutex );
616cdf0e10cSrcweir 	if( (mnNotifyLock == 0) && mpTableObj && mpTableObj->GetModel() )
617cdf0e10cSrcweir 	{
618cdf0e10cSrcweir 		mbNotifyPending = false;
619cdf0e10cSrcweir 
620cdf0e10cSrcweir 		::cppu::OInterfaceContainerHelper * pModifyListeners = rBHelper.getContainer( XModifyListener::static_type() );
621cdf0e10cSrcweir 		if( pModifyListeners )
622cdf0e10cSrcweir 		{
623cdf0e10cSrcweir 			EventObject aSource;
624cdf0e10cSrcweir 			aSource.Source = static_cast< ::cppu::OWeakObject* >(this);
625cdf0e10cSrcweir 			pModifyListeners->notifyEach( &XModifyListener::modified, aSource);
626cdf0e10cSrcweir 		}
627cdf0e10cSrcweir 	}
628cdf0e10cSrcweir 	else
629cdf0e10cSrcweir 	{
630cdf0e10cSrcweir 		mbNotifyPending = true;
631cdf0e10cSrcweir 	}
632cdf0e10cSrcweir 
633cdf0e10cSrcweir #ifdef PLEASE_DEBUG_THE_TABLES
634cdf0e10cSrcweir 		FILE* file = fopen( "c:\\table.xml","w" );
635cdf0e10cSrcweir 
636cdf0e10cSrcweir 		const sal_Int32 nColCount = getColumnCountImpl();
637cdf0e10cSrcweir 		const sal_Int32 nRowCount = getRowCountImpl();
638cdf0e10cSrcweir 
639cdf0e10cSrcweir         fprintf( file, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\r" );
640cdf0e10cSrcweir         fprintf( file, "<table columns=\"%ld\" rows=\"%ld\" updated=\"%s\">\n\r", nColCount, nRowCount, mbNotifyPending ? "false" : "true");
641cdf0e10cSrcweir 
642cdf0e10cSrcweir 		for( sal_Int32 nCol = 0; nCol < nColCount; ++nCol )
643cdf0e10cSrcweir 		{
644cdf0e10cSrcweir 			fprintf( file, "<column this=\"%lx\"/>\n\r", maColumns[nCol].get() );
645cdf0e10cSrcweir 		}
646cdf0e10cSrcweir 
647cdf0e10cSrcweir 		// first check merged cells before and inside the removed rows
648cdf0e10cSrcweir 		for( sal_Int32 nRow = 0; nRow < nRowCount; ++nRow )
649cdf0e10cSrcweir 		{
650cdf0e10cSrcweir 			fprintf( file, "<row this=\"%lx\">\n\r", maRows[nRow].get() );
651cdf0e10cSrcweir 			for( sal_Int32 nCol = 0; nCol < nColCount; ++nCol )
652cdf0e10cSrcweir 			{
653cdf0e10cSrcweir 				CellRef xCell( getCell( nCol, nRow ) );
654cdf0e10cSrcweir 				fprintf( file, "<cell this=\"%lx\"", xCell.get() );
655cdf0e10cSrcweir 
656cdf0e10cSrcweir 				sal_Int32 nRowSpan = xCell->getRowSpan();
657cdf0e10cSrcweir 				sal_Int32 nColSpan = xCell->getColumnSpan();
658cdf0e10cSrcweir 				sal_Bool bMerged = xCell->isMerged();
659cdf0e10cSrcweir 
660cdf0e10cSrcweir 				if( nColSpan != 1 )
661cdf0e10cSrcweir 					fprintf( file, " column-span=\"%ld\"", nColSpan );
662cdf0e10cSrcweir 				if( nRowSpan != 1 )
663cdf0e10cSrcweir 					fprintf( file, " row-span=\"%ld\"", nRowSpan );
664cdf0e10cSrcweir 
665cdf0e10cSrcweir 				if( bMerged )
666cdf0e10cSrcweir 					fprintf( file, " merged=\"true\"" );
667cdf0e10cSrcweir 
668cdf0e10cSrcweir 				fprintf( file, "/>" );
669cdf0e10cSrcweir 			}
670cdf0e10cSrcweir 			fprintf( file, "\n\r</row>\n\r" );
671cdf0e10cSrcweir 		}
672cdf0e10cSrcweir 
673cdf0e10cSrcweir 		fprintf( file, "</table>\n\r" );
674cdf0e10cSrcweir 		fclose( file );
675cdf0e10cSrcweir #endif
676cdf0e10cSrcweir }
677cdf0e10cSrcweir 
678cdf0e10cSrcweir // -----------------------------------------------------------------------------
679cdf0e10cSrcweir 
680cdf0e10cSrcweir CellRef TableModel::getCell( sal_Int32 nCol, sal_Int32 nRow ) const
681cdf0e10cSrcweir {
682cdf0e10cSrcweir 	if( ((nRow >= 0) && (nRow < getRowCountImpl())) && (nCol >= 0) && (nCol < getColumnCountImpl()) )
683cdf0e10cSrcweir 	{
684cdf0e10cSrcweir 		return maRows[nRow]->maCells[nCol];
685cdf0e10cSrcweir 	}
686cdf0e10cSrcweir 	else
687cdf0e10cSrcweir 	{
688cdf0e10cSrcweir 		CellRef xRet;
689cdf0e10cSrcweir 		return xRet;
690cdf0e10cSrcweir 	}
691cdf0e10cSrcweir }
692cdf0e10cSrcweir 
693cdf0e10cSrcweir // -----------------------------------------------------------------------------
694cdf0e10cSrcweir /*
695cdf0e10cSrcweir bool TableModel::getCellPos( const CellRef& xCell, ::sal_Int32& rnCol, ::sal_Int32& rnRow ) const
696cdf0e10cSrcweir {
697cdf0e10cSrcweir 	const sal_Int32 nRowCount = getRowCount();
698cdf0e10cSrcweir 	const sal_Int32 nColCount = getColumnCount();
699cdf0e10cSrcweir 	for( rnRow = 0; rnRow < nRowCount; rnRow++ )
700cdf0e10cSrcweir 	{
701cdf0e10cSrcweir 		for( rnCol = 0; rnCol < nColCount; rnCol++ )
702cdf0e10cSrcweir 		{
703cdf0e10cSrcweir 			if( maRows[rnRow]->maCells[rnCol] == xCell )
704cdf0e10cSrcweir 			{
705cdf0e10cSrcweir 				return true;
706cdf0e10cSrcweir 			}
707cdf0e10cSrcweir 		}
708cdf0e10cSrcweir 	}
709cdf0e10cSrcweir 	return false;
710cdf0e10cSrcweir }
711cdf0e10cSrcweir */
712cdf0e10cSrcweir 
713cdf0e10cSrcweir // -----------------------------------------------------------------------------
714cdf0e10cSrcweir 
715cdf0e10cSrcweir CellRef TableModel::createCell()
716cdf0e10cSrcweir {
717cdf0e10cSrcweir 	CellRef xCell;
718cdf0e10cSrcweir 	if( mpTableObj )
719cdf0e10cSrcweir 		mpTableObj->createCell( xCell );
720cdf0e10cSrcweir 	return xCell;
721cdf0e10cSrcweir }
722cdf0e10cSrcweir 
723cdf0e10cSrcweir // -----------------------------------------------------------------------------
724cdf0e10cSrcweir 
725cdf0e10cSrcweir void TableModel::insertColumns( sal_Int32 nIndex, sal_Int32 nCount )
726cdf0e10cSrcweir {
727cdf0e10cSrcweir 	if( nCount && mpTableObj )
728cdf0e10cSrcweir 	{
729cdf0e10cSrcweir 		try
730cdf0e10cSrcweir 		{
731cdf0e10cSrcweir 			SdrModel* pModel = mpTableObj->GetModel();
732cdf0e10cSrcweir 
733cdf0e10cSrcweir 			TableModelNotifyGuard aGuard( this );
734cdf0e10cSrcweir 			nIndex = insert_range<ColumnVector,ColumnVector::iterator,TableColumnRef>( maColumns, nIndex, nCount );
735cdf0e10cSrcweir 
736cdf0e10cSrcweir 			sal_Int32 nRows = getRowCountImpl();
737cdf0e10cSrcweir 			while( nRows-- )
738cdf0e10cSrcweir 				maRows[nRows]->insertColumns( nIndex, nCount );
739cdf0e10cSrcweir 
740cdf0e10cSrcweir 			ColumnVector aNewColumns(nCount);
741cdf0e10cSrcweir 			for( sal_Int32 nOffset = 0; nOffset < nCount; ++nOffset )
742cdf0e10cSrcweir 			{
743cdf0e10cSrcweir 				TableColumnRef xNewCol( new TableColumn( this, nIndex+nOffset ) );
744cdf0e10cSrcweir 				maColumns[nIndex+nOffset] = xNewCol;
745cdf0e10cSrcweir 				aNewColumns[nOffset] = xNewCol;
746cdf0e10cSrcweir 			}
747cdf0e10cSrcweir 
748cdf0e10cSrcweir 			const bool bUndo = pModel && mpTableObj->IsInserted() && pModel->IsUndoEnabled();
749cdf0e10cSrcweir 			if( bUndo )
750cdf0e10cSrcweir 			{
751cdf0e10cSrcweir 				pModel->BegUndo( ImpGetResStr(STR_TABLE_INSCOL) );
752cdf0e10cSrcweir 				pModel->AddUndo( pModel->GetSdrUndoFactory().CreateUndoGeoObject(*mpTableObj) );
753cdf0e10cSrcweir 
754cdf0e10cSrcweir 				TableModelRef xThis( this );
755cdf0e10cSrcweir 
756cdf0e10cSrcweir 				nRows = getRowCountImpl();
757cdf0e10cSrcweir 				CellVector aNewCells( nCount * nRows );
758cdf0e10cSrcweir 				CellVector::iterator aCellIter( aNewCells.begin() );
759cdf0e10cSrcweir 
760cdf0e10cSrcweir 				nRows = getRowCountImpl();
761cdf0e10cSrcweir 				for( sal_Int32 nRow = 0; nRow < nRows; ++nRow )
762cdf0e10cSrcweir 				{
763cdf0e10cSrcweir 					for( sal_Int32 nOffset = 0; nOffset < nCount; ++nOffset )
764cdf0e10cSrcweir 						(*aCellIter++) = getCell( nIndex + nOffset, nRow );
765cdf0e10cSrcweir 				}
766cdf0e10cSrcweir 
767cdf0e10cSrcweir 				pModel->AddUndo( new InsertColUndo( xThis, nIndex, aNewColumns, aNewCells ) );
768cdf0e10cSrcweir 			}
769cdf0e10cSrcweir 
770cdf0e10cSrcweir 			const sal_Int32 nRowCount = getRowCountImpl();
771cdf0e10cSrcweir 			// check if cells merge over new columns
772cdf0e10cSrcweir 			for( sal_Int32 nCol = 0; nCol < nIndex; ++nCol )
773cdf0e10cSrcweir 			{
774cdf0e10cSrcweir 				for( sal_Int32 nRow = 0; nRow < nRowCount; ++nRow )
775cdf0e10cSrcweir 				{
776cdf0e10cSrcweir 					CellRef xCell( getCell( nCol, nRow ) );
777cdf0e10cSrcweir 					sal_Int32 nColSpan = (xCell.is() && !xCell->isMerged()) ? xCell->getColumnSpan() : 1;
778cdf0e10cSrcweir 					if( (nColSpan != 1) && ((nColSpan + nCol ) > nIndex) )
779cdf0e10cSrcweir 					{
780cdf0e10cSrcweir 						// cell merges over newly created columns, so add the new columns to the merged cell
781cdf0e10cSrcweir 						const sal_Int32 nRowSpan = xCell->getRowSpan();
782cdf0e10cSrcweir 						nColSpan += nCount;
783cdf0e10cSrcweir                         merge( nCol, nRow, nColSpan, nRowSpan );
784cdf0e10cSrcweir 					}
785cdf0e10cSrcweir 				}
786cdf0e10cSrcweir 			}
787cdf0e10cSrcweir 
788cdf0e10cSrcweir 			if( bUndo )
789cdf0e10cSrcweir 				pModel->EndUndo();
790cdf0e10cSrcweir 
791cdf0e10cSrcweir 			if( pModel )
792cdf0e10cSrcweir 			    pModel->SetChanged();
793cdf0e10cSrcweir 
794cdf0e10cSrcweir 		}
795cdf0e10cSrcweir 		catch( Exception& )
796cdf0e10cSrcweir 		{
797cdf0e10cSrcweir 			DBG_ERROR("sdr::table::TableModel::insertColumns(), exception caught!");
798cdf0e10cSrcweir 		}
799cdf0e10cSrcweir         updateColumns();
800cdf0e10cSrcweir 		setModified(sal_True);
801cdf0e10cSrcweir 	}
802cdf0e10cSrcweir }
803cdf0e10cSrcweir 
804cdf0e10cSrcweir // -----------------------------------------------------------------------------
805cdf0e10cSrcweir 
806cdf0e10cSrcweir void TableModel::removeColumns( sal_Int32 nIndex, sal_Int32 nCount )
807cdf0e10cSrcweir {
808cdf0e10cSrcweir 	sal_Int32 nColCount = getColumnCountImpl();
809cdf0e10cSrcweir 
810cdf0e10cSrcweir 	if( mpTableObj && nCount && (nIndex >= 0) && (nIndex < nColCount) )
811cdf0e10cSrcweir 	{
812cdf0e10cSrcweir 		try
813cdf0e10cSrcweir 		{
814cdf0e10cSrcweir 			TableModelNotifyGuard aGuard( this );
815cdf0e10cSrcweir 
816cdf0e10cSrcweir 			// clip removed columns to columns actually avalaible
817cdf0e10cSrcweir 			if( (nIndex + nCount) > nColCount )
818cdf0e10cSrcweir 				nCount = nColCount - nIndex;
819cdf0e10cSrcweir 
820cdf0e10cSrcweir 			sal_Int32 nRows = getRowCountImpl();
821cdf0e10cSrcweir 
822cdf0e10cSrcweir 			SdrModel* pModel = mpTableObj->GetModel();
823cdf0e10cSrcweir 
824cdf0e10cSrcweir 			const bool bUndo = pModel && mpTableObj->IsInserted() && pModel->IsUndoEnabled();
825cdf0e10cSrcweir 			if( bUndo  )
826cdf0e10cSrcweir 			{
827cdf0e10cSrcweir 				pModel->BegUndo( ImpGetResStr(STR_UNDO_COL_DELETE) );
828cdf0e10cSrcweir 				pModel->AddUndo( pModel->GetSdrUndoFactory().CreateUndoGeoObject(*mpTableObj) );
829cdf0e10cSrcweir 
830cdf0e10cSrcweir 				TableModelRef xThis( this );
831cdf0e10cSrcweir 				ColumnVector aRemovedCols( nCount );
832cdf0e10cSrcweir 				sal_Int32 nOffset;
833cdf0e10cSrcweir 				for( nOffset = 0; nOffset < nCount; ++nOffset )
834cdf0e10cSrcweir 				{
835cdf0e10cSrcweir 					aRemovedCols[nOffset] = maColumns[nIndex+nOffset];
836cdf0e10cSrcweir 				}
837cdf0e10cSrcweir 
838cdf0e10cSrcweir 				CellVector aRemovedCells( nCount * nRows );
839cdf0e10cSrcweir 				CellVector::iterator aCellIter( aRemovedCells.begin() );
840cdf0e10cSrcweir 				for( sal_Int32 nRow = 0; nRow < nRows; ++nRow )
841cdf0e10cSrcweir 				{
842cdf0e10cSrcweir 					for( nOffset = 0; nOffset < nCount; ++nOffset )
843cdf0e10cSrcweir 						(*aCellIter++) = getCell( nIndex + nOffset, nRow );
844cdf0e10cSrcweir 				}
845cdf0e10cSrcweir 
846cdf0e10cSrcweir 				pModel->AddUndo( new RemoveColUndo( xThis, nIndex, aRemovedCols, aRemovedCells ) );
847cdf0e10cSrcweir 			}
848cdf0e10cSrcweir 
849cdf0e10cSrcweir 			// only rows before and inside the removed rows are considered
850cdf0e10cSrcweir 			nColCount = nIndex + nCount + 1;
851cdf0e10cSrcweir 
852cdf0e10cSrcweir 			const sal_Int32 nRowCount = getRowCountImpl();
853cdf0e10cSrcweir 
854cdf0e10cSrcweir 			// first check merged cells before and inside the removed rows
855cdf0e10cSrcweir 			for( sal_Int32 nCol = 0; nCol < nColCount; ++nCol )
856cdf0e10cSrcweir 			{
857cdf0e10cSrcweir 				for( sal_Int32 nRow = 0; nRow < nRowCount; ++nRow )
858cdf0e10cSrcweir 				{
859cdf0e10cSrcweir 					CellRef xCell( getCell( nCol, nRow ) );
860cdf0e10cSrcweir 					sal_Int32 nColSpan = (xCell.is() && !xCell->isMerged()) ? xCell->getColumnSpan() : 1;
861cdf0e10cSrcweir 					if( nColSpan <= 1 )
862cdf0e10cSrcweir 						continue;
863cdf0e10cSrcweir 
864cdf0e10cSrcweir 					if( nCol >= nIndex )
865cdf0e10cSrcweir 					{
866cdf0e10cSrcweir 						// current cell is inside the removed columns
867cdf0e10cSrcweir 						if( (nCol + nColSpan) > ( nIndex + nCount ) )
868cdf0e10cSrcweir 						{
869cdf0e10cSrcweir 							// current cells merges with columns after the removed columns
870cdf0e10cSrcweir 							const sal_Int32 nRemove = nCount - nCol + nIndex;
871cdf0e10cSrcweir 
872cdf0e10cSrcweir 							CellRef xTargetCell( getCell( nIndex + nCount, nRow ) );
873cdf0e10cSrcweir 							if( xTargetCell.is() )
874cdf0e10cSrcweir 							{
875cdf0e10cSrcweir 								if( bUndo )
876cdf0e10cSrcweir 									xTargetCell->AddUndo();
877cdf0e10cSrcweir 								xTargetCell->merge( nColSpan - nRemove, xCell->getRowSpan() );
878cdf0e10cSrcweir 								xTargetCell->replaceContentAndFormating( xCell );
879cdf0e10cSrcweir 							}
880cdf0e10cSrcweir 						}
881cdf0e10cSrcweir 					}
882cdf0e10cSrcweir 					else if( nColSpan > (nIndex - nCol) )
883cdf0e10cSrcweir 					{
884cdf0e10cSrcweir 						// current cells spans inside the removed columns, so adjust
885cdf0e10cSrcweir 						const sal_Int32 nRemove = ::std::min( nCount, nCol + nColSpan - nIndex );
886cdf0e10cSrcweir 						if( bUndo )
887cdf0e10cSrcweir 							xCell->AddUndo();
888cdf0e10cSrcweir 						xCell->merge( nColSpan - nRemove, xCell->getRowSpan() );
889cdf0e10cSrcweir 					}
890cdf0e10cSrcweir 				}
891cdf0e10cSrcweir 			}
892cdf0e10cSrcweir 
893cdf0e10cSrcweir 			// now remove the columns
894cdf0e10cSrcweir 			remove_range<ColumnVector,ColumnVector::iterator>( maColumns, nIndex, nCount );
895cdf0e10cSrcweir 			while( nRows-- )
896cdf0e10cSrcweir 				maRows[nRows]->removeColumns( nIndex, nCount );
897cdf0e10cSrcweir 
898cdf0e10cSrcweir 			if( bUndo )
899cdf0e10cSrcweir 				pModel->EndUndo();
900cdf0e10cSrcweir 
901cdf0e10cSrcweir 			if( pModel )
902cdf0e10cSrcweir                 pModel->SetChanged();
903cdf0e10cSrcweir 		}
904cdf0e10cSrcweir 		catch( Exception& )
905cdf0e10cSrcweir 		{
906cdf0e10cSrcweir 			DBG_ERROR("sdr::table::TableModel::removeColumns(), exception caught!");
907cdf0e10cSrcweir 		}
908cdf0e10cSrcweir 
909cdf0e10cSrcweir         updateColumns();
910cdf0e10cSrcweir 		setModified(sal_True);
911cdf0e10cSrcweir 	}
912cdf0e10cSrcweir }
913cdf0e10cSrcweir 
914cdf0e10cSrcweir // -----------------------------------------------------------------------------
915cdf0e10cSrcweir 
916cdf0e10cSrcweir void TableModel::insertRows( sal_Int32 nIndex, sal_Int32 nCount )
917cdf0e10cSrcweir {
918cdf0e10cSrcweir 	if( nCount && mpTableObj )
919cdf0e10cSrcweir 	{
920cdf0e10cSrcweir     	SdrModel* pModel = mpTableObj->GetModel();
921cdf0e10cSrcweir 		const bool bUndo = pModel && mpTableObj->IsInserted() && pModel->IsUndoEnabled();
922cdf0e10cSrcweir 		try
923cdf0e10cSrcweir 		{
924cdf0e10cSrcweir 			TableModelNotifyGuard aGuard( this );
925cdf0e10cSrcweir 
926cdf0e10cSrcweir 			nIndex = insert_range<RowVector,RowVector::iterator,TableRowRef>( maRows, nIndex, nCount );
927cdf0e10cSrcweir 
928cdf0e10cSrcweir 			RowVector aNewRows(nCount);
929cdf0e10cSrcweir 			const sal_Int32 nColCount = getColumnCountImpl();
930cdf0e10cSrcweir 			for( sal_Int32 nOffset = 0; nOffset < nCount; ++nOffset )
931cdf0e10cSrcweir 			{
932cdf0e10cSrcweir 				TableRowRef xNewRow( new TableRow( this, nIndex+nOffset, nColCount ) );
933cdf0e10cSrcweir 				maRows[nIndex+nOffset] = xNewRow;
934cdf0e10cSrcweir 				aNewRows[nOffset] = xNewRow;
935cdf0e10cSrcweir 			}
936cdf0e10cSrcweir 
937cdf0e10cSrcweir 			if( bUndo )
938cdf0e10cSrcweir 			{
939cdf0e10cSrcweir 				pModel->BegUndo( ImpGetResStr(STR_TABLE_INSROW) );
940cdf0e10cSrcweir 				pModel->AddUndo( pModel->GetSdrUndoFactory().CreateUndoGeoObject(*mpTableObj) );
941cdf0e10cSrcweir 				TableModelRef xThis( this );
942cdf0e10cSrcweir 				pModel->AddUndo( new InsertRowUndo( xThis, nIndex, aNewRows ) );
943cdf0e10cSrcweir 			}
944cdf0e10cSrcweir 
945cdf0e10cSrcweir 			// check if cells merge over new columns
946cdf0e10cSrcweir 			for( sal_Int32 nRow = 0; nRow < nIndex; ++nRow )
947cdf0e10cSrcweir 			{
948cdf0e10cSrcweir 				for( sal_Int32 nCol = 0; nCol < nColCount; ++nCol )
949cdf0e10cSrcweir 				{
950cdf0e10cSrcweir 					CellRef xCell( getCell( nCol, nRow ) );
951cdf0e10cSrcweir 					sal_Int32 nRowSpan = (xCell.is() && !xCell->isMerged()) ? xCell->getRowSpan() : 1;
952cdf0e10cSrcweir 					if( (nRowSpan > 1) && ((nRowSpan + nRow) > nIndex) )
953cdf0e10cSrcweir 					{
954cdf0e10cSrcweir 						// cell merges over newly created columns, so add the new columns to the merged cell
955cdf0e10cSrcweir 						const sal_Int32 nColSpan = xCell->getColumnSpan();
956cdf0e10cSrcweir 						nRowSpan += nCount;
957cdf0e10cSrcweir                         merge( nCol, nRow, nColSpan, nRowSpan );
958cdf0e10cSrcweir 					}
959cdf0e10cSrcweir 				}
960cdf0e10cSrcweir 			}
961cdf0e10cSrcweir 		}
962cdf0e10cSrcweir 		catch( Exception& )
963cdf0e10cSrcweir 		{
964cdf0e10cSrcweir 			DBG_ERROR("sdr::table::TableModel::insertRows(), exception caught!");
965cdf0e10cSrcweir 		}
966cdf0e10cSrcweir 		if( bUndo )
967cdf0e10cSrcweir 			pModel->EndUndo();
968cdf0e10cSrcweir 
969cdf0e10cSrcweir 		if( pModel )
970cdf0e10cSrcweir 			pModel->SetChanged();
971cdf0e10cSrcweir 
972cdf0e10cSrcweir         updateRows();
973cdf0e10cSrcweir 		setModified(sal_True);
974cdf0e10cSrcweir 	}
975cdf0e10cSrcweir }
976cdf0e10cSrcweir 
977cdf0e10cSrcweir // -----------------------------------------------------------------------------
978cdf0e10cSrcweir 
979cdf0e10cSrcweir void TableModel::removeRows( sal_Int32 nIndex, sal_Int32 nCount )
980cdf0e10cSrcweir {
981cdf0e10cSrcweir 	sal_Int32 nRowCount = getRowCountImpl();
982cdf0e10cSrcweir 
983cdf0e10cSrcweir 	if( mpTableObj && nCount && (nIndex >= 0) && (nIndex < nRowCount) )
984cdf0e10cSrcweir 	{
985cdf0e10cSrcweir 		SdrModel* pModel = mpTableObj->GetModel();
986cdf0e10cSrcweir 		const bool bUndo = pModel && mpTableObj->IsInserted()&& pModel->IsUndoEnabled();
987cdf0e10cSrcweir 
988cdf0e10cSrcweir 		try
989cdf0e10cSrcweir 		{
990cdf0e10cSrcweir 			TableModelNotifyGuard aGuard( this );
991cdf0e10cSrcweir 
992cdf0e10cSrcweir 			// clip removed rows to rows actually avalaible
993cdf0e10cSrcweir 			if( (nIndex + nCount) > nRowCount )
994cdf0e10cSrcweir 				nCount = nRowCount - nIndex;
995cdf0e10cSrcweir 
996cdf0e10cSrcweir 			if( bUndo )
997cdf0e10cSrcweir 			{
998cdf0e10cSrcweir 				pModel->BegUndo( ImpGetResStr(STR_UNDO_ROW_DELETE) );
999cdf0e10cSrcweir 				pModel->AddUndo( pModel->GetSdrUndoFactory().CreateUndoGeoObject(*mpTableObj) );
1000cdf0e10cSrcweir 
1001cdf0e10cSrcweir 				TableModelRef xThis( this );
1002cdf0e10cSrcweir 
1003cdf0e10cSrcweir 				RowVector aRemovedRows( nCount );
1004cdf0e10cSrcweir 				for( sal_Int32 nOffset = 0; nOffset < nCount; ++nOffset )
1005cdf0e10cSrcweir 					aRemovedRows[nOffset] = maRows[nIndex+nOffset];
1006cdf0e10cSrcweir 
1007cdf0e10cSrcweir 				pModel->AddUndo( new RemoveRowUndo( xThis, nIndex, aRemovedRows ) );
1008cdf0e10cSrcweir 			}
1009cdf0e10cSrcweir 
1010cdf0e10cSrcweir 			// only rows before and inside the removed rows are considered
1011cdf0e10cSrcweir 			nRowCount = nIndex + nCount + 1;
1012cdf0e10cSrcweir 
1013cdf0e10cSrcweir 			const sal_Int32 nColCount = getColumnCountImpl();
1014cdf0e10cSrcweir 
1015cdf0e10cSrcweir 			// first check merged cells before and inside the removed rows
1016cdf0e10cSrcweir 			for( sal_Int32 nRow = 0; nRow < nRowCount; ++nRow )
1017cdf0e10cSrcweir 			{
1018cdf0e10cSrcweir 				for( sal_Int32 nCol = 0; nCol < nColCount; ++nCol )
1019cdf0e10cSrcweir 				{
1020cdf0e10cSrcweir 					CellRef xCell( getCell( nCol, nRow ) );
1021cdf0e10cSrcweir 					sal_Int32 nRowSpan = (xCell.is() && !xCell->isMerged()) ? xCell->getRowSpan() : 1;
1022cdf0e10cSrcweir 					if( nRowSpan <= 1 )
1023cdf0e10cSrcweir 						continue;
1024cdf0e10cSrcweir 
1025cdf0e10cSrcweir 					if( nRow >= nIndex )
1026cdf0e10cSrcweir 					{
1027cdf0e10cSrcweir 						// current cell is inside the removed rows
1028cdf0e10cSrcweir 						if( (nRow + nRowSpan) > (nIndex + nCount) )
1029cdf0e10cSrcweir 						{
1030cdf0e10cSrcweir 							// current cells merges with rows after the removed rows
1031cdf0e10cSrcweir 							const sal_Int32 nRemove = nCount - nRow + nIndex;
1032cdf0e10cSrcweir 
1033cdf0e10cSrcweir 							CellRef xTargetCell( getCell( nCol, nIndex + nCount ) );
1034cdf0e10cSrcweir 							if( xTargetCell.is() )
1035cdf0e10cSrcweir 							{
1036cdf0e10cSrcweir 								if( bUndo )
1037cdf0e10cSrcweir 									xTargetCell->AddUndo();
1038cdf0e10cSrcweir 								xTargetCell->merge( xCell->getColumnSpan(), nRowSpan - nRemove );
1039cdf0e10cSrcweir 								xTargetCell->replaceContentAndFormating( xCell );
1040cdf0e10cSrcweir 							}
1041cdf0e10cSrcweir 						}
1042cdf0e10cSrcweir 					}
1043cdf0e10cSrcweir 					else if( nRowSpan > (nIndex - nRow) )
1044cdf0e10cSrcweir 					{
1045cdf0e10cSrcweir 						// current cells spans inside the removed rows, so adjust
1046cdf0e10cSrcweir 						const sal_Int32 nRemove = ::std::min( nCount, nRow + nRowSpan - nIndex );
1047cdf0e10cSrcweir 						if( bUndo )
1048cdf0e10cSrcweir 							xCell->AddUndo();
1049cdf0e10cSrcweir 						xCell->merge( xCell->getColumnSpan(), nRowSpan - nRemove );
1050cdf0e10cSrcweir 					}
1051cdf0e10cSrcweir 				}
1052cdf0e10cSrcweir 			}
1053cdf0e10cSrcweir 
1054cdf0e10cSrcweir 			// now remove the rows
1055cdf0e10cSrcweir 			remove_range<RowVector,RowVector::iterator>( maRows, nIndex, nCount );
1056cdf0e10cSrcweir 
1057cdf0e10cSrcweir 			if( bUndo )
1058cdf0e10cSrcweir 				pModel->EndUndo();
1059cdf0e10cSrcweir 
1060cdf0e10cSrcweir 			if( pModel )
1061cdf0e10cSrcweir                 pModel->SetChanged();
1062cdf0e10cSrcweir 		}
1063cdf0e10cSrcweir 		catch( Exception& )
1064cdf0e10cSrcweir 		{
1065cdf0e10cSrcweir 			DBG_ERROR("sdr::table::TableModel::removeRows(), exception caught!");
1066cdf0e10cSrcweir 		}
1067cdf0e10cSrcweir 
1068cdf0e10cSrcweir         updateRows();
1069cdf0e10cSrcweir 		setModified(sal_True);
1070cdf0e10cSrcweir 	}
1071cdf0e10cSrcweir }
1072cdf0e10cSrcweir 
1073cdf0e10cSrcweir // -----------------------------------------------------------------------------
1074cdf0e10cSrcweir 
1075cdf0e10cSrcweir TableRowRef TableModel::getRow( sal_Int32 nRow ) const throw (IndexOutOfBoundsException)
1076cdf0e10cSrcweir {
1077cdf0e10cSrcweir 	if( (nRow >= 0) && (nRow < getRowCountImpl()) )
1078cdf0e10cSrcweir 		return maRows[nRow];
1079cdf0e10cSrcweir 
1080cdf0e10cSrcweir 	throw IndexOutOfBoundsException();
1081cdf0e10cSrcweir }
1082cdf0e10cSrcweir 
1083cdf0e10cSrcweir // -----------------------------------------------------------------------------
1084cdf0e10cSrcweir 
1085cdf0e10cSrcweir TableColumnRef TableModel::getColumn( sal_Int32 nColumn ) const throw (IndexOutOfBoundsException)
1086cdf0e10cSrcweir {
1087cdf0e10cSrcweir 	if( (nColumn >= 0) && (nColumn < getColumnCountImpl()) )
1088cdf0e10cSrcweir 		return maColumns[nColumn];
1089cdf0e10cSrcweir 
1090cdf0e10cSrcweir 	throw IndexOutOfBoundsException();
1091cdf0e10cSrcweir }
1092cdf0e10cSrcweir 
1093cdf0e10cSrcweir // -----------------------------------------------------------------------------
1094cdf0e10cSrcweir 
1095cdf0e10cSrcweir /** deletes rows and columns that are completly merged. Must be called between BegUndo/EndUndo! */
1096cdf0e10cSrcweir void TableModel::optimize()
1097cdf0e10cSrcweir {
1098cdf0e10cSrcweir 	TableModelNotifyGuard aGuard( this );
1099cdf0e10cSrcweir 
1100cdf0e10cSrcweir 	bool bWasModified = false;
1101cdf0e10cSrcweir 
1102cdf0e10cSrcweir 	if( !maRows.empty() && !maColumns.empty() )
1103cdf0e10cSrcweir 	{
1104cdf0e10cSrcweir 		sal_Int32 nCol = getColumnCountImpl() - 1;
1105cdf0e10cSrcweir 		while( nCol > 0 )
1106cdf0e10cSrcweir 		{
1107cdf0e10cSrcweir 			bool bEmpty = true;
1108cdf0e10cSrcweir 			for( sal_Int32 nRow = 0; (nRow < getRowCountImpl()) && bEmpty; nRow++ )
1109cdf0e10cSrcweir 			{
1110cdf0e10cSrcweir 				Reference< XMergeableCell > xCell( getCellByPosition( nCol, nRow ), UNO_QUERY );
1111cdf0e10cSrcweir 				if( xCell.is() && !xCell->isMerged() )
1112cdf0e10cSrcweir 					bEmpty = false;
1113cdf0e10cSrcweir 			}
1114cdf0e10cSrcweir 
1115cdf0e10cSrcweir 			if( bEmpty )
1116cdf0e10cSrcweir 			{
1117cdf0e10cSrcweir 				if( nCol > 0 ) try
1118cdf0e10cSrcweir 				{
1119cdf0e10cSrcweir 					const OUString sWidth( RTL_CONSTASCII_USTRINGPARAM("Width") );
1120cdf0e10cSrcweir 					sal_Int32 nWidth1 = 0, nWidth2 = 0;
1121cdf0e10cSrcweir 					Reference< XPropertySet > xSet1( static_cast< XCellRange* >( maColumns[nCol].get() ), UNO_QUERY_THROW );
1122cdf0e10cSrcweir 					Reference< XPropertySet > xSet2( static_cast< XCellRange* >( maColumns[nCol-1].get() ), UNO_QUERY_THROW );
1123cdf0e10cSrcweir 					xSet1->getPropertyValue( sWidth ) >>= nWidth1;
1124cdf0e10cSrcweir 					xSet2->getPropertyValue( sWidth ) >>= nWidth2;
1125cdf0e10cSrcweir 					nWidth1 += nWidth2;
1126cdf0e10cSrcweir 					xSet2->setPropertyValue( sWidth, Any( nWidth1 ) );
1127cdf0e10cSrcweir 				}
1128cdf0e10cSrcweir 				catch( Exception& e )
1129cdf0e10cSrcweir 				{
1130cdf0e10cSrcweir 					(void)e;
1131cdf0e10cSrcweir 					DBG_ERROR("svx::TableModel::optimize(), exception caught!");
1132cdf0e10cSrcweir 				}
1133cdf0e10cSrcweir 
1134cdf0e10cSrcweir 				removeColumns( nCol, 1 );
1135cdf0e10cSrcweir 				bWasModified = true;
1136cdf0e10cSrcweir 			}
1137cdf0e10cSrcweir 
1138cdf0e10cSrcweir 			nCol--;
1139cdf0e10cSrcweir 		}
1140cdf0e10cSrcweir 
1141cdf0e10cSrcweir 		sal_Int32 nRow = getRowCountImpl() - 1;
1142cdf0e10cSrcweir 		while( nRow > 0 )
1143cdf0e10cSrcweir 		{
1144cdf0e10cSrcweir 			bool bEmpty = true;
1145cdf0e10cSrcweir 			for( nCol = 0; (nCol < getColumnCountImpl()) && bEmpty; nCol++ )
1146cdf0e10cSrcweir 			{
1147cdf0e10cSrcweir 				Reference< XMergeableCell > xCell( getCellByPosition( nCol, nRow ), UNO_QUERY );
1148cdf0e10cSrcweir 				if( xCell.is() && !xCell->isMerged() )
1149cdf0e10cSrcweir 					bEmpty = false;
1150cdf0e10cSrcweir 			}
1151cdf0e10cSrcweir 
1152cdf0e10cSrcweir 			if( bEmpty )
1153cdf0e10cSrcweir 			{
1154cdf0e10cSrcweir 				if( nRow > 0 ) try
1155cdf0e10cSrcweir 				{
1156cdf0e10cSrcweir 					const OUString sHeight( RTL_CONSTASCII_USTRINGPARAM("Height") );
1157cdf0e10cSrcweir 					sal_Int32 nHeight1 = 0, nHeight2 = 0;
1158cdf0e10cSrcweir 					Reference< XPropertySet > xSet1( static_cast< XCellRange* >( maRows[nRow].get() ), UNO_QUERY_THROW );
1159cdf0e10cSrcweir 					Reference< XPropertySet > xSet2( static_cast< XCellRange* >( maRows[nRow-1].get() ), UNO_QUERY_THROW );
1160cdf0e10cSrcweir 					xSet1->getPropertyValue( sHeight ) >>= nHeight1;
1161cdf0e10cSrcweir 					xSet2->getPropertyValue( sHeight ) >>= nHeight2;
1162cdf0e10cSrcweir 					nHeight1 += nHeight2;
1163cdf0e10cSrcweir 					xSet2->setPropertyValue( sHeight, Any( nHeight1 ) );
1164cdf0e10cSrcweir 				}
1165cdf0e10cSrcweir 				catch( Exception& e )
1166cdf0e10cSrcweir 				{
1167cdf0e10cSrcweir 					(void)e;
1168cdf0e10cSrcweir 					DBG_ERROR("svx::TableModel::optimize(), exception caught!");
1169cdf0e10cSrcweir 				}
1170cdf0e10cSrcweir 
1171cdf0e10cSrcweir 				removeRows( nRow, 1 );
1172cdf0e10cSrcweir 				bWasModified = true;
1173cdf0e10cSrcweir 			}
1174cdf0e10cSrcweir 
1175cdf0e10cSrcweir 			nRow--;
1176cdf0e10cSrcweir 		}
1177cdf0e10cSrcweir 	}
1178cdf0e10cSrcweir 	if( bWasModified )
1179cdf0e10cSrcweir 		setModified(sal_True);
1180cdf0e10cSrcweir }
1181cdf0e10cSrcweir 
1182cdf0e10cSrcweir // -----------------------------------------------------------------------------
1183cdf0e10cSrcweir 
1184cdf0e10cSrcweir void TableModel::merge( sal_Int32 nCol, sal_Int32 nRow, sal_Int32 nColSpan, sal_Int32 nRowSpan )
1185cdf0e10cSrcweir {
1186cdf0e10cSrcweir 	SdrModel* pModel = mpTableObj->GetModel();
1187cdf0e10cSrcweir 
1188cdf0e10cSrcweir     const bool bUndo = pModel && mpTableObj->IsInserted() && pModel->IsUndoEnabled();
1189cdf0e10cSrcweir 
1190cdf0e10cSrcweir 	const sal_Int32 nLastRow = nRow + nRowSpan;
1191cdf0e10cSrcweir 	const sal_Int32 nLastCol = nCol + nColSpan;
1192cdf0e10cSrcweir 
1193cdf0e10cSrcweir     if( (nLastRow > getRowCount()) || (nLastCol > getRowCount() ) )
1194cdf0e10cSrcweir     {
1195cdf0e10cSrcweir         DBG_ERROR("TableModel::merge(), merge beyound the table!");
1196cdf0e10cSrcweir     }
1197cdf0e10cSrcweir 
1198cdf0e10cSrcweir 	// merge first cell
1199cdf0e10cSrcweir 	CellRef xOriginCell( dynamic_cast< Cell* >( getCellByPosition( nCol, nRow ).get() ) );
1200cdf0e10cSrcweir 	if( xOriginCell.is() )
1201cdf0e10cSrcweir 	{
1202cdf0e10cSrcweir 	    if( bUndo )
1203cdf0e10cSrcweir 		    xOriginCell->AddUndo();
1204cdf0e10cSrcweir 		xOriginCell->merge( nColSpan, nRowSpan );
1205cdf0e10cSrcweir 	}
1206cdf0e10cSrcweir 
1207cdf0e10cSrcweir 	sal_Int32 nTempCol = nCol + 1;
1208cdf0e10cSrcweir 
1209cdf0e10cSrcweir 	// merge remaining cells
1210cdf0e10cSrcweir 	for( ; nRow < nLastRow; nRow++ )
1211cdf0e10cSrcweir 	{
1212cdf0e10cSrcweir 		for( ; nTempCol < nLastCol; nTempCol++ )
1213cdf0e10cSrcweir 		{
1214cdf0e10cSrcweir 			CellRef xCell( dynamic_cast< Cell* >( getCellByPosition( nTempCol, nRow ).get() ) );
1215cdf0e10cSrcweir 			if( xCell.is() && !xCell->isMerged() )
1216cdf0e10cSrcweir 			{
1217cdf0e10cSrcweir 			    if( bUndo )
1218cdf0e10cSrcweir 				    xCell->AddUndo();
1219cdf0e10cSrcweir 				xCell->setMerged();
1220cdf0e10cSrcweir 				xOriginCell->mergeContent( xCell );
1221cdf0e10cSrcweir 			}
1222cdf0e10cSrcweir 		}
1223cdf0e10cSrcweir 		nTempCol = nCol;
1224cdf0e10cSrcweir 	}
1225cdf0e10cSrcweir }
1226cdf0e10cSrcweir 
1227cdf0e10cSrcweir 
1228cdf0e10cSrcweir // -----------------------------------------------------------------------------
1229cdf0e10cSrcweir 
1230cdf0e10cSrcweir void TableModel::updateRows()
1231cdf0e10cSrcweir {
1232cdf0e10cSrcweir     sal_Int32 nRow = 0;
1233cdf0e10cSrcweir     RowVector::iterator iter = maRows.begin();
1234cdf0e10cSrcweir     while( iter != maRows.end() )
1235cdf0e10cSrcweir     {
1236cdf0e10cSrcweir         (*iter++)->mnRow = nRow++;
1237cdf0e10cSrcweir     }
1238cdf0e10cSrcweir }
1239cdf0e10cSrcweir 
1240cdf0e10cSrcweir // -----------------------------------------------------------------------------
1241cdf0e10cSrcweir 
1242cdf0e10cSrcweir void TableModel::updateColumns()
1243cdf0e10cSrcweir {
1244cdf0e10cSrcweir     sal_Int32 nColumn = 0;
1245cdf0e10cSrcweir     ColumnVector::iterator iter = maColumns.begin();
1246cdf0e10cSrcweir     while( iter != maColumns.end() )
1247cdf0e10cSrcweir     {
1248cdf0e10cSrcweir         (*iter++)->mnColumn = nColumn++;
1249cdf0e10cSrcweir     }
1250cdf0e10cSrcweir }
1251cdf0e10cSrcweir 
1252cdf0e10cSrcweir // -----------------------------------------------------------------------------
1253cdf0e10cSrcweir 
1254cdf0e10cSrcweir } }
1255