xref: /aoo42x/main/svx/source/unodraw/unoshcol.cxx (revision 86e1cf34)
1f6e50924SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3f6e50924SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4f6e50924SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5f6e50924SAndrew Rist  * distributed with this work for additional information
6f6e50924SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7f6e50924SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8f6e50924SAndrew Rist  * "License"); you may not use this file except in compliance
9f6e50924SAndrew Rist  * with the License.  You may obtain a copy of the License at
10f6e50924SAndrew Rist  *
11f6e50924SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12f6e50924SAndrew Rist  *
13f6e50924SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14f6e50924SAndrew Rist  * software distributed under the License is distributed on an
15f6e50924SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16f6e50924SAndrew Rist  * KIND, either express or implied.  See the License for the
17f6e50924SAndrew Rist  * specific language governing permissions and limitations
18f6e50924SAndrew Rist  * under the License.
19f6e50924SAndrew Rist  *
20f6e50924SAndrew Rist  *************************************************************/
21f6e50924SAndrew Rist 
22f6e50924SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_svx.hxx"
26cdf0e10cSrcweir #include <com/sun/star/document/EventObject.hpp>
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include "svx/unoshcol.hxx"
29cdf0e10cSrcweir #include <svx/unoprov.hxx>
30cdf0e10cSrcweir #include <comphelper/serviceinfohelper.hxx>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir using namespace ::cppu;
33cdf0e10cSrcweir using namespace ::com::sun::star;
34cdf0e10cSrcweir using namespace ::com::sun::star::uno;
35cdf0e10cSrcweir using namespace ::com::sun::star::lang;
36cdf0e10cSrcweir using namespace ::com::sun::star::container;
37cdf0e10cSrcweir using namespace ::com::sun::star::drawing;
38cdf0e10cSrcweir 
39cdf0e10cSrcweir /***********************************************************************
40cdf0e10cSrcweir *                                                                      *
41cdf0e10cSrcweir ***********************************************************************/
SvxShapeCollection()42cdf0e10cSrcweir SvxShapeCollection::SvxShapeCollection() throw()
43cdf0e10cSrcweir : maShapeContainer( maMutex ), mrBHelper( maMutex )
44cdf0e10cSrcweir {
45cdf0e10cSrcweir }
46cdf0e10cSrcweir 
47cdf0e10cSrcweir //----------------------------------------------------------------------
~SvxShapeCollection()48cdf0e10cSrcweir SvxShapeCollection::~SvxShapeCollection() throw()
49cdf0e10cSrcweir {
50cdf0e10cSrcweir }
51cdf0e10cSrcweir 
52cdf0e10cSrcweir 
53cdf0e10cSrcweir //----------------------------------------------------------------------
SvxShapeCollection_NewInstance()54cdf0e10cSrcweir Reference< uno::XInterface > SvxShapeCollection_NewInstance() throw()
55cdf0e10cSrcweir {
56cdf0e10cSrcweir 	Reference< drawing::XShapes > xShapes( new SvxShapeCollection() );
57cdf0e10cSrcweir 	Reference< uno::XInterface > xRef( xShapes, UNO_QUERY );
58cdf0e10cSrcweir 	return xRef;
59cdf0e10cSrcweir }
60cdf0e10cSrcweir 
61cdf0e10cSrcweir // XInterface
release()62cdf0e10cSrcweir void SvxShapeCollection::release() throw()
63cdf0e10cSrcweir {
64cdf0e10cSrcweir 	uno::Reference< uno::XInterface > x( xDelegator );
65cdf0e10cSrcweir 	if (! x.is())
66cdf0e10cSrcweir 	{
67cdf0e10cSrcweir 		if (osl_decrementInterlockedCount( &m_refCount ) == 0)
68cdf0e10cSrcweir 		{
69cdf0e10cSrcweir 			if (! mrBHelper.bDisposed)
70cdf0e10cSrcweir 			{
71cdf0e10cSrcweir 				uno::Reference< uno::XInterface > xHoldAlive( (uno::XWeak*)this );
72cdf0e10cSrcweir 				// First dispose
73cdf0e10cSrcweir 				try
74cdf0e10cSrcweir 				{
75cdf0e10cSrcweir 					dispose();
76cdf0e10cSrcweir 				}
77cdf0e10cSrcweir 				catch(::com::sun::star::uno::Exception&)
78cdf0e10cSrcweir 				{
79cdf0e10cSrcweir 					// release should not throw exceptions
80cdf0e10cSrcweir 				}
81cdf0e10cSrcweir 
82cdf0e10cSrcweir 				// only the alive ref holds the object
83cdf0e10cSrcweir 				OSL_ASSERT( m_refCount == 1 );
84cdf0e10cSrcweir 				// destroy the object if xHoldAlive decrement the refcount to 0
85cdf0e10cSrcweir 				return;
86cdf0e10cSrcweir 			}
87cdf0e10cSrcweir 		}
88cdf0e10cSrcweir 		// restore the reference count
89cdf0e10cSrcweir 		osl_incrementInterlockedCount( &m_refCount );
90cdf0e10cSrcweir 	}
91cdf0e10cSrcweir 	OWeakAggObject::release();
92cdf0e10cSrcweir }
93cdf0e10cSrcweir 
94cdf0e10cSrcweir // XComponent
disposing()95cdf0e10cSrcweir void SvxShapeCollection::disposing() throw()
96cdf0e10cSrcweir {
97cdf0e10cSrcweir 	maShapeContainer.clear();
98cdf0e10cSrcweir }
99cdf0e10cSrcweir 
100cdf0e10cSrcweir // XComponent
dispose()101cdf0e10cSrcweir void SvxShapeCollection::dispose()
102cdf0e10cSrcweir 	throw(::com::sun::star::uno::RuntimeException)
103cdf0e10cSrcweir {
104cdf0e10cSrcweir 	// An frequently programming error is to release the last
105cdf0e10cSrcweir 	// reference to this object in the disposing message.
106cdf0e10cSrcweir 	// Make it rubust, hold a self Reference.
107cdf0e10cSrcweir 	uno::Reference< lang::XComponent > xSelf( this );
108cdf0e10cSrcweir 
109cdf0e10cSrcweir 	// Guard dispose against multible threading
110cdf0e10cSrcweir 	// Remark: It is an error to call dispose more than once
111cdf0e10cSrcweir 	sal_Bool bDoDispose = sal_False;
112cdf0e10cSrcweir 	{
113cdf0e10cSrcweir 	osl::MutexGuard aGuard( mrBHelper.rMutex );
114cdf0e10cSrcweir 	if( !mrBHelper.bDisposed && !mrBHelper.bInDispose )
115cdf0e10cSrcweir 	{
116cdf0e10cSrcweir 		// only one call go into this section
117cdf0e10cSrcweir 		mrBHelper.bInDispose = sal_True;
118cdf0e10cSrcweir 		bDoDispose = sal_True;
119cdf0e10cSrcweir 	}
120cdf0e10cSrcweir 	}
121cdf0e10cSrcweir 
122cdf0e10cSrcweir 	// Do not hold the mutex because we are broadcasting
123cdf0e10cSrcweir 	if( bDoDispose )
124cdf0e10cSrcweir 	{
125cdf0e10cSrcweir 		// Create an event with this as sender
126cdf0e10cSrcweir 		try
127cdf0e10cSrcweir 		{
128cdf0e10cSrcweir 			uno::Reference< uno::XInterface > xSource( uno::Reference< uno::XInterface >::query( (lang::XComponent *)this ) );
129cdf0e10cSrcweir 			document::EventObject aEvt;
130cdf0e10cSrcweir 			aEvt.Source = xSource;
131cdf0e10cSrcweir 			// inform all listeners to release this object
132*86e1cf34SPedro Giffuni 			// The listener container are automatically cleared
133cdf0e10cSrcweir 			mrBHelper.aLC.disposeAndClear( aEvt );
134cdf0e10cSrcweir 			// notify subclasses to do their dispose
135cdf0e10cSrcweir 			disposing();
136cdf0e10cSrcweir 		}
137cdf0e10cSrcweir 		catch(::com::sun::star::uno::Exception& e)
138cdf0e10cSrcweir 		{
139cdf0e10cSrcweir 			// catch exception and throw again but signal that
140cdf0e10cSrcweir 			// the object was disposed. Dispose should be called
141cdf0e10cSrcweir 			// only once.
142cdf0e10cSrcweir 			mrBHelper.bDisposed = sal_True;
143cdf0e10cSrcweir 			mrBHelper.bInDispose = sal_False;
144cdf0e10cSrcweir 			throw e;
145cdf0e10cSrcweir 		}
146cdf0e10cSrcweir 
147cdf0e10cSrcweir 		// the values bDispose and bInDisposing must set in this order.
148cdf0e10cSrcweir 		// No multithread call overcome the "!rBHelper.bDisposed && !rBHelper.bInDispose" guard.
149cdf0e10cSrcweir 		mrBHelper.bDisposed = sal_True;
150cdf0e10cSrcweir 		mrBHelper.bInDispose = sal_False;
151cdf0e10cSrcweir 	}
152cdf0e10cSrcweir 	else
153cdf0e10cSrcweir 	{
154cdf0e10cSrcweir 		// in a multithreaded environment, it can't be avoided, that dispose is called twice.
155cdf0e10cSrcweir 		// However this condition is traced, because it MAY indicate an error.
156cdf0e10cSrcweir 		OSL_TRACE( "OComponentHelper::dispose() - dispose called twice" );
157cdf0e10cSrcweir 	}
158cdf0e10cSrcweir }
159cdf0e10cSrcweir 
160cdf0e10cSrcweir // XComponent
addEventListener(const::com::sun::star::uno::Reference<::com::sun::star::lang::XEventListener> & aListener)161cdf0e10cSrcweir void SAL_CALL SvxShapeCollection::addEventListener( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener >& aListener ) throw(::com::sun::star::uno::RuntimeException)
162cdf0e10cSrcweir {
163cdf0e10cSrcweir 	mrBHelper.addListener( ::getCppuType( &aListener ) , aListener );
164cdf0e10cSrcweir }
165cdf0e10cSrcweir 
166cdf0e10cSrcweir // XComponent
removeEventListener(const::com::sun::star::uno::Reference<::com::sun::star::lang::XEventListener> & aListener)167cdf0e10cSrcweir void SAL_CALL SvxShapeCollection::removeEventListener( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener >& aListener ) throw(::com::sun::star::uno::RuntimeException)
168cdf0e10cSrcweir {
169cdf0e10cSrcweir 	mrBHelper.removeListener( ::getCppuType( &aListener ) , aListener );
170cdf0e10cSrcweir }
171cdf0e10cSrcweir 
172cdf0e10cSrcweir // XShapes
173cdf0e10cSrcweir //----------------------------------------------------------------------
add(const Reference<drawing::XShape> & xShape)174cdf0e10cSrcweir void SAL_CALL SvxShapeCollection::add( const Reference< drawing::XShape >& xShape ) throw( uno::RuntimeException )
175cdf0e10cSrcweir {
176cdf0e10cSrcweir 	maShapeContainer.addInterface( xShape );
177cdf0e10cSrcweir }
178cdf0e10cSrcweir 
179cdf0e10cSrcweir //----------------------------------------------------------------------
remove(const uno::Reference<drawing::XShape> & xShape)180cdf0e10cSrcweir void SAL_CALL SvxShapeCollection::remove( const uno::Reference< drawing::XShape >& xShape ) throw( uno::RuntimeException )
181cdf0e10cSrcweir {
182cdf0e10cSrcweir 	maShapeContainer.removeInterface( xShape );
183cdf0e10cSrcweir }
184cdf0e10cSrcweir 
185cdf0e10cSrcweir //----------------------------------------------------------------------
getCount()186cdf0e10cSrcweir sal_Int32 SAL_CALL SvxShapeCollection::getCount() throw( uno::RuntimeException )
187cdf0e10cSrcweir {
188cdf0e10cSrcweir 	return maShapeContainer.getLength();
189cdf0e10cSrcweir }
190cdf0e10cSrcweir 
191cdf0e10cSrcweir //----------------------------------------------------------------------
getByIndex(sal_Int32 Index)192cdf0e10cSrcweir uno::Any SAL_CALL SvxShapeCollection::getByIndex( sal_Int32 Index )
193cdf0e10cSrcweir 	throw( lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException )
194cdf0e10cSrcweir {
195cdf0e10cSrcweir 	if( Index < 0 || Index >= getCount() )
196cdf0e10cSrcweir 		throw lang::IndexOutOfBoundsException();
197cdf0e10cSrcweir 
198cdf0e10cSrcweir 	uno::Sequence< Reference< uno::XInterface> > xElements( maShapeContainer.getElements() );
199cdf0e10cSrcweir 
200cdf0e10cSrcweir 
201cdf0e10cSrcweir 	return uno::makeAny( Reference< XShape>(static_cast< drawing::XShape* >( xElements.getArray()[Index].get())) );
202cdf0e10cSrcweir }
203cdf0e10cSrcweir 
204cdf0e10cSrcweir // XElementAccess
205cdf0e10cSrcweir 
206cdf0e10cSrcweir //----------------------------------------------------------------------
getElementType()207cdf0e10cSrcweir uno::Type SAL_CALL SvxShapeCollection::getElementType() throw( uno::RuntimeException )
208cdf0e10cSrcweir {
209cdf0e10cSrcweir 	return ::getCppuType(( const Reference< drawing::XShape >*)0);
210cdf0e10cSrcweir }
211cdf0e10cSrcweir 
212cdf0e10cSrcweir //----------------------------------------------------------------------
hasElements()213cdf0e10cSrcweir sal_Bool SAL_CALL SvxShapeCollection::hasElements() throw( uno::RuntimeException )
214cdf0e10cSrcweir {
215cdf0e10cSrcweir 	return getCount() != 0;
216cdf0e10cSrcweir }
217cdf0e10cSrcweir 
218cdf0e10cSrcweir //----------------------------------------------------------------------
219cdf0e10cSrcweir // XServiceInfo
220cdf0e10cSrcweir //----------------------------------------------------------------------
getImplementationName()221cdf0e10cSrcweir ::rtl::OUString SAL_CALL SvxShapeCollection::getImplementationName()
222cdf0e10cSrcweir 	throw( uno::RuntimeException )
223cdf0e10cSrcweir {
224cdf0e10cSrcweir 	return getImplementationName_Static();
225cdf0e10cSrcweir }
226cdf0e10cSrcweir 
getImplementationName_Static()227cdf0e10cSrcweir ::rtl::OUString SvxShapeCollection::getImplementationName_Static()
228cdf0e10cSrcweir {
229cdf0e10cSrcweir 	return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.drawing.SvxShapeCollection") );
230cdf0e10cSrcweir }
231cdf0e10cSrcweir 
supportsService(const::rtl::OUString & ServiceName)232cdf0e10cSrcweir sal_Bool SAL_CALL SvxShapeCollection::supportsService( const ::rtl::OUString& ServiceName )
233cdf0e10cSrcweir 	throw( uno::RuntimeException )
234cdf0e10cSrcweir {
235cdf0e10cSrcweir 	return comphelper::ServiceInfoHelper::supportsService( ServiceName, getSupportedServiceNames() );
236cdf0e10cSrcweir }
237cdf0e10cSrcweir 
getSupportedServiceNames()238cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > SAL_CALL SvxShapeCollection::getSupportedServiceNames() throw( uno::RuntimeException )
239cdf0e10cSrcweir {
240cdf0e10cSrcweir 	return getSupportedServiceNames_Static();
241cdf0e10cSrcweir }
242cdf0e10cSrcweir 
getSupportedServiceNames_Static()243cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > SvxShapeCollection::getSupportedServiceNames_Static()
244cdf0e10cSrcweir {
245cdf0e10cSrcweir 	uno::Sequence< ::rtl::OUString > aSeq(2);
246cdf0e10cSrcweir 	aSeq.getArray()[1] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.drawing.Shapes") );
247cdf0e10cSrcweir 	aSeq.getArray()[1] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.drawing.ShapeCollection") );
248cdf0e10cSrcweir 	return aSeq;
249cdf0e10cSrcweir }
250cdf0e10cSrcweir 
SvxShapeCollection_createInstance(const Reference<::com::sun::star::lang::XMultiServiceFactory> &)251cdf0e10cSrcweir Reference< XInterface > SAL_CALL SvxShapeCollection_createInstance( const Reference< ::com::sun::star::lang::XMultiServiceFactory >& )
252cdf0e10cSrcweir {
253cdf0e10cSrcweir 	return *( new SvxShapeCollection() );
254cdf0e10cSrcweir }
255