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 #include <ModifyListenerHelper.hxx>
21 #include <WeakListenerAdapter.hxx>
23 #include <tools/diagnose_ex.h>
25 using namespace ::com::sun::star
;
27 using ::com::sun::star::uno::Reference
;
32 void lcl_fireModifyEvent(
33 ::cppu::OBroadcastHelper
const & rBroadcastHelper
,
34 const Reference
< uno::XWeak
> & xEventSource
,
35 const lang::EventObject
* pEvent
)
37 ::cppu::OInterfaceContainerHelper
* pCntHlp
= rBroadcastHelper
.getContainer(
38 cppu::UnoType
<util::XModifyListener
>::get());
41 lang::EventObject aEventToSend
;
43 aEventToSend
= *pEvent
;
45 aEventToSend
.Source
.set( xEventSource
);
46 OSL_ENSURE( aEventToSend
.Source
.is(), "Sending event without source" );
48 ::cppu::OInterfaceIteratorHelper
aIt( *pCntHlp
);
50 while( aIt
.hasMoreElements())
52 Reference
< util::XModifyListener
> xModListener( aIt
.next(), uno::UNO_QUERY
);
53 if( xModListener
.is())
54 xModListener
->modified( aEventToSend
);
59 struct lcl_weakReferenceToSame
61 explicit lcl_weakReferenceToSame( const Reference
< util::XModifyListener
> & xModListener
) :
62 m_xHardRef( xModListener
)
65 // argument type is same as tListenerMap::value_type&
66 bool operator() ( const std::pair
<css::uno::WeakReference
< css::util::XModifyListener
>,
67 css::uno::Reference
< css::util::XModifyListener
> > & xElem
)
69 Reference
< util::XModifyListener
> xWeakAsHard( xElem
.first
);
71 return (xWeakAsHard
== m_xHardRef
);
76 Reference
< util::XModifyListener
> m_xHardRef
;
79 } // anonymous namespace
83 namespace ModifyListenerHelper
86 uno::Reference
< util::XModifyListener
> createModifyEventForwarder()
88 return new ModifyEventForwarder();
91 ModifyEventForwarder::ModifyEventForwarder() :
92 ::cppu::WeakComponentImplHelper
<
93 css::util::XModifyBroadcaster
,
94 css::util::XModifyListener
>( m_aMutex
),
95 m_aModifyListeners( m_aMutex
)
99 void ModifyEventForwarder::AddListener( const Reference
< util::XModifyListener
>& aListener
)
103 Reference
< util::XModifyListener
> xListenerToAdd( aListener
);
105 Reference
< uno::XWeak
> xWeak( aListener
, uno::UNO_QUERY
);
108 // remember the helper class for later remove
109 uno::WeakReference
< util::XModifyListener
> xWeakRef( aListener
);
110 xListenerToAdd
.set( new WeakModifyListenerAdapter( xWeakRef
));
111 m_aListenerMap
.emplace_back( xWeakRef
, xListenerToAdd
);
114 m_aModifyListeners
.addListener( cppu::UnoType
<decltype(xListenerToAdd
)>::get(), xListenerToAdd
);
116 catch( const uno::Exception
& )
118 DBG_UNHANDLED_EXCEPTION("chart2");
122 void ModifyEventForwarder::RemoveListener( const Reference
< util::XModifyListener
>& aListener
)
126 // look up fitting helper class that has been added
127 Reference
< util::XModifyListener
> xListenerToRemove( aListener
);
128 tListenerMap::iterator
aIt(
129 std::find_if( m_aListenerMap
.begin(), m_aListenerMap
.end(), lcl_weakReferenceToSame( aListener
)));
130 if( aIt
!= m_aListenerMap
.end())
132 xListenerToRemove
.set( (*aIt
).second
);
133 // map entry is no longer needed
134 m_aListenerMap
.erase( aIt
);
137 m_aModifyListeners
.removeListener( cppu::UnoType
<decltype(aListener
)>::get(), xListenerToRemove
);
139 catch( const uno::Exception
& )
141 DBG_UNHANDLED_EXCEPTION("chart2");
145 void ModifyEventForwarder::DisposeAndClear( const Reference
< uno::XWeak
> & xSource
)
147 ::cppu::OInterfaceContainerHelper
* pCntHlp
= m_aModifyListeners
.getContainer(
148 cppu::UnoType
<util::XModifyListener
>::get());
150 pCntHlp
->disposeAndClear( lang::EventObject( xSource
) );
153 // ____ XModifyBroadcaster ____
154 void SAL_CALL
ModifyEventForwarder::addModifyListener( const Reference
< util::XModifyListener
>& aListener
)
156 AddListener( aListener
);
159 void SAL_CALL
ModifyEventForwarder::removeModifyListener( const Reference
< util::XModifyListener
>& aListener
)
161 RemoveListener( aListener
);
164 // ____ XModifyListener ____
165 void SAL_CALL
ModifyEventForwarder::modified( const lang::EventObject
& aEvent
)
167 lcl_fireModifyEvent( m_aModifyListeners
, Reference
< uno::XWeak
>(), &aEvent
);
170 // ____ XEventListener (base of XModifyListener) ____
171 void SAL_CALL
ModifyEventForwarder::disposing( const lang::EventObject
& /* Source */ )
176 // ____ WeakComponentImplHelperBase ____
177 void SAL_CALL
ModifyEventForwarder::disposing()
179 // dispose was called at this
180 DisposeAndClear( this );
183 } // namespace ModifyListenerHelper
186 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */