xref: /aoo41x/main/sd/source/ui/unoidl/unopback.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sd.hxx"
30 #include <com/sun/star/drawing/BitmapMode.hpp>
31 #include <vos/mutex.hxx>
32 #include <vcl/svapp.hxx>
33 #include <svl/itemset.hxx>
34 #include <svx/svdpool.hxx>
35 #include <comphelper/extract.hxx>
36 #include <rtl/uuid.h>
37 #include <rtl/memory.h>
38 #include <svx/xflbstit.hxx>
39 #include <svx/xflbmtit.hxx>
40 #include <svx/svdobj.hxx>
41 #include <svx/unoprov.hxx>
42 #include <svx/unoshape.hxx>
43 #include <comphelper/serviceinfohelper.hxx>
44 
45 #include "unopback.hxx"
46 #include "unohelp.hxx"
47 #include "drawdoc.hxx"
48 #include "unokywds.hxx"
49 
50 using namespace ::rtl;
51 using namespace ::vos;
52 using namespace ::com::sun::star;
53 
54 const SvxItemPropertySet* ImplGetPageBackgroundPropertySet()
55 {
56 	static const SfxItemPropertyMapEntry aPageBackgroundPropertyMap_Impl[] =
57 	{
58 		FILL_PROPERTIES
59 		{0,0,0,0,0,0}
60 	};
61 
62     static SvxItemPropertySet aPageBackgroundPropertySet_Impl( aPageBackgroundPropertyMap_Impl, SdrObject::GetGlobalDrawObjectItemPool() );
63 	return &aPageBackgroundPropertySet_Impl;
64 }
65 
66 UNO3_GETIMPLEMENTATION_IMPL( SdUnoPageBackground );
67 
68 SdUnoPageBackground::SdUnoPageBackground(
69     SdDrawDocument* pDoc /* = NULL */,
70     const SfxItemSet* pSet /* = NULL */) throw()
71 :   mpPropSet(ImplGetPageBackgroundPropertySet()),
72     mpSet(NULL),
73     mpDoc(pDoc)
74 {
75 	if( pDoc )
76 	{
77 		StartListening( *pDoc );
78 		mpSet = new SfxItemSet( pDoc->GetPool(), XATTR_FILL_FIRST, XATTR_FILL_LAST );
79 
80 		if( pSet )
81 			mpSet->Put(*pSet);
82 	}
83 }
84 
85 SdUnoPageBackground::~SdUnoPageBackground() throw()
86 {
87 	if( mpDoc )
88 		EndListening( *mpDoc );
89 
90 	if( mpSet )
91 		delete mpSet;
92 }
93 
94 void SdUnoPageBackground::Notify( SfxBroadcaster&, const SfxHint& rHint )
95 {
96 	const SdrHint* pSdrHint = PTR_CAST( SdrHint, &rHint );
97 
98 	if( pSdrHint )
99 	{
100 		// delete item set if document is dying because then the pool
101 		// will also die
102 		if( pSdrHint->GetKind() == HINT_MODELCLEARED )
103 		{
104 			delete mpSet;
105 			mpSet = NULL;
106 			mpDoc = NULL;
107 		}
108 	}
109 
110 }
111 
112 void SdUnoPageBackground::fillItemSet( SdDrawDocument* pDoc, SfxItemSet& rSet ) throw()
113 {
114 	rSet.ClearItem();
115 
116 	if( mpSet == NULL )
117 	{
118 		StartListening( *pDoc );
119 		mpDoc = pDoc;
120 
121 		mpSet = new SfxItemSet( *rSet.GetPool(), XATTR_FILL_FIRST, XATTR_FILL_LAST );
122 
123 		if( mpPropSet->AreThereOwnUsrAnys() )
124 		{
125 			uno::Any* pAny;
126             PropertyEntryVector_t aProperties = mpPropSet->getPropertyMap()->getPropertyEntries();
127             PropertyEntryVector_t::const_iterator aIt = aProperties.begin();
128 
129 			while( aIt != aProperties.end() )
130 			{
131 				pAny = mpPropSet->GetUsrAnyForID( aIt->nWID );
132 				if( pAny )
133 				{
134 					OUString aPropertyName( aIt->sName );
135 					switch( aIt->nWID )
136 					{
137 						case XATTR_FILLFLOATTRANSPARENCE :
138 						case XATTR_FILLGRADIENT :
139 						{
140 							if ( ( pAny->getValueType() == ::getCppuType((const ::com::sun::star::awt::Gradient*)0) )
141 								&& ( aIt->nMemberId == MID_FILLGRADIENT ) )
142 							{
143 								setPropertyValue( aPropertyName, *pAny );
144 							}
145 							else if ( ( pAny->getValueType() == ::getCppuType((const ::rtl::OUString*)0) ) &&
146 										( aIt->nMemberId == MID_NAME ) )
147 							{
148 								setPropertyValue( aPropertyName, *pAny );
149 							}
150 						}
151 						break;
152 						case XATTR_FILLHATCH :
153 						{
154 							if ( ( pAny->getValueType() == ::getCppuType((const ::com::sun::star::drawing::Hatch*)0) )
155 								&& ( aIt->nMemberId == MID_FILLHATCH ) )
156 							{
157 								setPropertyValue( aPropertyName, *pAny );
158 							}
159 							else if ( ( pAny->getValueType() == ::getCppuType((const ::rtl::OUString*)0) ) &&
160 										( aIt->nMemberId == MID_NAME ) )
161 							{
162 								setPropertyValue( aPropertyName, *pAny );
163 							}
164 						}
165 						break;
166 						case XATTR_FILLBITMAP :
167 						{
168 							if ( ( ( pAny->getValueType() == ::getCppuType((const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XBitmap >*)0) ) ||
169 									( pAny->getValueType() == ::getCppuType((const ::com::sun::star::uno::Reference< ::com::sun::star::graphic::XGraphic >*)0) ) ) &&
170 									( aIt->nMemberId == MID_BITMAP ) )
171 							{
172 								setPropertyValue( aPropertyName, *pAny );
173 							}
174 							else if ( ( pAny->getValueType() == ::getCppuType((const ::rtl::OUString*)0) ) &&
175 										( ( aIt->nMemberId == MID_NAME ) || ( aIt->nMemberId == MID_GRAFURL ) ) )
176 							{
177 								setPropertyValue( aPropertyName, *pAny );
178 							}
179 						}
180 						break;
181 
182 						default:
183 							setPropertyValue( aPropertyName, *pAny );
184 					}
185 			    }
186 				++aIt;
187 			}
188 		}
189 	}
190 
191 	rSet.Put( *mpSet );
192 }
193 
194 // XServiceInfo
195 OUString SAL_CALL SdUnoPageBackground::getImplementationName()
196 	throw(uno::RuntimeException)
197 {
198 	return OUString::createFromAscii( sUNO_SdUnoPageBackground );
199 }
200 
201 sal_Bool SAL_CALL SdUnoPageBackground::supportsService( const OUString& ServiceName )
202 	throw(uno::RuntimeException)
203 {
204 	return comphelper::ServiceInfoHelper::supportsService( ServiceName, getSupportedServiceNames() );
205 }
206 
207 uno::Sequence< OUString > SAL_CALL SdUnoPageBackground::getSupportedServiceNames()
208 	throw(uno::RuntimeException)
209 {
210 	uno::Sequence< OUString > aNameSequence( 2 );
211 	OUString* pStrings = aNameSequence.getArray();
212 
213 	*pStrings++ = OUString( RTL_CONSTASCII_USTRINGPARAM( sUNO_Service_PageBackground ) );
214 	*pStrings   = OUString( RTL_CONSTASCII_USTRINGPARAM( sUNO_Service_FillProperties ) );
215 
216 	return aNameSequence;
217 }
218 
219 // XPropertySet
220 uno::Reference< beans::XPropertySetInfo > SAL_CALL SdUnoPageBackground::getPropertySetInfo()
221 	throw(uno::RuntimeException)
222 {
223 	return mpPropSet->getPropertySetInfo();
224 }
225 
226 void SAL_CALL SdUnoPageBackground::setPropertyValue( const OUString& aPropertyName, const uno::Any& aValue )
227 	throw(beans::UnknownPropertyException, beans::PropertyVetoException, lang::IllegalArgumentException, lang::WrappedTargetException, uno::RuntimeException)
228 {
229 	OGuard aGuard( Application::GetSolarMutex() );
230 
231 	const SfxItemPropertySimpleEntry* pEntry = getPropertyMapEntry( aPropertyName );
232 
233 	if( pEntry == NULL )
234 	{
235 		throw beans::UnknownPropertyException();
236 	}
237 	else
238 	{
239 		if( mpSet )
240 		{
241 			if( pEntry->nWID == OWN_ATTR_FILLBMP_MODE )
242 			{
243 				drawing::BitmapMode eMode;
244 				if( aValue >>= eMode )
245 				{
246 					mpSet->Put( XFillBmpStretchItem( eMode == drawing::BitmapMode_STRETCH ) );
247 					mpSet->Put( XFillBmpTileItem( eMode == drawing::BitmapMode_REPEAT ) );
248 					return;
249 				}
250 				throw lang::IllegalArgumentException();
251 			}
252 
253 			SfxItemPool& rPool = *mpSet->GetPool();
254 			SfxItemSet aSet( rPool,	pEntry->nWID, pEntry->nWID);
255 			aSet.Put( *mpSet );
256 
257 			if( !aSet.Count() )
258 				aSet.Put( rPool.GetDefaultItem( pEntry->nWID ) );
259 
260 			if( pEntry->nMemberId == MID_NAME && ( pEntry->nWID == XATTR_FILLBITMAP || pEntry->nWID == XATTR_FILLGRADIENT || pEntry->nWID == XATTR_FILLHATCH || pEntry->nWID == XATTR_FILLFLOATTRANSPARENCE ) )
261 			{
262 				OUString aName;
263 				if(!(aValue >>= aName ))
264 					throw lang::IllegalArgumentException();
265 
266 				SvxShape::SetFillAttribute( pEntry->nWID, aName, aSet );
267 			}
268 			else
269 			{
270 				SvxItemPropertySet_setPropertyValue( *mpPropSet, pEntry, aValue, aSet );
271 			}
272 
273 			mpSet->Put( aSet );
274 		}
275 		else
276 		{
277 			if(pEntry && pEntry->nWID)
278 				mpPropSet->setPropertyValue( pEntry, aValue );
279 		}
280 	}
281 }
282 
283 uno::Any SAL_CALL SdUnoPageBackground::getPropertyValue( const OUString& PropertyName )
284 	throw(beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
285 {
286 	OGuard aGuard( Application::GetSolarMutex() );
287 
288 	uno::Any aAny;
289 	const SfxItemPropertySimpleEntry* pEntry = getPropertyMapEntry(PropertyName);
290 
291 	if( pEntry == NULL )
292 	{
293 		throw beans::UnknownPropertyException();
294 	}
295 	else
296 	{
297 		if( mpSet )
298 		{
299 			if( pEntry->nWID == OWN_ATTR_FILLBMP_MODE )
300 			{
301 				XFillBmpStretchItem* pStretchItem = (XFillBmpStretchItem*)mpSet->GetItem(XATTR_FILLBMP_STRETCH);
302 				XFillBmpTileItem* pTileItem = (XFillBmpTileItem*)mpSet->GetItem(XATTR_FILLBMP_TILE);
303 
304 				if( pStretchItem && pTileItem )
305 				{
306 					if( pTileItem->GetValue() )
307 						aAny <<= drawing::BitmapMode_REPEAT;
308 					else if( pStretchItem->GetValue() )
309 						aAny <<= drawing::BitmapMode_STRETCH;
310 					else
311 						aAny <<= drawing::BitmapMode_NO_REPEAT;
312 				}
313 			}
314 			else
315 			{
316 				SfxItemPool& rPool = *mpSet->GetPool();
317 				SfxItemSet aSet( rPool,	pEntry->nWID, pEntry->nWID);
318 				aSet.Put( *mpSet );
319 
320 				if( !aSet.Count() )
321 					aSet.Put( rPool.GetDefaultItem( pEntry->nWID ) );
322 
323 				// Hole Wert aus ItemSet
324 				aAny = SvxItemPropertySet_getPropertyValue( *mpPropSet, pEntry, aSet );
325 			}
326 		}
327 		else
328 		{
329 			if(pEntry && pEntry->nWID)
330 				aAny = mpPropSet->getPropertyValue( pEntry );
331 		}
332 	}
333 	return aAny;
334 }
335 
336 void SAL_CALL SdUnoPageBackground::addPropertyChangeListener( const OUString& , const uno::Reference< beans::XPropertyChangeListener >&  ) throw(beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException) {}
337 void SAL_CALL SdUnoPageBackground::removePropertyChangeListener( const OUString& , const uno::Reference< beans::XPropertyChangeListener >&  ) throw(beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException) {}
338 void SAL_CALL SdUnoPageBackground::addVetoableChangeListener( const OUString& , const uno::Reference< beans::XVetoableChangeListener >&  ) throw(beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException) {}
339 void SAL_CALL SdUnoPageBackground::removeVetoableChangeListener( const OUString& , const uno::Reference< beans::XVetoableChangeListener >&  ) throw(beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException) {}
340 
341 // XPropertyState
342 beans::PropertyState SAL_CALL SdUnoPageBackground::getPropertyState( const OUString& PropertyName )
343 	throw(beans::UnknownPropertyException, uno::RuntimeException)
344 {
345 	OGuard aGuard( Application::GetSolarMutex() );
346 
347 	const SfxItemPropertySimpleEntry* pEntry = getPropertyMapEntry(PropertyName);
348 
349 	if( pEntry == NULL )
350 		throw beans::UnknownPropertyException();
351 
352 	if( mpSet )
353 	{
354 		if( pEntry->nWID == OWN_ATTR_FILLBMP_MODE )
355 		{
356 			if( mpSet->GetItemState( XATTR_FILLBMP_STRETCH, false ) == SFX_ITEM_SET ||
357 				mpSet->GetItemState( XATTR_FILLBMP_TILE, false ) == SFX_ITEM_SET )
358 			{
359 				return beans::PropertyState_DIRECT_VALUE;
360 			}
361 			else
362 			{
363 				return beans::PropertyState_AMBIGUOUS_VALUE;
364 			}
365 		}
366 
367 		switch( mpSet->GetItemState( pEntry->nWID, sal_False ) )
368 		{
369 		case SFX_ITEM_READONLY:
370 		case SFX_ITEM_SET:
371 			return beans::PropertyState_DIRECT_VALUE;
372 		case SFX_ITEM_DEFAULT:
373 			return beans::PropertyState_DEFAULT_VALUE;
374 		default:
375 //		case SFX_ITEM_DONTCARE:
376 //		case SFX_ITEM_DISABLED:
377 			return beans::PropertyState_AMBIGUOUS_VALUE;
378 		}
379 	}
380 	else
381 	{
382 		if( NULL == mpPropSet->GetUsrAnyForID(pEntry->nWID) )
383 			return beans::PropertyState_DEFAULT_VALUE;
384 		else
385 			return beans::PropertyState_DIRECT_VALUE;
386 	}
387 }
388 
389 uno::Sequence< beans::PropertyState > SAL_CALL SdUnoPageBackground::getPropertyStates( const uno::Sequence< OUString >& aPropertyName )
390 	throw(beans::UnknownPropertyException, uno::RuntimeException)
391 {
392 	OGuard aGuard( Application::GetSolarMutex() );
393 
394 	sal_Int32 nCount = aPropertyName.getLength();
395 	const OUString* pNames = aPropertyName.getConstArray();
396 
397 	uno::Sequence< beans::PropertyState > aPropertyStateSequence( nCount );
398 	beans::PropertyState* pState = aPropertyStateSequence.getArray();
399 
400 	while( nCount-- )
401 		*pState++ = getPropertyState( *pNames++ );
402 
403 	return aPropertyStateSequence;
404 }
405 
406 void SAL_CALL SdUnoPageBackground::setPropertyToDefault( const OUString& PropertyName )
407 	throw(beans::UnknownPropertyException, uno::RuntimeException)
408 {
409 	OGuard aGuard( Application::GetSolarMutex() );
410 
411 	const SfxItemPropertySimpleEntry* pEntry = getPropertyMapEntry(PropertyName);
412 
413 	if( pEntry == NULL )
414 		throw beans::UnknownPropertyException();
415 
416 	if( mpSet )
417 	{
418 		if( pEntry->nWID == OWN_ATTR_FILLBMP_MODE )
419 		{
420 			mpSet->ClearItem( XATTR_FILLBMP_STRETCH );
421 			mpSet->ClearItem( XATTR_FILLBMP_TILE );
422 		}
423 		else
424 		{
425 			mpSet->ClearItem( pEntry->nWID );
426 		}
427 	}
428 }
429 
430 uno::Any SAL_CALL SdUnoPageBackground::getPropertyDefault( const OUString& aPropertyName )
431 	throw(beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
432 {
433 	OGuard aGuard( Application::GetSolarMutex() );
434 
435 	const SfxItemPropertySimpleEntry* pEntry = getPropertyMapEntry(aPropertyName);
436 	if( pEntry == NULL || mpSet == NULL )
437 		throw beans::UnknownPropertyException();
438 
439 	uno::Any aAny;
440 	if( mpSet )
441 	{
442 		if( pEntry->nWID == OWN_ATTR_FILLBMP_MODE )
443 		{
444 			aAny <<= drawing::BitmapMode_REPEAT;
445 		}
446 		else
447 		{
448 			SfxItemPool& rPool = *mpSet->GetPool();
449 			SfxItemSet aSet( rPool,	pEntry->nWID, pEntry->nWID);
450 			aSet.Put( rPool.GetDefaultItem( pEntry->nWID ) );
451 
452 			aAny = SvxItemPropertySet_getPropertyValue( *mpPropSet, pEntry, aSet );
453 		}
454 	}
455 	return aAny;
456 }
457 
458 /** this is used because our property map is not sorted yet */
459 const SfxItemPropertySimpleEntry* SdUnoPageBackground::getPropertyMapEntry( const OUString& rPropertyName ) const throw()
460 {
461 	return mpPropSet->getPropertyMap()->getByName(rPropertyName);
462 }
463 
464