tdf#131581 inhibit updates during all_foreach

Change-Id: I8494b4c9175e427bfb89696d49c2da7607af37ef
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/102371
Tested-by: Jenkins
Reviewed-by: Caolán McNamara <caolanm@redhat.com>
diff --git a/include/vcl/toolkit/treelist.hxx b/include/vcl/toolkit/treelist.hxx
index 6f047c2..09ed5da 100644
--- a/include/vcl/toolkit/treelist.hxx
+++ b/include/vcl/toolkit/treelist.hxx
@@ -134,6 +134,7 @@ public:
                        );

    void                EnableInvalidate( bool bEnable );
    bool                IsEnableInvalidate() const { return mbEnableInvalidate; }

    // Notify all Listeners
    void                InvalidateEntry( SvTreeListEntry* );
diff --git a/vcl/source/app/salvtables.cxx b/vcl/source/app/salvtables.cxx
index 7c7bbbe..a580ff2 100644
--- a/vcl/source/app/salvtables.cxx
+++ b/vcl/source/app/salvtables.cxx
@@ -3303,6 +3303,37 @@ class SalInstanceTreeView;

static SalInstanceTreeView* g_DragSource;

namespace
{
    class UpdateGuard
    {
    private:
        SvTabListBox& m_rTreeView;
        bool m_bOrigUpdate;
        bool m_bOrigEnableInvalidate;

    public:
        UpdateGuard(SvTabListBox& rTreeView)
            : m_rTreeView(rTreeView)
            , m_bOrigUpdate(m_rTreeView.IsUpdateMode())
            , m_bOrigEnableInvalidate(m_rTreeView.GetModel()->IsEnableInvalidate())
        {
            if (m_bOrigUpdate)
                m_rTreeView.SetUpdateMode(false);
            if (m_bOrigEnableInvalidate)
                m_rTreeView.GetModel()->EnableInvalidate(false);
        }

        ~UpdateGuard()
        {
            if (m_bOrigUpdate)
                m_rTreeView.SetUpdateMode(true);
            if (m_bOrigEnableInvalidate)
                m_rTreeView.GetModel()->EnableInvalidate(true);
        }
    };
}

class SalInstanceTreeView : public SalInstanceContainer, public virtual weld::TreeView
{
private:
@@ -3460,6 +3491,13 @@ private:
        m_xTreeView->CheckBoxInserted(pEntry);
    }

    void InvalidateModelEntry(SvTreeListEntry* pEntry)
    {
        if (!m_xTreeView->GetModel()->IsEnableInvalidate())
            return;
        m_xTreeView->ModelHasEntryInvalidated(pEntry);
    }

    void do_set_toggle(SvTreeListEntry* pEntry, TriState eState, int col)
    {
        assert(col >= 0 && o3tl::make_unsigned(col) < pEntry->ItemCount());
@@ -3486,7 +3524,7 @@ private:
                break;
        }

        m_xTreeView->ModelHasEntryInvalidated(pEntry);
        InvalidateModelEntry(pEntry);
    }

    static TriState do_get_toggle(SvTreeListEntry* pEntry, int col)
@@ -3988,7 +4026,8 @@ public:
            assert(dynamic_cast<SvLBoxString*>(&rItem));
            static_cast<SvLBoxString&>(rItem).SetText(rText);
        }
        m_xTreeView->ModelHasEntryInvalidated(pEntry);

        InvalidateModelEntry(pEntry);
    }

    virtual void set_text(int pos, const OUString& rText, int col) override
@@ -4012,7 +4051,7 @@ public:
                if (rItem.GetType() == SvLBoxItemType::String)
                {
                    rItem.Enable(bSensitive);
                    m_xTreeView->ModelHasEntryInvalidated(pEntry);
                    InvalidateModelEntry(pEntry);
                    break;
                }
            }
@@ -4025,7 +4064,7 @@ public:
        SvLBoxItem& rItem = pEntry->GetItem(col);
        rItem.Enable(bSensitive);

        m_xTreeView->ModelHasEntryInvalidated(pEntry);
        InvalidateModelEntry(pEntry);
    }

    using SalInstanceWidget::set_sensitive;
@@ -4093,7 +4132,7 @@ public:
        assert(dynamic_cast<SvLBoxString*>(&rItem));
        static_cast<SvLBoxString&>(rItem).Emphasize(bOn);

        m_xTreeView->ModelHasEntryInvalidated(pEntry);
        InvalidateModelEntry(pEntry);
    }

    virtual void set_text_emphasis(const weld::TreeIter& rIter, bool bOn, int col) override
@@ -4129,7 +4168,7 @@ public:
        assert(dynamic_cast<SvLBoxString*>(&rItem));
        static_cast<SvLBoxString&>(rItem).Align(fAlign);

        m_xTreeView->ModelHasEntryInvalidated(pEntry);
        InvalidateModelEntry(pEntry);
    }

    virtual void set_text_align(const weld::TreeIter& rIter, double fAlign, int col) override
@@ -4191,7 +4230,7 @@ public:
        }

        m_xTreeView->SetEntryHeight(pEntry);
        m_xTreeView->ModelHasEntryInvalidated(pEntry);
        InvalidateModelEntry(pEntry);
    }

    virtual void set_image(int pos, const OUString& rImage, int col) override
@@ -4523,6 +4562,8 @@ public:

    virtual void all_foreach(const std::function<bool(weld::TreeIter&)>& func) override
    {
        UpdateGuard aGuard(*m_xTreeView);

        SalInstanceTreeIter aVclIter(m_xTreeView->First());
        while (aVclIter.iter)
        {