Resolves: tdf#152301 allow using an existing ColorListBox to speed init

Change-Id: I31fb350fd69831e68ca7c60ec758126aab086895
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/143791
Tested-by: Jenkins
Reviewed-by: Caolán McNamara <caolanm@redhat.com>
diff --git a/cui/source/options/optcolor.cxx b/cui/source/options/optcolor.cxx
index 42c299d..70345fc 100644
--- a/cui/source/options/optcolor.cxx
+++ b/cui/source/options/optcolor.cxx
@@ -208,20 +208,18 @@ private:

    // Entry -- a color config entry:
    // text (checkbox) + color list box
    class Entry
    struct Entry
    {
    public:
        Entry(weld::Window* pTopLevel, weld::Builder& rBuilder, const char* pTextWidget, const char* pColorWidget,
              const Color& rColor, int nCheckBoxLabelOffset, int* pColorWidthRequest, bool bCheckBox, bool bShow);
    public:
              const Color& rColor, int nCheckBoxLabelOffset, const ColorListBox* pCache, bool bCheckBox, bool bShow);
        void SetText(const OUString& rLabel) { dynamic_cast<weld::Label&>(*m_xText).set_label(rLabel); }
        int get_height_request() const
        {
            return std::max(m_xText->get_preferred_size().Height(),
                            m_xColorList->get_widget().get_preferred_size().Height());
        }
        void Hide ();
    public:
        void Hide();

        void SetLinks(Link<weld::Toggleable&,void> const&,
                      Link<ColorListBox&,void> const&,
                      Link<weld::Widget&,void> const&);
@@ -229,10 +227,10 @@ private:
        void Update (ExtendedColorConfigValue const&);
        void ColorChanged (ColorConfigValue&);
        void ColorChanged (ExtendedColorConfigValue&);
    public:

        bool Is(const weld::Toggleable* pBox) const { return m_xText.get() == pBox; }
        bool Is(const ColorListBox* pBox) const { return m_xColorList.get() == pBox; }
    private:

        // checkbox (CheckBox) or simple text (FixedText)
        std::unique_ptr<weld::Widget> m_xText;
        // color list box
@@ -285,9 +283,9 @@ ColorConfigWindow_Impl::Chapter::Chapter(weld::Builder& rBuilder, const char* pL
ColorConfigWindow_Impl::Entry::Entry(weld::Window* pTopLevel, weld::Builder& rBuilder,
                                     const char* pTextWidget, const char* pColorWidget,
                                     const Color& rColor, int nCheckBoxLabelOffset,
                                     int* pColorWidthRequestCache, bool bCheckBox, bool bShow)
                                     const ColorListBox* pCache, bool bCheckBox, bool bShow)
    : m_xColorList(new ColorListBox(rBuilder.weld_menu_button(pColorWidget),
                                    [pTopLevel]{ return pTopLevel; }, pColorWidthRequestCache))
                                    [pTopLevel]{ return pTopLevel; }, pCache))
    , m_aDefaultColor(rColor)
{
    if (bCheckBox)
@@ -405,7 +403,7 @@ void ColorConfigWindow_Impl::CreateEntries()
        m_nCheckBoxLabelOffset = aCheckSize.Width() - aFixedSize.Width();
    }

    int nColorWidthRequestCache = -1;
    const ColorListBox* pCache = nullptr;

    // creating entries
    vEntries.reserve(ColorConfigEntryCount);
@@ -414,9 +412,11 @@ void ColorConfigWindow_Impl::CreateEntries()
        vEntries.push_back(std::make_shared<Entry>(m_pTopLevel, *m_xBuilder,
            vEntryInfo[i].pText, vEntryInfo[i].pColor,
            ColorConfig::GetDefaultColor(static_cast<ColorConfigEntry>(i)),
            m_nCheckBoxLabelOffset, &nColorWidthRequestCache,
            m_nCheckBoxLabelOffset, pCache,
            vEntryInfo[i].bCheckBox,
            aModulesInstalled[vEntryInfo[i].eGroup]));
        if (!pCache)
            pCache = vEntries.back()->m_xColorList.get();
    }

    // extended entries
@@ -448,7 +448,7 @@ void ColorConfigWindow_Impl::CreateEntries()
                aExtConfig.GetComponentColorConfigValue(sComponentName, i);
            vEntries.push_back(std::make_shared<Entry>(m_pTopLevel, *vExtBuilders.back(),
                "label", "button", aColorEntry.getDefaultColor(),
                m_nCheckBoxLabelOffset, &nColorWidthRequestCache, false, true));
                m_nCheckBoxLabelOffset, pCache, false, true));
            vEntries.back()->SetText(aColorEntry.getDisplayName());
        }
    }
diff --git a/include/svx/Palette.hxx b/include/svx/Palette.hxx
index c23a09f..b24825b 100644
--- a/include/svx/Palette.hxx
+++ b/include/svx/Palette.hxx
@@ -52,7 +52,10 @@ typedef std::function<void(const OUString&, const svx::NamedThemedColor&)> Color

class Palette
{
protected:
    Palette(const Palette&) = default;
public:
    Palette() = default;
    virtual ~Palette();

    virtual const OUString&     GetName() = 0;
@@ -60,6 +63,8 @@ public:
    virtual void                LoadColorSet(SvxColorValueSet& rColorSet) = 0;

    virtual bool                IsValid() = 0;

    virtual Palette*            Clone() const = 0;
};

#endif // INCLUDED_SVX_PALETTE_HXX
diff --git a/include/svx/PaletteManager.hxx b/include/svx/PaletteManager.hxx
index 4e45df0..a21961e 100644
--- a/include/svx/PaletteManager.hxx
+++ b/include/svx/PaletteManager.hxx
@@ -49,6 +49,8 @@ class SVXCORE_DLLPUBLIC PaletteManager
    ColorSelectFunction maColorSelectFunction;

    std::unique_ptr<SvColorDialog> m_pColorDlg;

    PaletteManager(const PaletteManager* pClone);
public:
    PaletteManager();
    ~PaletteManager();
@@ -75,6 +77,8 @@ public:

    bool IsThemePaletteSelected() const;

    PaletteManager* Clone() const;

    static void GetThemeIndexLumModOff(sal_uInt16 nItemId, sal_Int16& rThemeIndex,
                                       sal_Int16& rLumMod, sal_Int16& rLumOff);

diff --git a/include/svx/colorbox.hxx b/include/svx/colorbox.hxx
index 887edb9..2705833 100644
--- a/include/svx/colorbox.hxx
+++ b/include/svx/colorbox.hxx
@@ -54,7 +54,8 @@ private:
public:
    // rTopLevelParentFunction will be used to get parent for any color picker dialog created
    ColorListBox(std::unique_ptr<weld::MenuButton> pControl,
                 TopLevelParentFunction aTopLevelParentFunction, int* pWidthRequestCache = nullptr);
                 TopLevelParentFunction aTopLevelParentFunction,
                 const ColorListBox* pCache = nullptr);
    ~ColorListBox();

    void SetSelectHdl(const Link<ColorListBox&, void>& rLink) { m_aSelectedLink = rLink; }
diff --git a/svx/inc/palettes.hxx b/svx/inc/palettes.hxx
index 12fdbb5..abeccf0 100644
--- a/svx/inc/palettes.hxx
+++ b/svx/inc/palettes.hxx
@@ -37,6 +37,7 @@ class PaletteASE final : public Palette
    ColorList   maColors;

    void        LoadPalette();
    PaletteASE(const PaletteASE&) = default;
public:
    PaletteASE( OUString aFPath, OUString aFName );
    virtual ~PaletteASE() override;
@@ -46,6 +47,8 @@ public:
    virtual void                LoadColorSet(SvxColorValueSet& rColorSet) override;

    virtual bool                IsValid() override;

    virtual Palette*            Clone() const override;
};

// GPL - this is *not* GNU Public License, but is the Gimp PaLette
@@ -62,6 +65,7 @@ class PaletteGPL final : public Palette
    bool        ReadPaletteHeader(SvFileStream& rFileStream);
    void        LoadPaletteHeader();
    void        LoadPalette();
    PaletteGPL(const PaletteGPL&) = default;
public:
    PaletteGPL( OUString aFPath, OUString aFName );
    virtual ~PaletteGPL() override;
@@ -71,6 +75,8 @@ public:
    virtual void                LoadColorSet(SvxColorValueSet& rColorSet) override;

    virtual bool                IsValid() override;

    virtual Palette*            Clone() const override;
};

// SOC - Star Office Color-table
@@ -81,6 +87,7 @@ class PaletteSOC final : public Palette
    OUString        maFPath;
    OUString        maSOCPaletteName;
    XColorListRef   mpColorList;
    PaletteSOC(const PaletteSOC&) = default;
public:
    PaletteSOC( OUString aFPath, OUString aFName );
    virtual ~PaletteSOC() override;
@@ -90,6 +97,8 @@ public:
    virtual void                LoadColorSet(SvxColorValueSet& rColorSet) override;

    virtual bool                IsValid() override;

    virtual Palette*            Clone() const override;
};

#endif // INCLUDED_SVX_INC_PALETTE_HXX
diff --git a/svx/source/tbxctrls/Palette.cxx b/svx/source/tbxctrls/Palette.cxx
index 64f2b64..63d1cf9 100644
--- a/svx/source/tbxctrls/Palette.cxx
+++ b/svx/source/tbxctrls/Palette.cxx
@@ -171,6 +171,11 @@ void PaletteASE::LoadPalette()
    mbValidPalette = true;
}

Palette* PaletteASE::Clone() const
{
    return new PaletteASE(*this);
}

// PaletteGPL ------------------------------------------------------------------

static OString lcl_getToken(OStringBuffer& rStr, sal_Int32& index);
@@ -286,6 +291,11 @@ void PaletteGPL::LoadPalette()
    } while (aFile.ReadLine(aLine));
}

Palette* PaletteGPL::Clone() const
{
    return new PaletteGPL(*this);
}

// finds first token in rStr from index, separated by whitespace
// returns position of next token in index
static OString lcl_getToken(OStringBuffer& rStr, sal_Int32& index)
@@ -362,6 +372,11 @@ bool PaletteSOC::IsValid()
    return true;
}

Palette* PaletteSOC::Clone() const
{
    return new PaletteSOC(*this);
}

namespace svx
{
NamedColor NamedThemedColor::ToNamedColor() const { return { m_aColor, m_aName }; }
diff --git a/svx/source/tbxctrls/PaletteManager.cxx b/svx/source/tbxctrls/PaletteManager.cxx
index 8f642e7..f5cb5d2 100644
--- a/svx/source/tbxctrls/PaletteManager.cxx
+++ b/svx/source/tbxctrls/PaletteManager.cxx
@@ -77,6 +77,25 @@ PaletteManager::PaletteManager() :

}

PaletteManager::PaletteManager(const PaletteManager* pClone)
    : mnMaxRecentColors(pClone->mnMaxRecentColors)
    , mnNumOfPalettes(pClone->mnNumOfPalettes)
    , mnCurrentPalette(pClone->mnCurrentPalette)
    , mnColorCount(pClone->mnColorCount)
    , mpBtnUpdater(nullptr)
    , pColorList(pClone->pColorList)
    , maRecentColors(pClone->maRecentColors)
    , maColorSelectFunction(PaletteManager::DispatchColorCommand)
{
    for (const auto& a : pClone->m_Palettes)
        m_Palettes.emplace_back(a->Clone());
}

PaletteManager* PaletteManager::Clone() const
{
    return new PaletteManager(this);
}

PaletteManager::~PaletteManager()
{
}
diff --git a/svx/source/tbxctrls/tbcontrl.cxx b/svx/source/tbxctrls/tbcontrl.cxx
index 7dd2e7e..8df4616 100644
--- a/svx/source/tbxctrls/tbcontrl.cxx
+++ b/svx/source/tbxctrls/tbcontrl.cxx
@@ -4229,7 +4229,7 @@ void ColorListBox::SetSlotId(sal_uInt16 nSlotId, bool bShowNoneButton)

ColorListBox::ColorListBox(std::unique_ptr<weld::MenuButton> pControl,
                           TopLevelParentFunction aTopLevelParentFunction,
                           int* pWidthRequestCache)
                           const ColorListBox* pCache)
    : m_xButton(std::move(pControl))
    , m_aColorWrapper(this)
    , m_aAutoDisplayColor(Application::GetSettings().GetStyleSettings().GetDialogColor())
@@ -4239,12 +4239,14 @@ ColorListBox::ColorListBox(std::unique_ptr<weld::MenuButton> pControl,
{
    m_xButton->connect_toggled(LINK(this, ColorListBox, ToggleHdl));
    m_aSelectedColor = svx::NamedThemedColor::FromNamedColor(GetAutoColor(m_nSlotId));
    int nWidthRequest = pWidthRequestCache ? *pWidthRequestCache : -1;
    if (nWidthRequest == -1)
        nWidthRequest = CalcBestWidthRequest();
    LockWidthRequest(nWidthRequest);
    if (pWidthRequestCache)
        *pWidthRequestCache = nWidthRequest;
    if (!pCache)
        LockWidthRequest(CalcBestWidthRequest());
    else
    {
        LockWidthRequest(pCache->m_xButton->get_size_request().Width());
        m_xPaletteManager.reset(pCache->m_xPaletteManager->Clone());
        m_xPaletteManager->SetColorSelectFunction(std::ref(m_aColorWrapper));
    }
    ShowPreview(m_aSelectedColor.ToNamedColor());
}