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 <com/sun/star/awt/XKeyListener.hpp>
23 #include <com/sun/star/awt/XPaintListener.hpp>
24 #include <com/sun/star/awt/XMouseMotionListener.hpp>
25 #include <com/sun/star/awt/XWindow.hpp>
26 #include <com/sun/star/awt/XWindowListener.hpp>
27 #include <com/sun/star/awt/XMouseListener.hpp>
28 #include <com/sun/star/awt/XFocusListener.hpp>
29 #include <comphelper/compbase.hxx>
30 #include <comphelper/interfacecontainer4.hxx>
31 #include <cppuhelper/weakref.hxx>
33 #include <type_traits>
35 namespace unocontrols
{
37 template <class Listener
> extern int Add
; // dummy
38 template <class Listener
> extern int Remove
; // dummy
40 template <> constexpr inline auto Add
<css::awt::XFocusListener
> = &css::awt::XWindow::addFocusListener
;
41 template <> constexpr inline auto Remove
<css::awt::XFocusListener
> = &css::awt::XWindow::removeFocusListener
;
43 template <> constexpr inline auto Add
<css::awt::XWindowListener
> = &css::awt::XWindow::addWindowListener
;
44 template <> constexpr inline auto Remove
<css::awt::XWindowListener
> = &css::awt::XWindow::removeWindowListener
;
46 template <> constexpr inline auto Add
<css::awt::XKeyListener
> = &css::awt::XWindow::addKeyListener
;
47 template <> constexpr inline auto Remove
<css::awt::XKeyListener
> = &css::awt::XWindow::removeKeyListener
;
49 template <> constexpr inline auto Add
<css::awt::XMouseListener
> = &css::awt::XWindow::addMouseListener
;
50 template <> constexpr inline auto Remove
<css::awt::XMouseListener
> = &css::awt::XWindow::removeMouseListener
;
52 template <> constexpr inline auto Add
<css::awt::XMouseMotionListener
> = &css::awt::XWindow::addMouseMotionListener
;
53 template <> constexpr inline auto Remove
<css::awt::XMouseMotionListener
> = &css::awt::XWindow::removeMouseMotionListener
;
55 template <> constexpr inline auto Add
<css::awt::XPaintListener
> = &css::awt::XWindow::addPaintListener
;
56 template <> constexpr inline auto Remove
<css::awt::XPaintListener
> = &css::awt::XWindow::removePaintListener
;
58 template <class Ifc
> class Listeners
61 comphelper::OInterfaceContainerHelper4
<Ifc
> list
;
64 template <class... Ifc
>
65 class ContainersHolder
: public comphelper::WeakImplHelper
<Ifc
...>, public Listeners
<Ifc
>...
68 template <typename F
> void for_each_container(F f
) { (..., f(Listeners
<Ifc
>::list
)); }
71 void notifyPeer(const css::uno::Reference
<css::awt::XWindow
>& peer
,
72 void (SAL_CALL
css::awt::XWindow::*func
)(const css::uno::Reference
<Ifc1
>&))
75 (peer
.get()->*func
)(this);
79 void add(std::unique_lock
<std::mutex
>& guard
, const css::uno::Reference
<Ifc1
>& listener
,
80 const css::uno::Reference
<css::awt::XWindow
>& peer
)
83 if (Listeners
<Ifc1
>::list
.addInterface(guard
, listener
) == 1)
85 // the first listener is added
86 notifyPeer(peer
, Add
<Ifc1
>);
91 void remove(std::unique_lock
<std::mutex
>& guard
, const css::uno::Reference
<Ifc1
>& listener
,
92 const css::uno::Reference
<css::awt::XWindow
>& peer
)
94 if (Listeners
<Ifc1
>::list
.removeInterface(guard
, listener
) == 0)
96 // the last listener is removed
97 notifyPeer(peer
, Remove
<Ifc1
>);
102 class OMRCListenerMultiplexerHelper final
: public ContainersHolder
< css::awt::XFocusListener
103 , css::awt::XWindowListener
104 , css::awt::XKeyListener
105 , css::awt::XMouseListener
106 , css::awt::XMouseMotionListener
107 , css::awt::XPaintListener
>
113 @descr Create a Multiplexer of XWindowEvents.
114 @param rControl The control. All listeners think that this is the original broadcaster.
115 @param rPeer The peer from which the original events are dispatched. Null is allowed.
118 OMRCListenerMultiplexerHelper( const css::uno::Reference
< css::awt::XWindow
>& xControl
,
119 const css::uno::Reference
< css::awt::XWindow
>& xPeer
);
121 virtual ~OMRCListenerMultiplexerHelper() override
;
123 OMRCListenerMultiplexerHelper
& operator= ( const OMRCListenerMultiplexerHelper
& aCopyInstance
);
128 @short Remove all listeners from the previous set peer and add the needed listeners to rPeer.
129 @param rPeer The peer from which the original events are dispatched. Null is allowed.
132 void setPeer( const css::uno::Reference
< css::awt::XWindow
>& xPeer
);
135 @short Remove all listeners and send a disposing message.
138 void disposeAndClear();
141 @short Add the specified listener to the source.
144 template <class Interface
> void advise(const css::uno::Reference
<Interface
>& xListener
)
146 std::unique_lock
aGuard(m_aMutex
);
147 add(aGuard
, xListener
, m_xPeer
);
151 @short Remove the specified listener from the source.
154 template <class Interface
> void unadvise(const css::uno::Reference
<Interface
>& xListener
)
156 std::unique_lock
aGuard(m_aMutex
);
157 remove(aGuard
, xListener
, m_xPeer
);
162 virtual void SAL_CALL
disposing(const css::lang::EventObject
& aSource
) override
;
166 virtual void SAL_CALL
focusGained(const css::awt::FocusEvent
& aEvent
) override
;
168 virtual void SAL_CALL
focusLost(const css::awt::FocusEvent
& aEvent
) override
;
172 virtual void SAL_CALL
windowResized(const css::awt::WindowEvent
& aEvent
) override
;
174 virtual void SAL_CALL
windowMoved(const css::awt::WindowEvent
& aEvent
) override
;
176 virtual void SAL_CALL
windowShown(const css::lang::EventObject
& aEvent
) override
;
178 virtual void SAL_CALL
windowHidden(const css::lang::EventObject
& aEvent
) override
;
182 virtual void SAL_CALL
keyPressed( const css::awt::KeyEvent
& aEvent
) override
;
184 virtual void SAL_CALL
keyReleased( const css::awt::KeyEvent
& aEvent
) override
;
188 virtual void SAL_CALL
mousePressed(const css::awt::MouseEvent
& aEvent
) override
;
190 virtual void SAL_CALL
mouseReleased(const css::awt::MouseEvent
& aEvent
) override
;
192 virtual void SAL_CALL
mouseEntered(const css::awt::MouseEvent
& aEvent
) override
;
194 virtual void SAL_CALL
mouseExited(const css::awt::MouseEvent
& aEvent
) override
;
196 // XMouseMotionListener
198 virtual void SAL_CALL
mouseDragged(const css::awt::MouseEvent
& aEvent
) override
;
200 virtual void SAL_CALL
mouseMoved(const css::awt::MouseEvent
& aEvent
) override
;
204 virtual void SAL_CALL
windowPaint(const css::awt::PaintEvent
& aEvent
) override
;
207 template <class Interface
, typename Event
>
208 void Multiplex(void (SAL_CALL
Interface::*method
)(const Event
&), const Event
& event
);
213 css::uno::Reference
< css::awt::XWindow
> m_xPeer
; /// The source of the events. Normally this is the peer object.
214 css::uno::WeakReference
< css::awt::XWindow
> m_xControl
;
220 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */