1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  */
10 
11 #pragma once
12 
13 #include <memory>
14 
15 #include <sfx2/devtools/DevelopmentToolDockingWindow.hxx>
16 
17 #include <com/sun/star/frame/XController.hpp>
18 #include <com/sun/star/view/XSelectionSupplier.hpp>
19 
20 #include <cppuhelper/compbase.hxx>
21 #include <cppuhelper/basemutex.hxx>
22 
23 typedef cppu::WeakComponentImplHelper<css::view::XSelectionChangeListener>
24     SelectionChangeHandlerInterfaceBase;
25 
26 /** Selection change handler to listen to document selection changes.
27  *
28  * Listens to the changes and notifies the docking window with a new
29  * selected object, when a change happens.
30  */
31 class SelectionChangeHandler final : private ::cppu::BaseMutex,
32                                      public SelectionChangeHandlerInterfaceBase
33 {
34 private:
35     css::uno::Reference<css::frame::XController> mxController;
36     VclPtr<DevelopmentToolDockingWindow> mpDockingWindow;
37 
38 public:
SelectionChangeHandler(const css::uno::Reference<css::frame::XController> & rxController,DevelopmentToolDockingWindow * pDockingWindow)39     SelectionChangeHandler(const css::uno::Reference<css::frame::XController>& rxController,
40                            DevelopmentToolDockingWindow* pDockingWindow)
41         : SelectionChangeHandlerInterfaceBase(m_aMutex)
42         , mxController(rxController)
43         , mpDockingWindow(pDockingWindow)
44     {
45         css::uno::Reference<css::view::XSelectionSupplier> xSupplier(mxController,
46                                                                      css::uno::UNO_QUERY);
47         xSupplier->addSelectionChangeListener(this);
48     }
49 
~SelectionChangeHandler()50     ~SelectionChangeHandler() { mpDockingWindow.disposeAndClear(); }
51 
selectionChanged(const css::lang::EventObject &)52     virtual void SAL_CALL selectionChanged(const css::lang::EventObject& /*rEvent*/) override
53     {
54         css::uno::Reference<css::view::XSelectionSupplier> xSupplier(mxController,
55                                                                      css::uno::UNO_QUERY);
56         if (xSupplier.is())
57         {
58             css::uno::Any aAny = xSupplier->getSelection();
59             auto xInterface = aAny.get<css::uno::Reference<css::uno::XInterface>>();
60             mpDockingWindow->selectionChanged(xInterface);
61         }
62     }
63 
stopListening()64     void stopListening()
65     {
66         css::uno::Reference<css::view::XSelectionSupplier> xSupplier(mxController,
67                                                                      css::uno::UNO_QUERY);
68         xSupplier->removeSelectionChangeListener(this);
69     }
70 
disposing(const css::lang::EventObject &)71     virtual void SAL_CALL disposing(const css::lang::EventObject& /*rEvent*/) override {}
disposing()72     virtual void SAL_CALL disposing() override {}
73 
74 private:
75     SelectionChangeHandler(const SelectionChangeHandler&) = delete;
76     SelectionChangeHandler& operator=(const SelectionChangeHandler&) = delete;
77 };
78 
79 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
80