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

Jan Holesovsky kendy at collabora.com
Wed Sep 24 00:13:35 PDT 2014


 include/vcl/button.hxx        |   13 +++++++++-
 vcl/source/control/button.cxx |   51 ++++++++++++++++++++++++++++++++++++++++++
 vcl/source/window/builder.cxx |    2 +
 3 files changed, 65 insertions(+), 1 deletion(-)

New commits:
commit 0a56da5b110d6a3329ed7ebf296856f839e9980c
Author: Jan Holesovsky <kendy at collabora.com>
Date:   Wed Sep 24 09:06:18 2014 +0200

    vcl button: Allow automatic handling of UNO commands (like .uno:Something).
    
    Change-Id: I71c00286dde2e5a01a7a592305c1790f1ed63a93

diff --git a/include/vcl/button.hxx b/include/vcl/button.hxx
index 7a96232..13fd506 100644
--- a/include/vcl/button.hxx
+++ b/include/vcl/button.hxx
@@ -42,7 +42,10 @@ class VCL_DLLPUBLIC Button : public Control
 {
 private:
     ImplCommonButtonData *mpButtonData;
-    Link                  maClickHdl;
+    Link maClickHdl;
+
+    /// Command URL (like .uno:Save) in case the button should handle it.
+    OUString maCommand;
 
     // Copy assignment is forbidden and not implemented.
     SAL_DLLPRIVATE                  Button (const Button &);
@@ -72,6 +75,9 @@ public:
     void                SetClickHdl( const Link& rLink ) { maClickHdl = rLink; }
     const Link&         GetClickHdl() const { return maClickHdl; }
 
+    /// Setup handler for UNO commands so that commands like .uno:Something are handled automagically by this button.
+    void                SetCommandHandler(const OUString& aCommand);
+
     static OUString     GetStandardText( StandardButtonType eButton );
 
     bool            SetModeImage( const Image& rImage );
@@ -87,6 +93,11 @@ public:
     bool                IsSmallSymbol() const;
     void                SetSmallSymbol(bool bSmall = true);
     virtual bool        set_property(const OString &rKey, const OString &rValue) SAL_OVERRIDE;
+
+protected:
+
+    /// Handler for click, in case we want the button to handle uno commands (.uno:Something).
+    static long         dispatchCommandHandler(void *, void *pCaller);
 };
 
 
diff --git a/vcl/source/control/button.cxx b/vcl/source/control/button.cxx
index cef9d0d..1ee4014 100644
--- a/vcl/source/control/button.cxx
+++ b/vcl/source/control/button.cxx
@@ -41,6 +41,16 @@
 #include <window.h>
 #include <controldata.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/util/URL.hpp>
+#include <com/sun/star/util/URLTransformer.hpp>
+
+using namespace css;
+
 #define PUSHBUTTON_VIEW_STYLE       (WB_3DLOOK |                        \
                                      WB_LEFT | WB_CENTER | WB_RIGHT |   \
                                      WB_TOP | WB_VCENTER | WB_BOTTOM |  \
@@ -94,6 +104,12 @@ Button::~Button()
     delete mpButtonData;
 }
 
+void Button::SetCommandHandler(const OUString& aCommand)
+{
+    maCommand = aCommand;
+    SetClickHdl(Link(NULL, dispatchCommandHandler));
+}
+
 void Button::Click()
 {
     ImplCallEventListenersAndHandler( VCLEVENT_BUTTON_CLICK, maClickHdl, this );
@@ -583,6 +599,41 @@ bool Button::set_property(const OString &rKey, const OString &rValue)
     return true;
 }
 
+long Button::dispatchCommandHandler(void *, void *pCaller)
+{
+    const Button *pButton = reinterpret_cast<Button*>(pCaller);
+    if (pButton == NULL)
+        return 0;
+
+    // Target where we will execute the .uno: command
+    uno::Reference<uno::XComponentContext> xContext = ::comphelper::getProcessComponentContext();
+    uno::Reference<frame::XDesktop2> xDesktop = frame::Desktop::create(xContext);
+
+    uno::Reference<frame::XFrame> xFrame(xDesktop->getActiveFrame());
+    if (!xFrame.is())
+        xFrame = uno::Reference<frame::XFrame>(xDesktop, uno::UNO_QUERY);
+
+    uno::Reference<frame::XDispatchProvider> xDispatchProvider(xFrame, uno::UNO_QUERY);
+    if (!xDispatchProvider.is())
+        return 0;
+
+    util::URL aCommandURL;
+    aCommandURL.Complete = pButton->maCommand;
+    uno::Reference<util::XURLTransformer> xParser = util::URLTransformer::create(xContext);
+    xParser->parseStrict(aCommandURL);
+
+    uno::Reference<frame::XDispatch> xDisp = xDispatchProvider->queryDispatch(aCommandURL, OUString(), 0);
+    if (!xDisp.is())
+        return 0;
+
+    // And do the work...
+    xDisp->dispatch(aCommandURL, uno::Sequence<beans::PropertyValue>());
+
+    return 1;
+}
+
+
+
 void PushButton::ImplInitPushButtonData()
 {
     mpWindowImpl->mbPushButton    = true;
diff --git a/vcl/source/window/builder.cxx b/vcl/source/window/builder.cxx
index e0c71e9..a66ad77 100644
--- a/vcl/source/window/builder.cxx
+++ b/vcl/source/window/builder.cxx
@@ -833,6 +833,8 @@ namespace
 
         Image aImage(VclBuilder::getCommandImage(aCommand, /* bLarge = */ false, xContext, rFrame, aModuleId));
         pButton->SetModeImage(aImage);
+
+        pButton->SetCommandHandler(aCommand);
     }
 
     Button* extractStockAndBuildPushButton(vcl::Window *pParent, VclBuilder::stringmap &rMap)


More information about the Libreoffice-commits mailing list