include/sfx2/sidebar/SidebarController.hxx |    2 +
 include/sfx2/sidebar/Tools.hxx             |    5 +++
 sc/source/ui/drawfunc/chartsh.cxx          |   45 +++++++++++++++++++++++++++++
 sc/source/ui/inc/chartsh.hxx               |    4 ++
 sc/source/ui/view/tabvwsh4.cxx             |   24 +++++++++++++--
 sfx2/source/sidebar/SidebarController.cxx  |    5 +++
 sfx2/source/sidebar/Tools.cxx              |   28 ++++++++++++++++++
 7 files changed, 110 insertions(+), 3 deletions(-)

New commits:
commit f5097bfa60451f39b761c7d7b415937d560d52fa
Author:     Tamás Zolnai <tamas.zol...@collabora.com>
AuthorDate: Thu Jan 21 12:12:53 2021 +0100
Commit:     Tamás Zolnai <tamas.zol...@collabora.com>
CommitDate: Fri Jan 22 15:45:02 2021 +0100

    tdf#139830: keep the right sidebar context for chart after view switch 
(calc).
    
    There is a general behavior to switch to the default sidebar context
    by view deactivation and switch back to the right context by view
    activation. See SfxShell::Activate() and SfxShell::Deactivate().
    By activation, we use the selection to find out the right sidebar
    context. See GetContextForSelection_SC() method for example. However,
    for charts, this does not work, because the chart window is a separate
    environment and the shell does not know what is selected inside the
    chart window. So let's avoid context switches when we have a chart
    window active. Let the chart controller handle sidebar context changes.
    
    Change-Id: I272ee5c35ac30221eed2930201c4710a9a5877c4
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/109790
    Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoff...@gmail.com>
    Reviewed-by: Tamás Zolnai <tamas.zol...@collabora.com>

diff --git a/include/sfx2/sidebar/SidebarController.hxx 
b/include/sfx2/sidebar/SidebarController.hxx
index 440ff55b7bfc..4de49fcbaea7 100644
--- a/include/sfx2/sidebar/SidebarController.hxx
+++ b/include/sfx2/sidebar/SidebarController.hxx
@@ -172,6 +172,8 @@ public:
 
     void SyncUpdate();
 
+    bool hasChartContextCurrently() const;
+
 private:
     SidebarController(SidebarDockingWindow* pParentWindow, const SfxViewFrame* 
pViewFrame);
 
diff --git a/include/sfx2/sidebar/Tools.hxx b/include/sfx2/sidebar/Tools.hxx
index 97b26c368dd9..25f3706b6471 100644
--- a/include/sfx2/sidebar/Tools.hxx
+++ b/include/sfx2/sidebar/Tools.hxx
@@ -31,9 +31,12 @@ namespace com::sun::star::frame { class XController; }
 namespace com::sun::star::frame { class XDispatch; }
 namespace com::sun::star::frame { class XFrame; }
 
+class SfxViewShell;
 
 namespace sfx2 { namespace sidebar {
 
+class SidebarController;
+
 class SFX2_DLLPUBLIC Tools
 {
 public:
@@ -56,6 +59,8 @@ public:
 
     static OUString GetModuleName (
         const css::uno::Reference<css::frame::XController>& rxFrame);
+
+    static sfx2::sidebar::SidebarController* 
GetSidebarController(SfxViewShell* pViewShell);
 };
 
 
diff --git a/sc/source/ui/drawfunc/chartsh.cxx 
b/sc/source/ui/drawfunc/chartsh.cxx
index b70ce15e57c2..75cfad0a52c7 100644
--- a/sc/source/ui/drawfunc/chartsh.cxx
+++ b/sc/source/ui/drawfunc/chartsh.cxx
@@ -29,6 +29,9 @@
 #include <viewdata.hxx>
 #include <drawview.hxx>
 #include <gridwin.hxx>
+#include <sfx2/sidebar/SidebarController.hxx>
+#include <sfx2/sidebar/Tools.hxx>
+#include <tabvwsh.hxx>
 
 #define ShellClass_ScChartShell
 #include <scslots.hxx>
@@ -37,6 +40,19 @@ using namespace css::uno;
 
 namespace drawing = com::sun::star::drawing;
 
+namespace {
+
+bool inChartContext(ScTabViewShell* pViewShell)
+{
+    sfx2::sidebar::SidebarController* pSidebar = 
sfx2::sidebar::Tools::GetSidebarController(pViewShell);
+    if (pSidebar)
+        return pSidebar->hasChartContextCurrently();
+
+    return false;
+}
+
+} // anonymous namespace
+
 SFX_IMPL_INTERFACE(ScChartShell, ScDrawShell)
 
 void ScChartShell::InitInterface_Impl()
@@ -48,6 +64,35 @@ void ScChartShell::InitInterface_Impl()
     GetStaticInterface()->RegisterPopupMenu("oleobject");
 }
 
+void ScChartShell::Activate(bool bMDI)
+{
+    if(!inChartContext(GetViewData()->GetViewShell()))
+        ScDrawShell::Activate(bMDI);
+    else
+    {
+        // Avoid context changes for chart during activation / deactivation.
+        const bool bIsContextBroadcasterEnabled 
(SfxShell::SetContextBroadcasterEnabled(false));
+
+        SfxShell::Activate(bMDI);
+
+        SfxShell::SetContextBroadcasterEnabled(bIsContextBroadcasterEnabled);
+    }
+}
+
+void ScChartShell::Deactivate(bool bMDI)
+{
+    if(!inChartContext(GetViewData()->GetViewShell()))
+        ScDrawShell::Deactivate(bMDI);
+    else
+    {
+        // Avoid context changes for chart during activation / deactivation.
+        const bool bIsContextBroadcasterEnabled 
(SfxShell::SetContextBroadcasterEnabled(false));
+
+        SfxShell::Deactivate(bMDI);
+
+        SfxShell::SetContextBroadcasterEnabled(bIsContextBroadcasterEnabled);
+    }
+}
 
 ScChartShell::ScChartShell(ScViewData* pData) :
     ScDrawShell(pData)
diff --git a/sc/source/ui/inc/chartsh.hxx b/sc/source/ui/inc/chartsh.hxx
index caa5a89f14f3..d0b007ba87d8 100644
--- a/sc/source/ui/inc/chartsh.hxx
+++ b/sc/source/ui/inc/chartsh.hxx
@@ -36,6 +36,10 @@ private:
     /// SfxInterface initializer.
     static void InitInterface_Impl();
 
+protected:
+    virtual void Activate(bool bMDI) override;
+    virtual void Deactivate(bool bMDI) override;
+
 public:
     ScChartShell(ScViewData* pData);
     virtual ~ScChartShell() override;
diff --git a/sc/source/ui/view/tabvwsh4.cxx b/sc/source/ui/view/tabvwsh4.cxx
index 72e68c5e0514..ee279ceec064 100644
--- a/sc/source/ui/view/tabvwsh4.cxx
+++ b/sc/source/ui/view/tabvwsh4.cxx
@@ -86,9 +86,24 @@
 #include <comphelper/flagguard.hxx>
 #include <LibreOfficeKit/LibreOfficeKitEnums.h>
 #include <comphelper/lok.hxx>
+#include <sfx2/sidebar/SidebarController.hxx>
+#include <sfx2/sidebar/Tools.hxx>
 
 using namespace com::sun::star;
 
+namespace {
+
+bool inChartContext(ScTabViewShell* pViewShell)
+{
+    sfx2::sidebar::SidebarController* pSidebar = 
sfx2::sidebar::Tools::GetSidebarController(pViewShell);
+    if (pSidebar)
+        return pSidebar->hasChartContextCurrently();
+
+    return false;
+}
+
+} // anonymous namespace
+
 void ScTabViewShell::Activate(bool bMDI)
 {
     SfxViewShell::Activate(bMDI);
@@ -204,9 +219,12 @@ void ScTabViewShell::Activate(bool bMDI)
     //  don't call CheckSelectionTransfer here - activating a view should not 
change the
     //  primary selection (may be happening just because the mouse was moved 
over the window)
 
-    ContextChangeEventMultiplexer::NotifyContextChange(
-        GetController(),
-        vcl::EnumContext::Context::Default);
+    if (!inChartContext(this))
+    {
+        ContextChangeEventMultiplexer::NotifyContextChange(
+            GetController(),
+            vcl::EnumContext::Context::Default);
+    }
 }
 
 void ScTabViewShell::Deactivate(bool bMDI)
diff --git a/sfx2/source/sidebar/SidebarController.cxx 
b/sfx2/source/sidebar/SidebarController.cxx
index bb1592627c94..c132b9e506d0 100644
--- a/sfx2/source/sidebar/SidebarController.cxx
+++ b/sfx2/source/sidebar/SidebarController.cxx
@@ -1625,6 +1625,11 @@ void SidebarController::saveDeckState()
     }
 }
 
+bool SidebarController::hasChartContextCurrently() const
+{
+    return GetCurrentContext().msApplication == 
"com.sun.star.chart2.ChartDocument";
+}
+
 } } // end of namespace sfx2::sidebar
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sfx2/source/sidebar/Tools.cxx b/sfx2/source/sidebar/Tools.cxx
index dcd90eb29d3b..da25f6dbfbd7 100644
--- a/sfx2/source/sidebar/Tools.cxx
+++ b/sfx2/source/sidebar/Tools.cxx
@@ -24,10 +24,14 @@
 #include <comphelper/processfactory.hxx>
 #include <vcl/commandinfoprovider.hxx>
 #include <vcl/gradient.hxx>
+#include <sfx2/viewsh.hxx>
 
 #include <com/sun/star/frame/XDispatchProvider.hpp>
 #include <com/sun/star/util/URLTransformer.hpp>
 #include <com/sun/star/frame/ModuleManager.hpp>
+#include <sfx2/sidebar/SidebarController.hxx>
+#include <com/sun/star/ui/XSidebarProvider.hpp>
+#include <com/sun/star/frame/XController2.hpp>
 
 #include <cstring>
 
@@ -135,6 +139,30 @@ OUString Tools::GetModuleName (
     return OUString();
 }
 
+sfx2::sidebar::SidebarController* Tools::GetSidebarController(SfxViewShell* 
pViewShell)
+{
+    if (!pViewShell)
+        return nullptr;
+
+    Reference<css::frame::XController2> 
xController(pViewShell->GetController(), UNO_QUERY);
+    if (!xController.is())
+        return nullptr;
+
+    // Make sure there is a model behind the controller, otherwise 
getSidebar() can crash.
+    if (!xController->getModel().is())
+        return nullptr;
+
+    Reference<css::ui::XSidebarProvider> xSidebarProvider = 
xController->getSidebar();
+    if (!xSidebarProvider.is())
+        return nullptr;
+
+    Reference<css::ui::XSidebar> xSidebar = xSidebarProvider->getSidebar();
+    if (!xSidebar.is())
+        return nullptr;
+
+    return dynamic_cast<sfx2::sidebar::SidebarController*>(xSidebar.get());
+}
+
 } } // end of namespace sfx2::sidebar
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
_______________________________________________
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits

Reply via email to