Sidebar lifecycle - dispose panel if we fail to construct it properly.

Change-Id: I5e46e72227ac8532aed10758e1ccbb2b452ba8d4
diff --git a/include/sfx2/sidebar/ControlFactory.hxx b/include/sfx2/sidebar/ControlFactory.hxx
index a078e00..142f249 100644
--- a/include/sfx2/sidebar/ControlFactory.hxx
+++ b/include/sfx2/sidebar/ControlFactory.hxx
@@ -22,6 +22,7 @@
#include <sfx2/dllapi.h>
#include <sfx2/sidebar/SidebarToolBox.hxx>
#include <vcl/button.hxx>
#include <vcl/vclptr.hxx>
#include <com/sun/star/frame/XFrame.hpp>
class ToolBox;

@@ -39,9 +40,9 @@ class SFX2_DLLPUBLIC ControlFactory
public:
    /** Create the menu button for the task bar.
    */
    static CheckBox* CreateMenuButton (vcl::Window* pParentWindow);
    static VclPtr<CheckBox> CreateMenuButton (vcl::Window* pParentWindow);

    static ImageRadioButton* CreateTabItem (vcl::Window* pParentWindow);
    static VclPtr<ImageRadioButton> CreateTabItem (vcl::Window* pParentWindow);
};


diff --git a/sfx2/source/sidebar/ControlFactory.cxx b/sfx2/source/sidebar/ControlFactory.cxx
index 23e17a6..d8a49b3 100644
--- a/sfx2/source/sidebar/ControlFactory.cxx
+++ b/sfx2/source/sidebar/ControlFactory.cxx
@@ -25,14 +25,14 @@

namespace sfx2 { namespace sidebar {

CheckBox* ControlFactory::CreateMenuButton (vcl::Window* pParentWindow)
VclPtr<CheckBox> ControlFactory::CreateMenuButton (vcl::Window* pParentWindow)
{
    return new MenuButton(pParentWindow);
    return VclPtr<CheckBox>(new MenuButton(pParentWindow), SAL_NO_ACQUIRE);
}

ImageRadioButton* ControlFactory::CreateTabItem (vcl::Window* pParentWindow)
VclPtr<ImageRadioButton> ControlFactory::CreateTabItem (vcl::Window* pParentWindow)
{
    return new TabItem(pParentWindow);
    return VclPtr<ImageRadioButton>(new TabItem(pParentWindow), SAL_NO_ACQUIRE);
}

} } // end of namespace sfx2::sidebar
diff --git a/sfx2/source/sidebar/Deck.cxx b/sfx2/source/sidebar/Deck.cxx
index a44217c..bbdac3e 100644
--- a/sfx2/source/sidebar/Deck.cxx
+++ b/sfx2/source/sidebar/Deck.cxx
@@ -48,11 +48,11 @@ Deck::Deck (
      maIcon(),
      mnMinimalWidth(0),
      maPanels(),
      mpTitleBar(new DeckTitleBar(rDeckDescriptor.msTitle, this, rCloserAction)),
      mpScrollClipWindow(new vcl::Window(this)),
      mpScrollContainer(new ScrollContainerWindow(mpScrollClipWindow.get())),
      mpFiller(new vcl::Window(this)),
      mpVerticalScrollBar(new ScrollBar(this))
      mpTitleBar(VclPtr<DeckTitleBar>::Create(rDeckDescriptor.msTitle, this, rCloserAction)),
      mpScrollClipWindow(VclPtr<vcl::Window>::Create(this)),
      mpScrollContainer(VclPtr<ScrollContainerWindow>::Create(mpScrollClipWindow.get())),
      mpFiller(VclPtr<vcl::Window>::Create(this)),
      mpVerticalScrollBar(VclPtr<ScrollBar>::Create(this))
{
    SetBackground(Wallpaper());

@@ -88,13 +88,12 @@ void Deck::dispose()
    // without updating maPanels.
    for (size_t i = 0; i < aPanels.size(); i++)
        aPanels[i].disposeAndClear();
    maPanels.clear();

    mpTitleBar.disposeAndClear();
    mpFiller.disposeAndClear();
    mpVerticalScrollBar.disposeAndClear();
    mpScrollContainer.disposeAndClear();
    mpScrollClipWindow.disposeAndClear();
    mpTitleBar.disposeAndClear();

    vcl::Window::dispose();
}
diff --git a/sfx2/source/sidebar/Panel.cxx b/sfx2/source/sidebar/Panel.cxx
index 8be4f28..c0246e5 100644
--- a/sfx2/source/sidebar/Panel.cxx
+++ b/sfx2/source/sidebar/Panel.cxx
@@ -51,7 +51,7 @@ Panel::Panel (
    const ::boost::function<Context(void)>& rContextAccess)
    : Window(pParentWindow),
      msPanelId(rPanelDescriptor.msId),
      mpTitleBar(new PanelTitleBar(
      mpTitleBar(VclPtr<PanelTitleBar>::Create(
              rPanelDescriptor.msTitle,
              pParentWindow,
              this)),
diff --git a/sfx2/source/sidebar/SidebarChildWindow.cxx b/sfx2/source/sidebar/SidebarChildWindow.cxx
index 05fad1c..c1b50bc 100644
--- a/sfx2/source/sidebar/SidebarChildWindow.cxx
+++ b/sfx2/source/sidebar/SidebarChildWindow.cxx
@@ -37,11 +37,11 @@ SidebarChildWindow::SidebarChildWindow (
    SfxChildWinInfo* pInfo)
    : SfxChildWindow(pParentWindow, nId)
{
    pWindow = new SidebarDockingWindow(
    pWindow.reset(VclPtr<SidebarDockingWindow>::Create(
        pBindings,
        *this,
        pParentWindow,
        WB_STDDOCKWIN | WB_OWNERDRAWDECORATION | WB_CLIPCHILDREN | WB_SIZEABLE | WB_3DLOOK | WB_ROLLABLE);
        WB_STDDOCKWIN | WB_OWNERDRAWDECORATION | WB_CLIPCHILDREN | WB_SIZEABLE | WB_3DLOOK | WB_ROLLABLE));
    eChildAlignment = SfxChildAlignment::RIGHT;

    pWindow->SetHelpId(HID_SIDEBAR_WINDOW);
diff --git a/sfx2/source/sidebar/SidebarController.cxx b/sfx2/source/sidebar/SidebarController.cxx
index ee99a28..d1acaaa 100644
--- a/sfx2/source/sidebar/SidebarController.cxx
+++ b/sfx2/source/sidebar/SidebarController.cxx
@@ -95,7 +95,7 @@ SidebarController::SidebarController (
    : SidebarControllerInterfaceBase(m_aMutex),
      mpCurrentDeck(),
      mpParentWindow(pParentWindow),
      mpTabBar(new TabBar(
      mpTabBar(VclPtr<TabBar>::Create(
              mpParentWindow,
              rxFrame,
              ::boost::bind(&SidebarController::OpenThenSwitchToDeck, this, _1),
@@ -572,7 +572,7 @@ void SidebarController::SwitchToDeck (
    if ( ! mpCurrentDeck)
    {
        mpCurrentDeck.reset(
            new Deck(
            VclPtr<Deck>::Create(
                rDeckDescriptor,
                mpParentWindow,
                ::boost::bind(&SidebarController::RequestCloseDeck, this)));
@@ -593,6 +593,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);
@@ -640,6 +641,7 @@ void SidebarController::SwitchToDeck (
                mpCurrentDeck->GetPanelParentWindow(),
                rPanelContexDescriptor.mbIsInitiallyVisible,
                rContext);
            aNewPanels[nWriteIndex].disposeAndClear();
            bHasPanelSetChanged = true;
        }
        if (aNewPanels[nWriteIndex] != nullptr)
@@ -658,6 +660,7 @@ void SidebarController::SwitchToDeck (
        }

    }
    // mpCurrentPanels - may miss stuff (?)
    aNewPanels.resize(nWriteIndex);

    // Activate the deck and the new set of panels.
@@ -683,7 +686,7 @@ void SidebarController::SwitchToDeck (
    UpdateTitleBarIcons();
}

Panel* SidebarController::CreatePanel (
VclPtr<Panel> SidebarController::CreatePanel (
    const OUString& rsPanelId,
    vcl::Window* pParentWindow,
    const bool bIsInitiallyExpanded,
@@ -694,7 +697,7 @@ Panel* SidebarController::CreatePanel (
        return NULL;

    // Create the panel which is the parent window of the UIElement.
    Panel *pPanel = new Panel(
    VclPtr<Panel> pPanel = VclPtr<Panel>::Create(
        *pPanelDescriptor,
        pParentWindow,
        bIsInitiallyExpanded,
@@ -714,7 +717,7 @@ Panel* SidebarController::CreatePanel (
    }
    else
    {
        pPanel = NULL;
        pPanel.disposeAndClear();
    }

    return pPanel;
@@ -1099,7 +1102,7 @@ void SidebarController::UpdateCloseIndicator (const bool bCloseAfterDrag)
        // Make sure that the indicator exists.
        if ( ! mpCloseIndicator)
        {
            mpCloseIndicator.reset(new FixedImage(mpParentWindow));
            mpCloseIndicator.reset(VclPtr<FixedImage>::Create(mpParentWindow));
            FixedImage* pFixedImage = static_cast<FixedImage*>(mpCloseIndicator.get());
            const Image aImage (Theme::GetImage(Theme::Image_CloseIndicator));
            pFixedImage->SetImage(aImage);
diff --git a/sfx2/source/sidebar/SidebarController.hxx b/sfx2/source/sidebar/SidebarController.hxx
index 68f634d..99941c3 100644
--- a/sfx2/source/sidebar/SidebarController.hxx
+++ b/sfx2/source/sidebar/SidebarController.hxx
@@ -198,7 +198,7 @@ private:
        const ::rtl::OUString& rsImplementationURL,
        const bool bWantsCanvas,
        const Context& rContext);
    Panel* CreatePanel (
    VclPtr<Panel> CreatePanel (
        const ::rtl::OUString& rsPanelId,
        vcl::Window* pParentWindow,
        const bool bIsInitiallyExpanded,
diff --git a/sfx2/source/sidebar/TabBar.cxx b/sfx2/source/sidebar/TabBar.cxx
index b055ec3..20c1da2 100644
--- a/sfx2/source/sidebar/TabBar.cxx
+++ b/sfx2/source/sidebar/TabBar.cxx
@@ -69,6 +69,11 @@ TabBar::~TabBar()

void TabBar::dispose()
{
    for(ItemContainer::iterator
            iItem(maItems.begin()), iEnd(maItems.end());
        iItem!=iEnd;
        ++iItem)
        iItem->mpButton.disposeAndClear();
    maItems.clear();
    mpMenuButton.disposeAndClear();
    vcl::Window::dispose();
@@ -254,12 +259,11 @@ bool TabBar::Notify (NotifyEvent&)
    return false;
}

RadioButton* TabBar::CreateTabItem (const DeckDescriptor& rDeckDescriptor)
VclPtr<RadioButton> TabBar::CreateTabItem (const DeckDescriptor& rDeckDescriptor)
{
    RadioButton* pItem = ControlFactory::CreateTabItem(this);
    VclPtr<RadioButton> pItem = ControlFactory::CreateTabItem(this);
    pItem->SetHelpText(rDeckDescriptor.msHelpText);
    pItem->SetQuickHelpText(rDeckDescriptor.msHelpText);

    return pItem;
}

diff --git a/sfx2/source/sidebar/TabBar.hxx b/sfx2/source/sidebar/TabBar.hxx
index 31778a5..a1061c3 100644
--- a/sfx2/source/sidebar/TabBar.hxx
+++ b/sfx2/source/sidebar/TabBar.hxx
@@ -111,7 +111,7 @@ private:
    sal_Int32 mnMenuSeparatorY;
    PopupMenuProvider maPopupMenuProvider;

    RadioButton* CreateTabItem (const DeckDescriptor& rDeckDescriptor);
    VclPtr<RadioButton> CreateTabItem (const DeckDescriptor& rDeckDescriptor);
    Image GetItemImage (const DeckDescriptor& rDeskDescriptor) const;
    void Layout (void);
    void UpdateButtonIcons (void);
diff --git a/sw/inc/AnnotationWin.hxx b/sw/inc/AnnotationWin.hxx
index 8533cfe..3f63b27 100644
--- a/sw/inc/AnnotationWin.hxx
+++ b/sw/inc/AnnotationWin.hxx
@@ -56,7 +56,7 @@ class SwAnnotationWin : public sw::sidebarwindows::SwSidebarWin
        virtual bool    IsProtected() SAL_OVERRIDE;

    protected:
        virtual MenuButton* CreateMenuButton() SAL_OVERRIDE;
        virtual VclPtr<MenuButton> CreateMenuButton() SAL_OVERRIDE;

    private:
        virtual SvxLanguageItem GetLanguage(void) SAL_OVERRIDE;
diff --git a/sw/inc/SidebarWin.hxx b/sw/inc/SidebarWin.hxx
index 3495713..6d5c90d9 100644
--- a/sw/inc/SidebarWin.hxx
+++ b/sw/inc/SidebarWin.hxx
@@ -184,7 +184,7 @@ class SwSidebarWin : public vcl::Window
        virtual void    LoseFocus() SAL_OVERRIDE;
        virtual void    Paint( const Rectangle& rRect) SAL_OVERRIDE;
        virtual void    GetFocus() SAL_OVERRIDE;
        virtual MenuButton* CreateMenuButton() = 0;
        virtual VclPtr<MenuButton> CreateMenuButton() = 0;

        void        SetSizePixel( const Size& rNewSize ) SAL_OVERRIDE;
        SfxItemSet  DefaultItem();
diff --git a/sw/source/uibase/docvw/AnnotationWin.cxx b/sw/source/uibase/docvw/AnnotationWin.cxx
index 2d253ba..c061708f 100644
--- a/sw/source/uibase/docvw/AnnotationWin.cxx
+++ b/sw/source/uibase/docvw/AnnotationWin.cxx
@@ -213,7 +213,7 @@ sal_uInt32 SwAnnotationWin::CountFollowing()
    return aCount - 1;
}

MenuButton* SwAnnotationWin::CreateMenuButton()
VclPtr<MenuButton> SwAnnotationWin::CreateMenuButton()
{
    mpButtonPopup = new PopupMenu(SW_RES(MN_ANNOTATION_BUTTON));
    OUString aText = mpButtonPopup->GetItemText( FN_DELETE_NOTE_AUTHOR );
@@ -221,7 +221,7 @@ MenuButton* SwAnnotationWin::CreateMenuButton()
    aRewriter.AddRule(UndoArg1,GetAuthor());
    aText = aRewriter.Apply(aText);
    mpButtonPopup->SetItemText(FN_DELETE_NOTE_AUTHOR,aText);
    MenuButton* pMenuButton = new AnnotationMenuButton( *this );
    VclPtr<MenuButton> pMenuButton( new AnnotationMenuButton( *this ), SAL_NO_ACQUIRE );
    pMenuButton->SetPopupMenu( mpButtonPopup );
    pMenuButton->Show();
    return pMenuButton;