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 "ModifyListenerHelper.hxx"
22 #include "WeakListenerAdapter.hxx"
25 #include <cppuhelper/interfacecontainer.hxx>
27 #include <com/sun/star/frame/XModel.hpp>
29 using namespace ::com::sun::star
;
31 using ::com::sun::star::uno::Reference
;
32 using ::com::sun::star::uno::Sequence
;
37 void lcl_fireModifyEvent(
38 ::cppu::OBroadcastHelper
& rBroadcastHelper
,
39 const Reference
< uno::XWeak
> & xEventSource
,
40 const lang::EventObject
* pEvent
)
42 ::cppu::OInterfaceContainerHelper
* pCntHlp
= rBroadcastHelper
.getContainer(
43 ::getCppuType( reinterpret_cast< Reference
< util::XModifyListener
> * >(0)));
46 lang::EventObject aEventToSend
;
48 aEventToSend
= *pEvent
;
50 aEventToSend
.Source
.set( xEventSource
);
51 OSL_ENSURE( aEventToSend
.Source
.is(), "Sending event without source" );
53 ::cppu::OInterfaceIteratorHelper
aIt( *pCntHlp
);
55 while( aIt
.hasMoreElements())
57 Reference
< util::XModifyListener
> xModListener( aIt
.next(), uno::UNO_QUERY
);
58 if( xModListener
.is())
59 xModListener
->modified( aEventToSend
);
64 struct lcl_weakReferenceToSame
: public ::std::unary_function
<
66 ::com::sun::star::uno::WeakReference
< ::com::sun::star::util::XModifyListener
>,
67 ::com::sun::star::uno::Reference
< ::com::sun::star::util::XModifyListener
> >,
70 lcl_weakReferenceToSame( const Reference
< util::XModifyListener
> & xModListener
) :
71 m_xHardRef( xModListener
)
74 bool operator() ( const argument_type
& xElem
)
76 Reference
< util::XModifyListener
> xWeakAsHard( xElem
.first
);
78 return (xWeakAsHard
== m_xHardRef
);
83 Reference
< util::XModifyListener
> m_xHardRef
;
86 } // anonymous namespace
88 // ================================================================================
92 namespace ModifyListenerHelper
95 uno::Reference
< util::XModifyListener
> createModifyEventForwarder()
97 return new ModifyEventForwarder();
100 ModifyEventForwarder::ModifyEventForwarder() :
101 ::cppu::WeakComponentImplHelper2
<
102 ::com::sun::star::util::XModifyBroadcaster
,
103 ::com::sun::star::util::XModifyListener
>( m_aMutex
),
104 m_aModifyListeners( m_aMutex
)
108 void ModifyEventForwarder::FireEvent( const lang::EventObject
& rEvent
)
110 lcl_fireModifyEvent( m_aModifyListeners
, Reference
< uno::XWeak
>(), & rEvent
);
113 void ModifyEventForwarder::AddListener( const Reference
< util::XModifyListener
>& aListener
)
117 Reference
< util::XModifyListener
> xListenerToAdd( aListener
);
119 Reference
< uno::XWeak
> xWeak( aListener
, uno::UNO_QUERY
);
122 // remember the helper class for later remove
123 uno::WeakReference
< util::XModifyListener
> xWeakRef( aListener
);
124 xListenerToAdd
.set( new WeakModifyListenerAdapter( xWeakRef
));
125 m_aListenerMap
.push_back( tListenerMap::value_type( xWeakRef
, xListenerToAdd
));
128 m_aModifyListeners
.addListener( ::getCppuType( &xListenerToAdd
), xListenerToAdd
);
130 catch( const uno::Exception
& ex
)
132 ASSERT_EXCEPTION( ex
);
136 void ModifyEventForwarder::RemoveListener( const Reference
< util::XModifyListener
>& aListener
)
140 // look up fitting helper class that has been added
141 Reference
< util::XModifyListener
> xListenerToRemove( aListener
);
142 tListenerMap::iterator
aIt(
143 ::std::find_if( m_aListenerMap
.begin(), m_aListenerMap
.end(), lcl_weakReferenceToSame( aListener
)));
144 if( aIt
!= m_aListenerMap
.end())
146 xListenerToRemove
.set( (*aIt
).second
);
147 // map entry is no longer needed
148 m_aListenerMap
.erase( aIt
);
151 m_aModifyListeners
.removeListener( ::getCppuType( &aListener
), xListenerToRemove
);
153 catch( const uno::Exception
& ex
)
155 ASSERT_EXCEPTION( ex
);
159 void ModifyEventForwarder::DisposeAndClear( const Reference
< uno::XWeak
> & xSource
)
161 ::cppu::OInterfaceContainerHelper
* pCntHlp
= m_aModifyListeners
.getContainer(
162 ::getCppuType( reinterpret_cast< Reference
< util::XModifyListener
> * >(0)));
164 pCntHlp
->disposeAndClear( lang::EventObject( xSource
) );
167 // ____ XModifyBroadcaster ____
168 void SAL_CALL
ModifyEventForwarder::addModifyListener( const Reference
< util::XModifyListener
>& aListener
)
169 throw (uno::RuntimeException
)
171 AddListener( aListener
);
174 void SAL_CALL
ModifyEventForwarder::removeModifyListener( const Reference
< util::XModifyListener
>& aListener
)
175 throw (uno::RuntimeException
)
177 RemoveListener( aListener
);
180 // ____ XModifyListener ____
181 void SAL_CALL
ModifyEventForwarder::modified( const lang::EventObject
& aEvent
)
182 throw (uno::RuntimeException
)
187 // ____ XEventListener (base of XModifyListener) ____
188 void SAL_CALL
ModifyEventForwarder::disposing( const lang::EventObject
& /* Source */ )
189 throw (uno::RuntimeException
)
194 // ____ WeakComponentImplHelperBase ____
195 void SAL_CALL
ModifyEventForwarder::disposing()
197 // dispose was called at this
198 DisposeAndClear( this );
201 } // namespace ModifyListenerHelper
204 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */