Properly dispose old sidebar Panels when we switch panel.

Introduced in:
     "vclwidget: convert lots more sites to use VclPtr"

Change-Id: I1a3741b8a0369a90d71885e9cbfa4a2b1408c410
diff --git a/sfx2/source/sidebar/Deck.cxx b/sfx2/source/sidebar/Deck.cxx
index bbdac3e..ec1a127 100644
--- a/sfx2/source/sidebar/Deck.cxx
+++ b/sfx2/source/sidebar/Deck.cxx
@@ -202,8 +202,21 @@ bool Deck::ProcessWheelEvent(CommandEvent* pCommandEvent)
    return true;
}

void Deck::SetPanels (const SharedPanelContainer& rPanels)
/**
 * This container may contain existing panels that are
 * being re-used, and new ones too.
 */
void Deck::ResetPanels (const SharedPanelContainer& rPanels)
{
    // First dispose old panels we no longer need.
    for (size_t i = 0; i < maPanels.size(); i++)
    {
        bool bFound = false;
        for (size_t j = 0; j < rPanels.size(); j++)
            bFound = bFound || (maPanels[i].get() == rPanels[j].get());
        if (!bFound) // this one didn't survive.
            maPanels[i].disposeAndClear();
    }
    maPanels = rPanels;

    RequestLayout();
diff --git a/sfx2/source/sidebar/Deck.hxx b/sfx2/source/sidebar/Deck.hxx
index d4416ab..b2f35af 100644
--- a/sfx2/source/sidebar/Deck.hxx
+++ b/sfx2/source/sidebar/Deck.hxx
@@ -52,7 +52,7 @@ public:
    const ::rtl::OUString& GetId (void) const { return msId;}
    DeckTitleBar* GetTitleBar (void) const;
    Rectangle GetContentArea (void) const;
    void SetPanels (const SharedPanelContainer& rPanels);
    void ResetPanels (const SharedPanelContainer& rPanels);
    const SharedPanelContainer& GetPanels (void) const { return maPanels;}
    void RequestLayout (void);
    vcl::Window* GetPanelParentWindow (void);
diff --git a/sfx2/source/sidebar/SidebarController.cxx b/sfx2/source/sidebar/SidebarController.cxx
index 35cf2a9..569ef73 100644
--- a/sfx2/source/sidebar/SidebarController.cxx
+++ b/sfx2/source/sidebar/SidebarController.cxx
@@ -527,7 +527,6 @@ void SidebarController::SwitchToDeck (

    const bool bForceNewDeck ((mnRequestedForceFlags&SwitchFlag_ForceNewDeck)!=0);
    const bool bForceNewPanels ((mnRequestedForceFlags&SwitchFlag_ForceNewPanels)!=0);
    mnRequestedForceFlags = SwitchFlag_NoForce;

    if ( ! msCurrentDeckId.equals(rDeckDescriptor.msId)
        || bForceNewDeck)
@@ -593,7 +592,7 @@ void SidebarController::SwitchToDeck (
    const sal_Int32 nNewPanelCount (aPanelContextDescriptors.size());
    SharedPanelContainer aNewPanels;
    const SharedPanelContainer& rCurrentPanels (mpCurrentDeck->GetPanels());
    // FIXME: concerns wrt. dispose / lifecycle when we re-use panels here...

    aNewPanels.resize(nNewPanelCount);
    sal_Int32 nWriteIndex (0);
    bool bHasPanelSetChanged (false);
@@ -609,21 +608,18 @@ void SidebarController::SwitchToDeck (

        // Find the corresponding panel among the currently active
        // panels.
        SharedPanelContainer::const_iterator iPanel;
        if (bForceNewPanels)
        {
            // All panels have to be created in any case.  There is no
            // point in searching already existing panels.
            iPanel = rCurrentPanels.end();
        }
        else
        SharedPanelContainer::const_iterator iPanel = rCurrentPanels.end();

        if (!bForceNewPanels)
        {
            iPanel = rCurrentPanels.end();
            for (auto a = rCurrentPanels.begin(); a != rCurrentPanels.end(); ++a)
            {
                iPanel = a;
                if ((*iPanel)->HasIdPredicate(rPanelContexDescriptor.msId))
                if ((*a)->HasIdPredicate(rPanelContexDescriptor.msId))
                {
                    iPanel = a;
                    break;
                }
            }
        }
        if (iPanel != rCurrentPanels.end())
@@ -669,7 +665,7 @@ void SidebarController::SwitchToDeck (
        mpParentWindow->GetSizePixel().Width()-TabBar::GetDefaultWidth() * mpTabBar->GetDPIScaleFactor(),
        mpParentWindow->GetSizePixel().Height());

    mpCurrentDeck->SetPanels(aNewPanels);
    mpCurrentDeck->ResetPanels(aNewPanels);
    mpCurrentDeck->Show();

    mpParentWindow->SetText(rDeckDescriptor.msTitle);