1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 #ifndef INCLUDED_FORMS_SOURCE_INC_INTERFACECONTAINER_HXX
21 #define INCLUDED_FORMS_SOURCE_INC_INTERFACECONTAINER_HXX
23 #include <comphelper/types.hxx>
24 #include <comphelper/uno3.hxx>
25 #include <com/sun/star/container/XNameReplace.hpp>
26 #include <com/sun/star/container/XIndexReplace.hpp>
27 #include <com/sun/star/container/XNameContainer.hpp>
28 #include <com/sun/star/lang/XEventListener.hpp>
29 #include <com/sun/star/lang/EventObject.hpp>
30 #include <com/sun/star/container/XEnumerationAccess.hpp>
31 #include <com/sun/star/io/XPersistObject.hpp>
32 #include <com/sun/star/beans/XPropertyChangeListener.hpp>
33 #include <com/sun/star/beans/PropertyChangeEvent.hpp>
34 #include <com/sun/star/beans/XPropertySet.hpp>
35 #include <com/sun/star/script/XEventAttacherManager.hpp>
36 #include <com/sun/star/script/ScriptEvent.hpp>
37 #include <com/sun/star/script/ScriptEventDescriptor.hpp>
38 #include <com/sun/star/container/XContainer.hpp>
39 #include <com/sun/star/container/XIndexContainer.hpp>
40 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
41 #include <com/sun/star/form/XFormComponent.hpp>
42 #include <com/sun/star/util/XCloneable.hpp>
43 #include <osl/mutex.hxx>
44 #include <comphelper/interfacecontainer2.hxx>
45 #include <cppuhelper/component.hxx>
46 #include <cppuhelper/implbase8.hxx>
47 #include <unordered_map>
49 namespace com
{ namespace sun
{ namespace star
{ namespace uno
{
50 class XComponentContext
;
53 using namespace comphelper
;
60 struct ElementDescription
63 css::uno::Reference
< css::uno::XInterface
> xInterface
;
64 css::uno::Reference
< css::beans::XPropertySet
> xPropertySet
;
65 css::uno::Reference
< css::container::XChild
> xChild
;
66 css::uno::Any aElementTypeInterface
;
69 ElementDescription( );
70 virtual ~ElementDescription();
73 ElementDescription( const ElementDescription
& ) = delete;
74 ElementDescription
& operator=( const ElementDescription
& ) = delete;
77 typedef std::vector
<css::uno::Reference
<css::uno::XInterface
>> OInterfaceArray
;
78 typedef std::unordered_multimap
< OUString
, css::uno::Reference
<css::uno::XInterface
>, OUStringHash
> OInterfaceMap
;
81 // OInterfaceContainer
82 // implements a container for form components
84 typedef ::cppu::ImplHelper8
< css::container::XNameContainer
85 , css::container::XIndexContainer
86 , css::container::XContainer
87 , css::container::XEnumerationAccess
88 , css::script::XEventAttacherManager
89 , css::beans::XPropertyChangeListener
90 , css::io::XPersistObject
91 , css::util::XCloneable
92 > OInterfaceContainer_BASE
;
94 class OInterfaceContainer
: public OInterfaceContainer_BASE
97 ::osl::Mutex
& m_rMutex
;
99 OInterfaceArray m_aItems
;
100 OInterfaceMap m_aMap
;
101 ::comphelper::OInterfaceContainerHelper2 m_aContainerListeners
;
103 const css::uno::Type m_aElementType
;
105 css::uno::Reference
< css::uno::XComponentContext
> m_xContext
;
109 css::uno::Reference
< css::script::XEventAttacherManager
> m_xEventAttacher
;
113 const css::uno::Reference
< css::uno::XComponentContext
>& _rxFactory
,
114 ::osl::Mutex
& _rMutex
,
115 const css::uno::Type
& _rElementType
);
117 OInterfaceContainer( ::osl::Mutex
& _rMutex
, const OInterfaceContainer
& _cloneSource
);
119 // late constructor for cloning
120 void clonedFrom(const OInterfaceContainer
& _cloneSource
) throw(css::uno::RuntimeException
, std::exception
);
123 virtual ~OInterfaceContainer();
126 // css::io::XPersistObject
127 virtual OUString SAL_CALL
getServiceName( ) throw(css::uno::RuntimeException
, std::exception
) override
= 0;
128 virtual void SAL_CALL
write( const css::uno::Reference
< css::io::XObjectOutputStream
>& OutStream
) throw(css::io::IOException
, css::uno::RuntimeException
, std::exception
) override
;
129 virtual void SAL_CALL
read( const css::uno::Reference
< css::io::XObjectInputStream
>& InStream
) throw(css::io::IOException
, css::uno::RuntimeException
, std::exception
) override
;
131 // css::lang::XEventListener
132 virtual void SAL_CALL
disposing(const css::lang::EventObject
& _rSource
) throw(css::uno::RuntimeException
, std::exception
) override
;
134 // css::beans::XPropertyChangeListener
135 virtual void SAL_CALL
propertyChange(const css::beans::PropertyChangeEvent
& evt
) throw (css::uno::RuntimeException
, std::exception
) override
;
137 // css::container::XElementAccess
138 virtual css::uno::Type SAL_CALL
getElementType() throw(css::uno::RuntimeException
, std::exception
) override
;
139 virtual sal_Bool SAL_CALL
hasElements() throw(css::uno::RuntimeException
, std::exception
) override
;
141 // css::container::XEnumerationAccess
142 virtual css::uno::Reference
< css::container::XEnumeration
> SAL_CALL
createEnumeration() throw(css::uno::RuntimeException
, std::exception
) override
;
144 // css::container::XNameAccess
145 virtual css::uno::Any SAL_CALL
getByName( const OUString
& aName
) throw(css::container::NoSuchElementException
, css::lang::WrappedTargetException
, css::uno::RuntimeException
, std::exception
) override
;
146 virtual css::uno::Sequence
<OUString
> SAL_CALL
getElementNames( ) throw(css::uno::RuntimeException
, std::exception
) override
;
147 virtual sal_Bool SAL_CALL
hasByName( const OUString
& aName
) throw(css::uno::RuntimeException
, std::exception
) override
;
149 // css::container::XNameReplace
150 virtual void SAL_CALL
replaceByName(const OUString
& Name
, const css::uno::Any
& _rElement
) throw(css::lang::IllegalArgumentException
, css::container::NoSuchElementException
, css::lang::WrappedTargetException
, css::uno::RuntimeException
, std::exception
) override
;
152 // css::container::XNameContainer
153 virtual void SAL_CALL
insertByName(const OUString
& Name
, const css::uno::Any
& _rElement
) throw(css::lang::IllegalArgumentException
, css::container::ElementExistException
, css::lang::WrappedTargetException
, css::uno::RuntimeException
, std::exception
) override
;
154 virtual void SAL_CALL
removeByName(const OUString
& Name
) throw(css::container::NoSuchElementException
, css::lang::WrappedTargetException
, css::uno::RuntimeException
, std::exception
) override
;
156 // css::container::XIndexAccess
157 virtual sal_Int32 SAL_CALL
getCount() throw(css::uno::RuntimeException
, std::exception
) override
;
158 virtual css::uno::Any SAL_CALL
getByIndex(sal_Int32 _nIndex
) throw(css::lang::IndexOutOfBoundsException
, css::lang::WrappedTargetException
, css::uno::RuntimeException
, std::exception
) override
;
160 // css::container::XIndexReplace
161 virtual void SAL_CALL
replaceByIndex(sal_Int32 _nIndex
, const css::uno::Any
& _rElement
) throw(css::lang::IllegalArgumentException
, css::lang::IndexOutOfBoundsException
, css::lang::WrappedTargetException
, css::uno::RuntimeException
, std::exception
) override
;
163 // css::container::XIndexContainer
164 virtual void SAL_CALL
insertByIndex(sal_Int32 _nIndex
, const css::uno::Any
& Element
) throw(css::lang::IllegalArgumentException
, css::lang::IndexOutOfBoundsException
, css::lang::WrappedTargetException
, css::uno::RuntimeException
, std::exception
) override
;
165 virtual void SAL_CALL
removeByIndex(sal_Int32 _nIndex
) throw(css::lang::IndexOutOfBoundsException
, css::lang::WrappedTargetException
, css::uno::RuntimeException
, std::exception
) override
;
167 // css::container::XContainer
168 virtual void SAL_CALL
addContainerListener(const css::uno::Reference
< css::container::XContainerListener
>& _rxListener
) throw(css::uno::RuntimeException
, std::exception
) override
;
169 virtual void SAL_CALL
removeContainerListener(const css::uno::Reference
< css::container::XContainerListener
>& _rxListener
) throw(css::uno::RuntimeException
, std::exception
) override
;
171 // css::script::XEventAttacherManager
172 virtual void SAL_CALL
registerScriptEvent( sal_Int32 nIndex
, const css::script::ScriptEventDescriptor
& aScriptEvent
) throw(css::lang::IllegalArgumentException
, css::uno::RuntimeException
, std::exception
) override
;
173 virtual void SAL_CALL
registerScriptEvents( sal_Int32 nIndex
, const css::uno::Sequence
< css::script::ScriptEventDescriptor
>& aScriptEvents
) throw(css::lang::IllegalArgumentException
, css::uno::RuntimeException
, std::exception
) override
;
174 virtual void SAL_CALL
revokeScriptEvent( sal_Int32 nIndex
, const OUString
& aListenerType
, const OUString
& aEventMethod
, const OUString
& aRemoveListenerParam
) throw(css::lang::IllegalArgumentException
, css::uno::RuntimeException
, std::exception
) override
;
175 virtual void SAL_CALL
revokeScriptEvents( sal_Int32 nIndex
) throw(css::lang::IllegalArgumentException
, css::uno::RuntimeException
, std::exception
) override
;
176 virtual void SAL_CALL
insertEntry( sal_Int32 nIndex
) throw(css::lang::IllegalArgumentException
, css::uno::RuntimeException
, std::exception
) override
;
177 virtual void SAL_CALL
removeEntry( sal_Int32 nIndex
) throw(css::lang::IllegalArgumentException
, css::uno::RuntimeException
, std::exception
) override
;
178 virtual css::uno::Sequence
< css::script::ScriptEventDescriptor
> SAL_CALL
getScriptEvents( sal_Int32 Index
) throw(css::lang::IllegalArgumentException
, css::uno::RuntimeException
, std::exception
) override
;
179 virtual void SAL_CALL
attach( sal_Int32 nIndex
, const css::uno::Reference
< css::uno::XInterface
>& xObject
, const css::uno::Any
& aHelper
) throw(css::lang::IllegalArgumentException
, css::lang::ServiceNotRegisteredException
, css::uno::RuntimeException
, std::exception
) override
;
180 virtual void SAL_CALL
detach( sal_Int32 nIndex
, const css::uno::Reference
< css::uno::XInterface
>& xObject
) throw(css::lang::IllegalArgumentException
, css::uno::RuntimeException
, std::exception
) override
;
181 virtual void SAL_CALL
addScriptListener( const css::uno::Reference
< css::script::XScriptListener
>& xListener
) throw(css::lang::IllegalArgumentException
, css::uno::RuntimeException
, std::exception
) override
;
182 virtual void SAL_CALL
removeScriptListener( const css::uno::Reference
< css::script::XScriptListener
>& Listener
) throw(css::lang::IllegalArgumentException
, css::uno::RuntimeException
, std::exception
) override
;
186 virtual void SAL_CALL
disposing();
187 void removeElementsNoEvents();
189 /** to be overridden if elements which are to be inserted into the container shall be checked
191 <p>the ElementDescription given can be used to cache information about the object - it will be passed
192 later on to implInserted/implReplaced.</p>
194 virtual void approveNewElement(
195 const css::uno::Reference
< css::beans::XPropertySet
>& _rxObject
,
196 ElementDescription
* _pElement
199 virtual ElementDescription
* createElementMetaData( );
201 /** inserts an object into our internal structures
204 the index at which position it should be inserted
206 if <TRUE/>, event knittings will be done
207 @param _pApprovalResult
208 must contain the result of an approveNewElement call. Can be <NULL/>, in this case, the approval
209 is done within implInsert.
211 if <TRUE/>, a notification about the insertion will be fired
215 const css::uno::Reference
< css::beans::XPropertySet
>& _rxObject
,
216 bool _bEvents
/* = sal_True */,
217 ElementDescription
* _pApprovalResult
/* = NULL */ ,
218 bool _bFire
/* = sal_True */
219 ) throw(css::lang::IllegalArgumentException
);
221 // called after the object is inserted, but before the "real listeners" are notified
222 virtual void implInserted( const ElementDescription
* _pElement
);
223 // called after the object is removed, but before the "real listeners" are notified
224 virtual void implRemoved(const css::uno::Reference
<css::uno::XInterface
>& _rxObject
);
226 /** called after an object was replaced. The default implementation notifies our listeners, after releasing
229 virtual void impl_replacedElement(
230 const css::container::ContainerEvent
& _rEvent
,
231 ::osl::ClearableMutexGuard
& _rInstanceLock
234 void SAL_CALL
writeEvents(const css::uno::Reference
< css::io::XObjectOutputStream
>& _rxOutStream
);
235 void SAL_CALL
readEvents(const css::uno::Reference
< css::io::XObjectInputStream
>& _rxInStream
);
237 /** replace an element, specified by position
239 @precond <arg>_nIndex</arg> is a valid index
240 @precond our mutex is locked exactly once, by the guard specified with <arg>_rClearBeforeNotify</arg>
243 void implReplaceByIndex(
244 const sal_Int32 _nIndex
,
245 const css::uno::Any
& _rNewElement
,
246 ::osl::ClearableMutexGuard
& _rClearBeforeNotify
249 /** removes an element, specified by position
251 @precond <arg>_nIndex</arg> is a valid index
252 @precond our mutex is locked exactly once, by the guard specified with <arg>_rClearBeforeNotify</arg>
255 void implRemoveByIndex(
256 const sal_Int32 _nIndex
,
257 ::osl::ClearableMutexGuard
& _rClearBeforeNotify
260 /** validates the given index
261 @throws css::lang::IndexOutOfBoundsException
262 if the given index does not denote a valid position in our children array
264 void implCheckIndex( const sal_Int32 _nIndex
);
267 // hack for Vba Events
268 void impl_addVbEvents_nolck_nothrow( const sal_Int32 i_nIndex
);
270 // the runtime event format has changed from version SO5.2 to OOo
276 void transformEvents( const EventFormat _eTargetFormat
);
278 void impl_createEventAttacher_nothrow();
281 typedef ::cppu::ImplHelper1
< css::form::XFormComponent
> OFormComponents_BASE
;
282 typedef ::cppu::OComponentHelper FormComponentsBase
;
283 // else MSVC kills itself on some statements
284 class OFormComponents
:public FormComponentsBase
285 ,public OInterfaceContainer
286 ,public OFormComponents_BASE
289 ::osl::Mutex m_aMutex
;
290 css::uno::Reference
<css::uno::XInterface
> m_xParent
;
293 OFormComponents(const css::uno::Reference
< css::uno::XComponentContext
>& _rxFactory
);
294 OFormComponents( const OFormComponents
& _cloneSource
);
295 virtual ~OFormComponents();
297 DECLARE_UNO3_AGG_DEFAULTS(OFormComponents
, FormComponentsBase
)
299 virtual css::uno::Any SAL_CALL
queryAggregation(const css::uno::Type
& _rType
) throw(css::uno::RuntimeException
, std::exception
) override
;
300 virtual css::uno::Sequence
< css::uno::Type
> SAL_CALL
getTypes( ) throw(css::uno::RuntimeException
, std::exception
) override
;
303 virtual void SAL_CALL
disposing() override
;
305 // css::form::XFormComponent
306 virtual css::uno::Reference
<css::uno::XInterface
> SAL_CALL
getParent() throw(css::uno::RuntimeException
, std::exception
) override
;
307 virtual void SAL_CALL
setParent(const css::uno::Reference
<css::uno::XInterface
>& Parent
) throw(css::lang::NoSupportException
, css::uno::RuntimeException
, std::exception
) override
;
310 using OInterfaceContainer::disposing
;
316 #endif // INCLUDED_FORMS_SOURCE_INC_INTERFACECONTAINER_HXX
318 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */