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 .
21 #include <com/sun/star/util/XModifyListener.hpp>
22 #include <com/sun/star/util/XModifyBroadcaster.hpp>
23 #include <comphelper/interfacecontainer4.hxx>
24 #include <comphelper/compbase.hxx>
25 #include <rtl/ref.hxx>
34 /** This helper class serves as forwarder of modify events. It can be used
35 whenever an object has to send modify events after it gets a modify event of
38 class ModifyEventForwarder final
:
39 public ::comphelper::WeakComponentImplHelper
<
40 css::util::XModifyBroadcaster
,
41 css::util::XModifyListener
>
44 ModifyEventForwarder();
46 // ____ XModifyBroadcaster ____
47 virtual void SAL_CALL
addModifyListener(
48 const css::uno::Reference
< css::util::XModifyListener
>& aListener
) override
;
49 virtual void SAL_CALL
removeModifyListener(
50 const css::uno::Reference
< css::util::XModifyListener
>& aListener
) override
;
52 // ____ XModifyListener ____
53 virtual void SAL_CALL
modified(
54 const css::lang::EventObject
& aEvent
) override
;
57 // ____ XEventListener (base of XModifyListener) ____
58 virtual void SAL_CALL
disposing(
59 const css::lang::EventObject
& Source
) override
;
61 // ____ WeakComponentImplHelperBase ____
62 virtual void disposing(std::unique_lock
<std::mutex
>& ) override
;
64 comphelper::OInterfaceContainerHelper4
<css::util::XModifyListener
> m_aModifyListeners
;
69 namespace chart::ModifyListenerHelper
75 template< class InterfaceRef
>
76 struct addListenerFunctor
78 explicit addListenerFunctor( css::uno::Reference
< css::util::XModifyListener
> xListener
) :
79 m_xListener(std::move( xListener
))
82 void operator() ( const InterfaceRef
& xObject
)
84 css::uno::Reference
< css::util::XModifyBroadcaster
>
85 xBroadcaster( xObject
, css::uno::UNO_QUERY
);
86 if( xBroadcaster
.is() && m_xListener
.is())
87 xBroadcaster
->addModifyListener( m_xListener
);
90 css::uno::Reference
< css::util::XModifyListener
> m_xListener
;
93 template< class InterfaceRef
>
94 struct removeListenerFunctor
96 explicit removeListenerFunctor( css::uno::Reference
< css::util::XModifyListener
> xListener
) :
97 m_xListener(std::move( xListener
))
100 void operator() ( const InterfaceRef
& xObject
)
102 css::uno::Reference
< css::util::XModifyBroadcaster
>
103 xBroadcaster( xObject
, css::uno::UNO_QUERY
);
104 if( xBroadcaster
.is() && m_xListener
.is())
105 xBroadcaster
->removeModifyListener( m_xListener
);
108 css::uno::Reference
< css::util::XModifyListener
> m_xListener
;
111 template< class Pair
>
112 struct addListenerToMappedElementFunctor
114 explicit addListenerToMappedElementFunctor( css::uno::Reference
< css::util::XModifyListener
> xListener
) :
115 m_xListener(std::move( xListener
))
118 void operator() ( const Pair
& aPair
)
120 css::uno::Reference
< css::util::XModifyBroadcaster
>
121 xBroadcaster( aPair
.second
, css::uno::UNO_QUERY
);
122 if( xBroadcaster
.is() && m_xListener
.is())
123 xBroadcaster
->addModifyListener( m_xListener
);
126 css::uno::Reference
< css::util::XModifyListener
> m_xListener
;
129 template< class Pair
>
130 struct removeListenerFromMappedElementFunctor
132 explicit removeListenerFromMappedElementFunctor( css::uno::Reference
< css::util::XModifyListener
> xListener
) :
133 m_xListener(std::move( xListener
))
136 void operator() ( const Pair
& aPair
)
138 css::uno::Reference
< css::util::XModifyBroadcaster
>
139 xBroadcaster( aPair
.second
, css::uno::UNO_QUERY
);
140 if( xBroadcaster
.is() && m_xListener
.is())
141 xBroadcaster
->removeModifyListener( m_xListener
);
144 css::uno::Reference
< css::util::XModifyListener
> m_xListener
;
149 template< class InterfaceRef
>
151 const InterfaceRef
& xObject
,
152 const css::uno::Reference
< css::util::XModifyListener
> & xListener
)
156 impl::addListenerFunctor
< InterfaceRef
> aFunctor( xListener
);
162 const rtl::Reference
<T
> & xBroadcaster
,
163 const css::uno::Reference
< css::util::XModifyListener
> & xListener
)
165 if( xBroadcaster
&& xListener
)
166 xBroadcaster
->addModifyListener( xListener
);
169 template< class Container
>
170 void addListenerToAllElements(
171 const Container
& rContainer
,
172 const css::uno::Reference
< css::util::XModifyListener
> & xListener
)
175 std::for_each( rContainer
.begin(), rContainer
.end(),
176 impl::addListenerFunctor
< typename
Container::value_type
>( xListener
));
180 void addListenerToAllElements(
181 const std::vector
<rtl::Reference
<T
>> & rContainer
,
182 const css::uno::Reference
< css::util::XModifyListener
> & xListener
)
186 for (auto const & i
: rContainer
)
187 i
->addModifyListener(xListener
);
190 template< class Container
>
191 void addListenerToAllMapElements(
192 const Container
& rContainer
,
193 const css::uno::Reference
< css::util::XModifyListener
> & xListener
)
196 std::for_each( rContainer
.begin(), rContainer
.end(),
197 impl::addListenerToMappedElementFunctor
< typename
Container::value_type
>( xListener
));
200 template< class InterfaceRef
>
202 const InterfaceRef
& xObject
,
203 const css::uno::Reference
< css::util::XModifyListener
> & xListener
)
207 impl::removeListenerFunctor
< InterfaceRef
> aFunctor( xListener
);
214 const rtl::Reference
<T
> & xBroadcaster
,
215 const css::uno::Reference
< css::util::XModifyListener
> & xListener
)
217 if( xBroadcaster
&& xListener
)
218 xBroadcaster
->removeModifyListener( xListener
);
221 template< class Container
>
222 void removeListenerFromAllElements(
223 const Container
& rContainer
,
224 const css::uno::Reference
< css::util::XModifyListener
> & xListener
)
227 std::for_each( rContainer
.begin(), rContainer
.end(),
228 impl::removeListenerFunctor
< typename
Container::value_type
>( xListener
));
232 void removeListenerFromAllElements(
233 const std::vector
<rtl::Reference
<T
>> & rContainer
,
234 const css::uno::Reference
< css::util::XModifyListener
> & xListener
)
238 for (auto const & i
: rContainer
)
239 i
->removeModifyListener(xListener
);
242 template< class Container
>
243 void removeListenerFromAllMapElements(
244 const Container
& rContainer
,
245 const css::uno::Reference
< css::util::XModifyListener
> & xListener
)
248 std::for_each( rContainer
.begin(), rContainer
.end(),
249 impl::removeListenerFromMappedElementFunctor
< typename
Container::value_type
>( xListener
));
252 } // namespace chart::ModifyListenerHelper
254 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */