LibreOffice Module vcl (master) 1
vclstatuslistener.hxx
Go to the documentation of this file.
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#pragma once
11
14#include <vcl/vclptr.hxx>
15
16#include <com/sun/star/frame/Desktop.hpp>
17#include <com/sun/star/frame/XStatusListener.hpp>
18#include <com/sun/star/frame/XDispatch.hpp>
19#include <com/sun/star/frame/XDispatchProvider.hpp>
20#include <com/sun/star/util/URL.hpp>
21#include <com/sun/star/util/URLTransformer.hpp>
22
23template <class T> class VclStatusListener final : public cppu::WeakImplHelper < css::frame::XStatusListener>
24{
25public:
26 VclStatusListener(T* widget, const css::uno::Reference<css::frame::XFrame>& rFrame, const OUString& aCommand);
27
28private:
32 css::uno::Reference<css::frame::XDispatch> mxDispatch;
33 css::util::URL maCommandURL;
34 css::uno::Reference<css::frame::XFrame> mxFrame;
35
36public:
37 void SAL_CALL statusChanged(const css::frame::FeatureStateEvent& rEvent) override;
38
39 void SAL_CALL disposing(const css::lang::EventObject& /*Source*/) override;
40
41 void startListening();
42
43 void dispose();
44};
45
46template<class T>
47VclStatusListener<T>::VclStatusListener(T* widget, const css::uno::Reference<css::frame::XFrame>& rFrame, const OUString& aCommand) :
48 mWidget(widget),
49 mxFrame(rFrame)
50{
51 css::uno::Reference<css::uno::XComponentContext> xContext = ::comphelper::getProcessComponentContext();
52 maCommandURL.Complete = aCommand;
53 css::uno::Reference<css::util::XURLTransformer> xParser = css::util::URLTransformer::create(xContext);
54 xParser->parseStrict(maCommandURL);
55}
56
57template<class T>
59{
60 css::uno::Reference<css::frame::XDispatchProvider> xDispatchProvider(mxFrame, css::uno::UNO_QUERY);
61 if (!xDispatchProvider.is())
62 return;
63
64 mxDispatch = xDispatchProvider->queryDispatch(maCommandURL, "", 0);
65 if (mxDispatch.is())
66 mxDispatch->addStatusListener(this, maCommandURL);
67}
68
69template<class T>
70void VclStatusListener<T>::statusChanged(const css::frame::FeatureStateEvent& rEvent)
71{
72 mWidget->statusChanged(rEvent);
73}
74
75template<class T>
76void VclStatusListener<T>::disposing(const css::lang::EventObject& /*Source*/)
77{
78 mxDispatch.clear();
79}
80
81template<class T>
83{
84 if (mxDispatch.is()) {
85 mxDispatch->removeStatusListener(this, maCommandURL);
86 mxDispatch.clear();
87 }
88 mxFrame.clear();
89 mWidget.clear();
90}
91
92/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void SAL_CALL statusChanged(const css::frame::FeatureStateEvent &rEvent) override
css::uno::Reference< css::frame::XDispatch > mxDispatch
The widget on which actions are performed.
void SAL_CALL disposing(const css::lang::EventObject &) override
VclStatusListener(T *widget, const css::uno::Reference< css::frame::XFrame > &rFrame, const OUString &aCommand)
css::util::URL maCommandURL
css::uno::Reference< css::frame::XFrame > mxFrame
Reference< XDispatch > mxDispatch
OUString aCommand