Kaydet (Commit) ca36bcc0 authored tarafından Samuel Mehrbrodt's avatar Samuel Mehrbrodt

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
üst 6adfbe62
/* -*- 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
......@@ -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 \
......
......@@ -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()
......
/* -*- 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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment