chart2/source/controller/main/ChartController.cxx       |    4 -
 desktop/source/lib/init.cxx                             |    8 +++
 include/sfx2/lokcomponenthelpers.hxx                    |    4 +
 include/sfx2/sidebar/SidebarController.hxx              |    7 +--
 sc/source/ui/drawfunc/chartsh.cxx                       |   12 ++---
 sc/source/ui/view/tabvwsh4.cxx                          |    6 +-
 sd/source/ui/slidesorter/shell/SlideSorterViewShell.cxx |    8 +--
 sfx2/source/sidebar/SidebarController.cxx               |   36 ++++++++++------
 sfx2/source/view/lokstarmathhelper.cxx                  |    9 ++++
 starmath/source/view.cxx                                |    7 ++-
 sw/source/uibase/shells/olesh.cxx                       |   12 ++---
 11 files changed, 74 insertions(+), 39 deletions(-)

New commits:
commit 7ba40c7d62c7c15f9c0e9cbc21b48f105454b885
Author:     Mike Kaganski <[email protected]>
AuthorDate: Tue Jul 19 17:20:17 2022 +0300
Commit:     Mike Kaganski <[email protected]>
CommitDate: Thu Jul 28 07:37:43 2022 +0200

    lok: Properly update sidebar context in Math
    
    Change-Id: I830c0d9bdeaa6340283eafc3e91251d02602d7aa
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/137247
    Tested-by: Jenkins
    Reviewed-by: Mike Kaganski <[email protected]>

diff --git a/chart2/source/controller/main/ChartController.cxx 
b/chart2/source/controller/main/ChartController.cxx
index a4bd7a3972cc..54c91a6147dd 100644
--- a/chart2/source/controller/main/ChartController.cxx
+++ b/chart2/source/controller/main/ChartController.cxx
@@ -379,7 +379,7 @@ void SAL_CALL ChartController::attachFrame(
     {
         auto pSidebar = 
dynamic_cast<sfx2::sidebar::SidebarController*>(xSidebar.get());
         assert(pSidebar);
-        sfx2::sidebar::SidebarController::registerSidebarForFrame(pSidebar, 
this);
+        pSidebar->registerSidebarForFrame(this);
         pSidebar->updateModel(getChartModel());
         css::lang::EventObject aEvent;
         mpSelectionChangeHandler->selectionChanged(aEvent);
@@ -745,7 +745,7 @@ void SAL_CALL ChartController::dispose()
         uno::Reference<ui::XSidebar> xSidebar = 
getSidebarFromModel(getChartModel());
         if (sfx2::sidebar::SidebarController* pSidebar = 
dynamic_cast<sfx2::sidebar::SidebarController*>(xSidebar.get()))
         {
-            
sfx2::sidebar::SidebarController::unregisterSidebarForFrame(pSidebar, this);
+            pSidebar->unregisterSidebarForFrame(this);
         }
     }
 
diff --git a/include/sfx2/sidebar/SidebarController.hxx 
b/include/sfx2/sidebar/SidebarController.hxx
index e996c853c5b0..208af98b2fc2 100644
--- a/include/sfx2/sidebar/SidebarController.hxx
+++ b/include/sfx2/sidebar/SidebarController.hxx
@@ -78,9 +78,9 @@ public:
     static SidebarController* GetSidebarControllerForFrame (
         const css::uno::Reference<css::frame::XFrame>& rxFrame);
 
-    static void registerSidebarForFrame(SidebarController* pController, const 
css::uno::Reference<css::frame::XController>& xFrame);
+    void registerSidebarForFrame(const 
css::uno::Reference<css::frame::XController>& xFrame);
 
-    static void unregisterSidebarForFrame(SidebarController* pController, 
const css::uno::Reference<css::frame::XController>& xFrame);
+    void unregisterSidebarForFrame(const 
css::uno::Reference<css::frame::XController>& xFrame);
 
     // ui::XContextChangeEventListener
     virtual void SAL_CALL notifyContextChangeEvent (const 
css::ui::ContextChangeEventObject& rEvent) override;
@@ -170,6 +170,7 @@ public:
 
     void SyncUpdate();
 
+    // Used to avoid wrong context update when an embedded object activation 
is in progress
     bool hasChartOrMathContextCurrently() const;
 
     static SidebarController* GetSidebarControllerForView(const SfxViewShell* 
pViewShell);
diff --git a/sfx2/source/sidebar/SidebarController.cxx 
b/sfx2/source/sidebar/SidebarController.cxx
index 6909f5c19d62..4384482e6740 100644
--- a/sfx2/source/sidebar/SidebarController.cxx
+++ b/sfx2/source/sidebar/SidebarController.cxx
@@ -120,6 +120,7 @@ SidebarController::SidebarController (
                      const ::std::vector<TabBar::DeckMenuData>& rMenuData) { 
return this->ShowPopupMenu(rMainMenu, rSubMenu, rMenuData); },
               this)),
       maCurrentContext(OUString(), OUString()),
+      maRequestedContext(OUString(), OUString()),
       mnRequestedForceFlags(SwitchFlag_NoForce),
       
mbMinimumSidebarWidth(officecfg::Office::UI::Sidebar::General::MinimumWidth::get()),
       msCurrentDeckId(gsDefaultDeckId),
@@ -143,7 +144,7 @@ rtl::Reference<SidebarController> 
SidebarController::create(SidebarDockingWindow
     rtl::Reference<SidebarController> instance(new 
SidebarController(pParentWindow, pViewFrame));
 
     const css::uno::Reference<css::frame::XFrame>& rxFrame = 
pViewFrame->GetFrame().GetFrameInterface();
-    registerSidebarForFrame(instance.get(), rxFrame->getController());
+    instance->registerSidebarForFrame(rxFrame->getController());
     rxFrame->addFrameActionListener(instance);
     // Listen for window events.
     instance->mpParentWindow->AddEventListener(LINK(instance.get(), 
SidebarController, WindowEventHandler));
@@ -190,27 +191,27 @@ SidebarController* 
SidebarController::GetSidebarControllerForFrame (
     return dynamic_cast<SidebarController*>(xListener.get());
 }
 
-void SidebarController::registerSidebarForFrame(SidebarController* 
pController, const css::uno::Reference<css::frame::XController>& xController)
+void SidebarController::registerSidebarForFrame(const 
css::uno::Reference<css::frame::XController>& xController)
 {
     // Listen for context change events.
     css::uno::Reference<css::ui::XContextChangeEventMultiplexer> xMultiplexer (
         css::ui::ContextChangeEventMultiplexer::get(
             ::comphelper::getProcessComponentContext()));
     xMultiplexer->addContextChangeEventListener(
-        static_cast<css::ui::XContextChangeEventListener*>(pController),
+        static_cast<css::ui::XContextChangeEventListener*>(this),
         xController);
 }
 
-void SidebarController::unregisterSidebarForFrame(SidebarController* 
pController, const css::uno::Reference<css::frame::XController>& xController)
+void SidebarController::unregisterSidebarForFrame(const 
css::uno::Reference<css::frame::XController>& xController)
 {
-    pController->saveDeckState();
-    pController->disposeDecks();
+    saveDeckState();
+    disposeDecks();
 
     css::uno::Reference<css::ui::XContextChangeEventMultiplexer> xMultiplexer (
         css::ui::ContextChangeEventMultiplexer::get(
             ::comphelper::getProcessComponentContext()));
     xMultiplexer->removeContextChangeEventListener(
-        static_cast<css::ui::XContextChangeEventListener*>(pController),
+        static_cast<css::ui::XContextChangeEventListener*>(this),
         xController);
 }
 
@@ -328,7 +329,7 @@ void 
SidebarController::disposing(std::unique_lock<std::mutex>&)
     if (!xController.is())
         xController = mxCurrentController;
 
-    unregisterSidebarForFrame(this, xController);
+    unregisterSidebarForFrame(xController);
 }
 
 void SAL_CALL SidebarController::notifyContextChangeEvent (const 
css::ui::ContextChangeEventObject& rEvent)
@@ -548,6 +549,8 @@ void SidebarController::UpdateConfigurations()
             (maCurrentContext.msApplication != 
maRequestedContext.msApplication))
     {
         OUString sLastActiveDeck = mpResourceManager->GetLastActiveDeck( 
maRequestedContext );
+        if (comphelper::LibreOfficeKit::isActive() && sLastActiveDeck == 
"PropertyDeck" && maRequestedContext.msApplication == 
"com.sun.star.formula.FormulaProperties")
+            sLastActiveDeck = "ElementsDeck"; // Manual override for lok
         if (!sLastActiveDeck.isEmpty())
             msCurrentDeckId = sLastActiveDeck;
     }
@@ -1591,9 +1594,9 @@ void SidebarController::frameAction(const 
css::frame::FrameActionEvent& rEvent)
     if (rEvent.Frame == mxFrame)
     {
         if (rEvent.Action == css::frame::FrameAction_COMPONENT_DETACHING)
-            unregisterSidebarForFrame(this, mxFrame->getController());
+            unregisterSidebarForFrame(mxFrame->getController());
         else if (rEvent.Action == css::frame::FrameAction_COMPONENT_REATTACHED)
-            registerSidebarForFrame(this, mxFrame->getController());
+            registerSidebarForFrame(mxFrame->getController());
     }
 }
 
@@ -1609,10 +1612,18 @@ void SidebarController::saveDeckState()
     }
 }
 
+static bool isChartOrMathContext(const Context& context)
+{
+    return context.msApplication == "com.sun.star.chart2.ChartDocument"
+           || context.msApplication == 
"com.sun.star.formula.FormulaProperties";
+}
+
 bool SidebarController::hasChartOrMathContextCurrently() const
 {
-    return GetCurrentContext().msApplication == 
"com.sun.star.chart2.ChartDocument"
-           || GetCurrentContext().msApplication == 
"com.sun.star.formula.FormulaProperties";
+    if ((maRequestedContext != maCurrentContext) && 
isChartOrMathContext(maRequestedContext))
+        return true; // We are not yet changed, but in the process
+
+    return isChartOrMathContext(maCurrentContext);
 }
 
 sfx2::sidebar::SidebarController* 
SidebarController::GetSidebarControllerForView(const SfxViewShell* pViewShell)
diff --git a/starmath/source/view.cxx b/starmath/source/view.cxx
index 56896a030572..71dd3bc53946 100644
--- a/starmath/source/view.cxx
+++ b/starmath/source/view.cxx
@@ -2099,29 +2099,6 @@ void SmViewShell::GetState(SfxItemSet &rSet)
 
 namespace
 {
-css::uno::Reference<css::ui::XSidebar>
-getSidebarFromModel(const css::uno::Reference<css::frame::XModel>& xModel)
-{
-    css::uno::Reference<css::container::XChild> xChild(xModel, 
css::uno::UNO_QUERY);
-    if (!xChild.is())
-        return nullptr;
-
-    css::uno::Reference<css::frame::XModel> xParent(xChild->getParent(), 
css::uno::UNO_QUERY);
-    if (!xParent.is())
-        return nullptr;
-
-    css::uno::Reference<css::frame::XController2> 
xController(xParent->getCurrentController(),
-                                                              
css::uno::UNO_QUERY);
-    if (!xController.is())
-        return nullptr;
-
-    css::uno::Reference<css::ui::XSidebarProvider> xSidebarProvider = 
xController->getSidebar();
-    if (!xSidebarProvider.is())
-        return nullptr;
-
-    return xSidebarProvider->getSidebar();
-}
-
 class SmController : public SfxBaseController
 {
 public:
@@ -2141,14 +2118,7 @@ public:
         SfxBaseController::attachFrame(xFrame);
 
         // No need to call mpSelectionChangeHandler->Connect() unless 
SmController implements XSelectionSupplier
-        if (auto xSidebar = getSidebarFromModel(getModel()))
-        {
-            auto pSidebar = 
dynamic_cast<sfx2::sidebar::SidebarController*>(xSidebar.get());
-            assert(pSidebar);
-            
sfx2::sidebar::SidebarController::registerSidebarForFrame(pSidebar, this);
-            pSidebar->updateModel(getModel());
-            mpSelectionChangeHandler->selectionChanged({}); // Installs the 
correct context
-        }
+        mpSelectionChangeHandler->selectionChanged({}); // Installs the 
correct context
     }
 
 private:
commit 30e08c83e502562966351f4236e9dd9230ffa435
Author:     Mike Kaganski <[email protected]>
AuthorDate: Thu Jun 30 06:09:23 2022 +0300
Commit:     Mike Kaganski <[email protected]>
CommitDate: Thu Jul 28 07:37:31 2022 +0200

    lok: Enable sidebar in Math
    
    Change-Id: I1554a924eef0bfcaffa893057a37ef45321a0cf7
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/137246
    Tested-by: Jenkins
    Reviewed-by: Mike Kaganski <[email protected]>

diff --git a/desktop/source/lib/init.cxx b/desktop/source/lib/init.cxx
index 029e8b72c667..c0f129fef72f 100644
--- a/desktop/source/lib/init.cxx
+++ b/desktop/source/lib/init.cxx
@@ -4345,7 +4345,13 @@ static void doc_postUnoCommand(LibreOfficeKitDocument* 
pThis, const char* pComma
         aChartDispatcher->dispatch(aCommandURL, 
comphelper::containerToSequence(aPropertyValuesVector));
         return;
     }
-    else if (bNotifyWhenFinished && 
pDocument->mpCallbackFlushHandlers.count(nView))
+    if (LokStarMathHelper aMathHelper(SfxViewShell::Current());
+        aMathHelper.GetGraphicWindow() && aCommand != ".uno:Save")
+    {
+        aMathHelper.Dispatch(aCommand, 
comphelper::containerToSequence(aPropertyValuesVector));
+        return;
+    }
+    if (bNotifyWhenFinished && pDocument->mpCallbackFlushHandlers.count(nView))
     {
         bResult = comphelper::dispatchCommand(aCommand, 
comphelper::containerToSequence(aPropertyValuesVector),
                 new DispatchResultListener(pCommand, 
pDocument->mpCallbackFlushHandlers[nView]));
diff --git a/include/sfx2/lokcomponenthelpers.hxx 
b/include/sfx2/lokcomponenthelpers.hxx
index 6a3d964689b4..02e8750ca381 100644
--- a/include/sfx2/lokcomponenthelpers.hxx
+++ b/include/sfx2/lokcomponenthelpers.hxx
@@ -15,8 +15,10 @@
 #include <vcl/vclptr.hxx>
 #include <vcl/window.hxx>
 
+namespace com::sun::star::beans { struct PropertyValue; }
 namespace com::sun::star::frame { class XController; }
 namespace com::sun::star::frame { class XDispatch; }
+namespace com::sun::star::uno { template<class E> class Sequence; }
 
 class SfxViewShell;
 class VirtualDevice;
@@ -72,6 +74,8 @@ public:
     vcl::Window* GetGraphicWindow();
     vcl::Window* GetWidgetWindow();
 
+    void Dispatch(const OUString& cmd, const 
css::uno::Sequence<css::beans::PropertyValue>& rArguments);
+
     bool postMouseEvent(int nType, int nX, int nY, int nCount, int nButtons, 
int nModifier,
                         double fScaleX = 1.0, double fScaleY = 1.0);
 
diff --git a/include/sfx2/sidebar/SidebarController.hxx 
b/include/sfx2/sidebar/SidebarController.hxx
index 8d8dcf215527..e996c853c5b0 100644
--- a/include/sfx2/sidebar/SidebarController.hxx
+++ b/include/sfx2/sidebar/SidebarController.hxx
@@ -170,7 +170,7 @@ public:
 
     void SyncUpdate();
 
-    bool hasChartContextCurrently() const;
+    bool hasChartOrMathContextCurrently() const;
 
     static SidebarController* GetSidebarControllerForView(const SfxViewShell* 
pViewShell);
 
diff --git a/sc/source/ui/drawfunc/chartsh.cxx 
b/sc/source/ui/drawfunc/chartsh.cxx
index ac54c435eeea..e6378a5fa9cb 100644
--- a/sc/source/ui/drawfunc/chartsh.cxx
+++ b/sc/source/ui/drawfunc/chartsh.cxx
@@ -42,11 +42,11 @@ namespace drawing = com::sun::star::drawing;
 
 namespace {
 
-bool inChartContext(const ScTabViewShell* pViewShell)
+bool inChartOrMathContext(const ScTabViewShell* pViewShell)
 {
     SidebarController* pSidebar = 
SidebarController::GetSidebarControllerForView(pViewShell);
     if (pSidebar)
-        return pSidebar->hasChartContextCurrently();
+        return pSidebar->hasChartOrMathContextCurrently();
 
     return false;
 }
@@ -66,11 +66,11 @@ void ScChartShell::InitInterface_Impl()
 
 void ScChartShell::Activate(bool bMDI)
 {
-    if(!inChartContext(GetViewData().GetViewShell()))
+    if(!inChartOrMathContext(GetViewData().GetViewShell()))
         ScDrawShell::Activate(bMDI);
     else
     {
-        // Avoid context changes for chart during activation / deactivation.
+        // Avoid context changes for chart/math during activation / 
deactivation.
         const bool bIsContextBroadcasterEnabled 
(SfxShell::SetContextBroadcasterEnabled(false));
 
         SfxShell::Activate(bMDI);
@@ -81,11 +81,11 @@ void ScChartShell::Activate(bool bMDI)
 
 void ScChartShell::Deactivate(bool bMDI)
 {
-    if(!inChartContext(GetViewData().GetViewShell()))
+    if(!inChartOrMathContext(GetViewData().GetViewShell()))
         ScDrawShell::Deactivate(bMDI);
     else
     {
-        // Avoid context changes for chart during activation / deactivation.
+        // Avoid context changes for chart/math during activation / 
deactivation.
         const bool bIsContextBroadcasterEnabled 
(SfxShell::SetContextBroadcasterEnabled(false));
 
         SfxShell::Deactivate(bMDI);
diff --git a/sc/source/ui/view/tabvwsh4.cxx b/sc/source/ui/view/tabvwsh4.cxx
index 94db6ca12a13..9bd541a04d03 100644
--- a/sc/source/ui/view/tabvwsh4.cxx
+++ b/sc/source/ui/view/tabvwsh4.cxx
@@ -91,11 +91,11 @@ using namespace sfx2::sidebar;
 
 namespace {
 
-bool inChartContext(const ScTabViewShell* pViewShell)
+bool inChartOrMathContext(const ScTabViewShell* pViewShell)
 {
     SidebarController* pSidebar = 
SidebarController::GetSidebarControllerForView(pViewShell);
     if (pSidebar)
-        return pSidebar->hasChartContextCurrently();
+        return pSidebar->hasChartOrMathContextCurrently();
 
     return false;
 }
@@ -218,7 +218,7 @@ 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)
 
-    if (!inChartContext(this))
+    if (!inChartOrMathContext(this))
     {
         ContextChangeEventMultiplexer::NotifyContextChange(
             GetController(),
diff --git a/sd/source/ui/slidesorter/shell/SlideSorterViewShell.cxx 
b/sd/source/ui/slidesorter/shell/SlideSorterViewShell.cxx
index af5bd5791ba4..4de3e6a1c095 100644
--- a/sd/source/ui/slidesorter/shell/SlideSorterViewShell.cxx
+++ b/sd/source/ui/slidesorter/shell/SlideSorterViewShell.cxx
@@ -74,7 +74,7 @@ namespace sd::slidesorter {
 
 namespace {
 
-bool inChartContext(const sd::View* pView)
+bool inChartOrMathContext(const sd::View* pView)
 {
     if (!pView)
         return false;
@@ -82,7 +82,7 @@ bool inChartContext(const sd::View* pView)
     SfxViewShell* pViewShell = pView->GetSfxViewShell();
     SidebarController* pSidebar = 
SidebarController::GetSidebarControllerForView(pViewShell);
     if (pSidebar)
-        return pSidebar->hasChartContextCurrently();
+        return pSidebar->hasChartOrMathContextCurrently();
 
     return false;
 }
@@ -459,9 +459,9 @@ void SlideSorterViewShell::ArrangeGUIElements()
 
 void SlideSorterViewShell::Activate (bool bIsMDIActivate)
 {
-    if(inChartContext(GetView()))
+    if(inChartOrMathContext(GetView()))
     {
-        // Avoid context changes for chart during activation / deactivation.
+        // Avoid context changes for chart/math during activation / 
deactivation.
         const bool bIsContextBroadcasterEnabled 
(SfxShell::SetContextBroadcasterEnabled(false));
 
         ViewShell::Activate(bIsMDIActivate);
diff --git a/sfx2/source/sidebar/SidebarController.cxx 
b/sfx2/source/sidebar/SidebarController.cxx
index 0bd71db0240d..6909f5c19d62 100644
--- a/sfx2/source/sidebar/SidebarController.cxx
+++ b/sfx2/source/sidebar/SidebarController.cxx
@@ -1609,9 +1609,10 @@ void SidebarController::saveDeckState()
     }
 }
 
-bool SidebarController::hasChartContextCurrently() const
+bool SidebarController::hasChartOrMathContextCurrently() const
 {
-    return GetCurrentContext().msApplication == 
"com.sun.star.chart2.ChartDocument";
+    return GetCurrentContext().msApplication == 
"com.sun.star.chart2.ChartDocument"
+           || GetCurrentContext().msApplication == 
"com.sun.star.formula.FormulaProperties";
 }
 
 sfx2::sidebar::SidebarController* 
SidebarController::GetSidebarControllerForView(const SfxViewShell* pViewShell)
diff --git a/sfx2/source/view/lokstarmathhelper.cxx 
b/sfx2/source/view/lokstarmathhelper.cxx
index 9df48759528d..9a704683aaec 100644
--- a/sfx2/source/view/lokstarmathhelper.cxx
+++ b/sfx2/source/view/lokstarmathhelper.cxx
@@ -13,6 +13,7 @@
 #include <sfx2/lokcomponenthelpers.hxx>
 #include <sfx2/lokhelper.hxx>
 
+#include <comphelper/dispatchcommand.hxx>
 #include <toolkit/helper/vclunohelper.hxx>
 #include <tools/fract.hxx>
 #include <vcl/layout.hxx>
@@ -43,6 +44,14 @@ css::uno::Reference<css::frame::XController>& 
LokStarMathHelper::GetXController(
     return mxController;
 }
 
+void LokStarMathHelper::Dispatch(const OUString& cmd,
+                                 const 
css::uno::Sequence<css::beans::PropertyValue>& rArguments)
+{
+    if (const css::uno::Reference<css::frame::XController>& xController = 
GetXController())
+        if (const css::uno::Reference<css::frame::XFrame> xFrame = 
xController->getFrame())
+            comphelper::dispatchCommand(cmd, xFrame, rArguments);
+}
+
 namespace
 {
 // Find a child SmGraphicWindow*
diff --git a/starmath/source/view.cxx b/starmath/source/view.cxx
index e2b60cb9c6bd..56896a030572 100644
--- a/starmath/source/view.cxx
+++ b/starmath/source/view.cxx
@@ -23,6 +23,7 @@
 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
 #include <com/sun/star/frame/Desktop.hpp>
 #include <com/sun/star/frame/XFramesSupplier.hpp>
+#include <com/sun/star/frame/XModel.hpp>
 #include <com/sun/star/container/XChild.hpp>
 
 #include <comphelper/lok.hxx>
@@ -44,6 +45,7 @@
 #include <sfx2/request.hxx>
 #include <sfx2/sfxbasecontroller.hxx>
 #include <sfx2/sidebar/SidebarChildWindow.hxx>
+#include <sfx2/sidebar/SidebarController.hxx>
 #include <sfx2/viewfac.hxx>
 #include <svl/eitem.hxx>
 #include <svl/itemset.hxx>
@@ -2097,6 +2099,29 @@ void SmViewShell::GetState(SfxItemSet &rSet)
 
 namespace
 {
+css::uno::Reference<css::ui::XSidebar>
+getSidebarFromModel(const css::uno::Reference<css::frame::XModel>& xModel)
+{
+    css::uno::Reference<css::container::XChild> xChild(xModel, 
css::uno::UNO_QUERY);
+    if (!xChild.is())
+        return nullptr;
+
+    css::uno::Reference<css::frame::XModel> xParent(xChild->getParent(), 
css::uno::UNO_QUERY);
+    if (!xParent.is())
+        return nullptr;
+
+    css::uno::Reference<css::frame::XController2> 
xController(xParent->getCurrentController(),
+                                                              
css::uno::UNO_QUERY);
+    if (!xController.is())
+        return nullptr;
+
+    css::uno::Reference<css::ui::XSidebarProvider> xSidebarProvider = 
xController->getSidebar();
+    if (!xSidebarProvider.is())
+        return nullptr;
+
+    return xSidebarProvider->getSidebar();
+}
+
 class SmController : public SfxBaseController
 {
 public:
@@ -2105,17 +2130,25 @@ public:
         , mpSelectionChangeHandler(new svx::sidebar::SelectionChangeHandler(
               GetContextName, this, vcl::EnumContext::Context::Math))
     {
-        mpSelectionChangeHandler->Connect();
         rViewShell.SetContextName(GetContextName());
     }
-    ~SmController() { mpSelectionChangeHandler->Disconnect(); }
+    // No need to call mpSelectionChangeHandler->Disconnect() unless 
SmController implements XSelectionSupplier
+    // ~SmController() { mpSelectionChangeHandler->Disconnect(); }
 
     // css::frame::XController
     void SAL_CALL attachFrame(const css::uno::Reference<css::frame::XFrame>& 
xFrame) override
     {
         SfxBaseController::attachFrame(xFrame);
 
-        mpSelectionChangeHandler->selectionChanged({}); // Installs the 
correct context
+        // No need to call mpSelectionChangeHandler->Connect() unless 
SmController implements XSelectionSupplier
+        if (auto xSidebar = getSidebarFromModel(getModel()))
+        {
+            auto pSidebar = 
dynamic_cast<sfx2::sidebar::SidebarController*>(xSidebar.get());
+            assert(pSidebar);
+            
sfx2::sidebar::SidebarController::registerSidebarForFrame(pSidebar, this);
+            pSidebar->updateModel(getModel());
+            mpSelectionChangeHandler->selectionChanged({}); // Installs the 
correct context
+        }
     }
 
 private:
diff --git a/sw/source/uibase/shells/olesh.cxx 
b/sw/source/uibase/shells/olesh.cxx
index acddd3eb6975..28e8e153eeb1 100644
--- a/sw/source/uibase/shells/olesh.cxx
+++ b/sw/source/uibase/shells/olesh.cxx
@@ -34,11 +34,11 @@ using namespace sfx2::sidebar;
 
 namespace {
 
-bool inChartContext(const SwView& rViewShell)
+bool inChartOrMathContext(const SwView& rViewShell)
 {
     SidebarController* pSidebar = 
SidebarController::GetSidebarControllerForView(&rViewShell);
     if (pSidebar)
-        return pSidebar->hasChartContextCurrently();
+        return pSidebar->hasChartOrMathContextCurrently();
 
     return false;
 }
@@ -56,11 +56,11 @@ void SwOleShell::InitInterface_Impl()
 
 void SwOleShell::Activate(bool bMDI)
 {
-    if(!inChartContext(GetView()))
+    if(!inChartOrMathContext(GetView()))
         SwFrameShell::Activate(bMDI);
     else
     {
-        // Avoid context changes for chart during activation / deactivation.
+        // Avoid context changes for chart/math during activation / 
deactivation.
         const bool bIsContextBroadcasterEnabled 
(SfxShell::SetContextBroadcasterEnabled(false));
 
         SwFrameShell::Activate(bMDI);
@@ -71,11 +71,11 @@ void SwOleShell::Activate(bool bMDI)
 
 void SwOleShell::Deactivate(bool bMDI)
 {
-    if(!inChartContext(GetView()))
+    if(!inChartOrMathContext(GetView()))
         SwFrameShell::Deactivate(bMDI);
     else
     {
-        // Avoid context changes for chart during activation / deactivation.
+        // Avoid context changes for chart/math during activation / 
deactivation.
         const bool bIsContextBroadcasterEnabled 
(SfxShell::SetContextBroadcasterEnabled(false));
 
         SwFrameShell::Deactivate(bMDI);

Reply via email to