summaryrefslogtreecommitdiff
path: root/vcl/source
diff options
context:
space:
mode:
authorSamuel Mehrbrodt <Samuel.Mehrbrodt@cib.de>2015-11-10 12:48:05 +0100
committerSamuel Mehrbrodt <Samuel.Mehrbrodt@cib.de>2015-11-12 11:45:39 +0100
commitca36bcc05d7159fd4cd8e3489fdf4b4551b696d6 (patch)
treea46e6b888c9027b282e24b3bc63161be1cda796f /vcl/source
parent6adfbe62f6bb67d7d78c1561e9af5169d7f5bb9a (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
Diffstat (limited to 'vcl/source')
-rw-r--r--vcl/source/control/button.cxx14
-rw-r--r--vcl/source/control/buttonstatuslistener.cxx66
2 files changed, 77 insertions, 3 deletions
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