defer finding dialog parent until we need it
[LibreOffice.git] / svx / source / unodraw / unoshcol.cxx
blob116247e1916b3578920a13e1042ba426d8263597
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <com/sun/star/document/EventObject.hpp>
21 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
22 #include <com/sun/star/uno/XComponentContext.hpp>
24 #include <cppuhelper/supportsservice.hxx>
25 #include <osl/diagnose.h>
26 #include <sal/log.hxx>
27 #include <shapecollection.hxx>
29 using namespace ::com::sun::star;
30 using namespace ::com::sun::star::uno;
32 SvxShapeCollection::SvxShapeCollection() noexcept
36 // XInterface
37 void SvxShapeCollection::release() noexcept
39 uno::Reference< uno::XInterface > x( xDelegator );
40 if (! x.is())
42 if (osl_atomic_decrement( &m_refCount ) == 0)
44 if (! bDisposed)
46 uno::Reference< uno::XInterface > xHoldAlive( getXWeak() );
47 // First dispose
48 try
50 dispose();
52 catch(css::uno::Exception&)
54 // release should not throw exceptions
57 // only the alive ref holds the object
58 OSL_ASSERT( m_refCount == 1 );
59 // destroy the object if xHoldAlive decrement the refcount to 0
60 return;
63 // restore the reference count
64 osl_atomic_increment( &m_refCount );
66 OWeakAggObject::release();
69 // XComponent
70 void SvxShapeCollection::dispose()
72 // An frequently programming error is to release the last
73 // reference to this object in the disposing message.
74 // Make it robust, hold a self Reference.
75 uno::Reference< lang::XComponent > xSelf( this );
77 // Guard dispose against multiple threading
78 // Remark: It is an error to call dispose more than once
80 std::unique_lock aGuard( m_aMutex );
81 if( bDisposed || bInDispose )
82 return;
83 // only one call go into this section
84 bInDispose = true;
87 // Do not hold the mutex because we are broadcasting
88 // Create an event with this as sender
89 try
91 document::EventObject aEvt;
92 aEvt.Source = uno::Reference< uno::XInterface >::query( static_cast<lang::XComponent *>(this) );
93 // inform all listeners to release this object
94 // The listener container are automatically cleared
95 std::unique_lock g(m_aMutex);
96 maEventListeners.disposeAndClear( g, aEvt );
97 maShapeContainer.clear();
99 // bDisposed and bInDispose must be set in this order.
100 bDisposed = true;
101 bInDispose = false;
103 catch(const css::uno::Exception&)
105 // catch exception and throw again but signal that
106 // the object was disposed. Dispose should be called
107 // only once.
108 bDisposed = true;
109 bInDispose = false;
110 throw;
114 // XComponent
115 void SAL_CALL SvxShapeCollection::addEventListener( const css::uno::Reference< css::lang::XEventListener >& aListener )
117 std::unique_lock g(m_aMutex);
118 maEventListeners.addInterface( g, aListener );
121 // XComponent
122 void SAL_CALL SvxShapeCollection::removeEventListener( const css::uno::Reference< css::lang::XEventListener >& aListener )
124 std::unique_lock g(m_aMutex);
125 maEventListeners.removeInterface( g, aListener );
128 // XShapes
130 void SAL_CALL SvxShapeCollection::add( const Reference< drawing::XShape >& xShape )
132 std::unique_lock g(m_aMutex);
133 maShapeContainer.push_back( xShape );
137 void SAL_CALL SvxShapeCollection::remove( const uno::Reference< drawing::XShape >& xShape )
139 std::unique_lock g(m_aMutex);
140 std::erase(maShapeContainer, xShape);
144 sal_Int32 SAL_CALL SvxShapeCollection::getCount()
146 std::unique_lock g(m_aMutex);
147 return maShapeContainer.size();
150 uno::Any SAL_CALL SvxShapeCollection::getByIndex( sal_Int32 Index )
152 if( Index < 0 || Index >= getCount() )
153 throw lang::IndexOutOfBoundsException();
155 std::unique_lock g(m_aMutex);
156 Reference<drawing::XShape> xShape = maShapeContainer[Index];
157 return uno::Any( xShape );
160 std::vector<css::uno::Reference<css::drawing::XShape>> SvxShapeCollection::getAllShapes() const
162 std::unique_lock g(m_aMutex);
163 return maShapeContainer;
166 // XElementAccess
167 uno::Type SAL_CALL SvxShapeCollection::getElementType()
169 return cppu::UnoType<drawing::XShape>::get();
172 sal_Bool SAL_CALL SvxShapeCollection::hasElements()
174 return getCount() != 0;
177 // XServiceInfo
178 OUString SAL_CALL SvxShapeCollection::getImplementationName()
180 return u"com.sun.star.drawing.SvxShapeCollection"_ustr;
183 sal_Bool SAL_CALL SvxShapeCollection::supportsService( const OUString& ServiceName )
185 return cppu::supportsService( this, ServiceName);
188 uno::Sequence< OUString > SAL_CALL SvxShapeCollection::getSupportedServiceNames()
190 return { u"com.sun.star.drawing.Shapes"_ustr, u"com.sun.star.drawing.ShapeCollection"_ustr };
193 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
194 com_sun_star_drawing_SvxShapeCollection_get_implementation(
195 css::uno::XComponentContext *,
196 css::uno::Sequence<css::uno::Any> const &)
198 return cppu::acquire(new SvxShapeCollection);
201 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */