Update view options string based on theme

Update the view options string based on the theme when tiled rendering,
and make sure only the currently active view is updated

Change-Id: Ib24111848f2926fe7ae858ca2e18e946d980e83f
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/148943
Tested-by: Paris Oplopoios <parisoplop@gmail.com>
Reviewed-by: Paris Oplopoios <parisoplop@gmail.com>
diff --git a/include/svtools/colorcfg.hxx b/include/svtools/colorcfg.hxx
index f0c0fea..8cf02de 100644
--- a/include/svtools/colorcfg.hxx
+++ b/include/svtools/colorcfg.hxx
@@ -110,6 +110,7 @@ public:
    // instead of the automatic color
    ColorConfigValue        GetColorValue(ColorConfigEntry eEntry, bool bSmart = true) const;
    static Color            GetDefaultColor(ColorConfigEntry eEntry);
    const OUString&         GetCurrentSchemeName() const;
};

class SVT_DLLPUBLIC EditableColorConfig
diff --git a/svtools/source/config/colorcfg.cxx b/svtools/source/config/colorcfg.cxx
index 45210436..359f55d 100644
--- a/svtools/source/config/colorcfg.cxx
+++ b/svtools/source/config/colorcfg.cxx
@@ -468,6 +468,11 @@ ColorConfigValue ColorConfig::GetColorValue(ColorConfigEntry eEntry, bool bSmart
    return aRet;
}

const OUString& ColorConfig::GetCurrentSchemeName() const
{
    return m_pImpl->GetLoadedScheme();
}

EditableColorConfig::EditableColorConfig() :
    m_pImpl(new ColorConfig_Impl),
    m_bModified(false)
diff --git a/sw/inc/viewopt.hxx b/sw/inc/viewopt.hxx
index 4c9dcf8..a6fd5ee 100644
--- a/sw/inc/viewopt.hxx
+++ b/sw/inc/viewopt.hxx
@@ -248,6 +248,7 @@ class SW_DLLPUBLIC SwViewOption
{
    SwViewColors m_aColorConfig;
    static SwViewColors s_aInitialColorConfig;
    OUString m_sThemeName;

    static sal_uInt16   s_nPixelTwips;// 1 Pixel == ? Twips

@@ -314,6 +315,16 @@ public:
        return m_aColorConfig;
    }

    void SetThemeName(const OUString& rThemeName)
    {
        m_sThemeName = rThemeName;
    }

    OUString GetThemeName() const
    {
        return m_sThemeName;
    }

    static void SetInitialColorConfig(const SwViewColors& rColorConfig)
    {
        s_aInitialColorConfig = rColorConfig;
diff --git a/sw/qa/extras/tiledrendering/tiledrendering.cxx b/sw/qa/extras/tiledrendering/tiledrendering.cxx
index fd5c469..230c358 100644
--- a/sw/qa/extras/tiledrendering/tiledrendering.cxx
+++ b/sw/qa/extras/tiledrendering/tiledrendering.cxx
@@ -1643,6 +1643,7 @@ CPPUNIT_TEST_FIXTURE(SwTiledRenderingTest, testGetViewRenderState)

    // Create a second view
    SfxLokHelper::createView();
    int nSecondViewId = SfxLokHelper::getView();
    ViewCallback aView2;
    {
        // Give the second view different options
@@ -1656,6 +1657,25 @@ CPPUNIT_TEST_FIXTURE(SwTiledRenderingTest, testGetViewRenderState)
    // Switch back to the first view, and check that the options string is the same
    SfxLokHelper::setView(nFirstViewId);
    CPPUNIT_ASSERT_EQUAL(OString("PS"), pXTextDocument->getViewRenderState());

    // Switch back to the second view, and change to dark mode
    SfxLokHelper::setView(nSecondViewId);
    {
        SwDoc* pDoc = pXTextDocument->GetDocShell()->GetDoc();
        SwView* pView = pDoc->GetDocShell()->GetView();
        uno::Reference<frame::XFrame> xFrame = pView->GetViewFrame().GetFrame().GetFrameInterface();
        uno::Sequence<beans::PropertyValue> aPropertyValues = comphelper::InitPropertySequence(
            {
                { "NewTheme", uno::Any(OUString("COLOR_SCHEME_LIBREOFFICE_DARK")) },
            }
        );
        comphelper::dispatchCommand(".uno:ChangeTheme", xFrame, aPropertyValues);
    }
    CPPUNIT_ASSERT_EQUAL(OString("SD"), pXTextDocument->getViewRenderState());

    // Switch back to the first view, and check that the options string is the same
    SfxLokHelper::setView(nFirstViewId);
    CPPUNIT_ASSERT_EQUAL(OString("PS"), pXTextDocument->getViewRenderState());
}

CPPUNIT_TEST_FIXTURE(SwTiledRenderingTest, testSetViewGraphicSelection)
diff --git a/sw/source/core/view/viewsh.cxx b/sw/source/core/view/viewsh.cxx
index e59c80a..73fa17f 100644
--- a/sw/source/core/view/viewsh.cxx
+++ b/sw/source/core/view/viewsh.cxx
@@ -2214,7 +2214,6 @@ void SwViewShell::ApplyViewOptions( const SwViewOption &rOpt )
        if(&rSh == this)
            continue;
        SwViewOption aOpt( *rSh.GetViewOptions() );
        aOpt.SetColorConfig( rOpt.GetColorConfig() );
        aOpt.SetFieldName( rOpt.IsFieldName() );
        aOpt.SetShowHiddenField( rOpt.IsShowHiddenField() );
        aOpt.SetShowHiddenPara( rOpt.IsShowHiddenPara() );
diff --git a/sw/source/uibase/app/apphdl.cxx b/sw/source/uibase/app/apphdl.cxx
index a78ba94..bbc2ada 100644
--- a/sw/source/uibase/app/apphdl.cxx
+++ b/sw/source/uibase/app/apphdl.cxx
@@ -79,6 +79,7 @@
#include <swabstdlg.hxx>
#include <comphelper/dispatchcommand.hxx>
#include <comphelper/processfactory.hxx>
#include <comphelper/lok.hxx>

#include <salhelper/simplereferenceobject.hxx>
#include <rtl/ref.hxx>
@@ -969,8 +970,9 @@ void SwModule::ConfigurationChanged( utl::ConfigurationBroadcaster* pBrdCst, Con
    }
    else if ( pBrdCst == m_pColorConfig.get() )
    {
        //invalidate all edit windows
        SfxViewShell* pViewShell = SfxViewShell::GetFirst();
        //invalidate only the current view in tiled rendering mode, or all views otherwise
        bool bOnlyInvalidateCurrentView = comphelper::LibreOfficeKit::isActive();
        SfxViewShell* pViewShell = bOnlyInvalidateCurrentView ? SfxViewShell::Current() : SfxViewShell::GetFirst();
        while(pViewShell)
        {
            if(pViewShell->GetWindow())
@@ -981,11 +983,14 @@ void SwModule::ConfigurationChanged( utl::ConfigurationBroadcaster* pBrdCst, Con
                   dynamic_cast< const SwSrcView *>( pViewShell ) !=  nullptr)
                {
                    SwViewOption aNewOptions = *pSwView->GetWrtShell().GetViewOptions();
                    aNewOptions.SetThemeName(m_pColorConfig->GetCurrentSchemeName());
                    aNewOptions.SetColorConfig(*m_pColorConfig);
                    pSwView->GetWrtShell().ApplyViewOptions(aNewOptions);
                    pViewShell->GetWindow()->Invalidate();
                }
            }
            if (bOnlyInvalidateCurrentView)
                break;
            pViewShell = SfxViewShell::GetNext( *pViewShell );
        }
    }
diff --git a/sw/source/uibase/config/viewopt.cxx b/sw/source/uibase/config/viewopt.cxx
index 04dfa0d..668256f 100644
--- a/sw/source/uibase/config/viewopt.cxx
+++ b/sw/source/uibase/config/viewopt.cxx
@@ -300,6 +300,7 @@ SwViewOption::SwViewOption(const SwViewOption& rVOpt)
    m_bReadonly = false;
    m_bSelectionInReadonly = false;
    // #114856# Form view
    m_sThemeName = rVOpt.m_sThemeName;
    mbFormView       = rVOpt.mbFormView;
    m_nZoom           = rVOpt.m_nZoom       ;
    m_aSnapSize       = rVOpt.m_aSnapSize   ;
@@ -343,6 +344,7 @@ SwViewOption::SwViewOption(const SwViewOption& rVOpt)
SwViewOption& SwViewOption::operator=( const SwViewOption &rVOpt )
{
    // #114856# Form view
    m_sThemeName = rVOpt.m_sThemeName;
    mbFormView       = rVOpt.mbFormView   ;
    m_nZoom           = rVOpt.m_nZoom       ;
    m_aSnapSize       = rVOpt.m_aSnapSize   ;
diff --git a/sw/source/uibase/uno/unotxdoc.cxx b/sw/source/uibase/uno/unotxdoc.cxx
index 57ed9d0..6fee616 100644
--- a/sw/source/uibase/uno/unotxdoc.cxx
+++ b/sw/source/uibase/uno/unotxdoc.cxx
@@ -3415,6 +3415,8 @@ OString SwXTextDocument::getViewRenderState()
                aState.append('P');
            if (pVOpt->IsOnlineSpell())
                aState.append('S');
            if (pVOpt->GetThemeName() == u"COLOR_SCHEME_LIBREOFFICE_DARK")
                aState.append('D');
        }
    }
    return aState.makeStringAndClear();