/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* * This file is part of the LibreOffice project. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #ifndef INCLUDED_VCL_VCLSTATUSLISTENER_HXX #define INCLUDED_VCL_VCLSTATUSLISTENER_HXX #include #include #include #include #include #include #include #include #include template class VclStatusListener final : public cppu::WeakImplHelper < css::frame::XStatusListener> { public: VclStatusListener(T* widget, const OUString& aCommand); private: VclPtr mWidget; /** The widget on which actions are performed */ /** Dispatcher. Need to keep a reference to it as long as this StatusListener exists. */ css::uno::Reference mxDispatch; css::util::URL maCommandURL; css::uno::Reference mxFrame; public: void SAL_CALL statusChanged(const css::frame::FeatureStateEvent& rEvent) override; void SAL_CALL disposing(const css::lang::EventObject& /*Source*/) override; const css::uno::Reference& getFrame() { return mxFrame; } void startListening(); void dispose(); }; template VclStatusListener::VclStatusListener(T* widget, const OUString& aCommand) { mWidget = widget; css::uno::Reference xContext = ::comphelper::getProcessComponentContext(); css::uno::Reference xDesktop = css::frame::Desktop::create(xContext); css::uno::Reference xFrame(xDesktop->getActiveFrame()); if (!xFrame.is()) xFrame = xDesktop; mxFrame = xFrame; maCommandURL.Complete = aCommand; css::uno::Reference xParser = css::util::URLTransformer::create(xContext); xParser->parseStrict(maCommandURL); } template void VclStatusListener::startListening() { if (mxDispatch.is()) mxDispatch->removeStatusListener(this, maCommandURL); css::uno::Reference xDispatchProvider(mxFrame, css::uno::UNO_QUERY); if (!xDispatchProvider.is()) return; mxDispatch = xDispatchProvider->queryDispatch(maCommandURL, "", 0); if (mxDispatch.is()) mxDispatch->addStatusListener(this, maCommandURL); } template void VclStatusListener::statusChanged(const css::frame::FeatureStateEvent& rEvent) { mWidget->statusChanged(rEvent); } template void VclStatusListener::disposing(const css::lang::EventObject& /*Source*/) { mxDispatch.clear(); } template void VclStatusListener::dispose() { if (mxDispatch.is()) { mxDispatch->removeStatusListener(this, maCommandURL); mxDispatch.clear(); } mxFrame.clear(); mWidget.clear(); } #endif // INCLUDED_VCL_VCLSTATUSLISTENER_HXX /* vim:set shiftwidth=4 softtabstop=4 expandtab: */