sc lok: fix spell checking languages in Calc

Change-Id: Iadcc3b55a30ce2a2c785366fb3807ff6e4bda1fe
Reviewed-on: https://gerrit.libreoffice.org/43581
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Henry Castro <hcastro@collabora.com>
diff --git a/include/svl/hint.hxx b/include/svl/hint.hxx
index 2a0e981..b987e90 100644
--- a/include/svl/hint.hxx
+++ b/include/svl/hint.hxx
@@ -33,6 +33,7 @@ enum class SfxHintId {
    Deinitializing,
    ModeChanged,
    ColorsChanged,
    LanguageChanged,

// VCL text hints
    TextParaInserted,
diff --git a/sc/qa/unit/tiledrendering/tiledrendering.cxx b/sc/qa/unit/tiledrendering/tiledrendering.cxx
index 1f56bba..bc477a0 100644
--- a/sc/qa/unit/tiledrendering/tiledrendering.cxx
+++ b/sc/qa/unit/tiledrendering/tiledrendering.cxx
@@ -85,6 +85,7 @@ public:
    void testInsertGraphicInvalidations();
    void testDocumentSizeWithTwoViews();
    void testDisableUndoRepair();
    void testLanguageStatus();

    CPPUNIT_TEST_SUITE(ScTiledRenderingTest);
    CPPUNIT_TEST(testRowColumnSelections);
@@ -112,6 +113,7 @@ public:
    CPPUNIT_TEST(testInsertGraphicInvalidations);
    CPPUNIT_TEST(testDocumentSizeWithTwoViews);
    CPPUNIT_TEST(testDisableUndoRepair);
    CPPUNIT_TEST(testLanguageStatus);
    CPPUNIT_TEST_SUITE_END();

private:
@@ -1412,6 +1414,55 @@ void ScTiledRenderingTest::testDisableUndoRepair()
        CPPUNIT_ASSERT_EQUAL(SfxItemState::SET, aSet2.GetItemState(SID_UNDO));
        CPPUNIT_ASSERT(dynamic_cast< const SfxStringItem* >(aSet2.GetItem(SID_UNDO)));
    }

    comphelper::LibreOfficeKit::setActive(false);
}

void ScTiledRenderingTest::testLanguageStatus()
{
    comphelper::LibreOfficeKit::setActive();
    ScModelObj* pModelObj = createDoc("small.ods");
    CPPUNIT_ASSERT(pModelObj);
    ScDocShell* pDocSh = dynamic_cast< ScDocShell* >( pModelObj->GetEmbeddedObject() );
    CPPUNIT_ASSERT(pDocSh);

    // view #1
    SfxViewShell* pView1 = SfxViewShell::Current();

    // view #2
    SfxLokHelper::createView();
    SfxViewShell* pView2 = SfxViewShell::Current();
    CPPUNIT_ASSERT(pView1 != pView2);
    const OUString aLangBolivia("Spanish (Bolivia)");
    {
        std::unique_ptr<SfxPoolItem> pItem1;
        std::unique_ptr<SfxPoolItem> pItem2;
        pView1->GetViewFrame()->GetBindings().QueryState(SID_LANGUAGE_STATUS, pItem1);
        pView2->GetViewFrame()->GetBindings().QueryState(SID_LANGUAGE_STATUS, pItem2);
        CPPUNIT_ASSERT(dynamic_cast< const SfxStringItem* >(pItem1.get()));
        CPPUNIT_ASSERT(dynamic_cast< const SfxStringItem* >(pItem2.get()));
        CPPUNIT_ASSERT(!dynamic_cast< const SfxStringItem* >(pItem1.get())->GetValue().isEmpty());
        CPPUNIT_ASSERT(!dynamic_cast< const SfxStringItem* >(pItem1.get())->GetValue().isEmpty());
    }

    {
        SfxStringItem aLangString(SID_LANGUAGE_STATUS, "Default_Spanish (Bolivia)");
        pView1->GetViewFrame()->GetDispatcher()->ExecuteList(SID_LANGUAGE_STATUS,
            SfxCallMode::SYNCHRON, { &aLangString });
    }

    {
        std::unique_ptr<SfxPoolItem> pItem1;
        std::unique_ptr<SfxPoolItem> pItem2;
        pView1->GetViewFrame()->GetBindings().QueryState(SID_LANGUAGE_STATUS, pItem1);
        pView2->GetViewFrame()->GetBindings().QueryState(SID_LANGUAGE_STATUS, pItem2);
        CPPUNIT_ASSERT(dynamic_cast< const SfxStringItem* >(pItem1.get()));
        CPPUNIT_ASSERT(dynamic_cast< const SfxStringItem* >(pItem2.get()));
        CPPUNIT_ASSERT_EQUAL(aLangBolivia, dynamic_cast< const SfxStringItem* >(pItem1.get())->GetValue());
        CPPUNIT_ASSERT_EQUAL(aLangBolivia, dynamic_cast< const SfxStringItem* >(pItem1.get())->GetValue());
    }

    comphelper::LibreOfficeKit::setActive(false);
}

}
diff --git a/sc/sdi/docsh.sdi b/sc/sdi/docsh.sdi
index 3160e88..fdb22ac 100644
--- a/sc/sdi/docsh.sdi
+++ b/sc/sdi/docsh.sdi
@@ -74,6 +74,7 @@ interface TableDocument
    SID_ATTR_CHAR_FONTLIST  [ StateMethod = GetState; ]
    SID_SHARE_DOC           [ ExecMethod = Execute; StateMethod = GetState; ]
    SID_NOTEBOOKBAR         [ ExecMethod = Execute; StateMethod = GetState; ]
    SID_LANGUAGE_STATUS     [ ExecMethod = Execute; StateMethod = GetState; ]
}


diff --git a/sc/source/ui/docshell/docsh4.cxx b/sc/source/ui/docshell/docsh4.cxx
index 2067d04..8cfb5fc 100644
--- a/sc/source/ui/docshell/docsh4.cxx
+++ b/sc/source/ui/docshell/docsh4.cxx
@@ -30,11 +30,13 @@ using namespace ::com::sun::star;

#include "scitems.hxx"
#include <editeng/flstitem.hxx>
#include <editeng/langitem.hxx>
#include <sfx2/fcontnr.hxx>
#include <sfx2/linkmgr.hxx>
#include <sfx2/objface.hxx>
#include <sfx2/docfile.hxx>
#include <svtools/ehdl.hxx>
#include <svtools/langtab.hxx>
#include <basic/sbxcore.hxx>
#include <basic/sberrors.hxx>
#include <svtools/sfxecode.hxx>
@@ -52,6 +54,7 @@ using namespace ::com::sun::star;
#include <svl/PasswordHelper.hxx>
#include <svl/documentlockfile.hxx>
#include <svl/sharecontrolfile.hxx>
#include <svl/slstitm.hxx>
#include <unotools/securityoptions.hxx>
#include <LibreOfficeKit/LibreOfficeKitEnums.h>

@@ -1120,6 +1123,83 @@ void ScDocShell::Execute( SfxRequest& rReq )
                sfx2::SfxNotebookBar::CloseMethod(*pBindings);
        }
        break;
        case SID_LANGUAGE_STATUS:
        {
            sal_Int32 nPos = 0;
            OUString aLangText;
            const SfxStringItem* pItem = rReq.GetArg<SfxStringItem>(nSlot);
            if ( pItem )
                aLangText = pItem->GetValue();

            if ( !aLangText.isEmpty() )
            {
                LanguageType eLang, eLatin, eCjk, eCtl;
                const OUString aDocLangPrefix("Default_");
                const OUString aNoLang("LANGUAGE_NONE");
                const OUString aResetLang("RESET_LANGUAGES");

                ScDocument& rDoc = GetDocument();
                rDoc.GetLanguage( eLatin, eCjk, eCtl );

                if ( aLangText == "*" )
                {
                    SfxAbstractDialogFactory* pFact = SfxAbstractDialogFactory::Create();
                    if (pFact)
                    {
                        ScopedVclPtr<VclAbstractDialog> pDlg(pFact->CreateVclDialog(GetActiveDialogParent(), SID_LANGUAGE_OPTIONS));
                        pDlg->Execute();
                    }

                    rDoc.GetLanguage( eLang, eCjk, eCtl );
                }
                else if ( (nPos = aLangText.indexOf(aDocLangPrefix)) != -1 )
                {
                    aLangText = aLangText.replaceAt(nPos, aDocLangPrefix.getLength(), "");

                    if ( aLangText == aNoLang )
                    {
                        eLang = LANGUAGE_NONE;
                        rDoc.SetLanguage( eLang, eCjk, eCtl );
                    }
                    else if ( aLangText == aResetLang )
                    {
                        bool bAutoSpell;

                        ScModule::GetSpellSettings(eLang, eCjk, eCtl, bAutoSpell);
                        rDoc.SetLanguage(eLang, eCjk, eCtl);
                    }
                    else
                    {
                        eLang = SvtLanguageTable::GetLanguageType( aLangText );
                        if ( eLang != LANGUAGE_DONTKNOW  && SvtLanguageOptions::GetScriptTypeOfLanguage(eLang) == SvtScriptType::LATIN )
                        {
                            rDoc.SetLanguage( eLang, eCjk, eCtl );
                        }
                        else
                        {
                            eLang = eLatin;
                        }
                    }
                }

                if ( eLang != eLatin )
                {
                    if ( ScTabViewShell* pViewSh = ScTabViewShell::GetActiveViewShell() )
                    {
                        ScInputHandler* pInputHandler = SC_MOD()->GetInputHdl(pViewSh);
                        if ( pInputHandler )
                            pInputHandler->UpdateSpellSettings();

                        pViewSh->UpdateDrawTextOutliner();
                    }

                    SetDocumentModified();
                    Broadcast(SfxHint(SfxHintId::LanguageChanged));
                    PostPaintGridAll();
                }
            }
        }
        break;
        default:
        {
            // small (?) hack -> forwarding of the slots to TabViewShell
@@ -1881,6 +1961,15 @@ void ScDocShell::GetState( SfxItemSet &rSet )
                }
                break;

            case SID_LANGUAGE_STATUS:
                {
                    LanguageType eLatin, eCjk, eCtl;

                    GetDocument().GetLanguage( eLatin, eCjk, eCtl );
                    rSet.Put(SfxStringItem(nWhich, SvtLanguageTable::GetLanguageString(eLatin)));
                }
                break;

            default:
                {
                }
diff --git a/sc/source/ui/view/gridwin4.cxx b/sc/source/ui/view/gridwin4.cxx
index e631bba..297008b 100644
--- a/sc/source/ui/view/gridwin4.cxx
+++ b/sc/source/ui/view/gridwin4.cxx
@@ -1197,6 +1197,7 @@ void ScGridWindow::PaintTile( VirtualDevice& rDevice,
        mpLOKDrawView.reset(new FmFormView(pModel, &rDevice));
        mpLOKDrawView->ShowSdrPage(mpLOKDrawView->GetModel()->GetPage(nTab));
        aOutputData.SetDrawView(mpLOKDrawView.get());
        aOutputData.SetSpellCheckContext(mpSpellCheckCxt.get());
    }

    // draw the content
diff --git a/sc/source/ui/view/tabvwsh5.cxx b/sc/source/ui/view/tabvwsh5.cxx
index fd69cc8..928d804 100644
--- a/sc/source/ui/view/tabvwsh5.cxx
+++ b/sc/source/ui/view/tabvwsh5.cxx
@@ -298,6 +298,14 @@ void ScTabViewShell::Notify( SfxBroadcaster& rBC, const SfxHint& rHint )
                SetTabNo( GetViewData().GetTabNo(), true );
                break;

            case SfxHintId::LanguageChanged:
            {
                GetViewFrame()->GetBindings().Invalidate(SID_LANGUAGE_STATUS);
                if ( ScGridWindow* pWin = GetViewData().GetActiveWin() )
                    pWin->ResetAutoSpell();
            }
                break;

            default:
                break;
        }
diff --git a/sc/uiconfig/scalc/menubar/menubar.xml b/sc/uiconfig/scalc/menubar/menubar.xml
index 6033b17..812903a 100644
--- a/sc/uiconfig/scalc/menubar/menubar.xml
+++ b/sc/uiconfig/scalc/menubar/menubar.xml
@@ -680,6 +680,8 @@
            <menu:menuitem menu:id=".uno:ThesaurusDialog"/>
            <menu:menu menu:id=".uno:LanguageMenu">
                <menu:menupopup>
                    <menu:menuitem menu:id=".uno:SetLanguageAllTextMenu"/>
                    <menu:menuseparator/>
                    <menu:menuitem menu:id=".uno:Hyphenate"/>
                    <menu:menuitem menu:id=".uno:ChineseConversion"/>
                    <menu:menuitem menu:id=".uno:HangulHanjaConversion"/>
diff --git a/sc/uiconfig/scalc/statusbar/statusbar.xml b/sc/uiconfig/scalc/statusbar/statusbar.xml
index 79a746f..49f69c7 100644
--- a/sc/uiconfig/scalc/statusbar/statusbar.xml
+++ b/sc/uiconfig/scalc/statusbar/statusbar.xml
@@ -21,6 +21,7 @@
 <statusbar:statusbaritem xlink:href=".uno:StatusDocPos" statusbar:align="left" statusbar:autosize="true" statusbar:width="58"/>
 <statusbar:statusbaritem xlink:href=".uno:RowColSelCount" statusbar:align="left" statusbar:autosize="true" statusbar:width="58"/>
 <statusbar:statusbaritem xlink:href=".uno:StatusPageStyle" statusbar:align="left" statusbar:autosize="true" statusbar:width="83"/>
 <statusbar:statusbaritem xlink:href=".uno:LanguageStatus" statusbar:align="center" statusbar:autosize="true" statusbar:width="100"/>
 <statusbar:statusbaritem xlink:href=".uno:InsertMode" statusbar:align="center" statusbar:width="55"/>
 <statusbar:statusbaritem xlink:href=".uno:StatusSelectionMode" statusbar:align="center" statusbar:ownerdraw="true" statusbar:width="16"/>
 <statusbar:statusbaritem xlink:href=".uno:ModifiedStatus" statusbar:align="center" statusbar:ownerdraw="true" statusbar:width="16"/>
diff --git a/sfx2/source/control/unoctitm.cxx b/sfx2/source/control/unoctitm.cxx
index c609bf2..1636be3 100644
--- a/sfx2/source/control/unoctitm.cxx
+++ b/sfx2/source/control/unoctitm.cxx
@@ -1161,10 +1161,19 @@ static void InterceptLOKStateChangeEvent(const SfxViewFrame* pViewFrame, const c
    }
    else if (aEvent.FeatureURL.Path == "LanguageStatus")
    {
        OUString sValue;
        css::uno::Sequence< OUString > aSeq;
        if (aEvent.IsEnabled && (aEvent.State >>= aSeq))

        if (aEvent.IsEnabled)
        {
            aBuffer.append(aSeq[0]);
            if (aEvent.State >>= sValue)
            {
                aBuffer.append(sValue);
            }
            else if (aEvent.State >>= aSeq)
            {
                aBuffer.append(aSeq[0]);
            }
        }
    }
    else