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/lang/XEventListener.hpp>
22 #include <com/sun/star/util/XModifyListener.hpp>
23 #include <com/sun/star/view/XSelectionChangeListener.hpp>
24 #include <cppuhelper/weakref.hxx>
25 #include <cppuhelper/implbase.hxx>
30 /** Adapter that enables adding listeners as weak UNO references. Thus, adding
31 an object as listener to a broadcaster does not increase its reference
34 <p>The helper class, of course, is held as hard reference at the
35 broadcaster, but this should never be a problem as the adapter's life time
36 depends on no other object.</p>
38 <p>Note that in order to remove an object as listener, you have to remove
39 the same wrapper that you added, i.e., you should store the adapter as a
40 member in the adapted class for later use.</p>
42 template< class Listener
>
43 class WeakListenerAdapter
: public
44 ::cppu::WeakImplHelper
< Listener
>
47 explicit WeakListenerAdapter( const css::uno::Reference
< Listener
> & xListener
) :
48 m_xListener( xListener
)
50 explicit WeakListenerAdapter( const css::uno::WeakReference
< Listener
> & xListener
) :
51 m_xListener( xListener
)
55 // ____ XEventListener (base of all listeners) ____
56 virtual void SAL_CALL
disposing(
57 const css::lang::EventObject
& Source
) override
59 css::uno::Reference
< css::lang::XEventListener
> xEventListener(
60 css::uno::Reference
< Listener
>( m_xListener
), css::uno::UNO_QUERY
);
61 if( xEventListener
.is())
62 xEventListener
->disposing( Source
);
65 css::uno::Reference
< Listener
> getListener() const
71 css::uno::WeakReference
< Listener
> m_xListener
;
74 class WeakModifyListenerAdapter
:
75 public WeakListenerAdapter
< css::util::XModifyListener
>
78 explicit WeakModifyListenerAdapter(
79 const css::uno::WeakReference
< css::util::XModifyListener
> & xListener
);
80 virtual ~WeakModifyListenerAdapter() override
;
83 // ____ XModifyListener ____
84 virtual void SAL_CALL
modified( const css::lang::EventObject
& aEvent
) override
;
87 class WeakSelectionChangeListenerAdapter
:
88 public WeakListenerAdapter
< css::view::XSelectionChangeListener
>
91 explicit WeakSelectionChangeListenerAdapter(
92 const css::uno::Reference
< css::view::XSelectionChangeListener
> & xListener
);
93 virtual ~WeakSelectionChangeListenerAdapter() override
;
96 // ____ XSelectionChangeListener ____
97 virtual void SAL_CALL
selectionChanged(
98 const css::lang::EventObject
& aEvent
) override
;
103 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */