[Libreoffice-commits] core.git: 2 commits - include/vcl vcl/Library_vcl.mk vcl/source

Samuel Mehrbrodt Samuel.Mehrbrodt at cib.de
Thu Nov 12 02:47:10 PST 2015


 include/vcl/button.hxx                      |    6 ++
 include/vcl/buttonstatuslistener.hxx        |   45 +++++++++++++++++++
 vcl/Library_vcl.mk                          |    1 
 vcl/source/control/button.cxx               |   26 +++++++++--
 vcl/source/control/buttonstatuslistener.cxx |   66 ++++++++++++++++++++++++++++
 5 files changed, 141 insertions(+), 3 deletions(-)

New commits:
commit b34eab5996c52269360d166815ef15a250627c48
Author: Samuel Mehrbrodt <Samuel.Mehrbrodt at cib.de>
Date:   Wed Nov 11 12:16:30 2015 +0100

    Vcl: Set PushButtons pressed if the uno slot is active
    
    Change-Id: I89708d13da2b3882aa0b7578b5acf48b6408010b

diff --git a/include/vcl/button.hxx b/include/vcl/button.hxx
index cfd32e0..93d5b99 100644
--- a/include/vcl/button.hxx
+++ b/include/vcl/button.hxx
@@ -31,6 +31,8 @@
 #include <vcl/vclptr.hxx>
 #include <vector>
 
+#include <com/sun/star/frame/FeatureStateEvent.hpp>
+
 class UserDrawEvent;
 class ImplCommonButtonData;
 enum class DrawButtonFlags;
@@ -91,6 +93,9 @@ public:
     void                SetSmallSymbol(bool bSmall = true);
     virtual bool        set_property(const OString &rKey, const OString &rValue) override;
 
+    /// Sets the button state according to the FeatureStateEvent emitted by an Uno state change.
+    virtual void        SetStateUno(const css::frame::FeatureStateEvent& rEvent);
+
 protected:
 
     /// Handler for click, in case we want the button to handle uno commands (.uno:Something).
@@ -182,6 +187,7 @@ public:
 
     void            SetState( TriState eState );
     TriState        GetState() const { return meState; }
+    virtual void    SetStateUno(const css::frame::FeatureStateEvent& rEvent) override;
 
     void            Check( bool bCheck = true );
     bool            IsChecked() const;
diff --git a/vcl/source/control/button.cxx b/vcl/source/control/button.cxx
index d3435a5..153c706 100644
--- a/vcl/source/control/button.cxx
+++ b/vcl/source/control/button.cxx
@@ -617,6 +617,11 @@ bool Button::set_property(const OString &rKey, const OString &rValue)
     return true;
 }
 
+void Button::SetStateUno(const css::frame::FeatureStateEvent& rEvent)
+{
+    Enable(rEvent.IsEnabled);
+}
+
 IMPL_STATIC_LINK_TYPED( Button, dispatchCommandHandler, Button*, pButton, void )
 {
     if (pButton == nullptr)
@@ -1601,6 +1606,13 @@ void PushButton::SetState( TriState eState )
     }
 }
 
+void PushButton::SetStateUno(const css::frame::FeatureStateEvent& rEvent)
+{
+    Button::SetStateUno(rEvent);
+    if (rEvent.State.has<bool>())
+        SetPressed(rEvent.State.get<bool>());
+}
+
 void PushButton::SetPressed( bool bPressed )
 {
     if ( mbPressed != bPressed )
diff --git a/vcl/source/control/buttonstatuslistener.cxx b/vcl/source/control/buttonstatuslistener.cxx
index 984842d..bad5452 100644
--- a/vcl/source/control/buttonstatuslistener.cxx
+++ b/vcl/source/control/buttonstatuslistener.cxx
@@ -45,7 +45,7 @@ ButtonStatusListener::ButtonStatusListener(Button* button, const rtl::OUString&
 void ButtonStatusListener::statusChanged(const css::frame::FeatureStateEvent& rEvent)
             throw(css::uno::RuntimeException, std::exception)
 {
-    mButton->Enable(rEvent.IsEnabled);
+    mButton->SetStateUno(rEvent);
 }
 
 void ButtonStatusListener::disposing(const css::lang::EventObject& /*Source*/)
commit ca36bcc05d7159fd4cd8e3489fdf4b4551b696d6
Author: Samuel Mehrbrodt <Samuel.Mehrbrodt at cib.de>
Date:   Tue Nov 10 12:48:05 2015 +0100

    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

diff --git a/include/vcl/buttonstatuslistener.hxx b/include/vcl/buttonstatuslistener.hxx
new file mode 100644
index 0000000..7721895
--- /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 1d33b9b..7592860 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 0bfd720..d3435a5 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 0000000..984842d
--- /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


More information about the Libreoffice-commits mailing list