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 .
22 #include <comphelper/uno3.hxx>
23 #include <com/sun/star/container/XNameContainer.hpp>
24 #include <com/sun/star/lang/EventObject.hpp>
25 #include <com/sun/star/container/XEnumerationAccess.hpp>
26 #include <com/sun/star/io/XPersistObject.hpp>
27 #include <com/sun/star/beans/XPropertyChangeListener.hpp>
28 #include <com/sun/star/beans/PropertyChangeEvent.hpp>
29 #include <com/sun/star/beans/XPropertySet.hpp>
30 #include <com/sun/star/script/XEventAttacherManager.hpp>
31 #include <com/sun/star/script/ScriptEventDescriptor.hpp>
32 #include <com/sun/star/container/XContainer.hpp>
33 #include <com/sun/star/container/XIndexContainer.hpp>
34 #include <com/sun/star/form/XFormComponent.hpp>
35 #include <com/sun/star/util/XCloneable.hpp>
36 #include <osl/mutex.hxx>
37 #include <comphelper/interfacecontainer3.hxx>
38 #include <cppuhelper/component.hxx>
39 #include <cppuhelper/implbase1.hxx>
40 #include <cppuhelper/implbase8.hxx>
41 #include <unordered_map>
43 namespace com::sun::star::uno
{ class XComponentContext
; }
45 using namespace comphelper
;
52 struct ElementDescription
55 css::uno::Reference
< css::uno::XInterface
> xInterface
;
56 css::uno::Reference
< css::beans::XPropertySet
> xPropertySet
;
57 css::uno::Reference
< css::container::XChild
> xChild
;
58 css::uno::Any aElementTypeInterface
;
61 ElementDescription( );
64 ElementDescription( const ElementDescription
& ) = delete;
65 ElementDescription
& operator=( const ElementDescription
& ) = delete;
68 typedef std::vector
<css::uno::Reference
<css::uno::XInterface
>> OInterfaceArray
;
69 typedef std::unordered_multimap
< OUString
, css::uno::Reference
<css::uno::XInterface
> > OInterfaceMap
;
72 // OInterfaceContainer
73 // implements a container for form components
75 typedef ::cppu::ImplHelper8
< css::container::XNameContainer
76 , css::container::XIndexContainer
77 , css::container::XContainer
78 , css::container::XEnumerationAccess
79 , css::script::XEventAttacherManager
80 , css::beans::XPropertyChangeListener
81 , css::io::XPersistObject
82 , css::util::XCloneable
83 > OInterfaceContainer_BASE
;
85 class OInterfaceContainer
: public OInterfaceContainer_BASE
88 ::osl::Mutex
& m_rMutex
;
90 OInterfaceArray m_aItems
;
92 ::comphelper::OInterfaceContainerHelper3
<css::container::XContainerListener
> m_aContainerListeners
;
94 const css::uno::Type m_aElementType
;
96 css::uno::Reference
< css::uno::XComponentContext
> m_xContext
;
100 css::uno::Reference
< css::script::XEventAttacherManager
> m_xEventAttacher
;
104 const css::uno::Reference
< css::uno::XComponentContext
>& _rxFactory
,
105 ::osl::Mutex
& _rMutex
,
106 const css::uno::Type
& _rElementType
);
108 OInterfaceContainer( ::osl::Mutex
& _rMutex
, const OInterfaceContainer
& _cloneSource
);
110 // late constructor for cloning
111 /// @throws css::uno::RuntimeException
112 void clonedFrom(const OInterfaceContainer
& _cloneSource
);
115 virtual ~OInterfaceContainer();
118 // css::io::XPersistObject
119 virtual OUString SAL_CALL
getServiceName( ) override
= 0;
120 virtual void SAL_CALL
write( const css::uno::Reference
< css::io::XObjectOutputStream
>& OutStream
) override
;
121 virtual void SAL_CALL
read( const css::uno::Reference
< css::io::XObjectInputStream
>& InStream
) override
;
123 // css::lang::XEventListener
124 virtual void SAL_CALL
disposing(const css::lang::EventObject
& _rSource
) override
;
126 // css::beans::XPropertyChangeListener
127 virtual void SAL_CALL
propertyChange(const css::beans::PropertyChangeEvent
& evt
) override
;
129 // css::container::XElementAccess
130 virtual css::uno::Type SAL_CALL
getElementType() override
;
131 virtual sal_Bool SAL_CALL
hasElements() override
;
133 // css::container::XEnumerationAccess
134 virtual css::uno::Reference
< css::container::XEnumeration
> SAL_CALL
createEnumeration() override
;
136 // css::container::XNameAccess
137 virtual css::uno::Any SAL_CALL
getByName( const OUString
& aName
) override
;
138 virtual css::uno::Sequence
<OUString
> SAL_CALL
getElementNames( ) override
;
139 virtual sal_Bool SAL_CALL
hasByName( const OUString
& aName
) override
;
141 // css::container::XNameReplace
142 virtual void SAL_CALL
replaceByName(const OUString
& Name
, const css::uno::Any
& _rElement
) override
;
144 // css::container::XNameContainer
145 virtual void SAL_CALL
insertByName(const OUString
& Name
, const css::uno::Any
& _rElement
) override
;
146 virtual void SAL_CALL
removeByName(const OUString
& Name
) override
;
148 // css::container::XIndexAccess
149 virtual sal_Int32 SAL_CALL
getCount() override
;
150 virtual css::uno::Any SAL_CALL
getByIndex(sal_Int32 _nIndex
) override
;
152 // css::container::XIndexReplace
153 virtual void SAL_CALL
replaceByIndex(sal_Int32 _nIndex
, const css::uno::Any
& _rElement
) override
;
155 // css::container::XIndexContainer
156 virtual void SAL_CALL
insertByIndex(sal_Int32 _nIndex
, const css::uno::Any
& Element
) override
;
157 virtual void SAL_CALL
removeByIndex(sal_Int32 _nIndex
) override
;
159 // css::container::XContainer
160 virtual void SAL_CALL
addContainerListener(const css::uno::Reference
< css::container::XContainerListener
>& _rxListener
) override
;
161 virtual void SAL_CALL
removeContainerListener(const css::uno::Reference
< css::container::XContainerListener
>& _rxListener
) override
;
163 // css::script::XEventAttacherManager
164 virtual void SAL_CALL
registerScriptEvent( sal_Int32 nIndex
, const css::script::ScriptEventDescriptor
& aScriptEvent
) override
;
165 virtual void SAL_CALL
registerScriptEvents( sal_Int32 nIndex
, const css::uno::Sequence
< css::script::ScriptEventDescriptor
>& aScriptEvents
) override
;
166 virtual void SAL_CALL
revokeScriptEvent( sal_Int32 nIndex
, const OUString
& aListenerType
, const OUString
& aEventMethod
, const OUString
& aRemoveListenerParam
) override
;
167 virtual void SAL_CALL
revokeScriptEvents( sal_Int32 nIndex
) override
;
168 virtual void SAL_CALL
insertEntry( sal_Int32 nIndex
) override
;
169 virtual void SAL_CALL
removeEntry( sal_Int32 nIndex
) override
;
170 virtual css::uno::Sequence
< css::script::ScriptEventDescriptor
> SAL_CALL
getScriptEvents( sal_Int32 Index
) override
;
171 virtual void SAL_CALL
attach( sal_Int32 nIndex
, const css::uno::Reference
< css::uno::XInterface
>& xObject
, const css::uno::Any
& aHelper
) override
;
172 virtual void SAL_CALL
detach( sal_Int32 nIndex
, const css::uno::Reference
< css::uno::XInterface
>& xObject
) override
;
173 virtual void SAL_CALL
addScriptListener( const css::uno::Reference
< css::script::XScriptListener
>& xListener
) override
;
174 virtual void SAL_CALL
removeScriptListener( const css::uno::Reference
< css::script::XScriptListener
>& Listener
) override
;
178 virtual void SAL_CALL
disposing();
179 void removeElementsNoEvents();
181 /** to be overridden if elements which are to be inserted into the container shall be checked
183 <p>the ElementDescription given can be used to cache information about the object - it will be passed
184 later on to implInserted/implReplaced.</p>
186 virtual void approveNewElement(
187 const css::uno::Reference
< css::beans::XPropertySet
>& _rxObject
,
188 ElementDescription
* _pElement
191 virtual ElementDescription
* createElementMetaData( );
193 /** inserts an object into our internal structures
196 the index at which position it should be inserted
198 if <TRUE/>, event knittings will be done
199 @param _pApprovalResult
200 must contain the result of an approveNewElement call. Can be <NULL/>, in this case, the approval
201 is done within implInsert.
203 if <TRUE/>, a notification about the insertion will be fired
204 @throws css::lang::IllegalArgumentException
208 const css::uno::Reference
< css::beans::XPropertySet
>& _rxObject
,
209 bool _bEvents
/* = sal_True */,
210 ElementDescription
* _pApprovalResult
/* = NULL */ ,
211 bool _bFire
/* = sal_True */
214 // called after the object is inserted, but before the "real listeners" are notified
215 virtual void implInserted( const ElementDescription
* _pElement
);
216 // called after the object is removed, but before the "real listeners" are notified
217 virtual void implRemoved(const css::uno::Reference
<css::uno::XInterface
>& _rxObject
);
219 /** called after an object was replaced. The default implementation notifies our listeners, after releasing
222 virtual void impl_replacedElement(
223 const css::container::ContainerEvent
& _rEvent
,
224 ::osl::ClearableMutexGuard
& _rInstanceLock
227 void SAL_CALL
writeEvents(const css::uno::Reference
< css::io::XObjectOutputStream
>& _rxOutStream
);
228 void SAL_CALL
readEvents(const css::uno::Reference
< css::io::XObjectInputStream
>& _rxInStream
);
230 /** replace an element, specified by position
232 @precond <arg>_nIndex</arg> is a valid index
233 @precond our mutex is locked exactly once, by the guard specified with <arg>_rClearBeforeNotify</arg>
236 void implReplaceByIndex(
237 const sal_Int32 _nIndex
,
238 const css::uno::Any
& _rNewElement
,
239 ::osl::ClearableMutexGuard
& _rClearBeforeNotify
242 /** removes an element, specified by position
244 @precond <arg>_nIndex</arg> is a valid index
245 @precond our mutex is locked exactly once, by the guard specified with <arg>_rClearBeforeNotify</arg>
248 void implRemoveByIndex(
249 const sal_Int32 _nIndex
,
250 ::osl::ClearableMutexGuard
& _rClearBeforeNotify
253 /** validates the given index
254 @throws css::lang::IndexOutOfBoundsException
255 if the given index does not denote a valid position in our children array
257 void implCheckIndex( const sal_Int32 _nIndex
);
260 // hack for Vba Events
261 void impl_addVbEvents_nolck_nothrow( const sal_Int32 i_nIndex
);
263 void transformEvents();
265 void impl_createEventAttacher_nothrow();
268 typedef ::cppu::ImplHelper1
< css::form::XFormComponent
> OFormComponents_BASE
;
269 class OFormComponents
:public ::cppu::OComponentHelper
270 ,public OInterfaceContainer
271 ,public OFormComponents_BASE
274 ::osl::Mutex m_aMutex
;
275 css::uno::Reference
<css::uno::XInterface
> m_xParent
;
278 OFormComponents(const css::uno::Reference
< css::uno::XComponentContext
>& _rxFactory
);
279 OFormComponents( const OFormComponents
& _cloneSource
);
280 virtual ~OFormComponents() override
;
282 DECLARE_UNO3_AGG_DEFAULTS(OFormComponents
, ::cppu::OComponentHelper
)
284 virtual css::uno::Any SAL_CALL
queryAggregation(const css::uno::Type
& _rType
) override
;
285 virtual css::uno::Sequence
< css::uno::Type
> SAL_CALL
getTypes( ) override
;
288 virtual void SAL_CALL
disposing() override
;
290 // css::form::XFormComponent
291 virtual css::uno::Reference
<css::uno::XInterface
> SAL_CALL
getParent() override
;
292 virtual void SAL_CALL
setParent(const css::uno::Reference
<css::uno::XInterface
>& Parent
) override
;
295 using OInterfaceContainer::disposing
;
301 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */