diff options
author | Samuel Mehrbrodt <Samuel.Mehrbrodt@cib.de> | 2015-11-10 12:48:05 +0100 |
---|---|---|
committer | Samuel Mehrbrodt <Samuel.Mehrbrodt@cib.de> | 2015-11-12 11:45:39 +0100 |
commit | ca36bcc05d7159fd4cd8e3489fdf4b4551b696d6 (patch) | |
tree | a46e6b888c9027b282e24b3bc63161be1cda796f | |
parent | 6adfbe62f6bb67d7d78c1561e9af5169d7f5bb9a (diff) |
Vcl: Disable buttons if the associated UNO slot is disabled
The goal of this is to have buttons in the Sidebar and NotebookBar automatically disabled
without an additional wrapper.
Change-Id: I8d25cdc6b87323e02daf6969c68582354f301375
-rw-r--r-- | include/vcl/buttonstatuslistener.hxx | 45 | ||||
-rw-r--r-- | vcl/Library_vcl.mk | 1 | ||||
-rw-r--r-- | vcl/source/control/button.cxx | 14 | ||||
-rw-r--r-- | vcl/source/control/buttonstatuslistener.cxx | 66 |
4 files changed, 123 insertions, 3 deletions
diff --git a/include/vcl/buttonstatuslistener.hxx b/include/vcl/buttonstatuslistener.hxx new file mode 100644 index 000000000000..772189591114 --- /dev/null +++ b/include/vcl/buttonstatuslistener.hxx @@ -0,0 +1,45 @@ +/* -*- 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_BUTTONSTATUSLISTENER_HXX +#define INCLUDED_VCL_BUTTONSTATUSLISTENER_HXX + +#include <cppuhelper/implbase.hxx> +#include <vcl/button.hxx> + +#include <com/sun/star/util/URL.hpp> +#include <com/sun/star/frame/XStatusListener.hpp> +#include <com/sun/star/frame/XDispatch.hpp> + + +class VCL_DLLPUBLIC ButtonStatusListener : public cppu::WeakImplHelper < css::frame::XStatusListener> +{ +public: + ButtonStatusListener(Button* button, const rtl::OUString& aCommand); + +private: + VclPtr<Button> mButton; /** The button on which actions are performed */ + + /** Dispatcher. Need to keep a reference to it as long as this StatusListener exists. */ + css::uno::Reference<css::frame::XDispatch> mxDispatch; + css::util::URL maCommandURL; + +public: + virtual void SAL_CALL statusChanged(const css::frame::FeatureStateEvent& rEvent) + throw(css::uno::RuntimeException, std::exception) override; + + virtual void SAL_CALL disposing(const css::lang::EventObject& /*Source*/) + throw( css::uno::RuntimeException, std::exception ) override; + + void dispose(); +}; + +#endif // INCLUDED_VCL_BUTTONSTATUSLISTENER_HXX + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
\ No newline at end of file diff --git a/vcl/Library_vcl.mk b/vcl/Library_vcl.mk index 1d33b9b8b1fa..7592860fd2ba 100644 --- a/vcl/Library_vcl.mk +++ b/vcl/Library_vcl.mk @@ -205,6 +205,7 @@ $(eval $(call gb_Library_add_exception_objects,vcl,\ vcl/source/window/winproc \ vcl/source/window/wrkwin \ vcl/source/control/button \ + vcl/source/control/buttonstatuslistener \ vcl/source/control/combobox \ vcl/source/control/ctrl \ vcl/source/control/edit \ diff --git a/vcl/source/control/button.cxx b/vcl/source/control/button.cxx index 0bfd72004960..d3435a57c8f2 100644 --- a/vcl/source/control/button.cxx +++ b/vcl/source/control/button.cxx @@ -32,6 +32,7 @@ #include <vcl/dialog.hxx> #include <vcl/fixed.hxx> #include <vcl/button.hxx> +#include <vcl/buttonstatuslistener.hxx> #include <vcl/salnativewidgets.hxx> #include <vcl/edit.hxx> #include <vcl/layout.hxx> @@ -43,6 +44,7 @@ #include <comphelper/dispatchcommand.hxx> + using namespace css; #define PUSHBUTTON_VIEW_STYLE (WB_3DLOOK | \ @@ -67,6 +69,9 @@ using namespace css; class ImplCommonButtonData { public: + ImplCommonButtonData(); + ~ImplCommonButtonData(); + Rectangle maFocusRect; long mnSeparatorX; DrawButtonFlags mnButtonState; @@ -76,9 +81,8 @@ public: ImageAlign meImageAlign; SymbolAlign meSymbolAlign; -public: - ImplCommonButtonData(); - ~ImplCommonButtonData(); + /** StatusListener. Updates the button as the slot state changes */ + rtl::Reference<ButtonStatusListener> mpStatusListener; }; ImplCommonButtonData::ImplCommonButtonData() : maFocusRect(), mnSeparatorX(0), mnButtonState(DrawButtonFlags::NONE), @@ -104,12 +108,16 @@ Button::~Button() void Button::dispose() { Control::dispose(); + if (mpButtonData->mpStatusListener.is()) + mpButtonData->mpStatusListener->dispose(); } void Button::SetCommandHandler(const OUString& aCommand) { maCommand = aCommand; SetClickHdl( LINK( this, Button, dispatchCommandHandler) ); + + mpButtonData->mpStatusListener = new ButtonStatusListener(this, aCommand); } void Button::Click() diff --git a/vcl/source/control/buttonstatuslistener.cxx b/vcl/source/control/buttonstatuslistener.cxx new file mode 100644 index 000000000000..984842df4d1c --- /dev/null +++ b/vcl/source/control/buttonstatuslistener.cxx @@ -0,0 +1,66 @@ +/* -*- 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/. + */ + +#include <vcl/buttonstatuslistener.hxx> +#include <comphelper/processfactory.hxx> + +#include <com/sun/star/frame/Desktop.hpp> +#include <com/sun/star/frame/XDispatch.hpp> +#include <com/sun/star/frame/XDispatchProvider.hpp> +#include <com/sun/star/frame/XStatusListener.hpp> +#include <com/sun/star/util/URL.hpp> +#include <com/sun/star/util/URLTransformer.hpp> + +ButtonStatusListener::ButtonStatusListener(Button* button, const rtl::OUString& aCommand) { + mButton = button; + + css::uno::Reference<css::uno::XComponentContext> xContext = ::comphelper::getProcessComponentContext(); + css::uno::Reference<css::frame::XDesktop2> xDesktop = css::frame::Desktop::create(xContext); + + css::uno::Reference<css::frame::XFrame> xFrame(xDesktop->getActiveFrame()); + if (!xFrame.is()) + xFrame = css::uno::Reference<css::frame::XFrame>(xDesktop, css::uno::UNO_QUERY); + + css::uno::Reference<css::frame::XDispatchProvider> xDispatchProvider(xFrame, css::uno::UNO_QUERY); + if (!xDispatchProvider.is()) + return; + + maCommandURL.Complete = aCommand; + css::uno::Reference<css::util::XURLTransformer> xParser = css::util::URLTransformer::create(xContext); + xParser->parseStrict(maCommandURL); + + mxDispatch = xDispatchProvider->queryDispatch(maCommandURL, "", 0); + if (!mxDispatch.is()) + return; + + mxDispatch->addStatusListener(this, maCommandURL); +} + +void ButtonStatusListener::statusChanged(const css::frame::FeatureStateEvent& rEvent) + throw(css::uno::RuntimeException, std::exception) +{ + mButton->Enable(rEvent.IsEnabled); +} + +void ButtonStatusListener::disposing(const css::lang::EventObject& /*Source*/) + throw( css::uno::RuntimeException, std::exception ) +{ + mxDispatch.clear(); +} + +void ButtonStatusListener::dispose() +{ + if (mxDispatch.is()) { + mxDispatch->removeStatusListener(this, maCommandURL); + mxDispatch.clear(); + } + mButton.clear(); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
\ No newline at end of file |