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