update dev300-m58
[ooovba.git] / svx / source / unodraw / unoshcol.cxx
blob0cc232efdce5f72b309217f610d417019bd03a4b
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: unoshcol.cxx,v $
10 * $Revision: 1.11 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svx.hxx"
33 #include <com/sun/star/document/EventObject.hpp>
35 #include "unoshcol.hxx"
36 #include <svx/unoprov.hxx>
38 using namespace ::cppu;
39 using namespace ::com::sun::star;
40 using namespace ::com::sun::star::uno;
41 using namespace ::com::sun::star::lang;
42 using namespace ::com::sun::star::container;
43 using namespace ::com::sun::star::drawing;
45 /***********************************************************************
46 * *
47 ***********************************************************************/
48 SvxShapeCollection::SvxShapeCollection() throw()
49 : maShapeContainer( maMutex ), mrBHelper( maMutex )
53 //----------------------------------------------------------------------
54 SvxShapeCollection::~SvxShapeCollection() throw()
59 //----------------------------------------------------------------------
60 Reference< uno::XInterface > SvxShapeCollection_NewInstance() throw()
62 Reference< drawing::XShapes > xShapes( new SvxShapeCollection() );
63 Reference< uno::XInterface > xRef( xShapes, UNO_QUERY );
64 return xRef;
67 // XInterface
68 void SvxShapeCollection::release() throw()
70 uno::Reference< uno::XInterface > x( xDelegator );
71 if (! x.is())
73 if (osl_decrementInterlockedCount( &m_refCount ) == 0)
75 if (! mrBHelper.bDisposed)
77 uno::Reference< uno::XInterface > xHoldAlive( (uno::XWeak*)this );
78 // First dispose
79 try
81 dispose();
83 catch(::com::sun::star::uno::Exception&)
85 // release should not throw exceptions
88 // only the alive ref holds the object
89 OSL_ASSERT( m_refCount == 1 );
90 // destroy the object if xHoldAlive decrement the refcount to 0
91 return;
94 // restore the reference count
95 osl_incrementInterlockedCount( &m_refCount );
97 OWeakAggObject::release();
100 // XComponent
101 void SvxShapeCollection::disposing() throw()
103 maShapeContainer.clear();
106 // XComponent
107 void SvxShapeCollection::dispose()
108 throw(::com::sun::star::uno::RuntimeException)
110 // An frequently programming error is to release the last
111 // reference to this object in the disposing message.
112 // Make it rubust, hold a self Reference.
113 uno::Reference< lang::XComponent > xSelf( this );
115 // Guard dispose against multible threading
116 // Remark: It is an error to call dispose more than once
117 sal_Bool bDoDispose = sal_False;
119 osl::MutexGuard aGuard( mrBHelper.rMutex );
120 if( !mrBHelper.bDisposed && !mrBHelper.bInDispose )
122 // only one call go into this section
123 mrBHelper.bInDispose = sal_True;
124 bDoDispose = sal_True;
128 // Do not hold the mutex because we are broadcasting
129 if( bDoDispose )
131 // Create an event with this as sender
134 uno::Reference< uno::XInterface > xSource( uno::Reference< uno::XInterface >::query( (lang::XComponent *)this ) );
135 document::EventObject aEvt;
136 aEvt.Source = xSource;
137 // inform all listeners to release this object
138 // The listener container are automaticly cleared
139 mrBHelper.aLC.disposeAndClear( aEvt );
140 // notify subclasses to do their dispose
141 disposing();
143 catch(::com::sun::star::uno::Exception& e)
145 // catch exception and throw again but signal that
146 // the object was disposed. Dispose should be called
147 // only once.
148 mrBHelper.bDisposed = sal_True;
149 mrBHelper.bInDispose = sal_False;
150 throw e;
153 // the values bDispose and bInDisposing must set in this order.
154 // No multithread call overcome the "!rBHelper.bDisposed && !rBHelper.bInDispose" guard.
155 mrBHelper.bDisposed = sal_True;
156 mrBHelper.bInDispose = sal_False;
158 else
160 // in a multithreaded environment, it can't be avoided, that dispose is called twice.
161 // However this condition is traced, because it MAY indicate an error.
162 OSL_TRACE( "OComponentHelper::dispose() - dispose called twice" );
166 // XComponent
167 void SAL_CALL SvxShapeCollection::addEventListener( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener >& aListener ) throw(::com::sun::star::uno::RuntimeException)
169 mrBHelper.addListener( ::getCppuType( &aListener ) , aListener );
172 // XComponent
173 void SAL_CALL SvxShapeCollection::removeEventListener( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener >& aListener ) throw(::com::sun::star::uno::RuntimeException)
175 mrBHelper.removeListener( ::getCppuType( &aListener ) , aListener );
178 // XShapes
179 //----------------------------------------------------------------------
180 void SAL_CALL SvxShapeCollection::add( const Reference< drawing::XShape >& xShape ) throw( uno::RuntimeException )
182 maShapeContainer.addInterface( xShape );
185 //----------------------------------------------------------------------
186 void SAL_CALL SvxShapeCollection::remove( const uno::Reference< drawing::XShape >& xShape ) throw( uno::RuntimeException )
188 maShapeContainer.removeInterface( xShape );
191 //----------------------------------------------------------------------
192 sal_Int32 SAL_CALL SvxShapeCollection::getCount() throw( uno::RuntimeException )
194 return maShapeContainer.getLength();
197 //----------------------------------------------------------------------
198 uno::Any SAL_CALL SvxShapeCollection::getByIndex( sal_Int32 Index )
199 throw( lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException )
201 if( Index < 0 || Index >= getCount() )
202 throw lang::IndexOutOfBoundsException();
204 uno::Sequence< Reference< uno::XInterface> > xElements( maShapeContainer.getElements() );
207 return uno::makeAny( Reference< XShape>(static_cast< drawing::XShape* >( xElements.getArray()[Index].get())) );
210 // XElementAccess
212 //----------------------------------------------------------------------
213 uno::Type SAL_CALL SvxShapeCollection::getElementType() throw( uno::RuntimeException )
215 return ::getCppuType(( const Reference< drawing::XShape >*)0);
218 //----------------------------------------------------------------------
219 sal_Bool SAL_CALL SvxShapeCollection::hasElements() throw( uno::RuntimeException )
221 return getCount() != 0;
224 //----------------------------------------------------------------------
225 // XServiceInfo
226 //----------------------------------------------------------------------
227 ::rtl::OUString SAL_CALL SvxShapeCollection::getImplementationName()
228 throw( uno::RuntimeException )
230 return getImplementationName_Static();
233 ::rtl::OUString SvxShapeCollection::getImplementationName_Static()
235 return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.drawing.SvxShapeCollection") );
238 sal_Bool SAL_CALL SvxShapeCollection::supportsService( const ::rtl::OUString& ServiceName )
239 throw( uno::RuntimeException )
241 return SvxServiceInfoHelper::supportsService( ServiceName, getSupportedServiceNames() );
244 uno::Sequence< ::rtl::OUString > SAL_CALL SvxShapeCollection::getSupportedServiceNames() throw( uno::RuntimeException )
246 return getSupportedServiceNames_Static();
249 uno::Sequence< ::rtl::OUString > SvxShapeCollection::getSupportedServiceNames_Static()
251 uno::Sequence< ::rtl::OUString > aSeq(2);
252 aSeq.getArray()[1] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.drawing.Shapes") );
253 aSeq.getArray()[1] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.drawing.ShapeCollection") );
254 return aSeq;
257 Reference< XInterface > SAL_CALL SvxShapeCollection_createInstance( const Reference< ::com::sun::star::lang::XMultiServiceFactory >& )
259 return *( new SvxShapeCollection() );