LOK: Navigator: fix expand problems

Save/load content types' expanded status to view, and make sure it
is synchronized with client.
Treeviews now receive/send collapse event/status from/to client.
I've rewritten the way how headings are opened by default
(because synch of collapsed status broke that, as sub-headings
were not expanded at core side).

Change-Id: I80f5b4d99fe5224391a92c4609f94ddbcf37b8ca
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/153771
Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoffice@gmail.com>
Reviewed-by: Andras Timar <andras.timar@collabora.com>
(cherry picked from commit b95750af717e0693a13c3ef35277779e1394e0ae)
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/158016
Tested-by: Jenkins
Reviewed-by: Caolán McNamara <caolan.mcnamara@collabora.com>
diff --git a/sw/inc/view.hxx b/sw/inc/view.hxx
index c5967bb..523f86c 100644
--- a/sw/inc/view.hxx
+++ b/sw/inc/view.hxx
@@ -355,6 +355,7 @@ class SW_DLLPUBLIC SwView: public SfxViewShell

public: // #i123922# Needs to be called from a 2nd place now as a helper method
    SAL_DLLPRIVATE bool          InsertGraphicDlg( SfxRequest& );
    sal_Int32 m_nNaviExpandedStatus = -1;

protected:

diff --git a/sw/source/uibase/inc/conttree.hxx b/sw/source/uibase/inc/conttree.hxx
index 495cdfa..9c6db5b 100644
--- a/sw/source/uibase/inc/conttree.hxx
+++ b/sw/source/uibase/inc/conttree.hxx
@@ -274,6 +274,8 @@ public:
    // return true if it has any children
    bool RequestingChildren(const weld::TreeIter& rParent);

    void ExpandAllHeadings();

    virtual void Notify(SfxBroadcaster& rBC, SfxHint const& rHint) override;

    sal_Int8 AcceptDrop(const AcceptDropEvent& rEvt);
diff --git a/sw/source/uibase/utlui/content.cxx b/sw/source/uibase/utlui/content.cxx
index d047251..02d3a66 100644
--- a/sw/source/uibase/utlui/content.cxx
+++ b/sw/source/uibase/utlui/content.cxx
@@ -1135,6 +1135,12 @@ SwContentTree::SwContentTree(std::unique_ptr<weld::TreeView> xTreeView, SwNaviga
        OUString sDocTitle = pView->GetDocShell()->GetTitle();
        if (lcl_DocOutLineExpandStateMap.find(sDocTitle) != lcl_DocOutLineExpandStateMap.end())
            mOutLineNodeMap = lcl_DocOutLineExpandStateMap[sDocTitle];
        if (comphelper::LibreOfficeKit::isActive()) {
            if (pView->m_nNaviExpandedStatus < 0)
                m_nActiveBlock = 1;
            else
                m_nActiveBlock = pView->m_nNaviExpandedStatus;
        }
    }

    m_aUpdTimer.SetInvokeHandler(LINK(this, SwContentTree, TimerUpdate));
@@ -1149,6 +1155,8 @@ SwContentTree::~SwContentTree()
    {
        OUString sDocTitle = pView->GetDocShell()->GetTitle();
        lcl_DocOutLineExpandStateMap[sDocTitle] = mOutLineNodeMap;
        if (comphelper::LibreOfficeKit::isActive())
            pView->m_nNaviExpandedStatus = m_nActiveBlock;
    }
    clear(); // If applicable erase content types previously.
    m_aUpdTimer.Stop();
@@ -2225,6 +2233,19 @@ bool SwContentTree::RequestingChildren(const weld::TreeIter& rParent)
    return false;
}

void SwContentTree::ExpandAllHeadings()
{
    if (HasHeadings())
    {
        std::unique_ptr<weld::TreeIter> xEntry = GetEntryAtAbsPos(0);
        if (xEntry)
        {
            if (!IsAllExpanded(*m_xTreeView, *xEntry))
                ExpandOrCollapseAll(*m_xTreeView, *xEntry);
        }
    }
}

SdrObject* SwContentTree::GetDrawingObjectsByContent(const SwContent *pCnt)
{
    SdrObject *pRetObj = nullptr;
diff --git a/sw/source/uibase/utlui/navipi.cxx b/sw/source/uibase/utlui/navipi.cxx
index c868592..3fd5b7f 100644
--- a/sw/source/uibase/utlui/navipi.cxx
+++ b/sw/source/uibase/utlui/navipi.cxx
@@ -736,12 +736,11 @@ SwNavigationPI::SwNavigationPI(weld::Widget* pParent,
        m_xGlobalTree->HideTree();

        //Open Headings by default
        if (m_xContentTree->HasHeadings())
        SwView *pView = GetCreateView();
        if (pView->m_nNaviExpandedStatus < 0)
        {
            auto& pTreeView = m_xContentTree->get_widget();
            std::unique_ptr<weld::TreeIter> itEntry(pTreeView.make_iterator());
            pTreeView.get_iter_first(*itEntry);
            pTreeView.expand_row(*itEntry);
            pView->m_nNaviExpandedStatus = 1;
            m_xContentTree->ExpandAllHeadings();
        }
    }
}
diff --git a/vcl/jsdialog/executor.cxx b/vcl/jsdialog/executor.cxx
index 17c56d7..46d5fb6 100644
--- a/vcl/jsdialog/executor.cxx
+++ b/vcl/jsdialog/executor.cxx
@@ -498,6 +498,16 @@ bool ExecuteAction(const OUString& nWindowId, const OUString& rWidget, StringMap
                    pTreeView->expand_row(*itEntry);
                    return true;
                }
                else if (sAction == "collapse")
                {
                    sal_Int32 nAbsPos = o3tl::toInt32(rData["data"]);
                    std::unique_ptr<weld::TreeIter> itEntry(pTreeView->make_iterator());
                    pTreeView->get_iter_abs_pos(*itEntry, nAbsPos);
                    pTreeView->set_cursor_without_notify(*itEntry);
                    pTreeView->grab_focus();
                    pTreeView->collapse_row(*itEntry);
                    return true;
                }
                else if (sAction == "dragstart")
                {
                    sal_Int32 nRow = o3tl::toInt32(rData["data"]);
diff --git a/vcl/source/treelist/svtabbx.cxx b/vcl/source/treelist/svtabbx.cxx
index 0c3a179..81f0f4e 100644
--- a/vcl/source/treelist/svtabbx.cxx
+++ b/vcl/source/treelist/svtabbx.cxx
@@ -104,6 +104,10 @@ static void lcl_DumpEntryAndSiblings(tools::JsonWriter& rJsonWriter,
                    rJsonWriter.put("ondemand", true);
            }
        }
        if (rChildren.size() > 0 && !pTabListBox->IsExpanded(pEntry))
        {
            rJsonWriter.put("collapsed", true);
        }

        if (bCheckButtons)
        {