xref: /aoo42x/main/svx/source/table/tablerow.cxx (revision f6e50924)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_svx.hxx"
26 
27 #include <com/sun/star/lang/DisposedException.hpp>
28 
29 #include "cell.hxx"
30 #include "tablerow.hxx"
31 #include "tableundo.hxx"
32 #include "svx/svdmodel.hxx"
33 #include "svx/svdotable.hxx"
34 
35 // -----------------------------------------------------------------------------
36 
37 using ::rtl::OUString;
38 using namespace ::com::sun::star::uno;
39 using namespace ::com::sun::star::lang;
40 using namespace ::com::sun::star::container;
41 using namespace ::com::sun::star::table;
42 using namespace ::com::sun::star::beans;
43 
44 // -----------------------------------------------------------------------------
45 
46 namespace sdr { namespace table {
47 
48 const sal_Int32 Property_Height = 0;
49 const sal_Int32 Property_OptimalHeight = 1;
50 const sal_Int32 Property_IsVisible = 2;
51 const sal_Int32 Property_IsStartOfNewPage = 3;
52 
53 // -----------------------------------------------------------------------------
54 // TableRow
55 // -----------------------------------------------------------------------------
56 
TableRow(const TableModelRef & xTableModel,sal_Int32 nRow,sal_Int32 nColumns)57 TableRow::TableRow( const TableModelRef& xTableModel, sal_Int32 nRow, sal_Int32 nColumns )
58 : TableRowBase( getStaticPropertySetInfo() )
59 , mxTableModel( xTableModel )
60 , mnRow( nRow )
61 , mnHeight( 0 )
62 , mbOptimalHeight( sal_True )
63 , mbIsVisible( sal_True )
64 , mbIsStartOfNewPage( sal_False )
65 {
66 	if( nColumns < 20 )
67 		maCells.reserve( 20 );
68 
69 	if( nColumns )
70 	{
71 		maCells.resize( nColumns );
72 		while( nColumns-- )
73 			maCells[ nColumns ] = mxTableModel->createCell();
74 	}
75 }
76 
77 // -----------------------------------------------------------------------------
78 
~TableRow()79 TableRow::~TableRow()
80 {
81 }
82 
83 // -----------------------------------------------------------------------------
84 
dispose()85 void TableRow::dispose()
86 {
87 	mxTableModel.clear();
88 	if( !maCells.empty() )
89 	{
90 		CellVector::iterator aIter( maCells.begin() );
91 		while( aIter != maCells.end() )
92 			(*aIter++)->dispose();
93 		CellVector().swap(maCells);
94 	}
95 }
96 
97 // -----------------------------------------------------------------------------
98 
throwIfDisposed() const99 void TableRow::throwIfDisposed() const throw (::com::sun::star::uno::RuntimeException)
100 {
101 	if( !mxTableModel.is() )
102 		throw DisposedException();
103 }
104 
105 // -----------------------------------------------------------------------------
106 
operator =(const TableRow & r)107 TableRow& TableRow::operator=( const TableRow& r )
108 {
109 	mnHeight = r.mnHeight;
110 	mbOptimalHeight = r.mbOptimalHeight;
111 	mbIsVisible = r.mbIsVisible;
112 	mbIsStartOfNewPage = r.mbIsStartOfNewPage;
113 	maName = r.maName;
114 
115 	return *this;
116 }
117 
118 // -----------------------------------------------------------------------------
119 
insertColumns(sal_Int32 nIndex,sal_Int32 nCount,CellVector::iterator * pIter)120 void TableRow::insertColumns( sal_Int32 nIndex, sal_Int32 nCount, CellVector::iterator* pIter /* = 0 */  )
121 {
122 	throwIfDisposed();
123 	if( nCount )
124 	{
125 		if( nIndex >= static_cast< sal_Int32 >( maCells.size() ) )
126 			nIndex = static_cast< sal_Int32 >( maCells.size() );
127 		if ( pIter )
128 			maCells.insert( maCells.begin() + nIndex, *pIter, (*pIter) + nCount );
129 		else
130 		{
131 			maCells.reserve( maCells.size() + nCount );
132 			for ( sal_Int32 i = 0; i < nCount; i++ )
133 				maCells.insert( maCells.begin() + nIndex + i, mxTableModel->createCell() );
134 		}
135 	}
136 }
137 
138 // -----------------------------------------------------------------------------
139 
removeColumns(sal_Int32 nIndex,sal_Int32 nCount)140 void TableRow::removeColumns( sal_Int32 nIndex, sal_Int32 nCount )
141 {
142 	throwIfDisposed();
143 	if( (nCount >= 0) && ( nIndex >= 0) )
144 	{
145 		if( (nIndex + nCount) < static_cast< sal_Int32 >( maCells.size() ) )
146 		{
147 			CellVector::iterator aBegin( maCells.begin() );
148 			while( nIndex-- && (aBegin != maCells.end()) )
149 				aBegin++;
150 
151 			if( nCount > 1 )
152 			{
153 				CellVector::iterator aEnd( aBegin );
154 				while( nCount-- && (aEnd != maCells.end()) )
155 					aEnd++;
156 				maCells.erase( aBegin, aEnd );
157 			}
158 			else
159 			{
160 				maCells.erase( aBegin );
161 			}
162 		}
163 		else
164 		{
165 			maCells.resize( nIndex );
166 		}
167 	}
168 }
169 
170 // -----------------------------------------------------------------------------
171 // XCellRange
172 // -----------------------------------------------------------------------------
173 
getCellByPosition(sal_Int32 nColumn,sal_Int32 nRow)174 Reference< XCell > SAL_CALL TableRow::getCellByPosition( sal_Int32 nColumn, sal_Int32 nRow ) throw (IndexOutOfBoundsException, RuntimeException)
175 {
176 	throwIfDisposed();
177 	if( nRow != 0 )
178 		throw IndexOutOfBoundsException();
179 
180 	return mxTableModel->getCellByPosition( nColumn, mnRow );
181 }
182 
183 // -----------------------------------------------------------------------------
184 
getCellRangeByPosition(sal_Int32 nLeft,sal_Int32 nTop,sal_Int32 nRight,sal_Int32 nBottom)185 Reference< XCellRange > SAL_CALL TableRow::getCellRangeByPosition( sal_Int32 nLeft, sal_Int32 nTop, sal_Int32 nRight, sal_Int32 nBottom ) throw (IndexOutOfBoundsException, RuntimeException)
186 {
187 	throwIfDisposed();
188 	if( (nLeft >= 0 ) && (nTop == 0) && (nRight >= nLeft) && (nBottom == 0)  )
189 	{
190 		return mxTableModel->getCellRangeByPosition( nLeft, mnRow, nRight, mnRow );
191 	}
192 	throw IndexOutOfBoundsException();
193 }
194 
195 // -----------------------------------------------------------------------------
196 
getCellRangeByName(const OUString &)197 Reference< XCellRange > SAL_CALL TableRow::getCellRangeByName( const OUString& /*aRange*/ ) throw (RuntimeException)
198 {
199 	throwIfDisposed();
200 	return Reference< XCellRange >();
201 }
202 
203 // -----------------------------------------------------------------------------
204 // XNamed
205 // -----------------------------------------------------------------------------
206 
getName()207 OUString SAL_CALL TableRow::getName() throw (RuntimeException)
208 {
209 	return maName;
210 }
211 
212 // -----------------------------------------------------------------------------
213 
setName(const OUString & aName)214 void SAL_CALL TableRow::setName( const OUString& aName ) throw (RuntimeException)
215 {
216 	maName = aName;
217 }
218 
219 // -----------------------------------------------------------------------------
220 // XFastPropertySet
221 // -----------------------------------------------------------------------------
222 
setFastPropertyValue(sal_Int32 nHandle,const Any & aValue)223 void SAL_CALL TableRow::setFastPropertyValue( sal_Int32 nHandle, const Any& aValue ) throw (UnknownPropertyException, PropertyVetoException, IllegalArgumentException, ::com::sun::star::lang::WrappedTargetException, RuntimeException)
224 {
225 	bool bOk = false;
226 	bool bChange = false;
227 
228 	TableRowUndo* pUndo = 0;
229 
230 	SdrModel* pModel = mxTableModel->getSdrTableObj()->GetModel();
231 
232 	const bool bUndo = mxTableModel.is() && mxTableModel->getSdrTableObj() && mxTableModel->getSdrTableObj()->IsInserted() && pModel && pModel->IsUndoEnabled();
233 
234 	if( bUndo )
235 	{
236 		TableRowRef xThis( this );
237 		pUndo = new TableRowUndo( xThis );
238 	}
239 
240 	switch( nHandle )
241 	{
242 	case Property_Height:
243 		{
244 			sal_Int32 nHeight = mnHeight;
245 			bOk = aValue >>= nHeight;
246 			if( bOk && (mnHeight != nHeight) )
247 			{
248 				mnHeight = nHeight;
249 				mbOptimalHeight = mnHeight == 0;
250 				bChange = true;
251 			}
252 			break;
253 		}
254 
255 	case Property_OptimalHeight:
256 		{
257 			sal_Bool bOptimalHeight = mbOptimalHeight;
258 			bOk = aValue >>= bOptimalHeight;
259 			if( bOk && (mbOptimalHeight != bOptimalHeight) )
260 			{
261 				mbOptimalHeight = bOptimalHeight;
262 				if( bOptimalHeight )
263 					mnHeight = 0;
264 				bChange = true;
265 			}
266 			break;
267 		}
268 	case Property_IsVisible:
269 		{
270 			sal_Bool bIsVisible = mbIsVisible;
271 			bOk = aValue >>= bIsVisible;
272 			if( bOk && (mbIsVisible != bIsVisible) )
273 			{
274 				mbIsVisible = bIsVisible;
275 				bChange = true;
276 			}
277 			break;
278 		}
279 
280 	case Property_IsStartOfNewPage:
281 		{
282 			sal_Bool bIsStartOfNewPage = mbIsStartOfNewPage;
283 			bOk = aValue >>= bIsStartOfNewPage;
284 			if( bOk && (mbIsStartOfNewPage != bIsStartOfNewPage) )
285 			{
286 				mbIsStartOfNewPage = bIsStartOfNewPage;
287 				bChange = true;
288 			}
289 			break;
290 		}
291 	default:
292 		throw UnknownPropertyException();
293 	}
294 	if( !bOk )
295 		throw IllegalArgumentException();
296 
297 	if( bChange )
298 	{
299 		if( pUndo )
300 		{
301 			pModel->AddUndo( pUndo );
302 			pUndo = 0;
303 		}
304 		mxTableModel->setModified(sal_True);
305 	}
306 
307 	if( pUndo )
308 		delete pUndo;
309 }
310 
311 // -----------------------------------------------------------------------------
312 
getFastPropertyValue(sal_Int32 nHandle)313 Any SAL_CALL TableRow::getFastPropertyValue( sal_Int32 nHandle ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException)
314 {
315 	switch( nHandle )
316 	{
317 	case Property_Height:			return Any( mnHeight );
318 	case Property_OptimalHeight:	return Any( mbOptimalHeight );
319 	case Property_IsVisible:		return Any( mbIsVisible );
320 	case Property_IsStartOfNewPage:	return Any( mbIsStartOfNewPage );
321 	default:						throw UnknownPropertyException();
322 	}
323 }
324 
325 // -----------------------------------------------------------------------------
326 
getStaticPropertySetInfo()327 rtl::Reference< ::comphelper::FastPropertySetInfo > TableRow::getStaticPropertySetInfo()
328 {
329 	static rtl::Reference< ::comphelper::FastPropertySetInfo > xInfo;
330 	if( !xInfo.is() )
331 	{
332         ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
333 		if( !xInfo.is() )
334 		{
335 			comphelper::PropertyVector aProperties(6);
336 
337 			aProperties[0].Name = OUString( RTL_CONSTASCII_USTRINGPARAM( "Height" ) );
338 			aProperties[0].Handle = Property_Height;
339 			aProperties[0].Type = ::getCppuType((const sal_Int32*)0);
340 			aProperties[0].Attributes = 0;
341 
342 			aProperties[1].Name = OUString( RTL_CONSTASCII_USTRINGPARAM( "OptimalHeight" ) );
343 			aProperties[1].Handle = Property_OptimalHeight;
344 			aProperties[1].Type = ::getBooleanCppuType();
345 			aProperties[1].Attributes = 0;
346 
347 			aProperties[2].Name = OUString( RTL_CONSTASCII_USTRINGPARAM( "IsVisible" ) );
348 			aProperties[2].Handle = Property_IsVisible;
349 			aProperties[2].Type = ::getBooleanCppuType();
350 			aProperties[2].Attributes = 0;
351 
352 			aProperties[3].Name = OUString( RTL_CONSTASCII_USTRINGPARAM( "IsStartOfNewPage" ) );
353 			aProperties[3].Handle = Property_IsStartOfNewPage;
354 			aProperties[3].Type = ::getBooleanCppuType();
355 			aProperties[3].Attributes = 0;
356 
357 			aProperties[4].Name = OUString( RTL_CONSTASCII_USTRINGPARAM( "Size" ) );
358 			aProperties[4].Handle = Property_Height;
359 			aProperties[4].Type = ::getCppuType((const sal_Int32*)0);
360 			aProperties[4].Attributes = 0;
361 
362 			aProperties[5].Name = OUString( RTL_CONSTASCII_USTRINGPARAM( "OptimalSize" ) );
363 			aProperties[5].Handle = Property_OptimalHeight;
364 			aProperties[5].Type = ::getBooleanCppuType();
365 			aProperties[5].Attributes = 0;
366 
367 			xInfo.set( new ::comphelper::FastPropertySetInfo(aProperties) );
368 		}
369 	}
370 
371 	return xInfo;
372 }
373 
374 // -----------------------------------------------------------------------------
375 
376 
377 } }
378